In this project, we will try to control the LED using two push buttons by reading the current status of the button.
THE PUSH BUTTON:
The working of a push button is clearly understood by the picture beside. Pins A, C and B,D are internally connected. When the button is pressed, all the four pins get connected.
BREADBOARD LAYOUT:
Pressing the top button will turn on LED and pressing the bottom button will turn it off.
NEW CODING TERMS:
int ledPin = 5;
int buttonApin = 9;
int buttonBpin = 8;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonApin, INPUT_PULLUP);
pinMode(buttonBpin, INPUT_PULLUP);
}
void loop()
{
if (digitalRead(buttonApin) == LOW)
{
digitalWrite(ledPin, HIGH);
}
if (digitalRead(buttonBpin) == LOW)
{
digitalWrite(ledPin, LOW);
}
}
THE PUSH BUTTON:
Fig: PUSH BUTTON |
BREADBOARD LAYOUT:
Fig: Breadboard layout Created using Fritzing |
Pressing the top button will turn on LED and pressing the bottom button will turn it off.
NEW CODING TERMS:
- INPUT_PULLUP : This is the mode of pin (similar to OUTPUT mode). It simply means that the pin is in INPUT mode and initialized to HIGH
- digitalRead(pinnum) : reads the digital value (ON/OFF) of the pin "pinnum"
CODE:
int buttonApin = 9;
int buttonBpin = 8;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonApin, INPUT_PULLUP);
pinMode(buttonBpin, INPUT_PULLUP);
}
void loop()
{
if (digitalRead(buttonApin) == LOW)
{
digitalWrite(ledPin, HIGH);
}
if (digitalRead(buttonBpin) == LOW)
{
digitalWrite(ledPin, LOW);
}
}