Arduino Manual
Contents are under development. I’ll try to update as much as possible while I am learning.
This is one of my DIY projects for building small scale robots.
Chapter 1. To be prepared.
Real quick talks on Arduino
Meaning of Arduino: “Friend” or “Brave Friend” in Italian.
Arduino is a mini circuit board. (Raspberry Pi is a mini PC).
: It is hard to tell you which is better for you among Arduino and Raspberry Pi.
: The choice depends on your purpose, interest, and affordability for software and peripherals.
: Arduino is mostly based on C++, Rapberry Pi is based on Python.
: But one thing I can tell is that Arduino is more likely a good choice when you want to build robots and some gadgets (Raspberry Pi is in fact a PC to support a program).
Arduino is a versatile tool to boost your sense on hardware and software at the same time.
In this Manual, ...
Most beginners when learning a new programming are eager to see how their projects really work. Understandable… That is an undeniable joy of DIY!
The best way of learning a programming is to do experiments by himself with some tangible results. So, in the following chapters, we will do step-by-step works bearing in mind this purpose.
The most important thing is to have a fun to work with Arduino.
Brief outlines for the Manual
Chap. 1 Hardware and software
Chap. 2 Play with inputs/outputs and sensors
Chap. 3 Build your robot cars (limited access)
Appendix. Electricity 101
Arduino Hardware
Board
There are various Arduino boards
In this manual, I exclusively focus on Arduino Uno board which is suitable with beginners: http://www.arduino.cc/en/Main/ArduinoBoardUno
Fig. 1 shows detail parts of explanations on Arduino board.
Fig.1. Arduino Uno Board
source: http://toweboffice.com/wo/wp-content/uploads/2012/07/arduino_uno_2.jpg
ATmega328: This is a real brain of the Arduino board, IC (Integrated Circuit: 집적회로) .
The voltage of Arduino is 5 volts which is enough to run most works. But remember that 5v is may be too high for lightning LEDs. For this reason, we need resistors.
Arduino receives electricity from the external power supply or USB socket.
I recommend you to use USB socket because it will automatically supply 5v electricity.
2) Bread Bed
Bread bed is a companion part of Arduino.
Fig. 2 Bread Bed
You can connect a bread bed with the board using ‘jumper wire’.
Many components could be built on the bread board: LED, resistors, diode, potentiometer, DC motors etc.
jumper wire
LED
resistor
diode
potentiometer
DC motor
piezo element
temp. sensor
transistor
push button
Don’t need to worry about how you are going to use all thses components. While going through each step presented in this Manual, you will have a thrilling experience to see them work.
Arduino Software
Arduino is run by C++ which could be one drawback of Arduino compared to Raspberry Pi using Python.
However, one convenient platform can be used to save your time and a lot of efforts: Arduino IDE.
Arduino IDE provides various levels of built-in C++ programs for Arduinoers to demonstrate. Besides, you can modify the programs for your own purpose.
Download site: http://www.arduino.cc/en/Main/Software
Installation of Arduino IDE
Before you install, your Arduino needs to be connected to your PC where the IDE is installed.
Choose your download option with proper OS.
Fig.3 Download Website
2) Open the Arduino IDE
After installation, you will see the following window with many buttons displayed in the upper level row.
You will learn what these buttons are and how they works.
Fig.4 Arduino IDE
Verify button
Upload button
check for syntax errors
run your program
Select your Arduino board: Tools => Board => Arduino Uno (if your board is Uno)
Select your Serial Port: Tools => Serial Port => Your port (if you don’t know which serial port you are using, just plug off your Arduino, then the right serial port you are looking for will disappear. It is the port you use.)
In my case, COM5 is the port at which my Arduino is connected.
Fig.5 Choose the right serial port
Chapter 2. Play with LED lights to learn inputs and outputs
1. On and off of LED
Work to Blink an LED
Programming for blinking an LED is a popular first step to learn Arduino in most textbooks.
You will learn how to connect Arduino board with the bed and to program using the sketch.
First, connect jumper wires as displayed in the Fig.6. Be careful to use the same pins. And then, insert LED into the bread bed.
Fig.6 Connection for blinking an LED
Next, open the sketch file for this work: File => Examples => Basics => Blink
Then, you will see the following sketch window which shows basically a C++ program to let the LED blink.
Click
to verify and then to run the program, if no any error is reported in the bottom message box.
What happened? Do you see the LED blink?
I bet you didn’t see any light coming out from the LED. Then, what’s the problem?
Fig.7 Blink sketch
Look at the Blink sketch and go to the line where is pinMode. The number 13 indicates the digital pin number to get the LED connected. But, in Fig.6, you see the LED is connected to the number 9 pin.
So, simply change the 13 pin to 9 pin at your Blink sketch.
Then, you will see the LED with on and off.
2) Understanding the Blink sketch
Now, it is the time to understand what’s going on in the Blink sketch which consists of a bunch of programming lines.
void setup() is a C language function.
: You should always put this void setup() for set up.
: This command runs only once and the properties described in the void setup() do not change in the course of running.
pinMode is a function to tell you where the pin is connected.
void loop() is a function for repetitive procedures used in the program.
Remember that void setup() and void loop() are always used in every Arduino sketches.
digitalWrite gives voltage to the designated pin.
delay: literally speaking, this function will stop the process for the given time interval. Millisecond is the basic unit, hence delay(1000) waits for one second.
Wrap-up
In this section, you learned how to connect your LED on the bread bed to the Arduino board.
You learned to use the built-in Blink sketch to let LED blink.
2. Fade function
Blinking LED requires a digital action, i.e., 0 and 1 only: 0 for off, 1 for on.
What if you want to fade in or fade out the LED instead of radical turn-on and off?
In fact, this is about what analog signal does… something between 0 and 1 to allow slightly dim lights.
For this purpose, you open the Fade sketch file: File => Examples => Basics => Fade
Fig.8 Fade sketch
=> this if means, if brightness is 0 or 255, you do the math in the loop by changing the value of fadeamount by its opposite value.
=> when press the button, 5v flows to give HIGH, then, digitalWrite gives HIGH to pin13..
In Fig.8, almost everything in Fade sketch look similar to the Blink except for a few added lines in the loop and a function of analogWrite() which is about giving analog power to the designated pin.
The voltage level could be any number between High and Low: 255 for high, 0 for low.
When you run the Fade by clicking , you see the level of light in the LED keep changing gradually with increasing and decreasing power.
3. Using the button to control LED
Next, you may want to use the button function to turn on and off the LED. Here’s what you need to do.
Open the sketch file: File => Examples => Digital => Button
Fig.9 Button sketch
=> use the built-in LED at pin 13
=> button as an input, and pin as an output
Fig.10 Button to light Pin13
Red line: 5v goes to one leg of the button . But without pressing the button, 5v does not flow into the board.
10k Ohm resistor is connected to the other leg of the button.
Yellow line: one leg of button is connected to pin 2.
Blue line is linked to the ground (GND).
When you complete the circuit buiding as Fig.10, click to run the button sketch.
You will see that the built-in LED will be turned on when you press the button.
If you don’t like to use the built-in LED, the following procedure with slight changes for the previous sketch and circuit building will work for you.
Configure the Arduino board like the following figure.
Think about a role of resistor being applied to the LED and button.
before push button
after push button
4. Temp sensor and alarm warning
In this section, let us work with temperature sensor. Suppose, you are concerned about getting heat more than 26 C degree. You want to have some warning system whenever the temp > 26.
Additional parts we need.
temp sensor
buzzer
I am using a temperature sensor made by Adafruit:
https://learn.adafruit.com/tmp36-temperature-sensor/using-a-temp-sensor
: The website gives detail instructions, e.g., do not use a 9v battery.
: Basic formula also is provided in the webstie.
Designate the pin number
const int temPin = 0;
const int buzzPin = 9;
Set the pin mode for output in void setup()
Serial.bigin (9600); // bit per second
pinMode(buzzPin, OUTPUT);
In void loop (),
: declare the new variables and calculate temperature.
float voltage, celcius;
voltage = getVolt(temPin); // getVolt func is used
celcius1 = (voltage - 0.5) *100;
: send the temp record to the print
Serial.print(“voltage: ”);
Serial.print(voltage);
Serial.print(“Celcius: “);
Serial.println(celcius ); // println changes to a new line
: Use if to make a buzz when the temp > 27 degree
if (celcius1 > 27)
{
tone(buzzPin,262,500); // 262 means the C note, 500 means 0.5 second.
}
Fig.11 Temp sensor with buzz
Now, you can verify the work you’ve done with the above coding.
What if you want to add another function, e.g., by allowing an LED to blink whenever the temp > 26 degree?
This kind of work requires to combine the section 1’s work on the blinks of LED. Can you do that?
: with a little bit of coding and circuit works (see Fig.12), you are now able to let the buzzer sound and the LED blink when temp > 26.
: Do you see the difference between Fig.11 and Fig.12? Note that the LED uses 3.3v electricity which still requires the resistor.
Fig.12 Temp sensor with buzzer and LED
5. Christmas lights
Before attempting to make Christmas lights, we first need to know how sound is working on Arudino.
The following sketch shows how to make your notes and beats (see Fig.13)
Information on sound frequency can be found in:
http://www.arduino.cc/en/Tutorial/Tone
Using the same method above, you can write Silent Night song with LED movement.
Required parts: 4 LEDs, four 330ohm resistors, and piezo speaker.
Keep in mind that the song of Silent Night needs more that 50 notes.
You need a minimum effort to revise the sketch program which was used in the above example.
: LED must be programmed to be turned on and off consecutively along the music notes
: Think about how you make an order of LED lights which have only 4 sockets but with many notes more than 50.
6. Working with a motor
When your task is to run a DC motor at Arduino, extra knowledge on electricity is required, such as why we need transistors and diodes.
The appendix in this Manual will help you comprehend for this issue. But at this stage you better just to keep in mind their roles, i.e., control of current flow (dioide), and amplifying/switching of electric signals (transistor).
Diode controls the direction of current. When you work with a DC motor, diode is an essential part to prevent electromagnets from causing a damage to your circuit.
The direction of anode and cathode is very important in your circuit work. Cathode (-): linked to the (-) part of the bread bed.
Anode (+): linked to the collector part of the transistor
Transistor changes a voltage or current. We need transistors especially when 5v of Arduino electricity is not sufficient to make a DC motor run.
7. Working with a sensor
Ultrasonic sensor is a must-have component when you want to build a robot car which is an our final task.
There are various types of ultrasonic sensors. I bought HC-SRO4 sensors which are really cheap and functioning well (only $8.99 with 2 sensors!. See http://www.amazon.com/gp/product/B00E0NXTJW/ref=oh_aui_detailpage_o01_s00?ie=UTF8&psc=1).
Fig. Ultrasonic sensor
I refered to the following website to work with a HC-SR04 sensor. The site provides detail procedures step-by-step and a sketch code:
http://www.instructables.com/id/Arduino-Distance-Detector-with-a-Buzzer-and-LEDs/?ALLSTEPS
Check with the following link to see what is a final look.
https://drive.google.com/open?id=0B130_glJSe6HaU9DZi1FUkVaTWM&authuser=0
; As an object is getting closer to the sensor, LEDs and buzz give a warning. Check for a kind of stealth move when tilting the object. :)
Sensor_motor_project: Putting the ultrasonic sensor together with all components so far we've studied such as buzz, LED lights for warning, and motor, finally we have the following result. When an object is within a pre-specified range of sensor, LEGO fan starts to move:
You can use a nice software, Fritzing, to draw schematic diagrams for board. Below figure was made to show a structure which I used for the Sensor_with_motor project.
Now, it is the time to start with a project to build your robot car. All knowledge for this robot car project is nothing else more than what we've studied so far. Every components will come together step-by-step and sketch program will be put together to be suitable for the project.
Chapter 3. Arduino Robot Project
Obstacle Avoidance Robot: Model = BOE Shield Bot
Connection of Arduino Uno (R3) and motor shield
Test of servos and battery pack
BOE Shield Bot w/o infrared senor
Robot Car: equipped with infrared sensor
Appendix. Matlab support for Arduino
Arduino could be run by Matlab. Packages for supporting Matlab can be downloaded from
: http://playground.arduino.cc/Interfacing/Matlab
:Matlab 2014 version uses the MATLAB Support Package for Arduino
http://www.mathworks.com/matlabcentral/fileexchange/47522
: For Matlab R2013 or earlier, use ArduinoIO Package
In this Manual, I use the ArduinoIO package.
After installing one of above packages, you update “set path” in Matlab to allow new path of ArduinoIO.
Fig. Calling Arduino at Matlab
a = arduino(‘COM5’): calling Arduino via the serial port, COM5.
: After opening the Arduino object in Matlab, you can see what kinds of Arduino functions are retrieved (they are called ‘method’ in Matlab)
: for example, ardio.pde provides pinMode digitalRead digitalWrite analogRead analogWrite analogReference so that they are available in Matlab.
Simple Arduino project can work with several pde files.
Above example shows how to turn on and off an LED inserted to the socket of bread board.
: I recommend you to do some your own experiments using analogRead and analogWrite (refer to FADE blink section in the Manual).