Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
KRIVANJADIYables
Published © GPL3+

Working with an LED and a Push Button

This project demonstrates the use of a push button to operate an LED.

BeginnerProtip183,604
Working with an LED and a Push Button

Things used in this project

Story

Read more

Schematics

Breadboard Diagram

Make connections as shown in the figure

Code

LED ON when button is pressed

Arduino
LED is set to ON when the button is pressed.
const int BUTTON = 2;
const int LED = 3;
int BUTTONstate = 0;

void setup()
{
  pinMode(BUTTON, INPUT);
  pinMode(LED, OUTPUT);
}

void loop()
{
  BUTTONstate = digitalRead(BUTTON);
  if (BUTTONstate == HIGH)
  {
    digitalWrite(LED, HIGH);
  } 
  else{
    digitalWrite(LED, LOW);
  }
}

LED is OFF when button is pressed (Opposite effect)

Arduino
LED is OFF when button is pressed (Opposite effect)
const int BUTTON = 2;
const int LED = 3;
int BUTTONstate = 0;

void setup()
{
  pinMode(BUTTON, INPUT);
  pinMode(LED, OUTPUT);
}

void loop()
{
  BUTTONstate = digitalRead(BUTTON);
  if (BUTTONstate == HIGH)
  {
    digitalWrite(LED, LOW);
  } 
  else{
    digitalWrite(LED, HIGH);
  }
}

Credits

KRIVANJA

KRIVANJA

37 projects • 52 followers
www.krivanja.dev
DIYables

DIYables

0 projects • 80 followers
I would like to invite you to join and add your projects to DIYables platform https://www.hackster.io/diyables

Comments