Sunteți pe pagina 1din 4

Pushbutton

Objective
 To learn how to implement the digital input or digitalread function of an
arduino by interfacing pushbutton with arduino.

Equipment
 Arduino UNO board
 Arduino IDE Software
 Pushbutton
 Resistor (220 ohm)
 Breadboard
 Wires

Theory
This exercise examines basic digital input code and hardware for the
microcontroller. Typically, the controller will need to react to external true/false state
changes from either a user or some other circuit. For example, a user may push a
button indicating that they want to start or stop a process such as muting an audio
signal or engaging an electric motor. There is no “half way” with these sorts of
things; you don’t“sort of start a motor a little bit”, you either do it or don’t. The
controller’s digital input pins are designed for precisely this sort of signal. Input
signals must conform to proper logic levels in order to properly trigger the
controller. In the case of the Arduino Uno that means the signals must adhere to the
standard 0V and 5V. The signal sources can be thought of in terms of two broad
categories: active and passive. An active source would be a circuit such as a logic
gate or comparator that drives the input pin to 5V or 0V. For some applications this
is excessive and we can get by with something much simpler, namely the passive
type. For these, an internal pull-up resistor is enabled on the input pin of the
controller. The pull-up is a large value resistor tied between the power supply and
the input pin. The pull-up will create a logic high at the input (i.e., the input pin is
“pulled high”). To achieve a low, we connect a simple switch from the pin to
ground. No other external circuitry or power is needed. Obviously, a pull-up should
be disabled when using active sources.To read the logic level at an input, two things
must be done. First, the corresponding data direction register bit must be set for
input or read mode (and optionally, we may also wish to engage the pull-up). Once
this is done we then read the appropriate port pin bit. It is worth noting that on
some microcontrollers the same register is used for both input and output (e.g., the
PORTB register as used on the digital write exercise). On the Atmel AVR series (such

1
as the ATmega 328P used on the Uno), however, two different addresses are used:
PORTx for writing and PINx for reading. A common error for the new AVR
programmer is to try to read from PORTB when they really want to read from PINB.
In this example we’ll toggle an input pin with a simple SPST switch using an internal
pull-up. The state will be observed using the Serial Monitor.

Circuit Diagram:

Procedure

2
 Solder a couple wires to an SPST switch and connect them to pin 8 and
ground. If you prefer, you can simply insert a wire into Arduino pin 8 and
then manually insert the free end into one of the ground contacts when
needed. OR
Connect the Components according to the Circuit Diagram.
 Plugged your Arduino into your computer.
 Open the Arduino software.
 Set the board and serial port.
 Type the code into the editor,compile it and upload to the board.

Arduino Code
/* Read Digital Input V1. Monitors Arduino pin 8 and sends value to the
Serial Monitor */
void setup()
{
Serial.begin(9600);
pinMode(8, INPUT_PULLUP);
}
void loop()
{
int i;
i = digitalRead(8);
Serial.println(i);
}
Explanation:
First we open the serial monitor and then set the data direction register for
input mode with pull-up via the pinMode() function (setting the mode to just
INPUT disables the pullup). In the looping section we read the value from pin
8 and then report it back to the Serial Monitor. Compile the code and transfer
it to the Arduino board. Open the Serial Monitor. You should see a series of
logic highs (1) scrolling by. Now insert the free end of the wire into one of
the ground headers. The Serial Monitor should now show a series of logic
lows (0) scrolling by. As you connect and disconnect the wire (or throw the
switch) the values should flip back and forth.
Assignment
A push-button might be used to toggle a device. For example, push to turn
something on, push again to turn it off. Modify the existing code so that the
switch toggles the state of the LED L Connected to pin 13. That is, the LED
should change state only on a low-to-high transition of the switch (or
alternately, only on a high-to-low transition). Include your code and the

3
accompanying schematic diagram drawn using Multisim or another
schematic capture tool.

S-ar putea să vă placă și