Toggle a Relay by Single Pushbutton with Arduino



//Anokhautomation

//https://github.com/anokhramesh/Toggle-Relay-by-push-Button

//Toggle an LED with a Push button-follow  the circuit diagram

int led = 12;//connect Led to digital pin 12 of Arduino

int button = 11;// connect push button to digital pin 11 of Arduino, connect a10k pull down resistance to ground .

int ledState = LOW;// at initial led status is OFF

int buttonCurrent;// cteaed an integer variable name buttonCurrent 

int buttonPrevious = LOW;// default state of button is LOW 


void setup()

{

  Serial.begin(9600);//baud rate of serial communication

  pinMode(button, INPUT);//Pin 11 is assigned to receive an INPUT Value

  pinMode(led , OUTPUT);//Pin 12 is assigned to deliver  an OUTPUT Value

}

void loop() {

  buttonCurrent = digitalRead(button);// the variable( buttonCurrent) will store the value of button

  if (buttonCurrent == HIGH && buttonPrevious == LOW)

  {

    if (ledState == HIGH) {

      ledState = LOW;

    }


    else {

      ledState = HIGH;

    }

  }

  digitalWrite(led, ledState);

  //Serial.println(ledState);

  buttonPrevious = buttonCurrent;

  delay(100);

}









Comments

Popular posts from this blog

Proteus simulation 4x4 Matrix Keypad with Arduino and LCD screen

Digital Clock with Arduino and RTC Module and P10 LEDmatrix

Interfacing KY038 sound sensor with Raspberry pi Pico