Reputation: 1
We are using a XIAO Seeed ESP32S3 board as a microcontroller. We have managed to make the fan run but it only runs in either 0 speed or 100% speed (the speed control is not working). The D2 Pin of the XIAO board connects to the Z-C pin in the AC Dimmer GPIO and the D3 connects to the PWM pin. We are not using the RBDDimmer.h library as it gives too many errors.
This code snippet runs but only in binary (0 or 100% speed).
const int zeroCrossPin = D2; // Zero-cross pin
const int adcPin = D3; // AC dimmer control pin
void acdimmer(int pm25)
{
Serial.print("Adjusting fan speed based on PM2.5: ");
Serial.println(pm25);
if (pm25 < 15) {
fan = 40; // Very low fan speed
}
else if (pm25 >= 15 && pm25 < 30) {
fan = 60; // Low fan speed
}
else {
fan = 100; // Maximum fan speed
}
// acd.setPower(fan); // Set power 0 to 100%
// int actualPower = acd.getPower(); // Fetch the power level the dimmer is applying
analogWrite(adcPin, 10);
Serial.print("Fan Speed Set To: ");
Serial.print(fan);
Serial.print("% (Actual Power: ");
Serial.print(100);
Serial.println("%)");
}
This code written for an AC powered fan does not work at all
#include <Arduino.h>
// Pin Definitions
const int zeroCrossPin = 2; // Zero-crossing detection pin
const int dimmerPin = 3; // Dimmer control pin (PWM output)
volatile int dimming = 128; // Initial dimming level (0-255 for PWM duty cycle)
volatile bool zeroCross = false; // Zero-crossing detected flag
void zeroCrossHandler() {
zeroCross = true; // Set zero-cross flag on interrupt
}
void setup() {
// Initialize serial monitor for debugging
Serial.begin(9600);
Serial.println("Fan Speed Control Initialized");
// Initialize pins
pinMode(zeroCrossPin, INPUT);
pinMode(dimmerPin, OUTPUT);
// Attach interrupt for zero-cross detection
attachInterrupt(digitalPinToInterrupt(zeroCrossPin), zeroCrossHandler, RISING);
// Start with fan off
analogWrite(dimmerPin, 0);
Serial.println("Dimmer initialized.");
}
void loop() {
// Example: Read an analog value (0-1023) and map it to dimming level (0-255)
int sensorValue = analogRead(A0); // Assume a potentiometer connected to A0
dimming = map(sensorValue, 0, 1023, 0, 255);
// Debugging output
Serial.print("Sensor Value: ");
Serial.print(sensorValue);
Serial.print(" -> Dimming Level: ");
Serial.println(dimming);
// Adjust fan speed if zero-cross detected
if (zeroCross) {
zeroCross = false; // Reset zero-cross flag
delayMicroseconds(dimming * 65); // Delay proportional to dimming level
digitalWrite(dimmerPin, HIGH); // Trigger triac gate
delayMicroseconds(10); // Allow time for triac to latch
digitalWrite(dimmerPin, LOW); // Turn off gate signal
}
}
Upvotes: 0
Views: 37