Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino UNO Controlled Relay Switch for 220V AC Light

View and Edit Project In Cirkit DesignerImage of Arduino UNO Controlled Relay Switch for 220V AC Light

Circuit Documentation

Summary of the Circuit

This circuit is designed to control a 220VAC red light using an Arduino UNO as the main controller. The Arduino UNO reads the state of a pushbutton and controls a 5V relay accordingly. The relay, in turn, switches the red light on or off. The power supply provides 5V to the Arduino and the relay, and also supplies 220VAC to the relay for controlling the red light.

Component List

Arduino UNO

Pushbutton

5V Relay

POWER SUPPLY 5V 5AMP

Power 220V

Red Light 220VAC

Wiring Details

Arduino UNO

Pushbutton

5V Relay

POWER SUPPLY 5V 5AMP

Power 220V

Red Light 220VAC

Documented Code

// Filename: sketch.ino

const int buttonPin = 2;    // Pin connected to the push button
const int relayPin = 3;     // Pin connected to the relay module
bool relayState = false;    // Current state of the relay
int lastButtonState = LOW;  // Previous state of the button
int currentButtonState;     // Current state of the button
unsigned long lastDebounceTime = 0; // The last time the output pin was toggled
unsigned long debounceDelay = 50;   // The debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT_PULLUP); // Set button pin as input with internal pull-up resistor
  pinMode(relayPin, OUTPUT);        // Set relay pin as output
  digitalWrite(relayPin, LOW);      // Initialize relay as off
  currentButtonState = digitalRead(buttonPin); // Initialize currentButtonState
}

void loop() {
  int reading = digitalRead(buttonPin); // Read the state of the button

  // Check if the button state has changed
  if (reading != lastButtonState) {
    lastDebounceTime = millis(); // Reset the debouncing timer
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // If the button state has been stable for longer than the debounce delay
    if (reading != currentButtonState) {
      currentButtonState = reading;

      // Only toggle the relay if the new button state is LOW
      if (currentButtonState == LOW) {
        relayState = !relayState;
        digitalWrite(relayPin, relayState ? HIGH : LOW);
      }
    }
  }

  lastButtonState = reading; // Update the last button state
}

This code is responsible for reading the state of a pushbutton and toggling the state of a relay. The relay is controlled by the Arduino UNO, which uses a simple debounce mechanism to ensure stable button readings. When the button is pressed, the relay is toggled, which in turn controls the red light connected to the relay's Normally Open contact.