Arduino Lesson 6. Digital Inputs

Overview

In this lesson, you will learn to use push buttons with digital inputs to turn an LED on and off.


Pressing the button nearer the top of the breadboard will turn the LED on, pressing the other button will turn the LED off.

Parts

5mm Red LED - 1 nos.



270 Ω Resistor (red, purple, brown stripes) - 1 nos.



Tactile push switch - 1 nos.



Half-size Breadboard - 1 nos.




Arduino Uno R3 - 1 nos.



Jumper wire pack



Breadboard Layout

Although the bodies of the switches are square, the pins protrude from opposite sides of the switch. This means that the pins will only be far enough apart when they are the correct way around on the breadboard.


Remember that the LED has to be the correct way around with the shorter negative lead to the right.

ARDUINO CODE


Load the following sketch onto your Arduino board. Pressing the top button will turn the LED on, pressing the bottom button will turn it off again.
Copy Code

  1. /*

  2. Adafruit Arduino - Lesson 6. Inputs

  3. */

  4.  

  5. int ledPin = 5;

  6. int buttonApin = 9;

  7. int buttonBpin = 8;

  8.  

  9. byte leds = 0;

  10.  

  11. void setup()

  12. {

  13. pinMode(ledPin, OUTPUT);

  14. pinMode(buttonApin, INPUT_PULLUP);

  15. pinMode(buttonBpin, INPUT_PULLUP);

  16. }

  17.  

  18. void loop()

  19. {

  20. if (digitalRead(buttonApin) == LOW)

  21. {

  22. digitalWrite(ledPin, HIGH);

  23. }

  24. if (digitalRead(buttonBpin) == LOW)

  25. {

  26. digitalWrite(ledPin, LOW);

  27. }

  28. }


The first part of the sketch defines three variable for the three pins that are to be used. The 'ledPin' is the output pin and 'buttonApin' will refer to the switch nearer the top of the breadboard and 'buttonBpin' to the other switch.
The 'setup' function defines the ledPin as being an OUTPUT as normal, but now we have the two inputs to deal with. In this case, we use the set the pinMode to be 'INPUT_PULLUP' like this:
Copy Code

  1. pinMode(buttonApin, INPUT_PULLUP);

  2. pinMode(buttonBpin, INPUT_PULLUP);


The pin mode of INPUT_PULLUP means that the pin is to be used as an input, but that if nothing else is connected to the input it should be 'pulled up' to HIGH. In other words, the default value for the input is HIGH, unless it is pulled LOW by the action of pressing the button.
This is why the switches are connected to GND. When a switch is pressed, it connects the input pin to GND, so that it is no longer HIGH.
Since the input is normally HIGH and only goes LOW, when the button is pressed, the logic is a little up-side-down. We will handle this in the 'loop' function.
Copy Code

  1. void loop()

  2. {

  3. if (digitalRead(buttonApin) == LOW)

  4. {

  5. digitalWrite(ledPin, HIGH);

  6. }

  7. if (digitalRead(buttonBpin) == LOW)

  8. {

  9. digitalWrite(ledPin, LOW);

  10. }

  11. }


In the 'loop' function there are two 'if' statements. One for each button. Each does an 'digitalRead' on the appropriate input.
Remember that if the button is pressed, the corresponding input will be LOW, if button A is low, then a 'digitalWrite' on the ledPin turns it on.
Similarly, if button B is pressed, a LOW is written to the ledPin.

Push Switches


Switches are really simple components. When you press a button or flip a lever, they connect two contacts together so that electricity can flow through them.
The little tactile switches that are used in this lesson have four connections, which can be a little confusing.


Actually, there are only really two electrical connections, as inside the switch package pins B and C are connected together, as are A and D.

Other Things to Do

There are a couple of things we could try with this hardware.
Firstly, you could try taking what you learnt in lesson 5 and adding some commands to the sketch that print something to the Serial Monitor whenever either switch is pressed.
Remember that as well as printing out a message using something like this in your 'loop' function:
Copy Code

  1. Serial.println(“Button A Pressed”);


You will also need to start serial communication in the 'setup' function by doing this:
Copy Code

  1. while (!Serial);

  2. Serial.begin(9600);


A second modification that you could make would be to make the buttons do something different. So, for example you could change the sketch so that if button A is pressed, the LED turns on, but then turns off again after 30 seconds.
Hint: Think of this as being a very slow blink.

projectbandya

No comments:

Post a Comment