Hello everyone,
I’m currently working on a project involving dimming a 230V AC light using the RobotDyn 4-Channel AC Dimmer (RBD Dimmer), integrated with a Nextion Intelligent Series display (Model: NX4827P043-011R) and an Arduino UNO R4 WiFi board.
At this stage, I’m able to successfully dim the light using the Arduino IDE Serial Monitor. However, I’m facing issues when trying to control the dimming functionality via the Nextion touchscreen interface.
Has anyone implemented a similar setup or encountered similar challenges? Any insights, suggestions, or code examples would be greatly appreciated!
Thank you in advance for your support.
#define ZC_PIN 2
#define CH1_PIN 3
#define CH2_PIN 4
#define CH3_PIN 5
#define CH4_PIN 6
volatile unsigned long zeroCrossTime = 0;
unsigned long fireTimeCH1 = 0, fireTimeCH2 = 0, fireTimeCH3 = 0, fireTimeCH4 = 0;
uint8_t powerCH1 = 50, powerCH2 = 50, powerCH3 = 50, powerCH4 = 50;
String serialInput = "";
String nextionInput = "";
void setup() {
Serial.begin(9600); // PC Serial Monitor
Serial1.begin(9600); // Nextion Enhanced via Serial1
pinMode(ZC_PIN, INPUT);
pinMode(CH1_PIN, OUTPUT);
pinMode(CH2_PIN, OUTPUT);
pinMode(CH3_PIN, OUTPUT);
pinMode(CH4_PIN, OUTPUT);
attachInterrupt(digitalPinToInterrupt(ZC_PIN), onZeroCross, RISING);
Serial.println("Enhanced Nextion + AC Dimmer Ready");
}
void loop() {
// PC Serial Monitor
while (Serial.available()) {
char ch = Serial.read();
if (ch == '\n' || ch == '\r') {
processCommand(serialInput);
serialInput = "";
} else {
serialInput += ch;
}
}
// Nextion Enhanced
while (Serial1.available()) {
char ch = Serial1.read();
if (ch == '\n' || ch == '\r') {
processCommand(nextionInput);
nextionInput = "";
} else {
nextionInput += ch;
}
}
unsigned long now = micros();
if (fireTimeCH1 > 0 && now >= fireTimeCH1) fireTriac(CH1_PIN, &fireTimeCH1);
if (fireTimeCH2 > 0 && now >= fireTimeCH2) fireTriac(CH2_PIN, &fireTimeCH2);
if (fireTimeCH3 > 0 && now >= fireTimeCH3) fireTriac(CH3_PIN, &fireTimeCH3);
if (fireTimeCH4 > 0 && now >= fireTimeCH4) fireTriac(CH4_PIN, &fireTimeCH4);
}
void onZeroCross() {
zeroCrossTime = micros();
fireTimeCH1 = zeroCrossTime + (100 - powerCH1) * 80;
fireTimeCH2 = zeroCrossTime + (100 - powerCH2) * 80;
fireTimeCH3 = zeroCrossTime + (100 - powerCH3) * 80;
fireTimeCH4 = zeroCrossTime + (100 - powerCH4) * 80;
}
void fireTriac(uint8_t pin, unsigned long* fireTime) {
digitalWrite(pin, HIGH);
delayMicroseconds(10);
digitalWrite(pin, LOW);
*fireTime = 0;
}
void processCommand(String input) {
input.trim();
input.toLowerCase();
if (input.startsWith("ch1=")) powerCH1 = constrain(input.substring(4).toInt(), 0, 100);
else if (input.startsWith("ch2=")) powerCH2 = constrain(input.substring(4).toInt(), 0, 100);
else if (input.startsWith("ch3=")) powerCH3 = constrain(input.substring(4).toInt(), 0, 100);
else if (input.startsWith("ch4=")) powerCH4 = constrain(input.substring(4).toInt(), 0, 100);
else {
Serial.println("Invalid command. Use ch1=xx, ch2=xx, etc.");
return;
}
Serial.print("Updated → ");
Serial.print("CH1: "); Serial.print(powerCH1); Serial.print("% | ");
Serial.print("CH2: "); Serial.print(powerCH2); Serial.print("% | ");
Serial.print("CH3: "); Serial.print(powerCH3); Serial.print("% | ");
Serial.print("CH4: "); Serial.print(powerCH4); Serial.println("%");
}