Hello all,
I know it is not new as a topic, but I really need your help because, after reading all the documentation possible and have tried almost anything, I am stuck with my project. What I want to achieve: With an Arduino Mega 2650 board with Ethernet shield and blynk running locally on a raspberry pi 3, I need to dim 7 (LED) bulbs.
For that, I have bought the following dimmer from AliExpress ( https://www.aliexpress.com/item/AC-Light-Dimmer-Module-for-PWM-control-1-Channel-3-3V-5V-logic-AC-50-60hz/32802025086.html?spm=a2g0s.9042311.0.0.27424c4dchWRVK ). It has zero-cross detection. I have adapted some sample code that I have found in the blynk community but still cannot make it working. I want to control everything from my iPhone 6s.
The dimmer is connected as follows:
- Dimmer Vcc to 5V pin on Arduino Mega
- GND dimmer pin to Arduino GND pin
- Zero-crossing pin to A0 pin on Mega 2650
- Dimmer PWM pin to D3 pin on Arduino Mega.
Built the blynk project, managed to connect to the device but, when trying to dim the bulb, I got no dimming. The bulb is always on, regardless where the slider is. I have to mention though that when I reach with the slider to the left side, I got the bulb flickering a bit but the intensity of the light remains unchanged. Also worthing to mention, there is a small blue LED on the dimmer that indicates the dimming progress. That LED is dimmed while moving the slider.
The dimmer board is working, I have tested the triac and it works fine.
As I spent weeks to understand what’;s going on without any success, here I am asking your help and guidance.
Below you can find my code for reference.
Thank you very much in advance for your time and support.
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <TimerOne.h>
volatile int i=0; // Variable to use as a counter volatile as it is in an interrupt
volatile boolean zero_cross=0; // Boolean to store a "switch" to tell us if we have crossed zero
int AC_pin = 13; // Output to Opto Triac
int dim = 0; // Dimming level (0-128) 0 = on, 128 = 0ff
int inc=1; // counting up or down, 1=up, -1=down
int brightness = 120;
int freqStep = 78; //75 initial value // This is the delay-per-brightness step in microseconds.
// For 60 Hz it should be 65
// It is calculated based on the frequency of your voltage supply (50Hz or 60Hz)
// and the number of brightness steps you want.
//
// Realize that there are 2 zero-crossing per cycle. This means
// zero crossing happens at 120Hz for a 60Hz supply or 100Hz for a 50Hz supply.
// To calculate freqStep divide the length of one full half-wave of the power
// cycle (in microseconds) by the number of brightness steps.
//
// (120 Hz=8333uS) / 128 brightness steps = 65 uS / brightness step
// (100Hz=10000uS) / 128 steps = 75uS/step
int value;
#define W5100_CS 10
#define SDCARD_CS 4
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxx";
IPAddress server_ip (x, x, x, x);
// Mac address should be different for each device in your LAN
byte arduino_mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress arduino_ip (x, x, x, x);
IPAddress dns_ip (193, 231, 252, 1);
IPAddress gateway_ip (10, 130, 27, 255);
IPAddress subnet_mask(255, 255, 255, 0);
void setup()
{
Serial.begin(115200);
pinMode(SDCARD_CS, OUTPUT);
digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
pinMode(AC_pin, OUTPUT); // Set the Triac pin as output
attachInterrupt(0, zero_cross_detect, RISING); // Attach an Interupt to Pin 2 (interupt 0)
// attachInterrupt(0, light, FALLING);
Timer1.initialize(freqStep); // Initialize TimerOne library
Timer1.attachInterrupt(dim_check, freqStep);
Blynk.begin(auth, server_ip, 8080, arduino_ip, dns_ip, gateway_ip, subnet_mask, arduino_mac);
}
void zero_cross_detect() {
zero_cross = true;
i=0;
digitalWrite(AC_pin, LOW); // turn off TRIAC (and AC)
}
// Turn on the TRIAC at the appropriate time
void dim_check() {
if(zero_cross == true) {
if(i>=dim) {
digitalWrite(AC_pin, HIGH); // turn on light
i=0; // reset time step counter
zero_cross = false; //reset zero cross detection
}
else {
i++; // increment time step counter
}
}
}
void loop()
{
Blynk.run();
}