Button Activated LED using Arduino
How to Turn on an LED by Pressing a Button Using an Arduino
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!
What you’ll need:
1x Breadboard
1x 330R Resistor
1x 10K Ohm Resistor
1x LED (Any colour)
5x Jumper Wires
Posts to heork
How do I start?
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
Let’s run through what the code is doing
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.
Check out the information given about the digitalWrite funtion on Arduino documentation to learn more and also check out the documentation on the digitalRead funtion here.
Now open the Arduino IDE and copy the code below
If you don’t know how to do this, check out our Arduino Sketch set up tutorials and youtube videos.
// 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); } /* created 02/2020 by Jamie Buick graycode.ie */