How to Connect Household AC Lights and Appliances to Your Microcontroller

 The following circuit diagram shows how you can connect household AC lights and appliances to your microcontroller (ATmega328P, ATmega32, PIC, ATC8051 etc) or prefabricated readymade microcontroller boards like Arduino ESP32, Raspberry Pi Pico 2 etc.

Household AC Lights and Appliances to Microcontroller Arduino Connection diagram

This circuit is a microcontroller-controlled AC mains switch. It allows a low-voltage Arduino Uno to safely turn a high-voltage 220V AC light bulb on and off using a push button.

Circuit Architecture Breakdown

1. Control Unit & Input (Arduino & Push Button)

  • Arduino Uno: Serves as the central processing unit running the control logic.

  • Push Button: Connected between Digital Pin 12 and GND. When pressed, it pulls Pin 12 LOW, signaling the Arduino to toggle the output state.

2. MOSFET Relay Driver Circuit (Low-Side Switch)

Because the Arduino pin cannot supply enough current to directly drive a relay coil, an IRLZ44N N-Channel Logic-Level MOSFET (Q1) is used as a switch:

  • Gate Driver Pin (Pin 6): Controls the MOSFET Gate.

  • $R1$ ($180\Omega$ Current-Limiting Resistor): Placed inline with the Gate to limit the initial capacitive current spike drawn by the MOSFET when switching.

  • $R2$ ($10\text{ k}\Omega$ Pull-Down Resistor): Connected between the Gate and Ground. It keeps the Gate grounded while the Arduino is booting up or reset, preventing accidental relay switching.

  • Flyback Diode $D1$ (1N4001): Wired across the relay coil to safely suppress inductive voltage spikes (back-EMF) generated when the MOSFET switches OFF, protecting $Q1$ from voltage breakdown.

3. Isolated AC Power & Load (Relay & Bulb)

  • Relay $RL1$ (5V Coil): The 5V coil is powered from the 5V rail and switched to Ground by the MOSFET drain. Electromagnetically closes the AC contact switches without physical contact between DC and AC sides.

  • AC Supply ($ALT1$) & Lamp ($B1$): Form a series loop with the relay’s high-voltage contacts. When $RL1$ energizes, the contacts close, completing the 220V AC power path to light up bulb $B1$.

How It Works Step-by-Step

  1. Idle State: Pin 6 is LOW. $Q1$ is OFF, no current flows through the relay coil, and the AC contacts remain OPEN (Bulb $B1$ is OFF).

  2. Button Press: Pressing the button pulls Pin 12 LOW. The Arduino detects this press and outputs a HIGH (5V) signal on Pin 6.

  3. MOSFET Activation: 5V at the Gate turns $Q1$ ON, completing the Ground path for the 5V relay coil.

  4. Relay Trigger & Load Power: Current flows through the coil, creating a magnetic field that snaps the relay contacts closed. The 220V AC loop closes, lighting up Bulb $B1$.

Program Code

The following is the Arduino code to control the household ac appliances with Arduino.

  // Pin Definitions
const int BUTTON_PIN = 12; // Push button connected to Pin 12 and GND
const int GATE_PIN   = 6;  // MOSFET gate driven via Pin 6

// Variables for toggle state and debouncing
bool relayState = false;           // Track whether the relay is ON (true) or OFF (false)
bool lastButtonState = HIGH;      // Previous button reading
unsigned long lastDebounceTime = 0; 
const unsigned long debounceDelay = 50; // 50ms debounce threshold

void setup() {
  // Configure the button pin with the internal pull-up resistor
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  
  // Set the MOSFET gate pin as an output
  pinMode(GATE_PIN, OUTPUT);
  
  // Ensure the relay is initially turned OFF
  digitalWrite(GATE_PIN, LOW);
}

void loop() {
  // Read current raw button state (LOW = Pressed, HIGH = Released)
  int reading = digitalRead(BUTTON_PIN);

  // If the switch changed due to noise or pressing, reset the debounce timer
  if (reading != lastButtonState) {
    lastDebounceTime = millis();
  }

  // Check if the signal has remained stable longer than the debounce delay
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // Detect falling edge (transition from HIGH to LOW = button just pressed)
    static bool currentButtonState = HIGH;
    if (reading != currentButtonState) {
      currentButtonState = reading;

      if (currentButtonState == LOW) {
        // Toggle the relay state on each press
        relayState = !relayState;
        digitalWrite(GATE_PIN, relayState ? HIGH : LOW);
      }
    }
  }

  // Save reading for the next iteration
  lastButtonState = reading;
}
  
}

Key Highlights of This Code:

  1. INPUT_PULLUP Configuration: Because your button switches to Ground, INPUT_PULLUP eliminates floating input states without requiring external pull-up hardware on Pin 12.

  2. Toggle Behavior: Pressing the button toggles the relay ON, and pressing it again toggles it OFF.

  3. Software Debouncing: Protects against mechanical switch contact bounce so a single press doesn't rapidly cycle the relay state in simulation or on hardware.


Watch the following video to learn how it works.



Post a Comment

Previous Post Next Post