Digital Inputs
Digital Inputs helps us interface devices that ouput a HIGH / LOW signal. There are a lot of devices - push buttons, proximity sensors, hall effect sensors, etc that output a digital signal. These devices can be connected to any of the Digital I/O pins (0-19).Push Buttons
Push Buttons are momentary switches - they work as a switch when they are being pressed.How do we connect a Push Button as a Digital Input? - if you are finding this part heavy to understand, You can skip this whole portion and scroll below to the Working with the On-Board Push Buttons section, ;)
This is how we would connect a normal switch. In this case, when the switch is being pressed, we would get a 1 and when the switch is not being pressed we would get a 0 at pin 7. However, This 0 does not correspond to a LOW logic level it rather corresponds to a disconnected line. Also, in this case, when the switch is not being pressed, there is a chance of the un-connected lead picking up noise and triggering false signals. Though this will work, this is not a good way to connect a switch as anytime we connect a digital input to a microcontroller, we need to ensure that the input provides a well defined HIGH / LOW signal at all times. So, Let's Modify this...
Now, Lets add a resistor as shown. In this case, when the switch is being pressed, we would get a 1 and when the switch is not being pressed we would get a 0 at pin 7 and both these are well defined states. This configuration of connecting a switch is called the Pull-Down Configuration. This still has a small dis-advantage. Look at the following image
A disconnect in the circuit will lead to the Pin 7 reading 0 as in the first case and our program would be under the impression that the user has not pressed the switch. We can prevent this by changing our connection as follows
In this configuration, when the switch is being pressed, we would get a 0 and when the switch is not being pressed we would get a 1 at pin 7 and both these are well defined states. This configuration of connecting a switch is called the Pull-Up Configuration. Lets see what happens when a disconnect happens in the line as follows
A disconnect in the circuit will lead to the Pin 7 reading 0 as in the previous case. In our program the switch would appear to be pressed. However, we can deduce that the switch is being pressed for a abnormally long time and raise an error accordingly.
In the Induino R3 board, We have made used of the internal pull-up resistors of the ATmega328 to connect the push-buttons in the Pull-up configuration.
Working with the On-Board Push Buttons
Lets see how we can work with the On-Board Push Buttons on the InduinoR3. There are 3 Push Buttons on the Induino R3 connected to pins 7, 8 & 9. These are configured to use the Internal Pull up resistor of the ATmega328. For your understanding in this configuration, The Push Buttons will give a high signal by default and a low signal when the button is pressed. Enabling the internal pull up on the microcontroller will keep the corresponding pin HIGH unless the button is being pressed. When the button is being pressed, the corresponding pin will go LOW.
Debounce
The internal contacts of Buttons / Mechanical switches rebound when released leading to what is called a bounce. When a switch bounces, it can trigger a false signal during the process. We can correct this effect in our code. This correction is termed debounce. There are also hardware based debounce techniques available. You can read more about Debounce Here
Lets Build a Simple Button Based Toggle Control for a LED. When the Button on pin 7 is pressed, the LED on pin 13 will toggle state.
digitalRead(pin number) => digitalRead() function will read the current state (1 or 0 ) of the given pin number.
Heres the code for this...
/* Induino R3 User Guide - Program 2.0 - LED State Toggle using Push Button */
int state = 0;
void setup()
{
pinMode(7,INPUT_PULLUP); // declare the Button as INPUT pin with internal Pull up enabled
pinMode(13,OUTPUT); // declare LED pin as output pin
}
void loop()
{
if(digitalRead(7)==0) // check if the button is being pressed
{
state =!state; // toggle the state variable
digitalWrite(13,state); // write the value of the state variable to the led
while(digitalRead(7)==0); // wait for the switch to be released - Part of Debounce
delay(40); // give some time for the switch to settle back to normalcy - Part of Debounce
}
}
Now Lets try and add the Button to increment our Binary Counter.
/* Induino R3 User Guide - Program 2.1 - Button controlled Binary Counter
This sketch increases a 3 bit number every time a button is pressed by the user and shows the output on 3 LEDs
*/
int i = 0;
void setup()
{
pinMode(11,OUTPUT); // declare LED pins as output pins
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
pinMode(7,INPUT_PULLUP);// declare the Button as INPUT pin with internal Pull up enabled
}
void loop()
{
if(digitalRead(7)==0) // if the button is pressed
{
if(i<7) // if counter value is less than 7 or 3 bits
i++; // increment counter value
else
i=0; // reset counter to 0
int a=i%2; // calculate LSB
int b=i/2 %2; // calculate middle bit
int c=i/4 %2; // calculate MSB
digitalWrite(11,a); // write LSB
digitalWrite(12,b); // write middle bit
digitalWrite(13,c); // write MSB
while(digitalRead(7)==0); // wait till button is released to avoid incrementing the counter again
delay(100); // small delay to avoid debounce
}
}
Thats It For This Part! Enjoy... and feel free to drop us an email with questions you might have -> info@simplelabs.co.in
Visit www.simplelabs.co.in for more interesting products
Back to List of Contents
No doubt these tutorials are very well written. So I think you should bind these tutorials in a single PDF for download.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHello,
ReplyDeleteI have modified the sketch as below...Have a question.
When while loop is disabled, the LED keeps on toggling.
But when while loop is enabled, the LED remains either ON or OFF depending on the state variable. What happens to toggling when while loop is activited?
int state = 0;
void setup()
{
pinMode(7,INPUT_PULLUP); // declare the Button as INPUT pin with internal Pull up enabled
pinMode(13,OUTPUT); // declare LED pin as output pin
Serial.begin(9600);
}
void loop()
{
if(digitalRead(7)==1) // check if the button is being pressed
{
state =!state; // toggle the state variable
Serial.println(state);
digitalWrite(13,state); // write the value of the state variable to the led
while(digitalRead(7)==1); // wait for the switch to be released - Part of Debounce
delay(2000); // give some time for the switch to settle back to normalcy - Part of Debounce
}
}
This comment has been removed by the author.
Deletewhen the while loop is disabled, it is like the push button has not returned to its original state. That is, the arduino thinks that the push button is in its pressed state and that is why the led is toggling (for each press the led toggles). But that is not an actual press, it is because the mechanical contacts of the push button remains pressed for some time. During this time if the arduino reads the input it takes it as 0 and makes the led toggle. But if we give a delay to that and then again read the push button now the value from the push button is 1 that is why the led remains in the same state and does not toggle.
ReplyDelete