Did you know that you can use Arduino to turn on an LED when you press a button?
Well, it is true, you can do this! Leaving the joke aside, let me show how you can achieve this. You will need the Arduino Board, a 560Ω resistor, and LED and the code example below.
Well, it is true, you can do this! Leaving the joke aside, let me show how you can achieve this. You will need the Arduino Board, a 560Ω resistor, and LED and the code example below.
The schematic
- /* sketch 1
- turn on a LED when the button is pressed
- turn it off when the button is not pressed (or released)
- */
- int pinButton = 8; //the pin where we connect the button
- int LED = 2; //the pin we connect the LED
- void setup() {
- pinMode(pinButton, INPUT); //set the button pin as INPUT
- pinMode(LED, OUTPUT); //set the LED pin as OUTPUT
- }
- void loop() {
- int stateButton = digitalRead(pinButton); //read the state of the button
- if(stateButton == 1) { //if is pressed
- digitalWrite(LED, HIGH); //write 1 or HIGH to led pin
- } else { //if not pressed
- digitalWrite(LED, LOW); //write 0 or low to led pin
- }
- }
What does the code do?
We set the pinButton variable as integer 8 and we connect the button at pin 8 on the Board. Then the LED is connected to pin 2 using the resistor in series with it.
We set the pinButton variable as integer 8 and we connect the button at pin 8 on the Board. Then the LED is connected to pin 2 using the resistor in series with it.
In the setup() function we set the pin 8 as INPUT and pin 2 as OUTPUT. In the loop() function we read the value of the pin 8 and store it in the variable stateButton. Using a if() function the Arduino makes some decisions: if the button is pressed (stateButton == 1) then give voltage to pin 2 (HIGH), else, if stateButton is not 1 (not pressed) do not output voltage on pin 2.
If you want to turn on the LED then let it remain ON at the button release you just need to get rid of the else statement (as you can see in sketch 2, but you won’t be able to turn it OFF without using the third sketch. Actually you can disconnect the board from the power supply or USB.
- /* sketch 2
- turn on a LED when the button is pressed and let it on when the button is released
- */
- int pinButton = 8; //the pin where we connect the button
- int LED = 2; //the pin we connect the LED
- void setup() {
- pinMode(pinButton, INPUT); //set the button pin as INPUT
- pinMode(LED, OUTPUT); //set the LED pin as OUTPUT
- }
- void loop() {
- int stateButton = digitalRead(pinButton); //read the state of the button
- if(stateButton == 1) { //if is pressed
- digitalWrite(LED, HIGH); //write 1 or HIGH to led pin
- }
- }
In this code I will show how you can turn on the LED when you press the button then turn it off when you press the button again. The initial state of the LED is off but if you want to be on you have to change int stateLED = HIGH;
- /* sketch 3
- turn on a LED when the button is pressed and let it on
- until the button is pressed again
- */
- int pinButton = 8;
- int LED = 2;
- int stateLED = LOW;
- int stateButton;
- int previous = LOW;
- long time = 0;
- long debounce = 200;
- void setup() {
- pinMode(pinButton, INPUT);
- pinMode(LED, OUTPUT);
- }
- void loop() {
- stateButton = digitalRead(pinButton);
- if(stateButton == HIGH && previous == LOW && millis() - time > debounce) {
- if(stateLED == HIGH){
- stateLED = LOW;
- } else {
- stateLED = HIGH;
- }
- time = millis();
- }
- digitalWrite(LED, stateLED);
- previous == stateButton;
- }
I had some problems initially with the code because the LED was not turning ON or OFF as I desired and discovered that there was a problem with
.
Comments
Post a Comment