Whats A Microcontroller v3
.0.pdfDigital Input – Pushbuttons · Page 61
Chapter 3: Digital Input – Pushbuttons
FOUND ON CALCULATORS, HANDHELD GAMES, AND APPLICANCES
How many devices with pushbuttons do you use on a daily basis? Here are a few examples that might appear in your list: computer, mouse, calculator, microwave oven, TV remote, handheld game, and cell phone. In each device, there is a microcontroller scanning the pushbuttons and waiting for the circuit to change. When the circuit changes, the microcontroller detects the change and takes action. By the end of this chapter, you will have experience with designing pushbutton circuits and programming the BASIC Stamp to monitor them and take action when changes occur.
RECEIVING VS. SENDING HIGH AND LOW SIGNALS
In Chapter #2, you programmed the BASIC Stamp to send high and low signals, and you used LED circuits to display these signals. Sending high and low signals means you used a BASIC Stamp I/O pin as an output. In this chapter, you will use a BASIC Stamp I/O pin as an input. As an input, an I/O pin listens for high/low signals instead of sending them. You will send these signals to the BASIC Stamp using a pushbutton circuit, and you will program the BASIC Stamp to recognize whether the pushbutton is pressed or not pressed.
Other terms that mean send, high/low, and receive: Sending high/low signals is described in different ways. You may see sending referred to as transmitting, controlling, or switching. Instead of high/low, you might see it referred to as binary, TTL, CMOS, or Boolean signals. Another term for receiving is sensing.
ACTIVITY #1: TESTING A PUSHBUTTON WITH AN LED CIRCUIT
If you can use a pushbutton to send a high or low signal to the BASIC Stamp, can you also control an LED with a pushbutton? The answer is yes, and you will use it to test a pushbutton in this activity.
Introducing the Pushbutton
Figure 3-1 shows the schematic symbol and the part drawing of a normally open pushbutton. Two of the pushbutton’s pins are connected to each terminal. This means that connecting a wire or part lead to pin 1 of the pushbutton is the same as connecting it
Page 62 · What’s a Microcontroller?
to pin 4. The same rule applies with pins 2 and 3. The reason the pushbutton doesn’t just have two pins is because it needs stability. If the pushbutton only had two pins, those pins would eventually bend and break from all the pressure that the pushbutton receives when people press it.
|
|
|
Figure 3-1 |
1, 4 |
1 |
4 |
Normally Open Pushbutton |
|
|
||
2, 3 |
2 |
3 |
Schematic symbol (left) and |
|
|
|
part drawing (right) |
The left side of Figure 3-2 shows how a normally open pushbutton looks when it’s not pressed. When the button is not pressed, there is a gap between the 1,4 and 2,3 terminals. This gap makes it so that the 1,4 terminal can not conduct current to the 2,3 terminal. This is called an open circuit. The name “normally open” means that the pushbutton’s normal state (not pressed) forms an open circuit. When the button is pressed, the gap between the 1,4 and 2,3 terminals is bridged by a conductive metal. This is called a closed circuit, and current can flow through the pushbutton.
1, 4 |
|
|
|
1, 4 |
|
|
|
Figure 3-2 |
|
|
|
|
|
|
Normally Open Pushbutton |
||
|
|
|
|
|||||
2, 3 |
|
|
|
2, 3 |
|
|
|
|
|
|
|
|
|
|
|||
|
|
|
|
|
Not pressed (left) and pressed (right)
Test Parts for the Pushbutton
(1) LED – pick a color
(1)Resistor – 470 Ω (yellow-violet-brown)
(1)Pushbutton – normally open
(1) Jumper wire
Building the Pushbutton Test Circuit
Figure 3-3 shows a circuit you can build to manually test the pushbutton.
Digital Input – Pushbuttons · Page 63
Always disconnect power from your Board of Education or BASIC Stamp HomeWork Board before making any changes to your test circuit. From here onward, the instructions will no longer say “Disconnect power…” between each circuit modification. It is up to you to remember to do this.
Always reconnect power to your Board of Education or BASIC Stamp HomeWork Board before downloading a program to the BASIC Stamp.
9 Build the circuit shown in Figure 3-3.
Vdd
1, 4
2, 3
470 Ω
LED
Vss
Vdd |
Vin |
Vss |
|
+ |
|
X3 |
P15 |
P14 |
P13 |
P12 |
P11 |
P10 |
P9 |
P8 |
P7 |
P6 |
P5 |
P4 |
P3 |
P2 |
P1 |
P0 |
X2 |
Figure 3-3
Pushbutton Test Circuit
Testing the Pushbutton
When the pushbutton is not pressed, the LED will be off. If the wiring is correct, when the pushbutton is pressed, the LED should be on (emitting light).
Warning Signs: If the “Pwr” LED on the Board of Education flickers, goes dim, or goes out completely when you reconnect power, it may mean that there is a short circuit from Vdd to Vss or from Vin to Vss. If this happens, disconnect power immediately and find and correct the mistake in your circuit.
The LED built into the HomeWork Board is different. It may either be labeled “Power” or “Running” and it only glows while a program is running. If a program ends, either because it executes an END command or because it runs out of commands to execute, the LED will turn off.
9 Verify that the LED in your test circuit is off.
Page 64 · What’s a Microcontroller?
9Press and hold the pushbutton, and verify that the LED emits light while you are holding the pushbutton down.
How the Pushbutton Circuit Works
The left side of Figure 3-4 shows what happens when the pushbutton is not pressed. The LED circuit is not connected to Vdd. It is an open circuit that cannot conduct current. By pressing the pushbutton, as shown on the right side of the figure, you close the connection between the terminals with conductive metal. This makes a pathway for electrons to flow through the circuit and so the LED emits light as a result.
|
Vdd |
|
|
|
1, 4 |
|
|
|
2, 3 |
|
|
No |
470 |
Ω |
|
Current |
|||
|
|
||
|
LED |
||
|
Vss |
|
Vdd
1, 4 2, 3
Current |
470 Ω |
|
|
LED
Vss
Figure 3-4
Pushbutton Not Pressed,
and Pressed
Pushbutton not pressed: circuit open and light off (left)
Pushbutton pressed: circuit closed and light on (right)
Your Turn – Turn the LED off with a Pushbutton
Figure 3-5 shows a circuit that will cause the LED to behave differently. When the button is not pressed, the LED stays on; when the button is pressed, the LED turns off. Since this pushbutton connects a conductor across terminals 1,4 and 2,3 when pressed, it means that electricity can take the path of least resistance through the pushbutton instead of through the LED. Unlike the potential short circuits discussed in the Warning Signs box, the short circuit the pressed pushbutton creates across the LED’s terminals does not damage any circuits and serves a useful purpose.
9Build the circuit shown in Figure 3-5.
9Repeat the tests you performed on the first pushbutton circuit you built with this new circuit.
Digital Input – Pushbuttons · Page 65
Vdd |
Vdd |
Vin |
Vss |
|
X3 |
|
|
1, 4 |
P15 |
|
|
P14 |
|
|
|
|
P13 |
|
|
|
P12 |
|
+ |
LED |
P11 |
|
|
P10 |
|
|
|
|
P9 |
|
|
2, 3 |
P8 |
|
|
|
P7 |
|
|
|
P6 |
|
|
470 Ω |
P5 |
|
|
P4 |
|
|
|
|
P3 |
|
|
|
P2 |
|
|
|
P1 |
|
|
Vss |
P0 |
|
|
X2 |
|
|
Figure 3-5
LED that Gets Shorted by Pushbutton
Can you really do that with the LED? Up until now, the LED’s cathode has always been connected to Vss. Now, the LED is in a different place in the circuit, with its anode connected to Vdd. People often ask if this breaks any circuit rules, and the answer is no. The electrical pressure supplied by Vdd and Vss is 5 volts. The red LED will always use about 1.7 volts, and the resistor will use the remaining 3.3 volts, regardless of their order.
ACTIVITY #2: READING A PUSHBUTTON WITH THE BASIC STAMP
In this activity, you will connect a pushbutton circuit to the BASIC Stamp and display whether or not the pushbutton is pressed. You will do this by writing a PBASIC program that checks the state of the pushbutton and displays it in the Debug Terminal.
Parts for a Pushbutton Circuit
(1) Pushbutton – normally open
(1)Resistor – 220 Ω (red-red-brown)
(1)Resistor – 10 kΩ (brown-black-orange)
(2)Jumper wires
Page 66 · What’s a Microcontroller?
Building a Pushbutton Circuit for the BASIC Stamp
Figure 3-6 shows a pushbutton circuit that is connected to BASIC Stamp I/O pin P3.
9 Build the circuit shown in Figure 3-6.
|
Vdd |
Vin |
Vss |
|
X3 |
|
|
Vdd |
P15 |
|
|
P14 |
|
|
|
|
P13 |
|
|
|
P12 |
|
|
|
P11 |
|
|
|
P10 |
|
|
|
P9 |
|
|
|
P8 |
|
|
P3 |
P7 |
|
|
P6 |
|
|
|
220 Ω |
P5 |
|
|
P4 |
|
|
|
10 kΩ |
|
|
|
P3 |
|
|
|
|
P2 |
|
|
|
P1 |
|
|
Vss |
P0 |
|
|
X2 |
|
|
Figure 3-6
Pushbutton Circuit Connected to I/O Pin P3
On the wiring diagram, the 220 Ω resistor is on the left side connecting the pushbutton to P3 while the 10 kΩ resistor is on the right, connecting the pushbutton circuit to Vss.
Figure 3-7 shows what the BASIC Stamp sees when the button is pressed, and when it’s not pressed. When the pushbutton is pressed, the BASIC Stamp senses that Vdd is connected to P3. Inside the BASIC Stamp, this causes it to place the number 1 in a part of its memory that stores information about its I/O pins. When the pushbutton is not pressed, the BASIC Stamp cannot sense Vdd, but it can sense Vss through the 10 kΩ and 220 Ω resistors. This causes it to store the number 0 in that same memory location that stored a 1 when the pushbutton was pressed.
Digital Input – Pushbuttons · Page 67
Vdd |
|
SOUT |
1 |
|
24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4 |
9 |
|
0 |
|
|
|
16 |
|
P11 |
||||||
|
|
|
|
10 kΩ |
|
|
P5 |
10 |
|
|
|
|
|
|
|
15 |
|
P10 |
|||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||||||||
|
|
|
|
|
|
|
|
|
P6 |
11 |
|
|
|
|
|
|
|
14 |
|
P9 |
|||||
|
|
|
|
|
|
|
|
|
|
|
|
P7 |
12 |
|
|
|
|
|
|
|
13 |
|
P8 |
||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Figure 3-7
BASIC Stamp Reading a
Pushbutton
When the pushbutton is pressed, the BASIC Stamp reads a 1 (above). When the pushbutton is not pressed, the BASIC Stamp reads a 0 (below).
BS2-IC
Vss
Binary and Circuits: The base-2 number system uses only the digits 1 and 0 to make numbers, and these binary values can be transmitted from one device to another. The BASIC Stamp interprets Vdd (5 V) as binary-1 and Vss (0 V) as binary-0. Likewise, when the BASIC Stamp sets an I/O pin to Vdd using HIGH, it sends a binary-1. When it sets an I/O pin to Vss using LOW, it sends a binary-0. This is a very common way of communicating binary numbers used by many computer chips and other devices.
Programming the BASIC Stamp to Monitor the Pushbutton
The BASIC Stamp stores the one or zero it senses at I/O pin P3 in a memory location called IN3. Here is an example program that shows how this works:
Example Program: ReadPushbuttonState.bs2
This next program makes the BASIC Stamp check the pushbutton every ¼ second and send the value of IN3 to the Debug Terminal.
Page 68 · What’s a Microcontroller?
Figure 3-8 shows the Debug Terminal while the program is running. When the pushbutton is pressed, the Debug Terminal displays the number 1, and when the pushbutton is not pressed, the Debug Terminal displays the number 0.
Figure 3-8
Debug Terminal Displaying
Pushbutton States
The Debug Terminal displays 1 when the pushbutton is pressed and 0 when it is not pressed.
9Enter the ReadPushbuttonState.bs2 program into the BASIC Stamp Editor.
9Run the program.
9Verify that the Debug Terminal displays the value 0 when the pushbutton is not pressed.
9Verify that the Debug Terminal displays the value 1 when the pushbutton is pressed and held.
'What's a Microcontroller - ReadPushbuttonState.bs2
'Check and send pushbutton state to Debug Terminal every 1/4 second.
'{$STAMP BS2}
'{$PBASIC 2.5}
DO
DEBUG ? IN3
PAUSE 250
LOOP
How ReadPushbuttonState.bs2 Works
The DO...LOOP in the program repeats every ¼ second because of the command PAUSE 250. Each time through the DO...LOOP, the command DEBUG ? IN3 sends the value of IN3 to the Debug Terminal. The value of IN3 is the state that I/O pin P3 senses at the instant the DEBUG command is executed.
Digital Input – Pushbuttons · Page 69
Your Turn – A Pushbutton with a Pull-up Resistor
The circuit you just finished working with has a resistor connected to Vss. This resistor is called a pull-down resistor because it pulls the voltage at P3 down to Vss (0 volts) when the button is not pressed. Figure 3-9 shows a pushbutton circuit that uses a pull-up resistor. This resistor pulls the voltage up to Vdd (5 volts) when the button is not pressed. The rules are now reversed. When the button is not pressed, IN3 stores the number 1, and when the button is pressed, IN3 stores the number 0.
The 220 Ω resistor is used in the pushbutton example circuits to protect the BASIC Stamp I/O pin. Although it’s a good practice for prototyping, in most products this resistor is replaced with a wire (since wires cost less than resistors).
9Modify your circuit as shown in Figure 3-9.
9Re-run ReadPushbuttonState.bs2.
9Using the Debug Terminal, verify that IN3 is 1 when the button is not pressed and 0 when the button is pressed.
|
Vdd |
Vin |
Vss |
Vdd |
X3 |
|
|
P15 |
|
|
|
|
|
|
|
|
P14 |
|
|
10 kΩ |
P13 |
|
|
P12 |
|
|
|
|
P11 |
|
|
P3 |
P10 |
|
|
P9 |
|
|
|
220 Ω |
P8 |
|
|
P7 |
|
|
|
|
P6 |
|
|
|
P5 |
|
|
|
P4 |
|
|
|
P3 |
|
|
Vss |
P2 |
|
|
P1 |
|
|
|
|
P0 |
|
|
|
X2 |
|
|
Figure 3-9
Modified Pushbutton
Circuit
Active-low vs. Active-high: The pushbutton circuit in Figure 3-9 is called active-low because it sends the BASIC Stamp a low signal (Vss) when the button is active (pressed). The pushbutton circuit in Figure 3-6 is called active-high because it sends a high signal (Vdd) when the button is active (pressed).
Page 70 · What’s a Microcontroller?
ACTIVITY #3: PUSHBUTTON CONTROL OF AN LED CIRCUIT
Figure 3-10 shows a zoomed-in view of a pushbutton and LED used to adjust the settings on a computer monitor. This is just one of many devices that have a pushbutton that you can press to adjust the device and an LED to show you the device’s status.
Figure 3-10
Button and LED on a Computer Monitor
The BASIC Stamp can be programmed to make decisions based on what it senses. For example, it can be programmed to decide to flash the LED on/off ten times per second when the button is pressed.
Pushbutton and LED Circuit Parts
(1) Pushbutton – normally open
(1)Resistor – 10 kΩ (brown-black-orange)
(1)LED – any color
(1)Resistor – 220 Ω (red-red-brown)
(1)Resistor – 470 Ω (yellow-violet-brown)
(2)Jumper wires