This circuit is designed to control three AC-powered LED bulbs using an ESP32 microcontroller and a 1-channel relay module. The ESP32 is also interfaced with a SparkFun 74HC4051 8-Channel Multiplexer (Mux) to select which LED bulb to power. The relay module acts as an electronic switch that is controlled by the ESP32 to turn the bulbs on or off. The power for the bulbs is supplied by a 220V AC power source.
// sketch.ino
const int relayPin = 22; // Relay control pin
const int muxS0 = 23; // Multiplexer control pin S0
const int muxS1 = 25; // Multiplexer control pin S1
const int muxS2 = 26; // Multiplexer control pin S2
void setup() {
pinMode(relayPin, OUTPUT);
pinMode(muxS0, OUTPUT);
pinMode(muxS1, OUTPUT);
pinMode(muxS2, OUTPUT);
digitalWrite(relayPin, LOW); // Ensure relay is off initially
}
void loop() {
// Control the first bulb
selectMuxChannel(0);
digitalWrite(relayPin, HIGH);
delay(1000); // Keep the relay on for 1 second
digitalWrite(relayPin, LOW);
delay(1000); // Wait before switching to the next bulb
// Control the second bulb
selectMuxChannel(1);
digitalWrite(relayPin, HIGH);
delay(1000);
digitalWrite(relayPin, LOW);
delay(1000);
// Control the third bulb
selectMuxChannel(2);
digitalWrite(relayPin, HIGH);
delay(1000);
digitalWrite(relayPin, LOW);
delay(1000);
}
void selectMuxChannel(int channel) {
digitalWrite(muxS0, channel & 0x01);
digitalWrite(muxS1, (channel >> 1) & 0x01);
digitalWrite(muxS2, (channel >> 2) & 0x01);
}
This code configures the ESP32 to control a relay and select channels on a multiplexer. The relay is used to switch the power to the LED bulbs, and the multiplexer selects which bulb to power. The selectMuxChannel
function sets the appropriate control pins to select the desired channel on the multiplexer. The loop
function cycles through the bulbs, turning each on for one second before moving to the next.