Arduino 05: Digital I/O Jeffrey A. Meunier jeffm@engr.uconn.edu University of Connecticut
About: How to use this document I designed this tutorial to be tall and narrow so that you can read it on one side of your screen while you follow along in the rest of the display area, like this: Tutorial work area Also, the pages in this tutorial are designed to be viewed as whole-page slides. I describe how to do that next. 2
Set your PDF viewer to single page mode (Mac) On a Mac, a PDF file will open in the Preview application by default. Hide the side bar and set it to Single Page. 3
Set your PDF viewer to single page mode (Windows) In Windows, a PDF file will open in the Reader application by default. You can open the menu from the icon in the upper left corner of the window and then choose Split Left or Split Right depending on your preference. Then right-click on the document and choose the One page viewing option. 4
Introduction A microcontroller is useful to interact with the outside world. Not only can it affect things in the real world by flashing a lights or spinning motors, it can also sense the real world by through the use of buttons, switches, and dials. In this exercise you'll use some of the parts in the electronics kit to do digital inputand digital output. 5
Objectives The objectives are: To learn about digital I/O. To connect some of the electronic components to the Arduino board. To control the Arduino and its components using Python. 6
Prerequisites Before proceeding with this tutorial you should have completed the tutorials for installing the necessary applications and files. In particular: Your Arduino board must have the firmware installed as described in tutorial Arduino 01 - Installing Arduino and firmware. You must have Python running and have it communicating with the Arduino board as described in Arduino 02 - Using Arduino with Python. If you did not do those tutorials yet, you must do them now. 7
Get the latest arduino.py file Before doing this exercise, download the latest arduino.py file from this web page: http://engr.uconn.edu/~jeffm/arduino.html Save it into a folder that you'll use for today's lab exercise, like CSE1010 / Lab 3 8
Outline These are the sections you'll go through in this exercise: 1. Background 2. Collect the required parts 3. Learn about the breadboard 4. Connect the LED 5. Plug in the Arduino board 6. Write a program 7. Connect the push button 8. Write a new program 9. Summary 9
1. Background: Digital Something is described as being digital if it is made up of digits. For example, the calculators that you have used are all digital calculators, meaning two things: 1. They display digits on the screen. 2. The brain that controls the calculator is actually a digital computer. 10
1. Background: Analog Contrast that digital calculator with this old slide rule: This is an analog calculator, whereby the answer is taken from a continuous scale and not a set of digits displayed on a screen. 11
1. Background: Decimal numbers The decimal number system uses the digits 0 through 9. Each column (or decimal place) of a number is 10 times the one to its right. The number 1234 means: 1 x 1000 + 2 x 100 + 3 x 10 + 4 x 1 = 1234 I point this out in order to contrast it with the binary number system on the next slide. 12
1. Background: Binary numbers Binary means having two parts. The binary number system uses the digits 0 and 1. Each column (or decimal place) of a number is 2 times the one to its right. The number 1010 means: 1 x 8 + 0 x 4 + 1 x 2 + 0 x 1 = 10 (in base 10) In this exercise you'll use the binary numbers 0 and 1. 13
1. Background: Other binary things Note that many other categories of things are binary: Colors Answers Directions Conditions Light and Dark White and Black Yes and No Left and Right Up and Down On and Off High and Low Anything that has an natural opposite can be considered to be binary, and thus can be represented using 1 and 0. 14
1. Background: I/O I/O is short for input and output. It's something that all computers do a lot of, and so we just abbreviate it because we talk about it so much. 15
1. Background: LED LED is an abbreviation for light emitting diode. It's a device that emits photons by causing electrons in the device to lose energy by combining with positively charged atoms. "LED" is always pronounced as its letters ELL EEE DEE. 16
Outline 1. Background 2. Collect the required parts 3. Learn about the breadboard 4. Connect the LED 5. Plug in the Arduino board 6. Write a program 7. Connect the push button 8. Write a new program 9. Summary 17
2. Collect the required parts: Board, cable, breadboard Locate the following parts in the electronics kit and take them out: Arduino board USB cable Breadboard 18
2. Collect the required parts: Your board may be blue The older kits used in CSE1010 had a blue board instead of a red one. They all work the same. 19
2. Collect the required parts: LED and push button LED: any color, as long as it has only 2 pins (there is a 4-pin LED in your kit don't choose that one) Push button: black base + blue cap, yours may have silver & yellow colors on it like the one pictured below, or it might be mostly black 20
2. Collect the required parts: 4 wires, 4 pins 4 wires: any color, peel them off the flat rainbow-colored cable 4 pins: you can twist off single pins from the long pin strip, or cut them using scissors, wire cutters, or even nail clippers 21
2. Collect the required parts: 220Ω resistor (or 2.2K) 220Ω resistor Your kit comes with resistors of two different resistance values: 220 Ω (where Ω = Ohm), and 10,000 Ω, usually written 10K Ω. The 220 Ω resistor is the one that has these color bands starting at one end: RED-RED-BLACK. Remove one resistor carefully from the tape and clean the goo from the ends. Use a 2.2K resistor if you don't have a 220. 22
2. Collect the required parts: Push button & resistor Push the blue cap onto the push button body. Under the switch cap there's a square hole that fits onto the square post on the switch. It should pop into place. Bend the pins (usually called leads) of the resistor into the U-shape shown on the previous slide. 23
2. Collect the required parts: Why use a resistor? The resistor will be placed in series with the LED in order to limit the amount of electrical current that flows through it. The internal resistance of an LED is very low when it's glowing, which allows a lot of current to flow through it. The more current there is, the more heat it generates. The core of the LED (called the junction) is encased in clear or colored plastic, which is a very poor heat conductor, so any amount of excess heat inside the LED will quickly melt the junction, turning your light emitting diode into a smoke emitting diode (not literally the LED will simply burn out and then stop working). You may put the kit aside for now 24
Outline 1. Background 2. Collect the required parts 3. Learn about the breadboard 4. Connect the LED 5. Plug in the Arduino board 6. Write a program 7. Connect the push button 8. Write a new program 9. Summary 25
3. About the breadboard The breadboard is also known as a prototyping board or just a proto board. It allows wires and components to be plugged in removed easily. 26
3. About the breadboard: The rows Notice that each row has 5 columns labeled a e on the left half and f j on the right half. In one half-row, all the holes are connected together electrically. 27
3. About the breadboard: The rows are isolated Thus, anything plugged into hole a1 is automatically connected to anything that's plugged into holes b1, c1, d1, or e1. It's the same on the right half: f1, g1, h1, i1, and j1 are all connected together. But be aware that the left half and the right half are not connected together. In other words, a-bc-d-e are not connected to f-g-h-i-j. We say that the two half-rows are isolated. 1 2 3 4 5 6 The same is true for all the rows on your breadboard, 1-30. 1 2 3 4 5 6 28
3. About the breadboard: The columns There are also four long columns, two along each side. Each single column is connected together electrically. The columns (sometimes called power rails) are often used for the power supply and return. 29
3. About the breadboard: All rows & columns are equal The rows and columns on the board are all created equally. It doesn't matter which row or column you use as long as you remember how the rows and columns are connected electrically. The labels and colors on the board are there for convenience only. 30
Outline 1. Background 2. Collect the required parts 3. Learn about the breadboard 4. Connect the LED 5. Plug in the Arduino board 6. Write a program 7. Connect the push button 8. Write a new program 9. Summary 31
4. Connect the LED: Anode and cathode Notice that the LED has two pins, one of which is shorter than the other. The short pin is referred to as the negative pin, or cathode, and the long pin is the positive pin, or anode. 32
4. Connect the LED: Polarized electrical devices Every electrical circuit is powered by a power source that has a positive side and a negative side (recall the + and you've seen on every battery). Some electrical components are polarized, meaning that they must be connected to the power source in only one direction. An LED is this kind of device. Thus, the manufacturer helps you identify the positive and negative sides by making the negative pin shorter than the positive pin. 33
4. Connect the LED: Plug in the LED Plug the LED into two rows of the breadboard, making a note of which rows the long and short pins are in. Orient the long pin to the upper row and the short pin to the lower row. The actual row numbers that you use don't matter. All the rows are the same. The circuit diagramming application that I use automatically highlighted the two breadboard rows in green. 34
4. Connect the LED: Plug in the resistor Plug the resistor into the breadboard so that one of its pins is in the same row as the resistor's negative pin. Its other pin can go into any other row. This resistor is beige colored, but your resistor will probably be light blue. 35
4. Connect the LED: Plug in the pins Plug one of the small connector pins into the same row as the LED's positive pin, and the other pin into the row that has the other end of the resistor. pin pin 36
4. Connect the LED: The complete diagram Here's a picture of my breadboard: My LED's long pin is in e3 and its short pin is in e4. One end of the resistor is in c4 (same row as the LED's short pin) and the other end is in c9. One connector pin is in a3 and the other is in a9. 37
4. Connect the LED: Connect wires to breadboard Take one of the wires and plug it onto the top pin on the breadboard, the one in the same row as the LED. I'll call this the LED wire. Plug the other wire into the lower pin on the breadboard in the same row as the lower end of the resistor lead. I'll call this the resistor wire. 38
4. Connect the LED: Connect wires to the Arduino The next few slides are in two sections, one for the red Arduino board and one for the blue Arduino board. Red board Blue board Follow the slides for the board of your color. 39
4. Connect the LED: Locate the digital pins Red board Locate the bank of digital I/O pins on the Arduino board. On a red board they're in the upper right side of the board: 40
4. Connect the LED: Locate D13 Red board Locate pin D13 on the board. It's the top left pin in the yellow row of pins labeled S (the S means Signal). D13 S V G D13 Digital D0 RoboRED 41
4. Connect the LED: Connect the wires Red board Connect the LED wire to the S pin in the yellow row of D13. Connect the resistor wire to the G pin in the blue row of D13. LED wire S G S V G D13 Digital D0 RoboRED Resistor wire 42
4. Connect the LED: Locate the digital pins Blue board On a blue board the digital I/O is done using the top row of sockets 43
4. Connect the LED: Locate D13 and GND Blue board You'll need to insert a pin into the D13 socket (twist a few more pins off the pin strip). Look for the socket with the number 13 printed below it. D13 GND Insert a pin into one of the two GND sockets as well. 44
4. Connect the LED: Connect the wires Blue board Connect the LED wire to the digital 13 pin in the top row of sockets. Connect the resistor wire to the GND pin in the bottom row of sockets. LED wire Resistor wire 45
Outline 1. Background 2. Collect the required parts 3. Learn about the breadboard 4. Connect the LED 5. Plug in the Arduino board 6. Write a program 7. Connect the push button 8. Write a new program 9. Summary 46
5. Plug in the Arduino board This step is easy. Plug the Arduino board into the computer using the USB cable. Be sure that no other application is using the Arduino. In particular, be sure that the Arduino application is not running. 47
Outline 1. Background 2. Collect the required parts 3. Learn about the breadboard 4. Connect the LED 5. Plug in the Arduino board 6. Write a program 7. Connect the push button 8. Write a new program 9. Summary 48
6. Write a program: Open IDLE Start IDLE (or use whatever editor you're accustomed to using). Enter this into a new file in IDLE: import arduino import time Importing the arduino module gives your program access to the Arduino over the USB port. All your Arduino programs will need to import that module. The time module will be used to add a delay into the program. Keep typing the statements on the next slide. 49
Write a program: More statements a = arduino.arduino() That creates an instance of the Arduino class in Python's memory. a.serialconnect() That opens a connection to the Arduino over the USB port. a.pinmode(13, 'o') That indicates that we will be setting the value of the digital pin 13 from the program and not reading its value instead. In other words, that pin will do output and not input. Keep typing on the next slide. 50
6. Write a program: More statements while True: This program will run in a loop until you press ^C. (indented inside the while loop:) a.digitalwrite(13, 1) time.sleep(0.5) That turns the LED on and then pauses 0.5 seconds. a.digitalwrite(13, 0) time.sleep(0.5) That turns the LED off, and pauses 0.5 seconds. 51
6. Write a program: Run the program In the IDLE editor window press F5 to run that program. It asks you where to save the program. Name the file blink.py and save it into a folder that has the arduino.py file in it already, or save it into a new folder and then be sure to copy arduino.py into that folder. The folder where you save blink.py must have the arduino.py file in it. The program starts running and it shows no output in the IDLE Python Shell window, but observe the on-board LED on the Arduino board. It's flashing on and off in ½ second intervals. 52
6. Write a program: Abort the program Press ^C (Ctrl-C) in the Python Shell window to abort the program. Enter this command in the Python Shell window: a.serialdisconnect() That closes the connection between Python and the Arduino over the USB port. 53
Outline 1. Background 2. Collect the required parts 3. Learn about the breadboard 4. Connect the LED 5. Plug in the Arduino board 6. Write a program 7. Connect the push button 8. Write a new program 9. Summary 54
7. Connect the push button: Push the cap onto the button It will end up looking something like this: 55
7. Connect the push button: Examine the pins Examine the push button carefully and you'll notice that the pins extend from opposite sides of the body of the push button. 56
7. Connect the push button: Plug it into the breadboard Insert the push button on the breadboard so that it sits across the channel that goes down the middle of the board. It doesn't matter what rows you plug it into, as long as it's not in the same rows as any of the other components on the board. You really have to push it down hard so that the bottom of the button is flush with the surface of the breadboard. You should be able to lift the push button afterward and the whole board will come up with it. 57
7. Connect the push button: If you mash the pins If you've accidentally mashed the pins of the button so that the sort of fold up under the button, pull out the button and do your best to straighten out the pins. Then try re-inserting the button into the breadboard. 58
7. Connect the push button: Plug in two pins Find two small pins, or twist or cut them off the pin strip. Plug the pins into the same rows as two of the legs on the same side of the push button. It doesn't matter if you use the left half of the breadboard with the a e letters or the right half with the f j letters, as long as both pins are on the same side. pin pin 59
7. Connect the push button: Connect wires Connect two wires to the pins by the switch. It doesn't matter which wire goes to which pin. Then do this: Red board Plug one of the wires into the S (yellow) row of D12, and the other into the V (red) row of D12. These wires should be right next to the wires you plugged into D13 for the LED. Blue board Plug one of the wires into digital socket 12 (plug in a pin first), and the other wire into the 5V socket next to the GND socket. 60
7. Connect the push button: The whole breadboard My whole breadboard is wired like this now: 61
Outline 1. Background 2. Collect the required parts 3. Learn about the breadboard 4. Connect the LED 5. Plug in the Arduino board 6. Write a program 7. Connect the push button 8. Write a new program 9. Summary 62
8. Write a new program: In IDLE: File New File In this new program file, enter statements to do this: 1. Import the arduino module. 2. Create a new Arduino instance and store it in a variable called a. 3. Open a serial connection to the Arduino. 4. Set the mode of pin 13 to output. These are the same statements that you used at the beginning of the blink.py program that you wrote already. Refer back to that program if you need to. Try it on your own first. The solution is on the next slide. 63
8. Write a new program: First few statements These are the first few statements of the new program import arduino a = arduino.arduino() a.serialconnect() a.pinmode(12, 'i') a.pinmode(13, 'o') The two pinmode statements mean that this program will expect to do input on pin 12 and output on pin 13. In other words it will read (or accept) a digital signal in from pin 12 and to write (or send) a digital signal out on pin 13. 64
Write a new program: The loop Below the pinmode statements start an unbounded while loop, just like in the blink program. Remember how to do that? Spoiler on the next slide. 65
8. Write a new program: The loop This program uses an unbounded while loop just like the previous one. while True: Inside the while loop we need to do this: 1. Read the value from pin 12. 2. Set the LED to be the value that we just read from pin 12. The digitalread function and digitalwrite functions will be used for those statements. 66
8. Write a new program: Reading pin 12 Here is the next statement. I show it indented because it must be indented under the while True statement. Indent the next statements that you write by either 2 spaces or 4 spaces. button = a.digitalread(12) That statement reads the value of the signal on digital pin 12, which is the one that the push button is connected to. The value is stored in a variable called button. Now you need to write the value of the button variable to digital pin 13. Can you write that statement on your own? Spoiler on the next slide. 67
8. Write a new program: Writing pin 13 This is the statement to write the value of the button variable to pin 13. a.digitalwrite(13, button) Make sure it's indented the same amount that the digitalread statement is indented. The whole program is shown on the next slide. Make sure yours looks very similar to it. 68
8. Write a new program: The finished program import arduino a = arduino.arduino() a.serialconnect() a.pinmode(12, 'i') a.pinmode(13, 'o') while True: button = a.digitalread(12) a.digitalwrite(13, button) Now it's time to run the program. 69
8. Write a new program: Run the program In the IDLE editor window press F5 to run that program. It asks you where to save the program. Name the file button.py and save it into the same folder where you save the blink.py program. Make sure that the arduino.py file is also in that folder. While the program is running it shows no output in the IDLE Python Shell window, and the Arduino's LED should turn on. Now push the button on the breadboard. The LED should turn light up as long as the button is pressed. Woo, fun! 70
8. Write a new program: Abort the program Press ^C (Ctrl-C) in the Python Shell window to abort the program. Enter this command in the Python Shell window: a.serialdisconnect() That closes the connection between Python and the Arduino over the USB port. If you don't disconnect the USB connection, the next time you try to run your program Python will complain because the USB connection to the Arduino is already in use. 71
Write a new program: Modify the program Now we'll modify the program so that the LED is on normally, and pressing the button causes the LED to turn off. Consider that the digitalread function call returns a 0 when the switch is not pressed and a 1 when the switch is pressed. We want to send a 0 to D13 when D12 returns a 1, and send a 1 to D13 when D12 returns a 0. Thus, we need to invert the value returned by digitalread(12). Be aware that the value returned by digitalread is an integer (1 or 0) and not a Boolean value (True or False). Can you do it? Spoiler follows. 72
8. Write a new program: Modify the program We need to do this: D12 D13 0 1 1 0 You could do that using an if statement, but it's simpler to subtract the value of the button variable from 1. button = a.digitalread(12) a.digitalwrite(13, 1 - button) This works because: 1-0 = 1 1-1 = 0 Now change the program and run it! 73
8. Write a new program: Modify the circuit? This logic inverting modification could have been done on the circuit board instead of in the program (it would require the use of another resistor on the circuit board). But think about the cost in time of modifying a circuit instead of modifying a program: What if you worked for a company and you had 1000 circuits to change. Would it be cheaper to modify 1000 circuits or modify 1000 programs? Usually, circuits are assembled permanently on a circuit board, so modifying a circuit would require cutting one of the traces (wires) on the board and soldering on a new component. It's very time consuming. 74
Outline 1. Background 2. Collect the required parts 3. Learn about the breadboard 4. Connect the LED 5. Plug in the Arduino board 6. Write a program 7. Connect the push button 8. Write a new program 9. Summary 75
9. Summary It seems like what you've done is very simple: get an LED to turn on and off. But note one important thing: the push button and the LED are not connected together directly. The program is what's causing the LED to turn on or off. Can you imagine any other electrical device that could be controlled like this? Yes: everything. ;) 76
9. Summary The circuit you've built is simple, but in this exercise you've actually become familiar with quite a few different things: Connecting the Arduino to the computer. Using components on a breadboard. Connecting wires between the breadboard and the Arduino. Writing some Python programs that control the Arduino. Digital input and output. Modifying software (the program) instead of modifying hardware (the circuit). That's quite a bit for just a few hours of work. 77
Outline 1. Background 2. Collect the required parts 3. Learn about the breadboard 4. Connect the LED 5. Plug in the Arduino board 6. Write a program 7. Connect the push button 8. Write a new program 9. Summary You're done! 78