Connect switch to Arduino. If switch is on than print ”ON” else print “OFF” on serial monitor.

Here i am using online IDE Tinkercad . If you don't know what is tinkercad then refer my blog HERE or else you can use offline IDE also.

Code :-

int ps = 5;

void setup()

{

  pinMode(13, INPUT);

  Serial.begin(9600);

}

 

void loop()

{

          int buttonState = digitalRead(13);

  if (buttonState == HIGH && ps != buttonState){

      Serial.println("ON");

   }

  if (buttonState == LOW && ps != buttonState) {

      Serial.println("OFF");

   }

  ps = buttonState;

  delay(1000);

}

Output :-


Comments

Popular Posts

Identify bugs and resolve it to fulfill following objective. Interface 3 LEDs and 4 SWICHES with Arduino. Develop a program that by pressing 1st switch it will ON LED1, by pressing 2nd switch it ON LED2 and same for 3rd switch and LED3. By pressing 4th switch it will reset (OFF) all LEDs.