This document contains slides from a workshop on controlling hardware with Arduino. It discusses connecting sensors like sonar and line sensors, reading their input, and using that input to control outputs like LEDs, motors, and servos. Key concepts covered include digital and analog I/O, different Arduino pin types, using libraries, reading sensors, conditional statements, and different loop structures. The overall aim is to provide the knowledge needed to program an autonomous robot.
Arduino Programming for Basic Robotics - University of Moratuwa
1. Workshop by the undergraduates of
Dept. of Electronics and Telecommunications Engineering
University of Moratuwa
Slides by Abarajithan G
2. How to control the hardware?
It is all about logic!
Slides by Abarajithan G
3. 1. Take down notes
2. Logic is important, understand every line of code
3. Ask Questions… Don’t be afraid
4. All the different files you make, save them separately.
We will move between files
Before we start… Remember:
Slides by Abarajithan G
4. Code copied from Internet
will NEVER work
We can get code from internet.
But we need to read it, understand
every line and then change it suitably,
to make it work properly.
GOLDEN RULE - 1
Slides by Abarajithan G
5. Programming a robot takes
many weeks of time
GOLDEN RULE - 2
Selecting &
Buying
Components
Assembling
Robot Programming & Troubleshooting
Total time available until competition
Slides by Abarajithan G
14. What is For Loop?
• Initialization
• Loop Condition
• Loop Increment
When will the loop stop?
What if I change the delay?
How can I make it go in reverse?
For Loop: Multiple LEDs
Slides by Abarajithan G
15. • Setup() : Runs at the beginning. Only once
• Loop() : Run again and again
• Delay : is in milliseconds. Delay(500) -> half a second
• For Loop : Custom defined loop.
If you know how many times to loop
KEY POINTS
Slides by Abarajithan G
16. Arduino, we need to talk: Serial
BAUD RATE SHOULD BE EQUAL
Slides by Abarajithan G
17. ENOUGH WITH LEDs
Let’s get serious
Connect a Sonar sensor
(called HC-SR04)
• Vcc : 5V
• GND : Ground
• Trig : 11
• Echo : 12
Slides by Abarajithan G
22. Close Arduino and open again!
If not, newly installed library will not work
Slides by Abarajithan G
23. Make the Sonar Work
Tell arduino we need
NewPing in this program
Where did we connect the
pins to?
Create a sonar object.
If you have many sonars,
many objects: sonar1, sonar2
sonar.ping_cm()
will give the distance in cm.
We display it using Serial
Slides by Abarajithan G
25. If… else if… else conditions
• Keep sonar connected
• Connect an LED to pin 10
IF ELSE… Make the Arduino decide
Switch on LED, only when object is closer than 10 cm
Switch off LED, when object is further away than 10 cm
Why do we write this separately?
Slides by Abarajithan G
28. KEY POINTS
• Digital : HIGH -> 5V LOW -> 0V
• Analog : Any value between 0V-5V
TYPES of pins in Arduino
Power Pins
Digital Pins
Analog Pins
PWM Pins
Slides by Abarajithan G
29. • When connected to computer, Arduino gets power
(5V) from computer
• Vin : Can give 9-12V here.
• But it is better to power Arduino by DC Jack.
• 5V : Can get 5V from here (for sensors)
• GND: Ground.
Power Pins
Slides by Abarajithan G
30. • Arduino cannot deliver lot of power. If you attempt, it might burn.
• If you have many sensors (like in a robot), DO NOT power them all from
Arduino.
• Better to create separate sensor-power supply using 7805 regulator.
• Do not connect motors (or other big loads to Arduino).
• Connect them to the motor controller and connect the controller to
battery.
Power Tips
Slides by Abarajithan G
31. • Can read/write (input/output) digital
• Arduino UNO : Pins 2 to 13
• Arduino MEGA : Pins 2 to 53
Digital Pins
Slides by Abarajithan G
32. Analog Pins
• Can read (input) analog
• Can write (output) digital
• Can read (input) digital
• Arduino UNO : Pins A0 to A5
• Arduino MEGA : Pins A0 to A15
Slides by Abarajithan G
33. PWM Pins (The wavy ones)
• Can write (output) analog
• Arduino UNO :Pins 3,5,6,9,10,11 (ones with wave)
• Arduino MEGA :Pins 2 to 13, then 44,45,46
Slides by Abarajithan G
34. I want to output 3.5 volts! analogWrite()
Connect LED to pin 9
For loop is used to
continuously increase the
voltage given to an LED.
Slides by Abarajithan G
35. How to read voltage? analogRead()
Connect potentiometer signal
(middle pin) to Analog 2
Continuously adjust the voltage given.
Slides by Abarajithan G
36. Range of Values: analogRead(), analogWrite()
ANALOG READ
(Input any Voltage to Analog Pin)
ANALOG WRITE
(Output Voltage to PWM pin)
0 Volts
Value = 0 Value = 1023
5V 0 Volts
Value = 0 Value = 255
5V
Value = 204.6 x Voltage Value = 51 x Voltage
Slides by Abarajithan G
37. SERVO MOTORS
Used for Robotic arms…etc.
Connection
• Red Wire : 5V
• Brown Wire : Ground
• Yellow Wire : pin 9 (PWM)
Slides by Abarajithan G
38. Using Servo Library
Signal wire of ardunio is
connected to PWM pin 6
30 and 100 are angles in
degrees.
Servo library is pre-installed in ardunio. No
need to install it
Tell arduino we need Servo
Library in this program
Create a servo object
Slides by Abarajithan G
39. Next Step… Let’s Read a line
Connect an IR sensor or QTR sensor
• Vcc : 5V
• GND : Ground
• D : to analog pins
• Emitter : to pin 2
Slides by Abarajithan G
40. Do it yourself…
Quickly install the
QTR library
(Used for IR line sensor)
CLOSE AND OPEN ARDUINO once you install
Slides by Abarajithan G
41. The Code…
Tell arduino we need QTR
Library in this program
This code is for 6 sensors,
4 samples per sensor
Analog Pin numbers
(in order) where you
connected data pins
Calibration code.
Runs for 10 seconds at
beginning.
sensorValues is an array of
analog values. Tells you
what each sensor “sees”
Print it in serial, so you can
“see” what the sensors
“see”
Gives line position
As soon as you
upload,
Move the panel on
a thick black line
on a white surface
to calibrate
For 10 seconds
Line position is
given from
0 to (n-1)x1000
Eg: 6 sensors:
0 to 5000
Slides by Abarajithan G
42. Switch on an LED if line is centered…
Use IF ELSE with QTR
Line position is given
from
0 to k = (n-1)x1000
(n is number of sensors)
Eg: 6 sensors:
0 to k= 5000
If position is
k/2 ,
then exact
center
Eg: 6 sensors:
center: 2500
If position is between
(k/2 -1000) and (k/2 +1000) ,
then almost center
Eg: 6 sensors: almost center:
between 1500, 3500
Understand the logic. Ask us.
Switch on LED if position is
between
(k/2 -1000) and (k/2 +1000) ,
Switch off LED otherwise
Slides by Abarajithan G
43. Control a Motor
Connect one Motor to Motor Controller
• Out1 : Motor terminal (any)
• Out2 : Motor terminal (any)
Connect Motor Controller to Arduino
• In1 : pin 3
• In2 : pin 4
• enA : pin 5 (PWM)
Connect Motor Controller to Power
• Vin : (+)ve 9V battery
• Vout : (-)ve 9V battery
• Also connect Vout to Arduino GND
Slides by Abarajithan G
44. Control a Motor
• We set the motor speed to 200
(out of 255)
• How do you make it turn in reverse?
• How do you stop the motor?
Slides by Abarajithan G
46. WHILE LOOP
• This is NOT a proper code.
• While code will keep running
again and again until ‘check
something’ becomes false
Slides by Abarajithan G
47. Understand the logic. Ask us.
Line position is 0 to k =
(n-1)x1000
k/2 is exact center
(k/2-1000, k/2+1000) is
almost center
If position is less than
(k/2 -1000)
we are on right side
of almost center
Keep turning motor in
one direction
Eg: 6 sensors:
Right: Less than1500
If position is more than
(k/2 +1000)
we are on left side of
almost center
Keep turning motor in
other direction
Eg: 6 sensors:
Right: Less than3500
If not (else), we are in
almost center
Stop motor
k/20 k(k/2+1000)(k/2-1000)
Turn one way Turn other waySTOP
Slides by Abarajithan G
48. Error in Line Centering
• Position is given from 0 to 5000
(0 to (n-1)*1000)
• Center of the line is 2500
[(n-1)*1000]/2
• Then error = (position – 2500)
• Error positive -> We are in left side
• Error negative -> We are in right side
Slides by Abarajithan G
49. Insert this before Setup()
Insert this inside Setup()
Insert this instead of if-else
Modify the QTR-
If-Else code
Slides by Abarajithan G
50. Write as two functions
outside the setup() and loop()
“call” functions inside loop()
FUNCTIONS
Advantage: Easy to read, easy to debug, easy to change
Change the code like this
Slides by Abarajithan G
51. The secret of good Line Following
PID ALGORITHM
Slides by Abarajithan G
52. PID
• Then error = (position – 2500)
• Error positive -> We are in left side
• Error more positive -> Turn right faster
• Error negative -> We are in right side
• Error more negative -> Turn right faster
• Add and subtract error from motor speeds
Slides by Abarajithan G
56. CONTACT
Aba
Email : abarajithan07@gmail.com
Phone : 0718626705
Facebook : fb.com/abarajithan11
Skype : abarajithan11
• Start building a robot for a competition.
• Ask us for help if needed.
Slides by Abarajithan G