Robotics and Electronics Unit 2. Arduino Objectives. Students will understand the basic characteristics of an Arduino Uno microcontroller. understand the basic structure of an Arduino program. know how to use the digitalwrite and analogwrite functions know the significance of data types to the Arduino programming language know how to read and write assignment statements. Below is a drawing of an Arduino Uno the type of microcontroller that we will be using in this class. A microcontroller is a kind of mini-computer to which we can easily attach other electronic devices. 2.1 mm barrel jack center positive Note. The above image and many others are courtesy of http://fritzing.org/home/ - this is a wonderful resource for designing circuits. Along the top and bottom edges of the Arduino are holes called I/O ( ) pins (aka pin-outs). We connect electronic devices to the Arduino via the pins. Here are some characteristics of an Uno. The recommended input voltage for the Uno is between volts. For simple applications you can power it via the USB port. Each I/O pin has a recommended current limit of. Total output current limit for all the pins is. of Flash Memory and of SRAM. Go to https://www.arduino.cc/en/tutorial/memory for more information about its memory. Here s a link with more information: https://learn.sparkfun.com/tutorials/what-is-an-arduino/whats-onthe-board We will discuss the different types of pins and their uses as the course progresses. 1
Hands-on Exercise 1. Create a folder on your Z drive for this course. Open up the Arduino software and copy this code. The Arduino programming language is case-sensitive so VOID is different from void. Save the file as Blinky on your Z drive. Move the mouse over the check button. The word Verify should appear to the right. Click on the check button. This compiles your sketch (Arduino s term for a program). This means that the software translates the code you wrote into ones and zeroes that the Uno can understand. We will describe what the code does later. Build the circuit as shown. There is a wire from pin 8 that connects to the positive end of the LED. The negative end of the LED connects to a 330 ohm (orange-orange-brown) resistor. The other end of the resistor is connected to a GND (Ground) pin of the Uno. The Uno has three GND pins you can use any of them. The Uno will power the circuit but nothing is happening yet because the Uno does not have power. In addition, we haven t uploaded our sketch Blinky to it yet. Connect the Uno to your computer with the USB cable. In the Arduino program, click on the Tools menu and select the Serial Port item. There should appear something like COM8 or COM12. If there is more than one, select the highest port. This establishes a connection between the computer and the Uno. After selecting the serial port, click the button with an arrow going to the right (the word Upload should appear as you hover over it). It will recompile the program and send it to the Uno. The LED on the breadboard will start blinking. 2
After you get the program to run and the LED is blinking on and off, you will measure the voltage drops across the LED and the resistor. Max. Voltage Drop across the LED Max. Voltage Drop across the resistor Sum of the two Voltage Drops The ground pin serves as the reference point for all the voltages in the circuit. USB, short for Universal Serial Bus, is an industry standard developed in the mid-1990s that defines the cables, connectors and communications protocols used in a bus for connection, communication, and power supply between computers and electronic devices. -https://en.wikipedia.org/wiki/usb A quick word about using a USB port to power a device. USB 2.0 is supposed to provide at a maximum of. USB 3.x should provide with between. Remember, the recommended input voltage for an Uno is volts. So when you use the USB cable to power an Uno, you are under-powering it and it might be a little unstable. Now let s examine the code. void setup() { pinmode( 8, OUTPUT); void loop() { digitalwrite(8, HIGH); delay(1000); digitalwrite(8, LOW); delay(1000); // // // // // Every Arduino sketch must have a setup() function and must have a loop() function. The setup() function is The loop() function is We will discuss what the keyword void means in a later unit. 3
1. Given we have a series circuit with an LED and a resistor. Select the TRUE statement. a) The LED will blink more often with the middle code than with the code on the right. b) The LED will blink less often with the middle code than with the code on the right. c) The LED will blink at the same rate with either code. 2. What does the 12 in this code refer to? void setup() { pinmode( 12, OUTPUT); void loop() { digitalwrite(12, HIGH); delay(500); delay(500); digitalwrite(12, LOW); delay(500); delay(500); void setup() { pinmode( 12, OUTPUT); void loop() { digitalwrite(12, HIGH); delay(1000); digitalwrite(12, LOW); delay(1000); Note. The GND pin is similar to the negative node on a battery and the numbered pin is similar to the positive node on a battery. Hands-on Exercise 3. Build two separate LED circuits and use one program to control both at the same time. Write the code so that when one LED is on, the other is off. Hints: 1. You will need to set two digital I/O pins to OUTPUT. 2. You will need two resistors. 3. You really need to create two separate circuits. Variables and Assignment Statements. A variable is a named storage location in memory. Think of it like a box that holds something. In the Arduino programming language, all variables must have a data type which defines the type of data that a variable can hold. One data type is an int. An int can store integers with values between and You create a variable by declaring it. For example: This declares a variable named x as an int. An assignment statement assigns a value to a variable. For example: int num = 3; num = 12; num = num + 5; 4
3. After the setup function has been executed, what is the value of num? 4. After the setup function has been executed, what is the value of x2? 5. After the setup function has been executed, what is the value of a? What is the value of b? 6. After 0.5 seconds, what is the value of count? 7. After 1.5 seconds, what is the value of count? 8. After 2.5 seconds, what is the value of count? 9. After 0.5 seconds, what is the value of v? 10. After 1.5 seconds, what is the value of v? 11. After 2.5 seconds, what is the value of v? int num = 5; num = num + 3; int x2 = 10; x2 = 11; x2 = 12; int a = 4; int b = 7; a = b; b = a; int count = 4; count = count + 3; delay( 1000 ); int v = 5; v = v - 4; delay( 1000 ); 5
The Serial Monitor. Create a new sketch. Save it and name it Variables. Then connect the Arduino to the computer. Upload the program to the Uno. There is no circuit to go along with this program. As the program runs, you will see a small yellow LED labelled TX light up every second. This indicates that the program is transmitting something to the computer. With the Uno plugged into the computer, click on the Tools menu and select Serial Monitor. This will automatically cause the program to restart and it opens a window that displays whatever the Uno is sending to your computer. Make some changes to the code, re-upload the sketch, and open the Serial Monitor to see the results. What happens if you press the reset button on the Uno while it is running? Arduino provides a number of functions to work with the Serial Monitor. Here are the functions that were used in the previous sketch. Serial.begin( 9600); Serial.println(); Serial.println( "Hey" ); Serial.print( "the variable..." ); Serial.println( num ); 6
Important. On the UNO, pin are used to transmit to the Serial Monitor. If your code is calling the Serial functions then you cannot use those pins for anything else. On the other hand, if you are not printing to the Serial Monitor then you may use those pins. Hands-on Exercise 4. In this extended exercise, you see some code. I d like you to predict what will be displayed and then run the code to see what is actually displayed. If the results do not agree with your prediction, then think about why it didn t. (You re not actually expected to accurately predict all the results but once you see the results, you might have a good idea what happened.) 4a. What do you think will be 4b. What do you think will be 4c. What do you think will be Serial.println( 28 / 10 ); Serial.println( 28.0/10 ); Serial.println( 28/10.0 ); Serial.println( 10 * (36/10) ); Serial.println( 10 * (36.0/10) ); Serial.println( 10.0 * (36/10) ); int x = 28 / 10; int y = 25 / 10; int z = 22 / 10; Serial.println( x ); Serial.println( y ); Serial.println( z ); 7
4d. What do you think will be 4e. What do you think will be 4f. What do you think will be 4g. What do you think will be int num1 = 46.7; int num2 = - 9.99; int num3 = 7.4999; Serial.println( num1 ); Serial.println( num2 ); Serial.println( num3 ); int x = 32767; Serial.println( x ); x = x + 1; Serial.println( x ); int a; a = 10 + 4 / 2; Serial.println( a ); int a = 49; int b; b = 10 * (a / 10); Serial.println( b ); b = 10 * (a / 10.0); Serial.println( b ); 8
4h. What do you think will be 4i. What do you think will be int x = 4; int y = 5; y = x; x = y; Serial.println( x ); Serial.println( y ); int blue = 8; int red = 12; blue = red; red = blue; Serial.println( blue ); Serial.println( red ); Conclusions The number 4 and the number 4.0 is. If you divide an int by an int If one of the operands in an operation is a decimal, If you assign a decimal to an int Expressions are evaluated using the same All assignments statements (other than the declaration/initialization statement) must occur If the value in an int goes out of bounds, either too big or too small, it wraps around. In other words, if an int keeps getting more and more negative sooner or later its value will become 32,767. Similarly, if an int keeps increasing eventually its value wraps around to -32,768. 9
Hands-on Exercise 5. Write a program that prints 1000, 2000, 3000, etc where there is a half-second delay between each number displaying. Use an int to store the number being displayed. Hands-on Exercise 6. Write a program that starts at 2000 and then every quarter of a second subtracts 500 from that number. Hands-on Exercise 7. Write a program that does the following: Prints y = 3x +2 once with a blank line after it. Has a variable x that starts at one and increases by one each time the loop function executes. The variable y s value is equal to three times x plus two. There should be a one second delay between print statements (though there doesn t need to be a delay between the first two lines and the rest of the output). Here s a hint (assuming you have a variable named x) Serial.print( "if x = " ); Serial.print( x ); Notice that Serial.println prints something and then goes to a new line. Serial.print prints something and does NOT go to a new line. 10
Hands-on Exercise 8. Write a program that does the following: Prints y = x^2 once with a blank line after it. Has a variable x that starts at 20 and decreases by 2 each time the loop function executes. The variable y s value is equal to x squared. There should be a one-second delay between print statements (though there doesn t need to be a delay between the first two lines and the rest of the output). The output should look like this: y = x^2 If x = 20, then y = 400 If x = 18, then y = 324 If x = 16, then y = 256 Hands-on Exercise 9. Build an LED circuit that connects to the Arduino - remember to use a 330 ohm resistor to keep the voltage drop across the LED to 2 volts. Write a program that makes the LED gradually blinks faster and faster. Here are some hints: 1. Create a variable and assign it a value of 2000 2. Use this variable with the delay function inside the loop function. 3. Decrease this variable by some amount (e.g. 50, 100, 200 ) inside the loop function so each time the delay is smaller and smaller. 4. Don t worry about when the value of the variable goes negative; nothing bad happens. 11
Hands-on Exercise 10. To the right is different looking schematic but it still represents a closed path. Create an LED circuit just as you did back in Exercise 1. Use a 330-ohm resistor (orange-orange-brown) to limit the resistance. Make sure you use pin 3. After building the circuit, upload this code to the Uno. pinmode( 3, OUTPUT ); analogwrite( 3, 255 ); delay( 3000 ); analogwrite( 3, 127 ); delay( 3000 ); analogwrite( 3, 0 ); delay( 3000 ); Notice what happens when we change the second number from 255 to 127 to 0 and back again. Measure the voltage drops across the LED and resistor as the program runs and fill in the table below. If you are too rushed, increase the delays so you have more time before it changes. Measure the voltages when the LED is on. Increase the delay if you feel too rushed. analogwrite value Voltage drop across the LED Voltage drop across the resistor Sum of the voltage drops 255 127 0 Current (ma) calculated The digitalwrite function either turns a pin on (about 5 volts) or off (zero volts). The analogwrite function allows you to set different voltage levels. You can only use analogwrite on digital pins. These are marked with a Hands-on Exercise 11. Build an LED circuit (with a 330 ohm resistor). Be sure to connect the circuit to one of the digital pins that supports the analogwrite function. Change the program so that the LED gradually gets brighter and brighter. You should use a variable to store the value you are using in the analogwrite function. Start the value at zero and add 10 to it every half-second. The value should therefore reach 250 in about 12.5 seconds. Once the variable has a value greater than 255, the LED will go out and then gradually become brighter again and so on. Why it behaves like this is beyond the scope of this course but it has something to do with bits and bytes. 12
Hands-on Exercise 12. Build two separate LED circuits. Each circuit will have its own 330 ohm resistor and connect to a different digital pin (that has a ~ next to it). The other end of each circuit will go to a ground port on the Arduino - the Arduino has three ground (GND) ports. Write a program where one LED gradually gets brighter while the other LED gradually gets dimmer. You should use two variables In the future you should always make sure that the value you pass to analogwrite is an integer between 0 and 255 but for now we ll just live with the results of passing out-of-range values. In the next unit we will learn how to check if a variable has a certain value and respond accordingly. 13