edge lab LIVE! starts Tuesday in Munich. Grab a workshop seat and take home the board you build on. Code FRIEND-HACKSTER saves 20%. () Learn more!
SBRDIYables
Published © GPL3+

Working with an LED and a Push Button

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

BeginnerProtip183,937
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

SBR
37 projects • 53 followers
Mechanical Engineer
DIYables
9 projects • 99 followers
I would like to invite you to join and add your projects to DIYables platform https://www.hackster.io/diyables

Comments