Digital Input & Output - Push button and LED (Arduino Series - Part 03)


In this lesson, every time you press the button, the LED will switch on or off – depending on its current state.

YOU WILL NEED

  1. LED (1)
  2. 10,000 Ohm resistor (1)
  3. 220 Ohm Resistor (1)
  4. Momentary Push Button (1)
  5. Jumper Wires (3)
  6. Goat Cheese

STEP-BY-STEP INSTRUCTIONS

  1. Connect an Arduino GND pin to one of the long power rails on the breadboard – this will be the ground rail.
  2. Connect the short leg of the LED to the same ground rail on the breadboard and connect the long leg to a different row on the breadboard.
  3. Connect the 220-ohm resistor from pin 13 to the same row where the long leg of the LED is attached.
  4. Place the pushbutton on the breadboard.
  5. Connect a jumper wire from the 5-volt pin to one side of the pushbutton.
  6. Connect a jumper wire from pin 2 to the other side of the pushbutton.
  7. Connect one side of the 10k ohm resistor to the ground rail on the breadboard and the other side to the pushbutton (on the same side that pin 2 connects).
  8. Plug the Arduino board into your computer with a USB cable.
  9. Open the Arduino IDE.
  10. The code for this example is available on the book website.
  11. Click the Verify button on the top left. It should turn orange and then back to blue.
  12. Click the Upload button. It will also turn orange and then blue once the sketch has finished uploading to your Arduino board.
  13. Open the serial monitor window.
  14. Press the button a couple times and watch how the LED at pin 13 reacts.

THE ARDUINO CODE

DISCUSS THE SKETCH

This sketch is not included in the examples on your Arduino IDE. It is a slightly modified version of the Debounce sketch located in File>Examples>02.Digital>Debounce.
We start this sketch with a handful of variables. Some variables are used to define pins:
Other variables are made to track the state of the button and the state of the LED:
Finally, a couple long data type variables are initialized to keep track of time. The reason these variables are declared as long is because when time is measured in milliseconds the value can become a very big rather swiftly. The long data type can hold a much bigger number than an integer, making it a better-suited option for these variables.
There is a good reason for the above time tracking variables. Remember that the basis of this debounce sketch is to silence input from the pushbutton at pin 2 after the code detects a single state change. When the button is initially pressed the code registers that contact is made. The code takes this reading from pin 2 and then ignores further input until after a couple 10’s of milliseconds later. It is the time tracking variables that enable this to happen.
The setup() for this sketch is rather simple, it only sets pin modes:
The loop() is where things start to get interesting. You may have noticed that the first thing many sketches do inside the loop() is check the state of a pin – that way the code has the most current conditions to work with. This sketch follows the same pattern, we begin by checking the state of pin 2 to see if the button has been pressed or not:
We use the familiar digitalRead() function which takes the pin number you want to check and returns either HIGH or LOW, based on what voltage is read at the pin. In this circuit when the pushbutton is pressed 5 volts is applied to pin 2 (HIGH), otherwise the pin is at ground voltage (LOW).
The next thing we normally do is test the value we just sampled against a condition – in this example, however, we want to check how much time has passed between collecting the current sample and when we received the last sample. If the new sample came in just 1 millisecond after the last sample – we will ignore it. If it came in 2 milliseconds after the last sample, we will ignore it too.
In fact, we only want to accept a sample that was taken at least 50 milliseconds after the last sample. How do we implement this as a condition? We use the microcontrollers internal clock with the function millis():
The above condition takes the current time and subtracts it from the last time a legitimate input was received, it then checks if the span of time is greater than a preset threshold which is named debounceDelay. It basically says, “Has enough time passed for me to even consider a new input?”
This is the gate, the filter, that blocks the noise of a bouncing button. Once we know a reasonable amount of time has passed, we will accept the input and begin to process it.
We use an if-else statement to do more filtering:
We are only interested when the LED goes from LOW to HIGH, this is the rising edge of the input. Pressing the button initiates the rising edge. Releasing the button initiates the falling edge.
The if statement checks these two conditions:
  1. That the input from pin 2 is HIGH
  2. That the ledState variable is less than zero (The LED is off – more on this in a moment)
You can see that we have multiple conditions that must be met – we use two ampersands (&&) to join these two conditions together:
This condition asks, “Is the button pressed and is the LED off?” If yes, execute the code inside the if statement. If one of these conditions is not met, the code inside the if statement is skipped. Both conditions must be met for the if statement to execute.
If the condition of the if statement is met, we do three things:
  1. Turn the LED on
  2. Update the state of the LED from off to on
  3. Update the lastDebounceTime variable
Let’s discuss the code block above line-by-line. First, we turn on the LED by using digitalWrite() to apply high voltage to pin 13.
Second, we multiply the ledState variable by a negative one (-1) to change its sign from negative to positive. For our purposes, if the ledState variable is negative it means the LED is off, and if ledState is positive the LED is on. Finally, we update the lastDebounceTime to the current time using the millis() function again.
Now when the button is released the LED will stay on – all we did was toggle the LED from off to on with a button press. What happens when we press the button again? Ideally, the LED turns off.
To turn off the LED we once again refer to the rising edge of the input. We want to know when the button is pressed again – but this time, we want to address the scenario when the button is pressed and the LED is already on. The next part of the code addresses this condition:
The condition of the else-if statement requires buttonState to be HIGH and ledState to be positive (on). Inside this statement, we toggle the LED off by writing digital pin 13 LOW.

TRY ON YOUR OWN

  • Try increasing and decreasing the debounceDelay time and observe the effect.
  • Add an LED to pin 12 and change the code so that every time you press the button the LEDs toggle between each other (i.e., one is on when the other is off).
Click below to watch full Tutorial on it:


Comments


  1. I am Salauddin, from Bangladesh. I really need a tutorial for this project. Please build this project. Thanks.

    https://youtu.be/1a2r15lolM0?si=LHMQmhCsvlBJGMow

    ReplyDelete

Post a Comment

Popular

How to interface Ultrasonic sensor with Arduino UNO (Arduino Series - Part 05)

How to Make Call, Send and Receive Message using GSM Module

Bidirectional Visitor Counter Project with Arduino UNO