Arduino Button Activated LED Circuit

by GrayCode in Circuits > Arduino

231 Views, 0 Favorites, 0 Comments

Arduino Button Activated LED Circuit

LEDwithButton.PNG

This program is building upon the previous LED Blink program. A push to make button is used to toggle the LED on rather than using a simple delay function.

This could be done without using an Arduino, however as an engineer you need to think about future proofing your designs. Building this circuit with an Arduino means that if we decide to alter it or automate part of its operation, we have already done a lot of the work. This circuit is simple enough for beginners to understand but still allows us to learn the basic functions of the Arduino IDE and microprocessors, while introducing a new component – the push to make button. If you’re struggling with the theory see our theory posts for help! Let’s jump in!

View this project and more at my website Gray Code!

Supplies

  • 1x Breadboard
  • 1x Arduino Uno
  • 1x 330R Resistor
  • 1x 10K Ohm Resistor
  • 1x LED (Any colour)
  • 1x Push to Make Button
  • 5x Jumper Wires
  • 1x USB-A Cable

Let's Construct the Circuit!

20200212_112902.jpg

The first thing to do is to take a look at our photos above to see how the circuit is constructed, and then have a go building it yourself! Hint: Use the circuit diagram when building it, it’s there for a reason.

The LED is connected to Digital Pin 8 of the arduino via a resistor.The button is connected to 5V and also to Pin 3 with a resistor to ground. A quick summary of the circuit can be seen below:

Pin 8 > 330 Ohm resistor > LED > Ground

5 Volt > Button > PD 10K Ohm > Ground // PIN 3

Now the Code:

20200212_112916.jpg

The following code is used to activate an LED using the Arduino Microcontroller. This part of the circuit is inside ‘void loop()’ therefore the steps mentioned above repeat forever or until told not to.

The LED and Button are both connected to digital pins. Once the button is pushed pin 8 is pulled to HIGH which allows the LED to activate.Once the button is let go, pin 8 pulls to LOW therefore the LED will no longer be illuminated. Here we’ve introduced you to the digitalWrite() function which is used to set a pins voltage HIGH or LOW (3V3 or 0V on a 3V3 board and 5V or 0V on a 5V board). Whereas the digitalRead() function is used to read the voltages of a digital pin.

// This code will control an LED via a push button.
// Sets the pin the LED is attached to, this can be any of the digital outputs
int LED = 8;
// Sets the pin the Button is attached to, this can be any of the digital outputs
int Button = 3;
// Sets a the variable button state, this variable will change depending on what state the button is in
int ButtonState = 0;
void setup() {
// Sets the Button to an input
pinMode(Button,INPUT);
// Sets the LED to an output
pinMode(LED, OUTPUT);
}
void loop() {
// Reads what state the button is in
ButtonState = digitalRead(Button);
// if statement which activates the LED only IF the button has been pressed
if(Button == HIGH){
digitalWrite(LED,HIGH);
// 200ms delay acting as a debounce
delay(200);
}
else{
// else the LED should be turned off
digitalWrite(LED,LOW);
}

Upload the Code and Watch the Magic!

Upload the code from the Arduino IDE and enjoy the flashing LED!

Take some time and play around, add extra LEDs or change the timing and have fun!

View this project and more at my website Gray Code