SlideShare a Scribd company logo
1 of 56
Download to read offline
Workshop by the undergraduates of
Dept. of Electronics and Telecommunications Engineering
University of Moratuwa
Slides by Abarajithan G
How to control the hardware?
It is all about logic!
Slides by Abarajithan G
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
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
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
Arduino
Two main types of Arduino boards are available
Slides by Abarajithan G
Installing Arduino Software
www.arduino.cc
Slides by Abarajithan G
Slides by Abarajithan G
Slides by Abarajithan G
Select Board Select Port
Slides by Abarajithan G
Breadboard
Slides by Abarajithan G
pinMode()… digitalWrite()… Let there be light!
Slides by Abarajithan G
setup(), loop(), delay()… Make it Blink!
Setup and Loop
Slides by Abarajithan G
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
• 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
Arduino, we need to talk: Serial
BAUD RATE SHOULD BE EQUAL
Slides by Abarajithan G
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
LIBRARIES
They make life easy!
Slides by Abarajithan G
Installing a Library from Internet
• Step 1 :
Google Search
“newping library”
• Step 2:
Choose GitHub
Slides by Abarajithan G
Installing
a Library…
Slides by Abarajithan G
Installing
a Library…
C:Program Files (x86)ArduinolibrariesCopy folder to :
Extract the ZIP file
Slides by Abarajithan G
Close Arduino and open again!
If not, newly installed library will not work
Slides by Abarajithan G
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
Serial Plotter
NOTE:
This works ONLY if your Serial.println() (not
print) only the needed values
Slides by Abarajithan G
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
digitalRead(): Pushbutton
Connect to pin 2
Use a pull down resistor
Slides by Abarajithan G
digitalRead() gives 1 or 0
Robot: use for calibration
Slides by Abarajithan G
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
• 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
• 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
• Can read/write (input/output) digital
• Arduino UNO : Pins 2 to 13
• Arduino MEGA : Pins 2 to 53
Digital Pins
Slides by Abarajithan G
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
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
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
How to read voltage? analogRead()
Connect potentiometer signal
(middle pin) to Analog 2
Continuously adjust the voltage given.
Slides by Abarajithan G
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
SERVO MOTORS
Used for Robotic arms…etc.
Connection
• Red Wire : 5V
• Brown Wire : Ground
• Yellow Wire : pin 9 (PWM)
Slides by Abarajithan G
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
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
Do it yourself…
Quickly install the
QTR library
(Used for IR line sensor)
CLOSE AND OPEN ARDUINO once you install
Slides by Abarajithan G
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
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
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
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
Change Motor direction,
based on which side of the line we are in
Welcome to WHILE loop
Slides by Abarajithan G
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
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
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
Insert this before Setup()
Insert this inside Setup()
Insert this instead of if-else
Modify the QTR-
If-Else code
Slides by Abarajithan G
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
The secret of good Line Following
PID ALGORITHM
Slides by Abarajithan G
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
GETTING HARDWARE
Slides by Abarajithan G
Buying Components Online
www.ebay.comlankatronics.com
Slides by Abarajithan G
Buying Components Cheaply
Unitech Trading,
2nd Cross Street,
In front of Fort Railway Station
Slides by Abarajithan G
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

More Related Content

What's hot

Physical prototyping lab1-input_output (2)
Physical prototyping lab1-input_output (2)Physical prototyping lab1-input_output (2)
Physical prototyping lab1-input_output (2)Tony Olsson.
 
Physical prototyping lab6-motors
Physical prototyping lab6-motorsPhysical prototyping lab6-motors
Physical prototyping lab6-motorsTony Olsson.
 
Physical prototyping lab3-serious_serial
Physical prototyping lab3-serious_serialPhysical prototyping lab3-serious_serial
Physical prototyping lab3-serious_serialTony Olsson.
 
Arduino slides
Arduino slidesArduino slides
Arduino slidessdcharle
 
Digital Alarm Clock (IC-TMS-8560)
Digital Alarm Clock (IC-TMS-8560)Digital Alarm Clock (IC-TMS-8560)
Digital Alarm Clock (IC-TMS-8560)Chintan Patel
 
Arduino Workshop
Arduino WorkshopArduino Workshop
Arduino Workshopatuline
 
Arduino workshop
Arduino workshopArduino workshop
Arduino workshopJonah Marrs
 
Etapes fab-venti-v2
Etapes fab-venti-v2Etapes fab-venti-v2
Etapes fab-venti-v2Jonah Marrs
 
Digital Alarm Clock 446 project report
Digital Alarm Clock 446 project reportDigital Alarm Clock 446 project report
Digital Alarm Clock 446 project reportAkash Mhankale
 
Digital clock workshop
Digital clock workshopDigital clock workshop
Digital clock workshopKedarv
 
Analog to digital converter
Analog to digital converterAnalog to digital converter
Analog to digital converterCorrado Santoro
 
Digital Clock Using Logic Gates
Digital Clock Using Logic GatesDigital Clock Using Logic Gates
Digital Clock Using Logic GatesJalpaMaheshwari1
 

What's hot (19)

Physical prototyping lab1-input_output (2)
Physical prototyping lab1-input_output (2)Physical prototyping lab1-input_output (2)
Physical prototyping lab1-input_output (2)
 
4. exp.2 rotary encoder
4. exp.2 rotary encoder4. exp.2 rotary encoder
4. exp.2 rotary encoder
 
Physical prototyping lab6-motors
Physical prototyping lab6-motorsPhysical prototyping lab6-motors
Physical prototyping lab6-motors
 
Introduction to Arduino
Introduction to ArduinoIntroduction to Arduino
Introduction to Arduino
 
Physical prototyping lab3-serious_serial
Physical prototyping lab3-serious_serialPhysical prototyping lab3-serious_serial
Physical prototyping lab3-serious_serial
 
2th year iv sem de lab manual
2th year iv sem de lab manual2th year iv sem de lab manual
2th year iv sem de lab manual
 
Arduino slides
Arduino slidesArduino slides
Arduino slides
 
Digital Alarm Clock (IC-TMS-8560)
Digital Alarm Clock (IC-TMS-8560)Digital Alarm Clock (IC-TMS-8560)
Digital Alarm Clock (IC-TMS-8560)
 
Arduino Workshop
Arduino WorkshopArduino Workshop
Arduino Workshop
 
Arduino workshop
Arduino workshopArduino workshop
Arduino workshop
 
Etapes fab-venti-v2
Etapes fab-venti-v2Etapes fab-venti-v2
Etapes fab-venti-v2
 
Digital Alarm Clock 446 project report
Digital Alarm Clock 446 project reportDigital Alarm Clock 446 project report
Digital Alarm Clock 446 project report
 
Arduino course
Arduino courseArduino course
Arduino course
 
Arduino Workshop 2011.05.31
Arduino Workshop 2011.05.31Arduino Workshop 2011.05.31
Arduino Workshop 2011.05.31
 
Digital clock workshop
Digital clock workshopDigital clock workshop
Digital clock workshop
 
Digital Clock
Digital ClockDigital Clock
Digital Clock
 
Analog to digital converter
Analog to digital converterAnalog to digital converter
Analog to digital converter
 
Experimentdsd[1]
Experimentdsd[1]Experimentdsd[1]
Experimentdsd[1]
 
Digital Clock Using Logic Gates
Digital Clock Using Logic GatesDigital Clock Using Logic Gates
Digital Clock Using Logic Gates
 

Similar to Arduino Programming for Basic Robotics - University of Moratuwa

Similar to Arduino Programming for Basic Robotics - University of Moratuwa (20)

Arduino Workshop (3).pptx
Arduino Workshop (3).pptxArduino Workshop (3).pptx
Arduino Workshop (3).pptx
 
Arduino programming
Arduino programmingArduino programming
Arduino programming
 
Intro to Arduino
Intro to ArduinoIntro to Arduino
Intro to Arduino
 
Intro_to_Arduino_-_v30.pptx
Intro_to_Arduino_-_v30.pptxIntro_to_Arduino_-_v30.pptx
Intro_to_Arduino_-_v30.pptx
 
Physical prototyping lab2-analog_digital
Physical prototyping lab2-analog_digitalPhysical prototyping lab2-analog_digital
Physical prototyping lab2-analog_digital
 
ArduinoSectionI-slides.ppt
ArduinoSectionI-slides.pptArduinoSectionI-slides.ppt
ArduinoSectionI-slides.ppt
 
Fun with arduino
Fun with arduinoFun with arduino
Fun with arduino
 
Arduino Slides With Neopixels
Arduino Slides With NeopixelsArduino Slides With Neopixels
Arduino Slides With Neopixels
 
Embedded system application
Embedded system applicationEmbedded system application
Embedded system application
 
Build cool stuff with arduino for sci camp 16 dec13
Build cool stuff with arduino for sci camp 16 dec13Build cool stuff with arduino for sci camp 16 dec13
Build cool stuff with arduino for sci camp 16 dec13
 
Electronics_Tutorial_2022_Slide543404210s.pdf
Electronics_Tutorial_2022_Slide543404210s.pdfElectronics_Tutorial_2022_Slide543404210s.pdf
Electronics_Tutorial_2022_Slide543404210s.pdf
 
Electronz_Chapter_2.pptx
Electronz_Chapter_2.pptxElectronz_Chapter_2.pptx
Electronz_Chapter_2.pptx
 
Lab2ppt
Lab2pptLab2ppt
Lab2ppt
 
Fire Fighting Robot
Fire Fighting RobotFire Fighting Robot
Fire Fighting Robot
 
Arduino Programming Basic
Arduino Programming BasicArduino Programming Basic
Arduino Programming Basic
 
Robotics Session day 1
Robotics Session day 1Robotics Session day 1
Robotics Session day 1
 
Arduino Workshop Slides
Arduino Workshop SlidesArduino Workshop Slides
Arduino Workshop Slides
 
Arduino.pptx
Arduino.pptxArduino.pptx
Arduino.pptx
 
Arduino windows remote control
Arduino windows remote controlArduino windows remote control
Arduino windows remote control
 
Introduction to arduino
Introduction to arduinoIntroduction to arduino
Introduction to arduino
 

Recently uploaded

Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 

Recently uploaded (20)

Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 

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
  • 6. Arduino Two main types of Arduino boards are available Slides by Abarajithan G
  • 10. Select Board Select Port Slides by Abarajithan G
  • 12. pinMode()… digitalWrite()… Let there be light! Slides by Abarajithan G
  • 13. setup(), loop(), delay()… Make it Blink! Setup and Loop 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
  • 18. LIBRARIES They make life easy! Slides by Abarajithan G
  • 19. Installing a Library from Internet • Step 1 : Google Search “newping library” • Step 2: Choose GitHub Slides by Abarajithan G
  • 21. Installing a Library… C:Program Files (x86)ArduinolibrariesCopy folder to : Extract the ZIP file 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
  • 24. Serial Plotter NOTE: This works ONLY if your Serial.println() (not print) only the needed values 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
  • 26. digitalRead(): Pushbutton Connect to pin 2 Use a pull down resistor Slides by Abarajithan G
  • 27. digitalRead() gives 1 or 0 Robot: use for calibration 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
  • 45. Change Motor direction, based on which side of the line we are in Welcome to WHILE loop 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
  • 53. GETTING HARDWARE Slides by Abarajithan G
  • 55. Buying Components Cheaply Unitech Trading, 2nd Cross Street, In front of Fort Railway Station 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