Arduino
Arduino
ca/
Published August 2013
share:
This manual is the intellectual property of MakeUseOf. It must only be published in its original form. Using parts or republishing altered parts of this guide is prohibited without permission from MakeUseOf.com
Think youve got what it takes to write a manual for MakeUseOf.com? Were always willing to hear a pitch! Send your ideas to justinpot@makeuseof.com.
Table Of Contents
1.Intro to the Arduino 2.What Can You Do With an Arduino? 3.What Is Inside an Arduino? 4.What You Will Need For This Guide 5.Electrical Component Overview 5.1What is a Breadboard? 5.2What is an LED? 5.3What is a Photo Resistor? 5.4What is a Tactile Switch? 5.5What is a Piezo Speaker? 5.6What is a Resistor? 5.7What are Jumper Wires? 6.Programming Overview 6.1Variables 6.2Functions 6.3Logic Overview == - The Equals operator && - The AND operator || - The OR operator ! - The NOT operator Using Multiple Expressions 7.Setting Up Your Arduino 7.1Installing the Arduino IDE on Windows Step 1: Download the Arduino software Step 2: Install the software 7.2Installing the Arduino IDE on Mac OS X Step 1: Download the Arduino software Step 2: Install the software 7.3Installing the Arduino IDE on Ubuntu/ Linux 7.4Running the Arduino Software HTTP://MAKEUSEOF.COM BRAD KENDALL, HTTP://WWW.BRADKENDALL.CA 5 6 7 8 9 9 9 10 10 10 11 11 12 12 12 12 13 13 13 13 13 14 14 14 14 15 15 15 15 15 3
share:
8.1Communicating Between Your Arduino and Your PC Reading from the Serial Port 8.2Building a Calculator 8.3Turning on an LED 8.4Making Your LED Blink 8.5Making Multiple LEDs Blink 8.6Pushbuttons with a Pull-up Resistor 8.7Turning on an LED with a Pushbutton 8.8Control an LEDs Brightness 8.9Observing Light with your Arduino 8.10Making Music with your Arduino 9.Where to go From Here
4
share:
There are plenty of other microcontrollers available. So you may be asking, why choose the Arduino? Arduino really simplifies the process of building projects on a microcontroller making it a great platform for amateurs. You can easily start working on one with no previous electronics experience. That is what this guide is about. In addition to Arduinos simplicity, it is also inexpensive, cross-platform and open source. The Arduino is based on Atmels ATMEGA8 and ATMEGA168 microcontrollers. The plans for the modules are published under a Creative Commons license, so experienced hobbyists and professionals can make their own version of the Arduino, extending it and improving it. Believe it or not,even relatively inexperienced users can build a version of the Arduino module on a breadboardin order to understand how it works and save a little bit of money.
The specs may seem meager compared to your desktop computer, but remember that the Arduino is an embedded device. We have a lot less to process than your desktop. Another wonderful feature of the Arduino is the ability to use what are called Shields. Although we will not be covering shields in this manual, an Arduino shield will give you crazy functionality like you wouldnt believe. Check out thislist of some really cool Arduino shieldsto take your projects to the next level.
5.2What is an LED?
An LED, short for Light Emitting Diode, is a semiconductor light source. LEDs are typically used as visual indicators. For instance, your new Arduino microcontroller has an LED on pin 13 that we frequently use to indicate an action or event.
A photo resistor allows us to measure light by decreasing its resistance when it detects an increase of light intensity.
A tactile switch is an electric switch that controls the flow of electricity. When pressed, the switch completes the circuit. Basically, it is a button.
A piezo speaker is a single frequency beeper that converts an electrical signal into a tone. This will allow your Arduino to sing to you.
5.6What is a Resistor?
Jumper wires are short wires that are used for prototyping circuits. These are what you will use to connect the various components electrically to your Arduino.
6.Programming Overview
If youre not too familiar with programming, this guide should get you used to some of the fundamentals. If youd like to learn more about Arduino-specific functions, http://www.arduino.cc/en/Reference/HomePage is an excellent resource.
6.1Variables
A variable is defined as a quantity that can assume any of a set of values. In the Arduino programming language, variables have types associated with them, which provide the set of valid values the variable can hold. Some languages are not strict and allow a variable to hold nearly anything, but that is out of the scope of this manual. For example, a variable with type int can only hold integer values like 1 or 12, and not 12.5 or cats. Unfortunately, no variable is capable of holding a cat, something the programming world is quite upset about. Variables are an excellent resource, as they improve code readability and reuse, and are extremely convenient for use as temporary storage. Before using a variable, you must declare it. This merely lets the Arduino compiler know what data type your variable will hold. An example of a variable declaration is as follows: int itemCount; In this case, the variable will be of type int, and therefore will only accept integers. Here are a few example assignments and operations. itemCount = 4; itemCount = itemCount + 8; // itemCount now holds the value 12. itemCount = 10; // This will not compile.
6.2Functions
A function is essentially a group of instructions that perform a specific task. There are many built-in functions, such as digitalWrite() or tone(). In those cases, you dont necessarily have to see the code, but can still reap the benefits. You can also specify your own functions. The general form of a function is: [return type] [function name] ({arguments}) { [ Code to execute ] } Note that functions can return data, as illustrated by the function having a return type. In many cases, there is no data to return, and in that case, the keyword void would be used. The function name is a user-friendly handle to reference later (digitalWrite would be the function name for the digitalWrite function). A function can accept zero or more arguments. Each argument must be of the form [datatype] [identifier]. For example, if we called a function foo as such: foo(10); The function header for foo would have to look like: void foo(int number) { } In the function, code can reference number to retrieve the passed value. Outside of the function, number would be undefined. Say we want to write a function to multiply two numbers, for whatever reason. This function would look like: int multiply(int num1, int num2) { int result; result = num1 * num2; return result; } Note that this could simply look like: int multiply(int num1, int num2) { return num1 * num2; } Its usually a good idea to be liberal with the use of spaces, as it makes for much easier debugging. To each their own, however.
6.3Logic Overview
Youll often find yourself wanting to execute certain code under certain conditions. This will give you a quick overview of the logical operators you have to work with. First up, with the exception of the NOT operator, each logical operation takes two operands.
The AND operator is quite similar to the equals operator, except it does not evaluate to true when both operands are false. For example: (true && true) evaluates to true, while (true && false) and (false && false) both evaluate to false.
|| - The OR operator
The OR operator will evaluate to true so long as at least one of the two operands is true. The only time OR will evaluate to false is if both the operands are false.
Copy the Arduino application into the Applications folder (or elsewhere on your computer). Since youre using an Arduino Uno, you dont have any drivers to install.
Now that our software is installed and our Arduino is setup, lets verify everything is working. The easiest way to do this is by using the Blink sample application. 1. Open the Arduino Software by Double-clicking the Arduino Application (./arduino on Linux). 2. Make sure the board is still connected to your computer. 3. Open the LED blink example sketch:File > Examples > 1.Basics > Blink. You should see the code for the application open and it should look like this:
4. Youll need to select the entry in theTools > Boardmenu that corresponds to your Arduino. Select the Arduino Uno Option. 5. Select the serial device of the Arduino board from theTools > Serial Portmenu. On Windows, This is likely to be COM3 or higher. On the Mac or on Linux, this should be something with /dev/ tty.usbmodem in it.
6. Now, simply click the Upload button in the environment. Wait a few seconds - you should see the RX and TX LEDs on the Arduino flashing. If the upload is successful, the message Done uploading. will appear in the status bar. A few seconds after the upload finishes, you should see the pin 13 (L) LED on the board start to blink. If it does, congratulations! Youve got your Arduino up and running.
8.Starter Projects
Okay, now is when the real fun begins. Lets get started.
Most of the communication youll be doing with the Arduino (for now) will be done via the Serial port (The USB cord). This is quite trivial to set up on the Arduino. Merely add the following line to your setup() method: Serial.begin(9600); 9600 is the baud rate, something we will not get into here (it essentially means the number of signal changes made per second, and merely ensures that the PC and the Arduino are on the same page in regards to this). Whenever you would like to write something to the serial port, simply use the Serial.print or Serial.println function, as so: Serial.print(Hello world!);
8.2Building a Calculator
To tie all of your new found programming knowledge together, we submit to you the following program that performs basic mathematical operations. We have clearly commented the code, so you should be able to understand each step. There is a download available for people who dont like typing at:http://www.bradkendall.ca/arduino Here we go! /* Example Arduino Calculator Communication protocol: Send an A, S, M, or D via serial, than two numbers. The arduino will reply with the result of the operation on the two numbers, (first number first). Note that the division will no doubt look strange - it is an integer division and therefore there will not be anything after the decimal point. */ void setup() { Serial.begin(9600); Serial.println(Calculator initiated.); } /* loop() This code gets executed over, and over, and over, and over, and over, and over, and over, and over, and over, and over, and over, and over, and over, and over, and over, and over again. Our loop pretty much starts the waiting for input stage, where we wait for the user to input a character (the mathematical operation), then two operands. After we output the result, we let the loop get hit again, and joy is had by all! */ void loop() { char operation; int number1; int number2; // hehe, Number 2. int result; // Hold the result of the operation. boolean success; // Indicates whether the operation // was successful (we knew what to // do - nothing bad was inputted) success = true; // Go ahead and set success to true ; // The only time we will be updating // this variable now is to set it to // false if weve encountered a // problem.
Serial.println(Pick an operation: Add, Subtract, Multiply, or Divide (Simply input the first letter in quotes.)); // We have to wait for the user to send something // here; the easiest way to do so is to simply loop // and waitfor Serial.available() to be true. while(Serial.available() == 0) { ; // ; indicates an empty statement. Or a sea // monster in Nethack. God those suck. } // This loop will continue executing while Serial. // available() == 0. Thus, it will be stuck here until // the serial has a character waiting. operation = Serial.read(); // We have to do the same thing to get the two // operands (numbers). // I have factored this code into a function so that // I do not have to rewrite it twice. See if you can // determine why I would not be able to use it (at // least intuitively) to get the operation. Serial.println(Okay, now please enter the two numbers, one at a time!); number1 = waitForNum(); Serial.print(Read: ); Serial.println(number1); number2 = waitForNum(); Serial.print(Read: ); Serial.println(number2); // Now we have read in all the data we need. It is // time to calculate the result. We will have to // determine what operation the user specified, and // perform the calculation from there. Serial.print(Operation: ); if(operation == A) { // This checks to see if the user sent along the // character A, specifying an add. Serial.println(ADD (Look, a kitty!)); result = number1 + number2; } else if(operation == S) { // Note that the above condition will only be // tested for if operation is not equal to A // hence the else. // This code executes if the operation is S for // subtract. Serial.println(Subtract); result = number1 - number2; } else if(operation == M) { // In this case, we will be multiplying. Serial.println(Multiply); result = number1 * number2; } else if(operation == D) { // Here we will be dividing. Serial.println(Divide); result = number1 / number2; } else{ // This code will be used if the character // specified doesnt match anything - in other // words, the user did not send A, S, D, or M, // and we dont know what to do. // Hence, set success to false success = false;
} // Now we should have our result. Time to send the // user back something! (Then start over again! Joy!) if(success) { // Note that print will not start a new // line, and the next print statement will // continue writing right // where the previous one left off. // Output the result. Serial.print(Result: ); Serial.println(result); } else { Serial.print(Sorry, I dont understand what you want me to do! (You inputted ); Serial.print(operation); Serial.println()); } } int waitForNum() { int ret; while(Serial.available() == 0) { ; } // Why minus 0? The value well get from Serial. // read() will be a character. What this means is // that its numeric value will not necessarily // reflect the number it represents. (Look at an // ASCII table, the character 0 actually has a // decimal value of 48!) // The take-away from this is that, since fortunately // all the numbers are in sequence, you can simply // subtract the decimal value of 0 from // whatever you read in, and youll be left with the // number itself. 5 - 0 = 5 . ret = Serial.read() - 0; // To handle numbers that span more than one // character (like 124, which spans three), we must // loop until there is no more input, and multiply // each number we read by one (as 124 would come in // like: 4 2 1 // And the number we build would be: // (((1 * 10) + 2) * 10) + 4, // or 124! The joys of the decimal numbering system! // Note that the delays are merely to slow things // down a bit - removing them would have the // code execute too quickly to notice more // characters waiting to come in from Serial. // A little strange, neh? Welcome to the joys of this // type of thing. =] delay(10); while(Serial.available() != 0) { ret = ret * 10; ret += Serial.read() - 0; delay(10); } return ret; }
8.3Turning on an LED
What You Need: 1 LED 1 Resistor 1 KOhm (brown, black, red) 4 Jumper Wires
You will build a circuit by plugging the LED and resistor leads into small holes called sockets on the breadboard. Lets get started! Hardware Setup: Step 1 - Unplug the USB cord from your Arduino. Step 2 - Plug a jumper wire from the 5V port on your Arduino into the positive section of your breadboards power lane. Step 3 - Plug a jumper wire from the GND port on your Arduino into the negative section of your breadboards ground lane. Step 4 - Plug the LEDs cathode (the short lead) into the I-2 socket on your breadboard. Step 5 - Plug the LEDs anode (the long lead) into the I-4 socket on your breadboard. Step 6 - Plug one of the resistors leads into the H-4 socket on your breadboard. Step 7 - Plug the resistors other lead into the H-9 socket on your breadboard. Step 8 - Connect a jumper wire from your breadboards power lane to the J-9 socket on your breadboard. Step 9 - Connect a jumper wire from your breadboards ground lane to the J-2 socket on your
Summary: Once power is applied to the circuit, the LED will turn on. This is about as simple as a circuit gets.
Hardware Setup: Step 1 - Unplug the USB cord from your Arduino. Step 2 - Plug a jumper wire from the Digital IO pin 8 into the I-12 socket on your breadboard. Step 3 - Plug one of the resistors leads into the H-12 socket on your breadboard. Step 4 - Plug the resistors other lead into the H-4 socket on your breadboard. Step 5 - Plug the LEDs cathode (the short lead) into the I-2 socket on your breadboard. Step 6 - Plug the LEDs anode (the long lead) into the I-4 socket on your breadboard. Step 7 - Connect a jumper wire from your breadboards ground lane to the J-2 socket on your
breadboard. Ensure that the ground lane is still grounded. Step 8 - Reconnect the USB cable to your Arduino.
Software Setup: Open up your Arduino Development Environment and create a new sketch (File > New). Enter the following code into your sketch: void setup() { // initialize the digital pin as an output. // Pin 8 is our output pin pinMode(8, OUTPUT); } void loop() { digitalWrite(8, HIGH); // set the LED on delay(1000); // wait for a second digitalWrite(8, LOW); // set the LED off delay(1000); // wait for a second } After you enter the code, press the upload button and your LED should start blinking. Summary: The digitalWrite(8, HIGH); command sets the output pin 8 on the Arduino to 5V. The digitalWrite(8, LOW); command sets the output pin 8 on the Arduino to 0V. The delay(1000); command pauses execution on the Arduino for 1000 ms or 1 second. Since this in the loop() function, the code is called over and over again. Pretty cool, huh?
What You Need: 2 LEDs 2 Resistor 1 K Ohm (brown, black, red) 4 Jumper Wires
Hardware Setup: Step 1 - Unplug the USB cord from your Arduino. Step 2 - Setup the project board the same as in Project 3. Step 3 - Plug a jumper wire from the Digital IO pin 9 into the I-16 socket on your breadboard. Step 4 - Plug one of the resistors leads into the H-16 socket on your breadboard. Step 5 - Plug the resistors other lead into the H-24 socket on your breadboard. Step 6 - Plug the LEDs anode (the long lead) into the I-24 socket on your breadboard. Step 7 - Plug the LEDs cathode (the short lead) into the I-26 socket on your breadboard. Step 8 - Connect a jumper wire from your breadboards ground lane to the J-26 socket on your breadboard. Ensure that the ground lane is still grounded. Step 9 - Reconnect the USB cable to your Arduino.
Software Setup: Open up your Arduino Development Environment and create a new sketch (File > New). Enter the following code into your sketch: void setup() { // initialize the digital pins as an output. pinMode(8, OUTPUT); pinMode(9, OUTPUT); } void loop() { digitalWrite(8, HIGH); // set the LED on digitalWrite(9, LOW); // set the LED on
delay(1000); // wait for a second digitalWrite(8, LOW); // set the LED off digitalWrite(9, HIGH); // set the LED on delay(1000); // wait for a second } After you enter the code, press the upload button and both your LEDs should start blinking. Summary: This project is exactly the same as the last project, except we have added an additional LED on output pin 9 that turns off when the other LED is on. Can you think of any other ways to expand on this?
Hardware Setup: Step 1 - Unplug the USB cord from your Arduino. Step 2 - Connect a jumper wire from your breadboards power lane to the C-3 socket on your breadboard. Ensure that the power lane is still connected. Step 3 - Plug one of the 2 K resistors leads into the B-3 socket on your breadboard. Step 4 - Plug the 2 K resistors other lead into the B-7 socket on your breadboard. Step 5 - Plug a tactile switch so the pins are in the F-9, F-7, E-9 and E-7 on your breadboard. Step 6 - Plug one of the 1K resistors leads into the H-7 socket on your breadboard. Step 7 - Plug the 1K resistors other lead into the H-14 socket on your breadboard. Step 8 - Plug a jumper wire from the Digital IO pin 9 into the I-14 socket on your breadboard. Step 9 - Connect a jumper wire from your breadboards ground lane to the H-9 socket on your breadboard. Ensure that the ground lane is still connected. Step 10 - Reconnect the USB cable to your Arduino.
Software Setup: Open up your Arduino Development Environment and create a new sketch (File >New). Enter the following code into your sketch: void setup() { // initialize the digital pin 9 as an input. pinMode(9, INPUT); // initialize the serial port. Serial.begin(9600); } void loop() { int buttonStatus = digitalRead(9); if (buttonStatus == LOW) //The button is down { Serial.println(The button is down); } } After you enter the code, press the upload button and open the Serial Monitor (Tools > Serial Monitor). When you press the tactile switch, the serial monitor should print The button is down. Summary: This project reads the digital input for 5v (HIGH). When the button is pressed, the voltage is set to 0v (LOW) and the Arduino executes the code in our if statement.
What You Need: 1 - LED 1 Resistor 1 K Ohm (brown, black, red) 1 Tactile Switch 4 Jumper Wires
Hardware Setup: Step 1 - Unplug the USB cord from your Arduino. Step 2 - Plug a tactile switch so the pins are in H-9, H-11, J-9 and J-11 on your breadboard. Step 3 - Plug a jumper wire from the GND port on your Arduino into the negative section on your breadboards ground lane. Step 4 - Plug the LEDs cathode (the short lead) into the I-2 socket on your breadboard. Step 5 - Plug the LEDs anode (the long lead) into the I-4 socket on your breadboard. Step 6 - Plug one of the resistors leads into the H-4 socket on your breadboard. Step 7 - Plug the resistors other lead into the H-9 socket on your breadboard. Step 8 - Connect a jumper wire from your breadboards power lane to the G-11 socket on your breadboard. Step 9 - Connect a jumper wire from your breadboards ground lane to the J-2 socket on your breadboard. Step 10 - Reconnect the USB cable to your Arduino.
Hardware Setup: Step 1 - Unplug the USB cord from your Arduino. Step 2 - Plug one of the photo resistors leads into the I-14 socket on your breadboard. Step 3 - Plug the photo resistors other lead into the I-15 socket on your breadboard. Step 4 - Plug the LEDs cathode (the short lead) into the H-17 socket on your breadboard. Step 5 - Plug the LEDs anode (the long lead) into the H-15 socket on your breadboard. Step 6 - Plug one of the resistors lead into the I-17 socket on your breadboard. Step 7 - Plug the resistors other lead into the I-22 socket on your breadboard. Step 8 - Connect a jumper wire from your breadboards power lane to the J-14 socket on your breadboard. Ensure that the power lane is still connected. Step 9 - Connect a jumper wire from your breadboards ground lane to the J-22 socket on your breadboard. Ensure that the ground lane is still connected. Step 10 - Reconnect the USB cable to your Arduino.
Summary: As you see, the resistance of the photo resistor decreases with more light. The lower the resistance, the brighter the LED. Combine this with the pull up resistor project (Chapter 8.6) and watch the opposite effect.
Hardware Setup: Step 1 - Unplug the USB cord from your Arduino. Step 2 - Plug one of the photo resistors leads into the F-16 socket on your breadboard. Step 3 - Plug the photo resistors other lead into the F-15 socket on your breadboard. Step 4 - Plug one of the resistors leads into the I-15 socket on your breadboard. Step 5 - Plug the resistors other lead into the I-10 socket on your breadboard. Step 6 - Connect a jumper wire from your breadboards power lane to the G-16 socket on your breadboard. Ensure that the power lane is still connected. Step 7 - Connect a jumper wire from your breadboards ground lane to the J-10 socket on your breadboard. Ensure that the ground lane is still connected. Step 8 - Plug a jumper wire from the Analog IO pin 0 into the G-15 socket on your breadboard Step 9 - Reconnect the USB cable to your Arduino.
Software Setup: Open up your Arduino Development Environment and create a new sketch (File > New).
Enter the following code into your sketch: int lightPin = 0; //define a pin for Photo resistor void setup() { Serial.begin(9600); //Begin serial communication } void loop() { //Write the value of the photo resistor to the serial //monitor. int lightValue = analogRead(lightPin); Serial.println(lightValue); delay(1000); //pause for 1000 ms or 1 second. } After you enter the code, press the upload button and open the Serial Monitor (Tools > Serial Monitor). The console should give a light reading in the form of an integer. When you reduce the amount of light, the number will be lower. Summary: This project is the same as the previous project, except we are reading the values from your Arduino instead of outputting to an LED. The resistance of the photo resistor decreases with more light. You could use logic to reverse this effect!
Hardware Setup: Step 1 - Unplug the USB cord from your Arduino. Step 2 - Plug the positive lead of your piezo speaker into the E-15 socket on your breadboard. Step 3 - Plug the negative lead of your piezo speaker into the F-15 socket on your breadboard. Step 4 - Plug a jumper wire from the Digital IO pin 8 into the A-15 socket on your breadboard. Step 5 - Connect a jumper wire from your breadboards ground lane to the J-15 socket on your breadboard. Ensure that the ground lane is still connected. Step 6 - Reconnect the USB cable to your Arduino.
Easy, right?
Software Setup: This project is included in the Examples Section. No typing on this one! Open up your Arduino Development Environment .Open the toneMelody Example Sketch (File > Examples > Digital > toneMelody). After you enter the code, press the upload button and your piezo speaker will start making noise. You can modify the sound by modifying the melody[] and noteDurations[] arrays. Summary: This project produces sound out of the piezo speaker.
Did you like this PDF Guide? Then why not visit MakeUseOf.com for daily posts on cool websites, free software and internet tips?
If you want more great guides like this, why not subscribe to MakeUseOf and receive instant access to 50+ PDF Guides like this one covering wide range of topics. Moreover, you will be able to download free Cheat Sheets, Free Giveaways and other cool things.
Follow MakeUseOf:
Think youve got what it takes to write a manual for MakeUseOf.com? Were always willing to hear a pitch! Send your ideas to justinpot@makeuseof.com.