Arduino Examples 3
Arduino Examples 3
Advanced Projects
1
Table of Contents
SAFETY TIPS....................................................................................................................................... 6
INTRODUCTION................................................................................................................................ 7
2
Arduino Uno Tour
Time Required: 10 minutes
Spend a few moments looking at the diagram below and compare it to the Arduino
included in your kit.
The Arduino has been labeled to help you learn all the different connectors and parts.
3
Getting to Know your Breadboard
Time Required: 20 minutes
In order for us to connect our tiny components together, we need our breadboard. A
breadboard is great for prototyping since it does not create a permanent connection
between components like soldering does. Everything is held together by friction when
you insert them into those tiny holes inside your breadboard.
Power rails
Connectors
Five holes in each of
the horizontal rows
are connected.
What a breadboard looks like if we could see the wires under the breadboard
4
The previous page is an example of how a breadboard usually looks; on the right is how
a breadboard would look if we could see the wires that connect all of the holes together.
Those hidden wires are used to connect all your components to each other while you
prototype.
Take a look at your breadboard. You may have noticed that the breadboard holes are all
labeled A to J (vertically) and from 1-30 (horizontally). This is used to indicate where to
place your components.
Throughout this guide, we will be asking you to place your components in very specific
holes within your breadboard. For example, we might ask you to put a wire into hole 3E.
Now is the time to get familiar with the layout of your breadboard.
5
Safety Tips
This kit is not a toy and is not appropriate for small children. Small parts may present
a choking hazard. Not for children under 3.
Avoid touching the exposed end of ground and power wires when connected to the
Arduino.
Do not use lithium ion batteries, they may explode when shorted
Do not use on metallic surfaces, such as your Macbook. Place the Arduino on a non-
metal surface and refrain from working on the surface of your Macbook.
The library is not responsible for damage to any equipment and hardware used with
the kit, including personal computers, laptops or tablets.
Turn off/disconnect all power sources before modifying a circuit. While you’re
connecting components, keep your Arduino unplugged. Only connect it to a computer
or power source after the circuit is complete.
6
Introduction
You assembled kit includes all the parts you’ll need for the Arduino projects outlined in
this manual. When you’re done with your projects, please return the parts to their
proper slots, as indicated in this diagram, for the next person to enjoy.
7
The USB cable is used to connect the Arduino to your
computer.
9
DC Motor uses electricity to convert it to rotational
mechanical energy. Connect the positive and negative to
power and it will spin. Reverse the positive and negative pin
to reverse the power flow and it will spin the opposite
direction.
10
Servo Motor can be commanded to rotate to a specific angle.
These motors cannot rotate continuously and only have a
movement of 180 degrees.
11
Part Inventory for the Advanced Kit
The Arduino is the The breadboard is Jumper wires connect When you push the
microcontroller and used to temporarily the components button, it completes
brains of our project. connect multiple completing a circuit. the circuit.
It stores programs components and Note: The colour of
and processes inputs wires together during the wires do not
and outputs. prototyping. matter when building
the projects.
1x Potentiometer
1x H-Bridge IC
1x DC Motor 1x Servo Motor
chip
Can be used to detect
the position of a knob It is your standard Servo motor can be
Used to control the
when connected to DC motor. It spins commanded to rotate
direction of two
the Arduino analog when you apply to a specific angle (0-
motors.
input pins. power. ~180 deg).
12
Controlling a Servo with a Potentiometer
Time Required: 25 minutes
In this project, we will be using a potentiometer to control the movement of our servo
motor. When we twist our potentiometer knob, the servo will also rotate approximately
the same amount. Remember, it doesn’t matter what color the wires are in the diagram
compared to the ones you use.
Potentiometers are like a dynamic resistor, as you twist, the resistance value
changes (this will make the voltage change from 0-5v). These changes in
resistance is what our Arduino will read and convert into a number between 0-1023.
Positive Power
Ground
13
Required Components
3x
1x
1x Arduino 1x Breadboard Jumper 1x Servo Motor
potentiometer
wires
Wiring
14
Diagrams for this project
What are the pins on
our Servo Motor?
Power
Ground
(4.8-6v)
Signal (tells
Servo where
to move)
15
Code
Include the ability to use the Servo motor library (enables us to use the servo commands).
Line 1. #include <Servo.h>
Line 2.
Line 3. const int servoPin = 2; Create two constant integer variables. Variable servoPin will store the number
Line 4. const int userInputPin = A0; 2, and userInputPin will store the number A0.
Line 5.
Line 6. Servo myservo; Create a new servo called myservo. We give it a name since we could control more than one servo at a time.
Line 7. int pos = 0;
Line 8. Create an integer variable with the value of 0. This will be used to store the position the servo should
Line 9. void setup() { move to.
Line 10.
Line 11. myservo.attach(servoPin); Tell the Arduino to use (attach) the servoPin (pin #2) to the servo called “myservo”.
Line 12.
Set the potentiometer that is connected to userInputPin (pin #A0) as an
Line 13. pinMode(userInputPin, INPUT);
INPUT.
Line 14.
Line 15. }
Line 16.
Line 17. void loop() { Analog read the inputPin (A0), and store the value in our variable
Line 18. called pos.
Line 19. pos = analogRead(userInputPin);
Line 20. Map the raw sensor data (our sensor gives us a value between 0-1023) to a
Line 21. pos = map(pos, 0, 1023, 0, 180); value between (0-255) and store the converted value back to the variable
Line 22. pos.
Line 23. myservo.write(pos);
Line 24. Move the servo motor (called myservo) with the calculation we did
previously stored in the variable servoPos.
Line 25. delay(100);
Line 26. } Delay (pause) for 100 micoseconds. This will allow the servo motor to go
to its position before being told to move again.
16
Creating a 30 Second Countdown Timer
Time Required: 40 minutes
In this project, we will be using a servo motor as a countdown timer. The Servo motor will rotate 3 degrees of movement
for every second for a total of 30 seconds. We have a button that is connected to the Arduino so we can restart the 30
second timer when needed.
Required Components
1x
1x Arduino 1x Breadboard 5x Jumper Wires 1x Servo Motor
Pushbutton
17
Wiring
18
Code
Include the ability to use the Servo motor library (enables us to use the servo commands).
Line 1. #include <Servo.h>
Create a new instance of a servo called myservo. We give it a name since
Line 2. Servo myservo; we could control more than one servo at a time.
Line 7. const int timerSetValue = 30; Create a constant integer variable called timerSetValue with a
value of 30. This is used for how long we want the timer for.
19
Line 8. void setup() {
Tell the Arduino to use (attach) the servoPin (pin #9) to the
Line 9. myservo.attach(servoPin); servo called “myservo”.
Wait two seconds before running the rest of our program. This will give our servo a
Line 13. delay(2000); moment to move to its 0 degree position if it needed to move.
Line 14. }
What is INPUT_PULLUP?
When setting an Arduino Uno pin as INPUT_PULLUP this
activates the internal 20kohm resistor in the processor. This allows us
to connect a button to ground, then to the pin we want the button to
be connected to on the Arduino without using a resistor. This
however will reverse the logical of reading the button compared to
using INPUT. When the button is not pressed the pin will read
HIGH, and when the button is pressed it will read LOW.
20
Line 15. void loop() {
Read if the button is pressed. Since the
Line 16. if(digitalRead(buttonPin) == LOW) { buttonPin (pin #2) is using INPUT_PULLUP
instead of INPUT, the button pin will read LOW
when pressed. When it is pressed reset the timer
Line 17. timerCurValue = 0;
back to 0 and output the text “*** TIMER
RESTARTED ***” in the serial monitor.
Line 18. Serial.println("*** TIMER RESTARTED ***");
Line 19. }
The timer starts at 0. Use the current elapsed time, multiple it by 3 and
store this calculation in the variable servoPos. This means for every second
Line 20. servoPos = timerCurValue * 3; the servo will move 3 degrees.
21
Output a line break to our serial monitor. This is only to
Line 23. Serial.println(); make it easier to read the outputs and is for aesthetics.
Line 25. if (timerCurValue < timerSetValue) { If the current time (using our variable timerCurValue) is less
than the set timer time (using the variable timerSetValue
which is 30 seconds), increase the current time by one.
Line 26. timerCurValue = timerCurValue + 1;
Line 27. }
Wait one second before looping again. This makes it take one
Line 28. delay(1000); second to increase the timerCurValue variable by one and
move the servo 3 degrees every second.
Line 29. }
22
What is an H-Bridge?
Time Required: < 15 minutes
An H-Bridge is an electric integrated circuit that allows the ability to control up to two motors. These two
motors can be independently controlled what direction it will spin. This could allow us to create a robot for
example that could move forwards, backwards, turn left and turn right and have our Arduino program
those movements. We are just using half the chip for all the projects, most of the pins on the left-hand side of
the chip are for controlling a second motor. See the diagram on the next page for what each pin does from the H-Bridge.
23
This indented semicircle is used to
indicate the top of the IC H-bridge chip
Connect the
positive and
Ground connectors for the IC chip. negative pins from
Only one needs to be connected. the motor to the
output.
Positive voltage to power the motors When you send 5v to this pin, it will allow the motor
5-36v up to 600mA per motor. connected on the right side of the h-bridge to be enabled.
24
H-Bridge Controlled by an Arduino
Time Required: 30 minutes
In this project, we will be using an H-Bridge (Model L293D) connected with our Arduino to control the motor so it can
rotate clockwise or counter clockwise.
Pin #9 from our Arduino must output HIGH (output 5v) to enable the h-bridge to work. Pins #11 and pin #10 is used to
control which direction the motor will turn. If you have pin #11 high (output 5v) the motor will spin one direction, while
if you output high on pin #9 it will spin the other direction. The motor will not turn if both pin #10 and pin #11 is high
(outputting 5volts) since you are telling the h-bridge to go clockwise and counter clockwise at the same time.
This project will rotate the motor clockwise for 6 seconds, stop spinning the motor for 2 seconds, rotate the motor
counterclockwise for 6 seconds, stop the motor for 2 seconds and then repeat forever.
Required Components
14x Jumper
1x Arduino 1x Breadboard 1x H-Bridge 1x DC Motor
Wires
25
This indented semicircle is used to indicate the top of the IC
Wiring H-bridge chip. Make sure you put in the h-bridge correctly.
26
Code
27
H-Bridge Using Potentiometer
Time Required: 50 minutes
In this project, we will using an H-Bridge (Model L293D) with an Arduino to control the direction of the rotation of the
motor based on the position of the potentiometer.
Pin #9 from our Arduino will be used to control the speed of the motor attached (output 5v). Pins #11 and pin #10 is used
to control which direction the motor will turn. If you have pin #11 high (output 5v) the motor will spin one direction,
while if you output high on pin #10 it will spin the other direction. The motor will not turn if both pin #10 and pin #11 is
high (outputting 5v) since you telling the h-bridge to go clockwise and counter clockwise at the same time.
The potentiometer will be used at the 12 o’clock position to stop all motor movement. If you turn the potentiometer
counterclockwise, the motor will spin in that direction as well. If you turn the potentiometer clockwise the motor will spin
in the direction as well. As you rotate the potentiometer more and more clockwise or counterclockwise, the motor will
increase in speed until you reached the limit of how far it can move in either direction.
Required Components
12x Jumper 1x
1x Arduino 1x Breadboard 1x H-Bridge 1x DC Motor
Wires Potentiometer
28
The above diagram above is used to illustrate the movement of the motor when you rotate the potentiometer for this project.
29
Wiring
This
indented
semicircle
is used to
indicate
the top of
the IC H-
bridge
chip.
30
Code
Line 3. const int motorBkwdPin = 11; Create a constant integer variable called motoBkwdPin with a value of 11
(the pin is used to tell the h-bridge to move the motor counterclockwise).
Line 4.
Create a constant integer variable called potInputPin with a value of A0
Line 5. const int potInputPin = A0;
(the pin is used to read the value from the potentiometer).
Line 13.
Set the potentiometer that is connected to potInputPin (pin #A0) as an
Line 14. pinMode(potInputPin, INPUT); INPUT.
31
Start serial communication when the Arduino boots. Set the
Line 15. Serial.begin(9600);
communication speed to 9600 baud.
Line 16. }
Line 17. void loop() { Store the value from the potentiometer using analogRead
for the potInputPin (#A0) into the variable inputValue.
Line 18. inputValue = analogRead(potInputPin);
When the potentiometer knob input is between 0 –
Line 19. 472, output the Serial message “Clockwise”, set the
motorFwdPin to HIGH and the motorBkwdPin to LOW
Line 20. if(inputValue >= 0 & inputValue <= 472) {
(this will make the H-Bridge be told to go
clockwise). Store the calculated speed for the motor
Line 21. Serial.println("Clockwise");
to spin using the variable motorSpeed. We use the
map function to read the potentiometer so that when
Line 22. digitalWrite(motorFwdPin, HIGH);
the knob is reading 0, the speed will be 255 (speed is
a value between 0-255). When the potentiometer is
Line 23. digitalWrite(motorBkwdPin, LOW);
reading 472, set the motor speed to 0. It will then
Line 24. motorSpeed = map(inputValue, 0, 472, 255, 0); calculate automatically the motor speed to adjust
smoothly with the input from the potentiometer.
Line 25. }
When the potentiometer knob input is between 552
– 1023, output the Serial message
Line 26. else if(inputValue >= 552 & inputValue <= 1023) {
“Counterclockwise”, set the motorFwdPin to LOW
and the motorBkwdPin to HIGH (this will make the
Line 27. Serial.println("Counterclockwise");
H-Bridge be told to go counterclockwise). Store the
Line 28. digitalWrite(motorFwdPin, LOW); calculated speed for the motor to spin using the
variable motorSpeed. We use the map function to
Line 29. digitalWrite(motorBkwdPin, HIGH); read the potentiometer so that when the knob is
reading 552, the speed will be 0 (speed is a value
Line 30. motorSpeed = map(inputValue, 552, 1023, 0, 255); between 0-255). When the potentiometer is reading
1023, set the motor speed to 255. It will then
Line 31. } calculate automatically the motor speed to adjust
smoothly with the input from the potentiometer.
32
Line 32. else {
Any other value for the potentiometer knob input
Line 33. Serial.println("Stop");
(which would be between 473 – 551), output the
Serial message “Stop”, set the motorFwdPin to
Line 34. digitalWrite(motorFwdPin, LOW);
LOW and the motorBkwdPin to LOW (this will
Line 35. digitalWrite(motorBkwdPin, LOW); make the H-Bridge be told to stop moving the
motor). Set the speed for the motor to 0.
Line 36. motorSpeed = 0;
Line 40.
Output the serial message “Speed: ”, and then
output on the same line the contents of the
Line 41. Serial.print("Speed: ");
variable from the motorSpeed. Since we are using
Line 42. Serial.println(motorSpeed); println, this will create a line break after output
the contents of that variable.
Line 43.
Set the speed of the motor. This is done by using
Line 44. analogWrite(motorEnPin, motorSpeed); analogWrite to the motorEnPin (pin #9, which is
connected to the h-bridge from that pin)
Line 45. outputting the number stored in motorSpeed.
Line 46. delay(100); Create a delay for 100 microseconds only so we can slow
down the serial text output to make it easier to read.
33
Recommended Resources
Get these for free at the Toronto Public Library
Want to learn more about Arduinos? Here is a list of our favorite Toronto Public Library books and resources. When
using the Arduino Kit, please stick to the projects outlined in this manual. Additional projects found in the recommended
resources are for educational and entertainment purposes and are only intended for use with your personal Arduino.
http://www.Arduino.cc
The official website has great tutorials and reference
resources. The website includes information on all
commands you can do for the Arduino programming
language and examples on how to use them.
34