SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to...

81
PORPOISE ROBOTICS Precision Oceanographic Robotics Program On and In the Sea Environment SEMESTER 1 Introduction to Mechatronics STUDENT CLASSBOOK Developed with the cooperation of Washington Prep. Science & Math Magnet School. Los Angeles, CA. CONTACT FOR INFORMATION David Grober, President (310) 951-1110 [email protected] Kevin Bowen, Vice President for Technology and Education (858) 997-4792 [email protected] Jennifer Fox, Education Corordinator, (206) 714-5116 [email protected] Sponsored by: ©PORPOISE ROBOTICS. 2014. All content and images herein are owned, licensed by PORPOISE Robotics, Inc., or come from an open source. Unauthorized use of this curriculum, or any materials herein which are not open source, is prohibited. If you wish to use this material, please ask. This text and all PORPOISE materials are provided on an “as is” basis. There are no warrantees, of any kind, given or implied. Any user of these materials agrees to unconditionally hold the PORPOISE Robotics program and all of its staff and volunteers free and clear of any and all liabilities of any kind, whatsoever, and to be responsible for any costs, including legal fees, incurred by PORPOISE Robotics or it staff or volunteers as a result of any use. September 5, 2014. Ver. 19G

Transcript of SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to...

Page 1: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

PORPOISE ROBOTICS

Precision Oceanographic Robotics Program On and In the Sea Environment

SEMESTER 1 Introduction to Mechatronics STUDENT CLASSBOOK

Developed with the cooperation of Washington Prep. Science & Math Magnet School. Los Angeles, CA.

CONTACT FOR INFORMATION

David Grober, President (310) 951-1110 [email protected] Kevin Bowen, Vice President for Technology and Education (858) 997-4792 [email protected] Jennifer Fox, Education Corordinator, (206) 714-5116 [email protected]

Sponsored by:

©PORPOISE ROBOTICS. 2014. All  content  and  images    herein  are  owned,  licensed  by  PORPOISE  Robotics,  Inc.,  or  come  from  an  open  source.  Unauthorized  use  of  this  curriculum,  or  any  materials  herein  which  are  not  open  source,  is  prohibited.    If  you  wish  to  use  this  material,  please  ask.    This  text  and  all  PORPOISE  materials  are  provided  on  an  “as  is”  basis.  There  are  no  warrantees,  of  any  kind,  given  or  implied.    Any  user  of  these  materials  agrees  to  unconditionally  hold  the  PORPOISE  Robotics  program  and  all  of  its  staff  and  volunteers  free  and  clear  of  any  and  all  liabilities  of  any  kind,  whatsoever,  and  to  be  responsible  for  any  costs,  including  legal  fees,  incurred  by  PORPOISE  Robotics  or  it  staff  or  volunteers  as  a  result  of  any  use.    

September 5, 2014. Ver. 19G

Page 2: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

2

Table  of  Contents   PREFACE ..........................................................................................................................................4  PORPOISE ROBOTICS, STEM and STEAM: .............................................................................4  Unit I – Introduction To Mechatronics ...........................................................................................6  Unit II - Basic Circuitry ...................................................................................................................9  

Conductors and Insulators .............................................................................................................. 9  Ohm’s Law ................................................................................................................................... 10  Types of Circuits .......................................................................................................................... 11  Open, Closed and Short Circuits .................................................................................................. 11  Series Circuits .............................................................................................................................. 12  Parallel Circuits ............................................................................................................................ 12  Circuit Schematics ....................................................................................................................... 13  Practice  problems: ....................................................................................................................... 13  BreadBoards ................................................................................................................................. 14  From Schematic to Breadboard ................................................................................................... 16  Multimeter .................................................................................................................................... 17  Measuring  Voltage ....................................................................................................................... 17  Measuring Resistance .................................................................................................................. 18  Measuring Current ....................................................................................................................... 18  

Unit III – The Arduino Programming Environment & Language ............................................21  Introduction to the Arduino Microprocessor ............................................................................... 21  Downloading Protocal ................................................................................................................. 22  Understanding the Arduino .......................................................................................................... 22  Blink Sketch ................................................................................................................................. 27  Noise Sketch ................................................................................................................................ 31  Turning a Buzzer On and Off ...................................................................................................... 33  Pulse Width Modulation .............................................................................................................. 34  

Unit IV – The Land Shark Assembly ............................................................................................37  Assembled Landshark .................................................................................................................. 44  Wiring the Land Shark ................................................................................................................. 45  

Unit V – Motors, Buttons & Potentiometers ................................................................................49  Motors ..............................................................................................................................................  Motor Control Test Sketch ............................................................................................................... Land Shark Turn Signals and Range Check .................................................................................... Plotting a Course .............................................................................. ………………………………    Measuring the Speed of the Land Shark ........................................................................ …………..    Buttons ................................................................................................ …………………………….    Potentiometer ....................................................................................................... …………………    Servo Motors ............................................................................................................................ ……    

Unit VI – Sensors ........................................................................................................................……    Ultrasonic Range Finder ............................................................................................................. ….    Scanning with the Ping (Ultrasonic Ranger) on a Servo ............................................................ ….    Photoresistor ............................................................................................................................... ….    Thermistor ................................................................................................................................... ….    IR Sensor (Infrared) ................................................................................................................... ….    One Wire Digital Temperature Sensor - DS18B20 .................................................................... ….    

Page 3: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

3

Unit VII – Student Projects ...........................................................................................................….    Here are Arduino Language References ................................................................................. …….  Here are some tutorials that may give you ideas for additional projects. ...................................... ..    The Old Lady in her Rocker – Halloween Decoration. ........................................................... ……    

SEMESTER 2. Down to the Sea in Ships. .......................................................................................  

Unit VIII: Marine Environments. ...................................................................................................  Unit IX: Naval Architecture and the Engineering Process .........................................................  Unit X : Building the Sea Hawk surface boat. .............................................................................  Unit XI : Building the Porpoise submersible. ...............................................................................  Unit XII: Test and Evaluation. Critical thinking. .........................................................................  Unit XIII: Students are the Next Generation of Pioneers. .............................................................  

PORPOISE Robotics was developed with cooperation of Washington Prep. Science & Math Magnet School (WPMS) and the Los Angeles Unified School District (LAUSD). WPMS PRINCIPAL: DR. TODD ULLAH. Magnet Coordinator: MR. JOHN BRANDON PORPOISE COORDINATORS: DAVID GROBER & KEVIN BOWEN WPMS INSTRUCTOR: MICHAEL LUSK STUDENT CLASSBOOK: KEVIN BOWEN, DAVID GROBER, MICHAEL LUSK, TIM LAREN, BRANT LaVALLA, DR. THOMAS CULHANE MENTORS: LOS ANGELES ROBOTICS CLUB, LOUIS PARKER GRANTS AND EDUCATION: JENNIFER FOX, ALICE TAYLOR, VANESSA BUTLER

Page 4: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

4

PREFACE Welcome  to  PORPOISE  ROBOTICS,  (Precision  Oceanographic  Robotics  Program  on  and  in  the  Sea  Environment),  with  initial  sponsorship  by  the  Office  of  Naval  Research  in  cooperation  with  Motion  Picture  Marine,  a  nautical  film  production  and  technology  company.      PORPOISE  ROBOTICS  is  a  unique  program  that  combines  Science,  Technology,  Engineering  and  Math  (STEM)  into  a  multi-­‐semester  accredited  program  that  promotes  learning  through  hands-­‐on  aquatic  based  robotics.      PORPOISE  ROBOTICS  is  geared  to  enlighten,  empower  and  excite  students  by  having  fun  with  robots  while  learning  the  underlying  sciences.    We  invite  you  down  the  yellow  brick  road  to  an  Oz,  where  you  are  all  wizards,  using  microcontrollers,  sensors  and  actuators  to  help  solve  the  great  challenges  that  face  humanity  in  the  21st  century!    Many  of  you  have  participated  in  other  robotic  programs  such  as  FIRST,  LEGO,  VEX  or  Sea  Perch.  PORPOISE  ROBOTICS  will  build  upon  those  experiences.    If  you  have  no  previous  robotic  experience,  the  path  is  fun  and  easy.    You  will  begin  to  understand  about  the  robots  that  share  the  world  around  you.    You  will  begin  to  understand  how  they  work,  and  to  program  them  to  your  specifications.    Along  our  pathway,  we  will  emphasize  Aquatic  and  Maritime  robotics.    For  those  of  you  interested,  we  will  guide  you  on  our  yellow  brick  road  to  engineering  colleges  and  careers  in  nautical  engineering  and  the  marine  sciences.    There  are  fabulous  careers  available,  working  for  small  boutique  companies  such  as  “M  Ship”  who  are  ship  architects,  to  the  Jet  Propulsion  Labs  (JPL)  who  placed  the  rovers  on  Mars,  looking  for  water,  to  giants  like  Raytheon,  Boeing  and  Siemens,  whom  delve  into  every  arena  of  robotic  science  and  engineering.        Other  career  opportunities  abound  in  government  such  as  with  Navy’s  prestigious  Office  of  Naval  Research  and  the  Navy  Research  Labs.    There  is  also  DARPA  (Defense  Advanced  Research  Projects  Agency)  which  we  affectionately  call  the  “pie  in  the  sky”  guys.    Take  a  few  minutes  to  explore  their  websites.    If  you  can  picture  yourself  alongside  their  scientists  and  explorers,  just  remember  that  each  of  them  was,  at  one  time,  just  like  you,  a  student  with  a  vision  and  a  dream.                  

PORPOISE ROBOTICS, STEM and STEAM:

With  the  ever  expanding  world  of  technology  knocking  at  the  door,  PORPOISE  ROBOTICS  offers  an  unparalleled  opportunity  for  students  to  develop  21st  century  skills  in  STEM  fields.  (Science  Technology,  Engineering  and  Math.)    Because  PORPOISE  is  also  heavily  oriented  to  the  creative  arts,  such  as  the  artistic  and  mechanical  design  of  robots,  the  term  “ART”  is  often  included,  resulting  in  a  “STEAM”  program.    (Science,  Technology,  Engineering,  Arts,  and  Math.)      PORPOISE  ROBOTICS  is  your  opportunity,  as  students,  to  rub  shoulders  and  explore  mind  bending  technology  and  concepts  with  roboticists  from  all  over  the  world.    Ultimately,  if  you  fall  in  love  with  robotics,  as  we  have,  you  could  find  PORPOISE  ROBOTICS  is  your  ticket  into  an  engineering  program  at  the  college  of  your  choice,  hopefully  with  a  scholarship.      PORPOISE  ROBOTICS  is  an  OPEN-­‐SOURCE  curriculum,  so  don't  feel  you  have  to  be  enrolled  in  our  class  in  order  to  participate!    You  can  use  this  student  workbook  and  jump  right  in.    As  the  

Page 5: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

5

PORPOISE  program  expands  and  our  website  is  developed,  you  will  be  able  to  communicate  with  other  PORPOISE  participants,  whom  we  call  PORPOISEINIANS.    It's  a  wonderful  club  to  belong  to,  with  lots  of  exciting  friends  for  you  to  meet,  across  the  country  and  around  the  world.          All  of  the  hardware  components  that  Porpoiseinians  use  can  be  inexpensively  purchased  locally,  or  be  scavenged  from  you  homes,  garages  and  recycle  bins.    PORPOISE  ROBOTICS  will  guide  you  through  the  amazing  and  fun  aspects  of  designing  and  building  nautical  robots,  writing  creative  C++  coding,  and  controlling  robots  from  your  computer,  iPhones  or  Androids.          As  you  develop  your  areas  of  interest,  you  may  meet  up  with  your  peers  at  other  high  schools,  as  well  as  college  students  and  professors  with  similar  interests.    PORPOISE  ROBOTICS  has  contacts  or  pathways  to  University  programs  at  schools  including  MIT,  University  of  Rhode  Island,  UCLA,  University  of  Colorado,  Northwestern,  Purdue  and  others.    You  can  web  connect  with  these  folks,  learn  what  it  takes  to  successfully  apply  for  their  college  engineering  programs,  and  earn  scholarships.    If  you  love  STEM,    enjoy  the  thrill  of  creating  your  own  robots,  and  let  your  mind  and  hands  create  cool  robots,  you  could  have  a  fascinating  and  rewarding  career  in  robotic  engineering.    In  the  next  decade  our  country  will  need  over  400,000  new  engineers,  and  you  should  one  of  them.    So  jump  right  in  and  let’s  begin  our  journey.    

   

Check  out  these  sites  to  see  where  PORPOISE  Robotics  can  lead  you:    

Office  of  Naval  Reasearch    (ONR)    http://www.onr.navy.mil/  DARPA:      http://www.darpa.mil/  Navy  Research  Labs:    http://www.nrl.navy.mil/  M  Ship:    http://www.mshipco.com/index.html  Raytheon:    http://www.raytheon.com/  Jet  Propulsion  Laboratory:    http://www.jpl.nasa.gov/  

Page 6: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

6

Unit I – Introduction To Mechatronics Much  of  the  design,  development,  and  creation  of  robots  is  part  of  a  field  of  study  called  “mechatronics”.    Mechantronics  encompasses  mechanical  and  electrical  systems  that  are  controlled  by  computers.    In  this  course  we  will  study  robotics  design  principles  applied  to  unmanned  vehicle  systems.  During  semester  1  we  will  assemble  and  program  a  small  unmanned  ground  vehicle.  In  semester  2  multidisciplinary  teams  of  four  students  will  apply  the  system  engineering  process  to  a  ground  surface  or  undersea  vehicle.    

!Engineering!

!System!

Engineering!!

Mechanical!Engineering!

!

Electrical!Engineering!

!

Software!Engineering!

!

Other!Engineering!

!

Mechatronics!!

Mechanical!Systems!

!

Electrical!Systems!

!

Software!Systems!

!

Other!Systems!

!

Robotics!!

Automobiles! Appliances! Printers!!

Others!!

Factory!Automation!Systems!

!

Pick!and!Place!Robotics!

!

Unmanned!Vehicle!Systems!

Systems!!

Humanoids!(Robots)!

!

Others!!

Unmanned!Surface!!Vehicle!Systems!

Unmanned!Ground!!!Vehicle!!Systems!

Other!Unmanned!Vehicle!Systems!

!

Unmanned!!Air!!!!!!!!!

Vehicle!Systems!

!

Unmanned!!Undersea!Vehicle!Systems!

Page 7: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

7

 The   principles   of   math   and   science   enable   vitully   all   technology.   PORPOISE   Robotics   gives  teachers  a  venue  to  map  these  priciples  to  technologies  and  demonstrate  them  via  engineering  projects.   Typical   technologies   we   are   concerned   with   are   mechanical,   electrical,   electronic  components   and   systems.   Since   our   path   to  mechatronics   is   through   robotics   and   unmanned  vehicle  systems,  motors  and  actuators  play  a  critical  part.      We  also  need  geometry  to  design  our  systems,  trigonometry  to  navigate  them,  Newton’s  Law  to  understand   forces,   velocities,   distance.   Ohm’s   Law   to   determine   circuit   design,   energy   usage,  endurance.  We  follow  a  systems  engineering  approach  to  coordinate  mechanical,  electrical  and  software  engineering  to  create  unmanned  vehicles  systems.  Through  this,  students  will  learn  the  practical  application  of  math,  science  and  engineering  to  prepare  for  vocational  occupations  and  advanced  studies.      Students   will   be   organized   into   mutidisciplianary   teams   learning   to   collaborate,   reason   and  collectively  develop  interesting  applications.  What  they  learn  in  this  course  can  be  applied  across  the  entire  engineering  spectrum.  Students  entering  other  fields  that  require  technical  knowledge,  such   as   law   and   technical   writing,   will   greatly   benefit   from   this   process.   Students   entering  robotics   competitions   will   understand   why   and   how   to   apply   their   knowledge   to   solve   new  challenges.      

                   

Math%Algebra(Geometry(

Trigonometry(Calculus(

Science%Newton’s(Laws(Ohm’s(Law(Energy(

Thermodynamics(

%Technology%

Mechanical(Devices(Electrical(Components(Electronic(Components(

Motors/Actuators(Mechatronics(

((

Mechanical,%%Electrical,%

and%So4ware%Engineering%

Applied(to(Unmanned(

Vehicle(Systems(

Enable(To(create(

Page 8: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

8

 In  semester  1  we  will  study  Ohm’s  Law  and  apply  this  to  series  and  parallel  circuits.  We  will  build  them  on  breadboards  powered  by  9V  DC  batteries.      Next   we   will   download   the   Arduino   Interactive   Developmet   Environment   onto   our   personal  computers.  We  will  connect  the  PC  to  an  Arduino  Mega  2560  computer  and  run  a  program.  The  Arduino  will  be  powered  via  the  USB  port  from  our  pC.    We  will   then   connect   the  Arduino   computer   to   our   Breadboard   and   assemble   circuits,   control  them  with  C++  programs  developed  using  the  IDE  on  our  personal  computer  and  downloaded  to  the  Arduino  computer.    In  this  course,  we  will  install  the  Arduino  computer  and  breadboard  onto  our  Land  Shark  ground  vehicle.  We  will  then  download  movement,  turn  signal  and  range  finder  programs  onto  the  Land  Shark  Arduino  computer.  The  motor  system  will  be  powered  by  6VDC  batteries.  The  Arduino  and  sensors  will  be  powered  by  a  9VDC  battery.    

     Though  you  may  want  to  jump  right  in  and  begin  using  your  Arduino,   it   is   important  that  a  few  concepts  be  developed  for  your  safety  and  your  comprehension.    You  need  to  understand  how  these   sensors,   actuators,  wires,   and   the   Arduino  microprocessor   connect   together   to   create   a  robotic  system.  

Personal)Computer))

Interac(ve*Development*Environment*

(IDE)*

Arduino)Computer)Mega*2680*70**I/O*pins*

Breadboard)Sensors*&**Effectors*

Land)Shark)Unmanned)Ground)Vehicle)Arduino*computer,*breadboard,*baFeries,*motors,*turn*signals,*range*finder*

Page 9: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

9

Unit II - Basic Circuitry The   foundation   of   electronic   circuits   lies   in   the   concept   of   electricity   and   its   transportation   of  particles.     If   you  were   to   observe   a   rainy   night   and   see   lightning   fill   the   night   sky,   you  would  immidiately   understand   the   power   and   phenomenon   known   as   electricity.     The   electrical  phenomenon  has  fascinated  humans  for  thousands  of  years.    But,  if  you  ever  witnessed  lightning,  you  also  understand  that  such  a  phenomenon  holds  dangers.    Therefore,  one  should  always  be  attentive  and  cautious  when  dealing  with  electricity.    Porpoisinians  need  to  maintain  their  safety,  and  the  safety  of  others.    Through   observations   and   personal   experiences,   all   of   you   have   in   one   way   or   another   used  electricity.    But  how  many  of  you  considered  what  electricity  actually   is,  how  does   it  work  and  where  does  it  come  from?    Benjamin  Franklin  asked  these  questions,  and  as  many  of  you  know,  flew   a   kite   into   a   thunderstorm   to   learn   some   of   the   answers.     Fortunately   for   our   country,  Benjamin  Franklin  was  not  electrocuted.    You  have   to  be  more  careful   than  Benjamin  Franklin.  However,  his  discoveries  and  those  of  other  scientists  concluded  that  electricity  is  the  movement  of  charged  particles  called  electrons  and  protons.    Electrons  and  protons  are  infitessimally  small,  yet  they  have  an  charge.    Electrons  have  a  negative  charge,  protons  have  a  positive  charge.    In  working  circuits  there  is  always  a  flow  of  charged  particles;  by  convention  we  consider  the  flow  of   electrons   through   the   circuit.     Like   water   flowing   through   a   river,   these   electrons   “flow”  through   the   circuit   and   get   a   similar   name   to   the   flow   of  water   through   a   river.     The   flow   of  electrons  is  known  as  electrical  current.  

Conductors and Insulators

Within  the  study  of  circuits  there  are  various  materials  that  are  used  throughout  the  construction  of  circuits.    These  materials  are  very  important  to  why  they  are  selected  and  how  they  are  used.    The  most  significant  concept  is  the  difference  between  conductors  and  insulators.    A  conductor  is  something   that   easily   allows   electricity   to   flow.     For   example,   copper   is   a   great   conductor   of  electricity,  therefore  if  you  were  to   look  at  the  inside  of  many  of  the  wires  you  will  notice  they  are  copper.    Other  materials  do  not  conduct  electricity  very  well  and  they  are  called  insulators.    If  you  were  to   look  at  the  same  wire  you  would  notice   it  has  a  colored  casing  that   is  constructed  out   of   rubber.     This   prevents   the   flow   of   electrons   from   from   jumping   out   of   the   circuit   and  causing  a  multitude  of  dangerous  scenarios.    

Page 10: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

10

The   concept   of   electrical   conductors   and   insulators   are   analogous   to   thermal   conductors   and  insulators.    For  example,   if  you  were  to  touch  a  metal  spoon  that  was   in  hot  water,  you  would  feel   the  heat,   if   your  were   to  put  a  wood  spoon   in   the  same  water  you  would  not.    Metal   is  a  thermal  conductor,  while  wood  is  a  thermal  insulator.    Tutorial 03 for Arduino: Electrical Engineering Basics (16min 12 sec) http://www.youtube.com/watch?v=abWCy_aOSwY    Practice  problems:    1. Compare  and  contrast  electrical  insulators  and  conductors  2. What  are  some  common  electrical  insulators?  Conductors?  

Ohm’s Law

Georg  Ohm  was  a  physicist  who  studied  the  properties  of  electrical  current.    In  his  studies  he  determined  a  fascinating  relationship  between  the  potential  difference,  electrical  current,  and  the  resistence  to  that  current.    His  findings  can  be  simplified  in  to  a  mathematical  equation:  

Voltage = Current X Resistance

This   relationship   governs   all   circuits   and   can   be   used   to   determine   important   information  regarding   your   circuits   used   during   the   robotics   construction   phase.     Voltage   is   a   potential  difference   within   a   circuit   and   is   usally   created   by   the   presence   of   a   power   source   such   as   a  battery.     The   current   is   as   we   already   mentioned   the   flow   of   electrons,   or   electrical   charge  through  the  circuit.    The  resistance  is,  as  it  sounds,  the  restrictive  nature  of  the  circuit  on  the  flow  of  electricity.  

Page 11: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

11

We  can  think  of  water  flowing  through  a  pipe  as  an  analogy  to  our  circuit  environment.    Pretend  for  a  minute  that  a  battery  works  as  a  pressure  difference  in  a  water  pipe.    The  flow  of  the  water  (electrons)  would  be  related  to  the  pressure  difference.     If  you  increase  the  pressure  difference  (voltage)   than   the   current  would   increase.     Decrease   this   pressure   difference   and   the   current  would  decrease.    The  resistance  works  similarly  with  the  water  analogy;  if  you  were  to  add  bends  and  curves  in  the  pipe  you  would  restrict  the  flow  of  water  through  the  pipes,  lowering  the  current.    Remove  these  bends  and  you  would  increase  the  current.    Therefore,  as  you  increase  the  resistance  of  a  circuit  you  decrease  the  current,  lower  the  resistance  and  you  increase  the  current.    As   you   use   Ohm’s   law   this   relationship   will   become   better   known   to   you.     It   is   important   to  remember   that   each   variable   has   a   specific   unit   of   measurment   similar   to   how   you   measure  distance  in  meters  or  feet.    The  table  below  shows  the  unit  for  each  variable  that   is  used  while  using  Ohm’s  Law.  

Variable Unit Voltage (V) Volts (V) Current (I) Amperes (A)

Resistance (R) Ohms (Ω)

Types of Circuits When  constructing  a  circuit  there  are  two  patterns  in  which  the  circuit  elements  can  be  constructed  in.    The  two  structures  that  can  be  constructed  are  a  Series  Circuit  and  a  Parallel  Circuit.    Each  circuit  structure  has  different  properties  associated  with  their  construction,  and  therefore  should  be  used  when  desired  characteristics  of  each  type  are  selected.    There  is  also  a  distinction  between  an  open  circuit,  closed  circuit  and  short  circuit  which  will  effect  the  circuits  performance.  

Open, Closed and Short Circuits

An  open  circuit  is  one  in  which  there  is  a  break  either  accidental  or  purposeful  within  the  circuit.    In  such  a  case,  the  circuit  will  not  pass  current.  A  closed  circuit  is  one  in  which  all  elements  are  connected,  and  electrical  current  can  flow  undisrupted.  

Page 12: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

12

A  Short  circuit  is  an  unintentional  portion  of  a  circuit  that  has  low  resistance.    This  will  cause  all  current  to  flow  out  of  this  section,  bypassing  other  portions  of  a  circuit.    Short  circuits  are  dangerous  at  times  and  can  lead  to  smoke,  fires,  damaged  components  and  other  hazards.  

Series Circuits

                   Characteristics  of  a  series  circuit  include  the  construction  of  elements  of  a  circuit  in  one  loop.    Based  on  this  style  of  circuit,  the  current  throughout  the  circuit  is  constant,  but  the  voltage  across  each  element  differs  based  on  their  resistance  values.    It  should  also  be  pointed  out  that  each  element  in  the  circuit  must  be  functional  for  the  circuit  to  work.  Rtotal = R1 + R2 + R3 ... + Rn

Parallel Circuits

A  parallel  circuit  is  defined  by  more  than  one  loop,  and  is  diagramed  above  with  2  loops.    In  this  style  circuit   it   is  possible   for  one   loop   to  work  while   the  other  one  does  not.    As  well,   it   is  has  properties  in  which  the  current  of  each  loop  can  differ,  while  the  voltage  across  each  loop  is  the  same.  Rtotal  =  1/(1/R1  +  1/R2  +  1/R3  ...  +  1/Rn)   Ttotal  =    R1  *  R2  

         R1  +  R2  Resistor Color Codes http://www.electronics-tutorials.ws/resistor/res_2.html Ohm’s Law http://learnabout-electronics.org/resistors_11.php Series/Parallel calculations http://learnabout-electronics.org/resistors_20.php http://www.allaboutcircuits.com/vol_1/chpt_6/2.html http://www.electronics2000.co.uk/calc/series-parallel-resistor-calculator.php Resistor Power Ratings http://www.electronics-tutorials.ws/resistor/res_7.html

Battery +

-

R1

R2

R3

Page 13: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

13

Circuit Schematics Like  communicating  with  your  Arduino  Board,  it  is  important  to  be  able  to  communicate  with  other  robotics  enthusiasts.    Therefore,  there  has  been  a  set  language  for  drawing  circuit  diagrams  that  are  used  universally.    This  helps  when  constructing  circuit  diagrams  for  your  circuit  and  sending  them  off  to  other  people  to  construct,  troubleshoot,  or  to  learn  from.    Without  this  language  it  would  be  extremely  difficult  for  other  individuals  to  follow  along  with  other  peoples  projects  and  can  lead  to  confusion,  and  headaches!    A  list  of  symbols  and  their  names  has  been  provided  to  you  as  a  reference  for  constructing  your  own  diagrams.      There  are  websites  on  google  that  list  hundreds  of  different  symbols  for  parts,  one  such  site  is  www.electronic-­‐symbols.com.  With  modern  technology  these  sketches  are  no  longer  hand  drawn,  but  can  be  done  in  computer  programs  that  make  them  clean,  easy  to  read.    The  most  basic  of  these  programs  that  has  been  used  is  the  paint  program.    Though  this  is  laughable  at  many  levels,  it  is  a  possible  way  to  construct  them  when  nothing  else  is  available.    Other  more  advanced  programs  are  available  on  the  internet  for  free  while  others  charge.    PSPICE  is  one  example  of  such  a  program;  Arduino  has  available  programs  as  well  such  as  Fritzing  and  Wiring  which  are  both  open  source  and  arduino  compatible.

Practice  problems:   1. A  1000  ohm  resistor  is  attached  to  a  3  volt  battery  source.    What  is  the  current  in  the  circuit?  2. 0.3  amps  are  flowing  through  a  1500  ohm  circuit.    What  is  the  supply  voltage?  3. Using  circuit  schematics,  sketch  a  series  circuit  with  3  resistors.  4. Sketch  a  parallel  circuit  with  3  resistors  in  parallel.  5. Sketch  a  parallel  circuit  with  2  resistors  in  series  and  a  resistor  in  parallel  with  the  other  two.  6. Two  100  ohm  resistors  are  hooked  in  parallel  what  is  the  voltage  drop  across  each  resistor.    

The  supply  voltage  is  5  volts.  

Page 14: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

14

7. The  same  two  100  ohm  resistors  are  now  hooked  in  series.    What  is  the  voltage  drop  across  each  resistor?    The  supply  voltage  is  still  5  volts.  

8. 9  volts  are  supplied  to  a  series  circuit  with  0.5  amps  of  current.    There  are  two  resistors  in  the  circuit.    If  one  resistor  is  980  ohms,  what  is  the  resistance  of  the  second  resistor?  

BreadBoards

Often  times  circuits  are  soldered  together  and  are  meant  to  be  unaltered.    A  good  example  of  this  concept  would  be  your  Arduino  processor.    If  you  look  at  the  bottom  side  of  the  Arduino  you  will  notice  that  all  the  electronic  components  are  fixed  to  the  board  by  a  metallic  material  called  solder.    Solder  is  a  metallic  alloy  (mixture  of  one  or  more  metals)  that  is  used  to  hold  together  electrical  components.    This  idea  is  great  if  your  project  is  already  completed  and  ready  to  go  off  to  production.    But  if  you  are  still  in  the  process  of  testing,  altering,  and  manipulating  the  layout  of  your  circuits  it  is  not  a  great  idea  to  solder  them  in  place.    Rather  than  using  solder  to  hold  the  circuits  together,  it  would  be  better  to  use  a  breadboard.    Though  there  are  many  sizes  and  types  of  breadboards,  the  standard  breadboard  is  a  720  point  board.    There  is  also  a  300  point  breadboard  and  a  170  point  breadboard.    The  720  and  300  point  breadboards  are  similar,  they  have  two  power  busses  running  along  each  side  of  the  board.    They  also  have  two  rows  of  points  that  allow  you  to  insert  a  part,  like  an  IC  in  the  board.    As  well,  each  leg  of  the  part  has  4  additional  locations  to  connect  wires.    

 This  is  the  a  wiring  diagram  of  a  300  point  BreadBoard.  If  you  have  the  170  point  board  then  you  do  not  have  the  busses  at  the  top  and  bottom  of  this  image.      A  bus  system  is  a  comuter  system  that  is  used  to  communicate  between  wires.    In  the  diagram  each  section  that  shows  a  connection  is  a  bus.      

Page 15: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

15

 This  is  the  a  wiring  diagram  of  a  720  point  BreadBoard.  Please  note  that  the  bus  bars  are  in  three  seperate  parts,  if  you  want  to  use  the  whole  bar  you  will  have  to  jumper  the  two  gaps  on  each  bar.    It  is  a  good  idea  when  you  first  get  your  breadboard,  either  mark  or  jumper  these  segments  together.  Here  is  a  sample  of  how  I  would  mark  this  board,  showing  where  the  bus  is  open.  

The  Rows  labeled  X  and  Y,  Across  the  top  an  bottom  are  typically  used  for  power  and  ground.  On  some  boards  they  run  the  entire  length  of  the  board  and  others  they  are  broken  into  segments.  On  this  board  the  first  three  and  the  last  three  are  separate  from  the  center  4  groups  of  5.  You  can  plug  most  small  components  into  these  boards  and  get  a  good  connection.    You  can  also  use  standard  solid  phone  wire,  or  any  other  wire  available  on  the  market.    If  you  have  a  breadboard  that  is  not  attached  to  anything,  be  careful  to  not  press  out  the  bridges  when  inserting  parts  and  wires.    It  is  best  to  screw  down  or  use  a  strong  double  backed  tape  to  fasten  the  board  to  a  hard  surface.  

This information provided at http://www.hacker.instanet.net/forums/topic62.html  

     Using  a  breadboard  allows  us  to  test  this  ultrasonic  sensor  and    

LED  to  determine  if  we’ve  written  our  code  correctly.        

Remember  if  you  are  designing  a  circuit  that  has  high  frequencies  or  has  very  sensitive  circuits,  a    breadboard  may  cause  some  undesired  results.    These  bridges  are  all  parallel  to  each  other  and  have  high  capacitance  which  can  cause  some  issues.  

Page 16: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

16

From Schematic to Breadboard The  below  schematic  is  of  three  (Red,  Yellow  and  Green)  Light  Emitting  Diodes  (LEDs)  wired  in  parallel.  They  are  powered  from  a  9V  DC  Battery.  A  470  ohm  resistor  is  placed  in  series  with  each  LED  to  limit  the  current  to  a  safe  value.    The  current  through  the  resistors  is  equal  to  9V  minus  the  voltage  drop  across  the  LED  (i.e.  2V).  The  current  will  be  approximately:  I  =  (9-­‐2)  V  /  470  Ω    =  0.0149  A  =  14.9mA  through  each  of  the  LEDs.    

   Use  your  470Ω  resistors,  LEDs  and  jumpers  to  wire  up  the  following  breadboard.  Remember  to  place  the  longer  lead  of  the  LED  closest  to  the  battery  as  current  will  flow  only  in  one  direction.  Trim  the  resistors  to  lay  flush  on  the  breadboard.      When  that  is  complete  and  checked,  attach  the  9V  DC  battery  to  the  positive  and  negative  bus  of  the  breadboard  per  the  following  diagram.  The  three  LEDs  will  light.  This  breadboard  layout  will  be  used  on  your  Land  Shark  vehicle  in  Unit  IV  The  Land  Shark  Assembly  so  the  spacing  shown  is  desired.    

   

+"

9V"DC"Ba)ery"

-"

470"Ω"Resistor"

470"Ω"Resistor"

470"Ω"Resistor"

+" +" +"

-" -" -"

Posi%ve(Bus(

Nega%ve(Bus(

470(Ω(Resistor(

LED(

9(VDC(Ba;ery(

Page 17: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

17

Multimeter

Multimeters  are  electrical  instruments  that  are  used  to  analyze  specific  information  from  a  circuit.    Most  multimeters  have  the  capabilities  of  measuring  voltage,  current,  and  resistance  and  on  various  scales.    It  is  important  that  while  using  the  multimeter  to  maintain  correct  usage  for  each  setting.    Failure  to  correctly  use  the  multimeter  can  damge  the  device,  or  blow  out  a  fuse.  

 Two  Porpoiseinians  check  the  voltage  on  the  breadboard  using  a  multimeter.  

 

Measuring  Voltage  Since  in  a  parallel  circuit  each  branch  has  of  the  circuit  has  the  same  voltage,  voltage  can  be  measured  using  a  multimeter  (voltmeter)  by  connecting  one  lead  to  the  high  potential  side  and  the  other  lead  to  the  low  potential  side.    By  doing  so  you  have  effectively  added  another  loop  and  therefore  have  connected  the  meter  in  a  parallel  fashion.    

Page 18: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

18

In  essence,  for  any  electrical  element,  the  voltage  drop  can  be  measured  by  comparing  the  potential  difference  from  the  high  and  low  sides.    Understanding  the  way  to  use  a  multimeter  will  almost  gurantee  your  mastery  of  basic  circuits.    It  is  a  good  way  to  recall  basic  principles  that  govern  the  world  of  circuits.

Measuring Resistance Make  sure  the  battery  is  disconnected  or  power  is  off.  Like  voltage,  resistance  can  be  measured  with  a  multimeter  (ohmmeter)  by  connecting  one  lead  to  the  high  end  and  the  other  to  the  low  end  of  a  circuit  while  the  meter  is  in  the  resistance  setting.    As  well,  resistance  for  devices  can  be  measured  outside  of  a  circuit  by  holding  one  end  with  each  of  your  fingers  and  connecting  them  to  the  leads.    This  is  a  quick  and  easy  way  to  check  the  resistance  values  of  resistors  without  looking  at  the  color  codes  displayed  on  the  resistor.  

Measuring Current

While  measuring  current  within  an  electrical  circuit  it  is  important  to  remember  facts  about  our  circuits  discussed  early  in  this  section.    Specifically,  in  what  type  of  circuit  does  current  remain  the  same  throughout?    If  you  want  to  look  quickly,  go  for  it!    If  not  we  will  take  the  time  to  discuss  it  here.    Since  a  series  circuit  only  has  one  path  for  the  current  to  flow,  then  all  the  current  in  a  series  circuit  remains  constant  so  long  as  the  voltage  and  resistance  do  not  change.    Therefore  to  measure  current  through  a  specific  branch  of  a  circuit  the  multimeter  (here  called  an  ammerter)  should  always  be  hooked  up  in  series!        Even  if  the  circuit  is  a  parallel  circuit,  for  the  given  strand  that  you  will  be  taking  a  measurment  for  must  be  hooked  up  with  an  ammeter  in  series.    

 To  operate  the  multimeter  Put  red  test  lead  in  10A  MAX  mode  Rotate  dial  to  mA/A  mode  Press  RANGE  for  40/400mA  and  4/10A  ranges,  Press  SELECT  to  switch  from  AC  to  DC  measurement  Apply  power.  If  value  less  than  400mA    and  red  lead  connected  to  10A  MAX,  then  power  off  and  switch  red  lead  to  V.mA.Ω  Rotate  the  function  dial  to  μA/A  or  mA/A,  depending  on  value  just  measured,    to  get  highest  resolution  of  measurement.  Connect  to  the  9V  battery.  Measure  the  current  flow.      

Method  for  measuring  current  using  a  digital  multimeter.  

Page 19: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

19

  THE BEST Multimeter tutorial (HD) http://www.youtube.com/watch?v=bF3OyQ3HwfU How To Use A Multimeter http://www.youtube.com/watch?v=zb7WHaL_dz8 Measuring Current http://www.youtube.com/watch?v=7lwZkl0yBqA How to measure Voltage, Resistance and Current with a Digital M ultimeter http://www.youtube.com/watch?v=sKuPd3XYwuA How to use a Multimeter for beginners: Part 1 - Voltage measureme nt http://www.youtube.com/watch?v=ZBbgiBU96mM How to use a Multimeter for beginners: Part 2a - Measuring Curren t http://www.youtube.com/watch?v=EVFkKBFJsZg How to use a Multimeter for beginners - Part 2b - Current (terminology corrections and extension) http://www.youtube.com/watch?v=JID_6JSNwoQ How to use a Multimeter for beginners: Part 3 - Resistance and Continuity http://www.youtube.com/watch?v=InJhgwmj2So  

 

 Determining  the  proper  resistors  to  use  in  conjunction  with  LED’s    

using  a  breadboard  and  multimeter.        

Page 20: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

20

Practice  problems:    1. Measure  the  resistance  of  several  resistors.    What  do  you  notice  about  the  resistances,  why?  2. Put  a  resistor  in  series  with  a  3  volt  source.    Calculate  using  ohms  the  law  the  current  through  

the  circuit.  3. Measure  the  current  of  the  resistor  in  problem  2.    Compare  to  the  calculated  value.  4. Put  a  1000  ohm  and  a  100  ohm  resistor  in  parallel  attached  to  a  5  volt  source.    Measure  the  

voltage  drop  across  each  resistor.    What  do  you  notice?  5. Why  do  you  think  these  values  occurred?  6. Measure  and  calculate  the  current  through  each  loop  for  problem  4.  

Page 21: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

21

Unit III – The Arduino Programming Environment & Language

Introduction to the Arduino Microprocessor The  Arduino  micro-­‐controller  is  a  “computer  chip”  that  is  on  a  silicon  circuit  board.    The  Arduino  circuit  board   is   therefore  an   interface   that  allows   the  human  world  can  communicate  with   the  mechatronic  world.  This  communication  is  easy  to  accomplish  with  a  little  bit  of  practice.    Because  Arduino  is  “Open  source”,  that  means  that  the  source  code  and  design  specifications  are  freely  available  to  anyone,  generally  over  the  web.    You  have  access  to  everything  that  went  into  creating  the  Arduino  and  the  tools  to  utilize  it.        The  Arduino,  according  to  Wikipedia,  was  “designed  to  make  the  process  of  using  electronics  in  multidisciplinary   projects   more   accessible.     Because   Arduino   is   “open   source”   it   creates   an  atmosphere   that   encourages   the   sharing   of   ideas   and   code.     In   most   cases,   if   you   want   to  acomplish  a  task,  or  series  of  tasks,    someone  has  done  something  similar.    You  can  Google,  or  use  other  search  engines  to  find  out  how  they  did  it.      

UNO Nano

There  are  different  models  of   the  Arduino.  The  UNO,  NANO  and   the  MEGA  2560  are   the  most  popular.   Because   it   is   open   source,   there   are   many   other   models   created   for   more   specific  projects.   The  Mega   2560   is   the   Arduino   that   we   will   use.   The   reason   we   decided   to   use   this  particular  model   is   because   it   has   8   times   the   code   space   and   70   I/O   pins   (Input/output   pins)    where  the  Arduino  Uno  and  Nano  have  just  19  I/O  pins.  For  further  information,  go  to:  http://arduino.cc/en/Main/ArduinoBoardMega2560#.UxY2BXl6Mds  

Page 22: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

22

Mega 2560

Part  of  the  beauty  of  the  Arduino  boards,   is  they  have  created  a  standard.  This  standard  allows  you  to  use  your  C++  program  on  any  Arduino  that  fits  your  code  size  and  I/O  requirements.  The  UNO  and  the  Mega2560  have  different  processors,  but  these  microprocessors  all  understand  the  same  code.     Most  Arduino  have  a  USB  port  that  allows  us  to  connect  our  computer  directly  to  the  board.      The  Arduino  boards  are  programmed  using  C++,  a  rather  modern  language  that  is  well  supported  in   the   Open   Source   community.   C++   is   compiled   and   loaded   automatically   onto   your   Arduino  from  within  the  Interactive  Development  Environment  (IDE)  or  Arduino  Program.  The  compiled  code  is  much  smaller  and  more  efficient  than  many  other  computer  languages.    In   order   to   program   our   Arduino,   we   have   to   download   and   install   some   software   onto   our  computers.    

Downloading Protocal Download   the   Arduino   software   onto   the   available   computers   that   will   be   used.     The   current  version   is  available  at:      http://arduino.cc/en/Main/Software.     It   is  available   for   the  MAC,  Linux  and  Windows.  There  is  detailed  instruction  detailing  the  download  and  installation  procedure  for  each  platform  at  this  web  site.  

Understanding the Arduino

 As   already   mentioned,   the   Arduino   is   the   key   to   working   with   our   robots.     It   is   the   most  important   component   to  understand  as  you  move   forward.    Regardless  of   the   type  of  Arduino  board  that  you,  they  all  operate  very  similarly.    If  you  understand  the  MEGA  that  we  use,  you  will  understand   and   be   able   to   use   the   UNO,   the   NANO,   and   other   Arduino   based   boards  interchangeably.    For  now  we  will  just  discuss  the  basic  functionality  of  the  board  so  that  you  are  not   intimidated   by   all   its   compnents.     As   you   read   this   section   it   may   help   if   you   have   your  Arduino  to  look  at  while  reading.    The   Arduino   circuit   board   functions   using   I/O   pins.     As   mentioned,   each   board   type   has   a  different   number  of   these   little   pins.     These  pins   are  used   to   connect  wires   to  other   electrical  

Page 23: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

23

components,  such  as  LED  lights,  motors,  speakers  and  actuators.  The  name  I/O  pin  is  a  nickname  for  what  they  really  do,  which   is   to   input  and  output   information.    Through  these  pins,  you,  or  your   sensors,   input   information   into   the   Arduino   computer   chip,   which   then   processes   those  signals  according  to  the  C++  code  that  you  write.    The  results  of  your  C++  code  are  then  output  through  these  pins  to  the  wires  that  go  to  your  LED’s,  motors  or  actuators.    The  result  is  that  your  robot  will  perform  the  actions  you’ve  written  into  your  code.            For  further  information,  go  to:    Arduino Digital Pins = http://arduino.cc/en/Tutorial/DigitalPins  http://arduino.cc/en/Tutorial/DigitalPins#.UyhtxXl6Mds    There  are  other  large  components  on  the  board  that  all  Porpoiseinians  need  to  know  about.    One  is  a  connector  so  that  you  can  attach  an  external  power  source,  and  the  other  is  a  USB  hub.    Take  time  out   to   identify   these   two  connections  and   familiarize  yourself  with   the   I/O  pins  and   their  arrangement.    Your  Arduino  will   receive   information   from   your   code   or   sensors,   then   process   it   according   to  your  C++  code  instructions,  which  are  called  “Sketches”.    Once  processed,  the  Arduino  will  send  output  control  signals  to  a  variety  of    LED’s,  motors,  actuators,  or  other  electronic  components.    Lets  review  a  few  of  these.        A   sensor   is,   as   the   same   suggests,   a   device   that   senses   its   surroundings.     The  way   in  which   it  works  is  by  sensing  a  physical  measurement  such  as  light,  sound,  temperature  or  pressure,    and  converting   it   into   a   signal   reading   that   can   be   understood   and   processed   by   a   computer.    Conversly,  an  actuator,  is  as  its  name  suggests,  caused  an  action.        Some  examples  of  actuators  are  motors  and  switches.      Tutorial 01 for Arduino: Getting Acquainted with Arduino (14 min 31 sec) http://www.youtube.com/watch?v=fCxzA9_kg6s  There  are   two  sections   to  any  sketch   that  are  always   required.  First   is   “setup()”.     In   setup  you  should   include  all  parts  of  your  program  that  are  required  to  be   identified  once,  as  well  as   the  commands  that  initialize  the  program  and  your  hardware.    In  the  Blink  Sketch  we  programmed  a  specific  pin  to  be  an  output  pin,  and  hence  we  “initialized”  that  I/O  pin,  or  defined  exactly  what  is  was.    Each  pin  that  we  used,  needs  to  be  spcifically  identified  to  the  Arduino  processor.        The  other  section  that  is  required  is  a  “loop”.  When  your  program  runs,  it  first  runs  setup()  once,  and  then  ‘loops’  thru  loop()  till  you  reset  or  power  off.    

void setup() { // put your setup code here, to run once: } void loop() { // put your main code here, to run repeatedly: }

 

Page 24: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

24

Both   setup()   and   loop()   return  no   results,   so  we  define   then  with   a   “void”.  Void   specifies   that  nothing   is   returned.     In   C++,   all   routines  must   be   defined   is   this   way.   If   a   routine   returns   an  integer  then  we  define  it  with  the  ‘int’  type  such  as;    int  temperature(int  sensor){.    If  we  want  it  to  return  a  character  then  we  define  it  with  the  ‘char’  type.  char  direction(int  compass){.        These  required  routines,  setup()  and  loop()  also  take  no  parameters  so  the  ()  are  empty.    In  C++,  we  specify  the  parameters  that  can  be  passed  to  a  routine  within  the  ()  brackets.  Here  is  an  example  of  a  routine  that  returns  an  int  and  takes  an  int  also:  

int dump(int a) { return a; }

 This   routine   does   not   do  much   but   show   the   syntax   of   defining   and   passing   parameters   to   a  routine.   You   will   also   notice   that   there   are   curly   brackets   {}   ‘holding’   the   routine   together.  Everything  between  the  {  and  the  }  are  part  of  this  routine.  We  must  be  very  careful  to  match  the  brackets  or  we  will  get  an  error  when  trying  to  compile  the  Sketch.    Curly  brackets  are  also  used  to   ‘hold’  other   things  on  our  program.    For  example  when  we   loop  or   test   (if)  we  use  brackets  also.      Here  is  a  snippet  of  code  showing  a  test  and  a  loop:  

int dump(int a) { if(a > 10) { while(a > 5) { a = a -1; } } return a; }

 This  code  takes  a  value  passed  to  the  routine,  testes  to  see  if  it  is  larger  than  10,  if  so,  then  we  do  the  loop.    In  the  ‘while’  loop  we  test  to  see  if  the  value  is  larger  than  5,  and  if  it  is,  we  subtract  one  and  loop  again.    If  the  number  was  not  larger  than  10,  then  we  just  return  the  number.    You  will  notice  that  there  are  three  sets  of  curly  brackets  and  that  they  are  all  matched  around  the  code.    There  is  also  another  way  to  write:  

a = a – 1; Like  this:  

a--; This   is  a  shortcut.    As  you  start   looking  at  other  people’s  programs  you  will  see  there  are  many  shortcuts.  Some  make  the  code  easier  to  read,  or  are   just  shorter  to  type.    You  may  find  some  shortcuts  use  less  bytes  in  the  compiled  sketch,  while  other  makes  no  difference  at  all.    

Page 25: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

25

You  should  also  see  that  we  indent  sections  of  code  to  make  it  easier  to  follow.    In  the  example  above,  you  will  see  that  at  the  ‘if’  we  start   indenting  to  the  end  of  the  ‘if’  at  the  closing  ‘}’.  We  also  indented  again  at  the  while().    You  may  have  noticed  several  places  I  have  used  two  slashes  ‘//’  to  add  in  comments.    Anything  on  that  line  after  the  double  slash  is  ignored  by  the  compiler.    If  you  want  to  create  a  large  block  of   comments,   like   credits   at   the   top  or   the  description  of   a   routine,   you   can   create   a  block  of  comments  like  this:  

/* This is my favorite sketch. It controls my robot Requires a Mega2560 Arduino */

The  ‘/*’  tells  the  compiler  to  ignore  everything  until  it  sees  a  ‘*/’.  Most  people  do  not  put  enough  comments  into  their  sketches.  Comments  are  very  important.    You  need  to  comment  your  code  so  that  when  you  share  your  sketches  with  other  people,  they  will  understand  exactly  what  you  were  trying  to  accomplish.    Comments  also  make  your  code  easier  to  follow  by  using  descriptive  variables.    For  example,  I  used  LED  to  describe  what  pin  the  LED  would  be  connected  to.    I  also  used  Speaker  to  define  the  speaker  pin.    Talking  about  LED  (and  Speaker),  you  will  notice  that  we  defined  an  int  to  specify  what  pin  we  will  use  for  the  LED.    If  we  later  decide  to  change  what  pin  we  want  to  use  for  the  LED,  we  just  change  it  once  at  the  top  of  the  sketch  and  don’t  have  to  change  any  other  code.  If  you  notice,  we  never  change  the  value  if  LED,  as  the  LED  will  always  be  on  the  same  pin  for  this  sketch.      Another  thing  we  can  do  is  define  LED  as  a  constant  as  follows:    

const  int  led  =  6;    There  are  advantages  to  doing  this.  First,  constants  take  less  memory,  only  a  few  bytes  less  but  in  large  programs  it  will  add  up.    Also,  it’s  a  few  bytes  for  every  place  the  constant  is  used.    If  you  define  a  variable,  it  is  read  only.  If  the  program  tries  to  modify  a  constant  you  will  get  an  error.    This  may  assist  you  in  finding  a  problem  when  you  accidently  use  the  wrong  variable  name.    

Page 26: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

26

 Practice  problems:  1. Make  a  sketch  that  uses  one  subroutine  to  turn  on  the  LED  and  another  to  turn  off  the  LED.  

Make  calls  to  these  routines  to  blink  the  LED.  2. Make  a  sketch  that  uses  subroutines  to  make  beeps.  Make  calls  to  these  routines  that  beeps  

once  for  1  second  every  3  seconds.  3. Make  a  sketch  that  used  subroutines  to  beep  for  an  amount  of  time  passed  to  the  routine.  

Make  calls  to  this  routine  several  times  with  delays  between  each  delay  and  passing  different  beep  times  for  each  call.  

To prepare for the next sections you may want to view this video. TechBits  13  -­‐  Analog  and  Digital  Signals  (17  min  19  sec)     http://www.youtube.com/watch?v=Z3rsO912e3I    

Page 27: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

27

Blink Sketch In  the  Arduino  world,  a  set  of  instructions  or  program,  is  call  a  Sketch.    We  will  use  a  Sketch  that  is  already  written  and  is  located  in  the  IDE  that  we  previously  installed.        Find  and  run  the  ‘Arduino’  program  on  the  class  computer.    

     The  IDE  screen  should  look  like  the  left  screen.                    Next  we  want  to  select  File  à  Examples  à  01.Basic  à  Blink.    The  screen  should  look  like  the  right  screen:  

 We   can   now   connect   our   Arduino   to   our   computer.   If   this   is   the   first   time   we   connect   this  Arduino   to   this   particular   computer,   and   it   is   a   Windows   computer,   we   will   have   to   tell   the  Operating  System  what  the  device  is.  When  we  plug  the  USB  cable  in,  Windows  will  notify  us  that  a   new  device  has  been   added  and  may   ask  where   the  driver   is.     At   this   point  we  need   to   tell  Windows  that  the  driver  is  in  the  Arduino  program  directory  under  drivers.    Typically  this  will  be:    C:/Program  Files  (x86)/Arduino/drivers  

 

Page 28: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

28

 Do  not  specify  a   file,   just   the  driver’s  directory.  After   the  driver   is   loaded  you  will  get  a   screen  similar  to  this:  

 We  need  to  tell  the  IDE  what  Arduino  we  have  connected  and  where.    For  a  PC,  we  click  Tools  à  Board   and   select   ‘Arduino  Mega   2560   or  Mega   ADK’.     Then   we   tell   it   where   the   board   is   by  specifying   the  COM  port.   It   is  most   likely   the  port   that   is  not  COM1  or  COM2.    As  you  can  see  above,  mine  was  at  COM20.    Once  that   is  set,  we  can  click  on  the  RUN  Arrow  and  compile  and  upload  our  program  to  our  Arduino  Board.        Here is the example as it comes: CODE: SELECT ALL /* Blink Turns on an LED on for one second, then off for one second, repeatedly. This example code is in the public domain. */ // Pin 13 has an LED connected on most Arduino boards. // give it a name: int led = 13; // the setup routine runs once when you press reset: void setup() { // initialize the digital pin as an output. pinMode(led, OUTPUT); } // the loop routine runs over and over again forever: void loop() { digitalWrite(led, HIGH); // turn the LED on (HIGH is the

Page 29: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

29

voltage level) delay(1000); // wait for a second digitalWrite(led, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second } You  can  either  use  the  “Example”  program  “Blink”  sketch  or  do  the  following  yourself.  Copy  this  code  into  the  Arduino  Development  Environment  blank  sketch  area  under  the  label  “Sketch  and  todays  date”.  Comment  out  the  explanations  in  black  text,  keeping  the  code  in  green  as  is.    

• For  one  line  insert  //  at  the  beginning.    • For  several  lines  insert  /*  at  the  beginning  and  */  at  the  end.  

 Verify/Compile  the  code  by  selecting  the  check  mark  ✓  at  the  top  of  your  screen.    Or  you  can  select  “Sketch”,  then  “Verify/Compile”.  If  it  does  not  compile,  look  at  the  error  messages  at  the  bottom  of  the  screen  and  try  to  fix  your  code.  Check  your  changes  by  selecting  the  check  mark  �.    After  it  compiles  successfully,  save  the  program  by  selecting  the  down  arrow  ê    at  the  top  of  the  screen  and  giving  it  a  name  “My  Blink”.      Or  you  can  select  “File”,  then  “Save  As”  and  give  it  a  name  “My  Blink”.      You  can  verify  that  the  program  is  saved  by  selecting  “File”,  then  “Sketchbook”.  You  will  see  your  file  name  listed.    Then  upload  the  program  to  the  Arduino  computer  by  selecting  the  right  arrow  è  at  the  top  of  the  screen.  Or  you  can  select  “File”,  then  “Upload”.  You  will  see  the  Rx  (Receive)  and  Tx  (Transmit)  light  blink  very  fast  as  the  code  is  being  transferred  to  the  Arduino  processor.    The  code  will  begin  executing  right  away.  You  will  see  the  Mega  2560  board  LED  blink  on  and  off  at  a  1  second  rate.    Some  simple  changes  would  be  to  change  the  blink  rate  by  changing  the  delay(1000)  to  another  value.  The  1000  milliseconds  =  1  second  so  if  you  change  the  first  delay  to  2000  then  the  LED  will  stay  lit  for  2  seconds  and  be  off  for  1  second.    Once  you  are  tired  of  that  you  should  try  connecting  up  your  own  LED.  Here  is  an  example  of  using  the  solderless  breadboard  fom  Unit  II.  The  red  LED  should  flash.  If   it  does  not  do  what  you  want,  or   stops  working  all   together,   just   close   the  program  without  saving  and  reload  it.    

Page 30: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

30

     Congratulations.    You’ve  finished  your  first  Sketch  and  are  on  your  way  to  controlling  your  robot.    Practice  Problems:    

1. Make  a  sketch  that  blinks  the  LED  once  for  1  second  every  3  seconds.  2. Make  a  sketch  that  blinks  the  LED  once  for  1  second  after  3  seconds  and  then  stops.  3. Modify  the  program  such  that  the  green  and  yellow  LEDs  will  blink.    4. Vary  the  blink  on  and  off  time  for  each  of  the  LEDs.    5. Have  them  all  flash  at  the  same  time.    6. Simulate  a  traffic  light  by  transitioning  from  green  to  yellow  to  red.    

 Later,  when  we  hook  up  the  Range  Finder,  you  can  have  an  approaching  object  cause  the  light  to  go  from  red  to  green.  

 

22"

12""13"Gnd"

Arduino"Computer"

Vin"Gnd"Gnd"5V"

Posi6ve"Bus"

Nega6ve"Bus"

470"Ω"Resistor"

LED"

Page 31: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

31

Noise Sketch This  will  be  our  ‘Official’  second  sketch.    Not  only  will  we  be  writing  it  but  we  will  also  connect  up  a  speaker  to  our  Arduino.  I  will  show  you  how  to  make  the  basic  Noise  Sketch,  and  you  will  be  in  charge  of  getting  creative  with  the  sounds  that  eminate  from  it.      The   speaker  we  have   comes  with  no  wires  attached.    We  will   have   to   solder   two  wires   to   the  terminals  on  the  back  of  the  speaker.  I  suggest  getting  two  different  color  wires  and  cut  them  to  about  6”.    For  use  with  the  Solderless  breadboard,  we  need  to  use  solid  wire  rather  than  stranded  wire.    We  need  to  strip  off  about  1/3  inch  of  the  insulation  off  each  end.    Attach  and  then  solder  one  end  of  each  wire  to  each  of  the  two  terminals  on  the  speaker.    The  other  end  will  plug  into  our  Solderless  breadboard.    We  now  connect  one  side  of  the  speaker  to  the  Ground  Pin  on  the  Arduino  and  the  other  thru  a  resistor   to  an   I/O  Pin.    We  will   use  Pin  6.     You  will   notice   that   all   the  pins  on   the  Arduino  are  marked  with  their  names.    The  Ground  Pin  is  GND  and  Pin  6  just  has  a  “6”.      The  resistor  will   limit  the  current  thru  the  speaker  and  also  protect  Pin  6  of  the  Arduino.    It  is  a  good  practice  to  put  a  resistor  in  series  with  the  I/O  pins  of  the  Arduino  if  you  are  worried  about  drawing  too  much  current.  It  is  better  to  have  a  little  lower  volume  speaker  than  an  Arduino  with  one  less  I/O  pin.      

   

Page 32: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

32

 We’ve  seen  this  picture  before,  but  now  you  can  understand  the  entire  project.    A  couple  of  clever  Porpoiseinians  created  a  “Late  Student  Alarm”.    When  this  is  placed  by  the  clasroom  door,  anyone  entering  late,  triggers  the  ultrasonic  “Ping”  sensor  (the  two  eyes)  which  then  causes  the  the  LED  above  the  sensor  to  blink,  and  the  speaker  begins  to  wail.    No  more  sneaking  into  class  late!    So  lets  now  make  the  speaker  Buzz.    In  our  first  Sketch  we  turned  on  and  off  the  LED  at  a  fairly  slow  rate.    In  this  Sketch  we  are  going  to  turn  on  and  off  the  voltage  to  the  speaker  at  a  faster  rate.    We  will  do  this  the  same  way  as  we  did  with  the  LED,  but  change  the  delay  to  an  even  smaller  number.      Here  is  the  modified  code  created  for  this  example:

int Speaker = 6; // note I said I wanted to sue pin 6 void setup() { pinMode(Speaker, OUTPUT); // initialize the digital pin as an output. } void loop() { digitalWrite(Speaker, HIGH); // turn the Speaker on delay(10); digitalWrite(Speaker, LOW); // turn the Speaker off delay(10); }

If delay(1000) delays for 1 second, then delay(1) will delay 1/1000 second. Because it takes two of these for each cycle of the speaker, we will hear a 500 Hz tone. This would be the highest frequency tone we could generate using this method. You can experiment with changing one or both of the delays to see what you can make. Because there is always another way to do things, we are now going to use a different command to make our tone. Because programming should be logical, the command is; Tone(). The tone command takes one parameter, the tone in Hertz Here is an example of a two tone Sketch:

int Speaker = 6; // note I said I wanted to sue pin 6 void setup() {

Page 33: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

33

} void loop() { tone(Speaker, 150); delay(500); tone(Speaker, 250); delay(500); }

Note  there  is  nothing  to  do  in  setup,  so  I  left  it  blank.    To  learn  more  about  the  tone  command,  refer  to  the  reference  included  with  the  IDE.    To  get  to  the  support  documents,  click  on  Help  à  Reference.   Then   on   the   right   side   is   Advanced   I/O   including   tone().   In   this   section   there   are  several  examples  worth  working  with.    Practice  Problems:  1. Make  a  sketch  that  plays  a  sequence  of  tones  and  repeats.    2. Make  a  sketch  that  emulates  a  police  or  fire  truck  siren.  3. Make  a  sketch  that  beeps  once  for  1  second  every  3  seconds.  4. Make  a  sketch  that  beeps  at  three  different  tones  and  then  repeats.    

Turning a Buzzer On and Off Since   small   speakers   may   not   be   available,   this   project   can   be   done   with   a   3V,   15ma,   75dB  buzzer.  Putting  a  push  button  switch  in  series  will  silence  the  buzzer  until  you  are  ready  to  test.  Wire  the  breadboad  and  Arduino  as  follows:  

 Input  the  code  to  your  Arduino  Integrated  Development  Environment  Sketch  area  as  follows:  

int Buzzer = 6; // use pin 6 void setup() { pinMode(Buzzer, OUTPUT); // initialize the digital pin as an output. } void loop() { digitalWrite(Buzzer, HIGH); // turn the Buzzer on delay(1000); digitalWrite(Buzzer, LOW); // turn the Buzzer off delay(1000); }

 Verify and Upload your file. Press the push button switch to hear the buzzer. Change  the  delay  time  as  required  to  vary  the  ON  and  OFF  time.  

Page 34: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

34

Pulse Width Modulation  Another  function  that  we  can  try  is  PWM,  Pulse  Width  Modulation.  This  creates  a  waveform  that  is  not  square  but  rather  a  ratio  of  “On”  to  “Off”  based  on  the  input  value.    If  we  give  a  0,  then  the  signal  is  off  all  the  time.  A  value  of    255  leaves  the  signal  on  all  the  time.    If  we  send  a  64  we  get  a  25%  duty  cycle  that  is  “on”,  and  75%    of  the  time  it  is  “off”.  

This  is  not  very  good  for  creating  sounds,  but  it  works.    It  is  better  for  adjusting  a  motor  or  LED.    If  we  run  a  25%  duty  cycle  signal  into  a  motor  we  will  get  about  25%  of  full  power.    Motors  require  a  minimum  voltage  and  current   to  get   started,   so  we  will   calibrate  our   routine   to  get  accurate  speed  control.    That’s  for  later.    For  now,  connect  an  LED  and  experiment  with  the  command.    

       Porpoiseinians find that programming in C++ is fun. Here they prepare code to run lights on their Land Shark cars which we will build in the next unit.  The  command  we  use  for  PWM  is  “analogWrite()”.    The  command  is  mostly  used  for  controlling  motors  and  lights  so  it  looks  like  it  is  controlling  the  voltage  on  the  pin,  hence  the  “analogWrite()”  name.  

int LED=13; void setup() {

Page 35: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

35

} void loop() { analogWrite(LED, 64); // 25% duty cycle delay(2000); analogWrite(LED, 192); // 75% duty cycle delay(2000); }

Once  again,  experiment  with  different  values  and  delays  to  see  if  you  can  make  the  sketch  make  the  LED  brighter  or  dimmer.  

     You  can  try  a  variation  of  this  circuit.  

int LED=13; int LED2=12; void setup() { } void loop() { analogWrite(LED, 64); // 25% duty cycle analogWrite(LED2, 192); // 75% duty cycle delay(5000); }

Which  LED  is  brighter?  

22"

12""13"Gnd"

Arduino"Computer"

Vin"Gnd"Gnd"5V"

Posi6ve"Bus"

Nega6ve"Bus"

470"Ω"Resistor"

LED"

Page 36: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

36

Are  you  ready  for    the  next  unit?    We’re  going  to  build  the  Land  Shark!

Page 37: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

37

Unit IV – The Land Shark Assembly The   Land   Shark   is   a   robotic   car   that   we   use   to   learn   hardware   assembly   and   Arduino  programming  skills.     The  Land  Shark  can  operate  on   land,   in   the  classroom,  allowing  us   to   test  and  debug  our  code.    It  is  very  similar  in  operation  to  the  Sea  Hawk  boat  which  we  will  make  in  the  second  semester.    The  Land  Shark   is  operated  by  two  motors,  similar   to  the  Sea  Hawk  surface  vehicle.  The  words  “surface  vehicle”  refer  to  a  vehicle  or  craft  that  operates  on  the  surface  of  the  water.    The  Land  Shark  has  two  wheels  while  the  Sea  Hawk  has  two  propellers.    The  wheels  give  traction  on  hard  surfaces  and  the  propellers  give  thrust  in  water.    If  we  drive  the  left  motor  and  not  the  right,  the  robots  will  turn  right.    When  we  power  both  motors  forward,  our  robots  will  move  forward.        We  can  mount  sensors  on  the  Land  Shark,  modify  existing  C++  code,  or  write  your  own,  and  get  your  tasks  working  right  on  your  desktop  or  the  classroom  floor.      Once  you’ve  done  that,  you  will  be  able  to  transfer  the  electronics  to  the  Sea  Hawk  or  any  surface  or  underseas  vehicle  that  you  build.  You  will  see  that  there  are  some  differences  in  how  the  Land  Shark  and  Sea  Hawk  complete  tasks,   one   on   the   land   and   the   other   on   the  water.     That  will   be   the   fun   of   experimentation.    Undoubtedely  you  will  crash  your  car,  and  your  first  boats  will  sit  lopsided  in  the  water,  and  if  you  build  a  submarine  –  oh  boy,  that  brings  with  it   its  own  set  of  challenges,   like  properly  fitting  O-­‐rings.      Just  try  not  to  emulate  Captian  Destructo!!!          Our  Land  Shark  will  need  to  be  assembled.  That’s  part  of  the  fun.  The  Land  Shark  has  an  acrylic  base  that  is  pre-­‐cut  and  drilled,  two  motors,  two  wheels,  a  caster  and  all  the  little  parts  to  hold  it  together.    

 

Page 38: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

38

             So  let’s  build  a  Land  Shark.    First,  you  can  remove  the  protective  paper  coating  from  the  acrylic  base,  or  what  we  will  call  the  chassis.    (Note  that  some  Porpoiseinians  leave  it  on  to  protect  the  plastic  chassis  during  the  build  stage.)    Start  at  an  edge,  and  if  you  are  careful  you  can  get  each  side  off   in  a  single  piece.     If  you  use  a  sharp  object   to   lift  off   the  paper,  be  very  careful  not   to  scratch  the  plastic.      You  will  also  find  in  a  small  bag  two  slotted  discs  and  four  ‘T’s.      The  four  ‘T’s  are  mount  brackets  for  your  motors.  You  will  need  to  remove  the  backing  from  them.    There  are  two  disks  that  are  encoders   for   the  motors.     Pull   the  backing   tape   from   the  encoder  disks.     Encoders   allow  us   to  verify  how  many  rotations,  or  parts  of  a  rotation  the  motor  has  turned.  Taking  the  derivative  over  time,  (that’s  a  big  word,  but  you  will  learn  what  it  means  shortly)  you  can  calculate  how  fast  the  motor   is  turning,   just   like  the  speedometer   in  your  car  tells  how  fast  you  are  traveling.  We  will  also  need  the  four  long  screws,  four  nuts  (no  not  your  team  members)  and  the  two  motors.    

 

Page 39: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

39

 Assembling  the  Motors,  Wheels  and  caster.    (Refer  to  the  above  diagram.)      

Page 40: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

40

Your  motors  may  already  have  electric  wires  soldered  to  them.    If  electric  wires  are  NOT  soldered  to  each  motor,  you  will  need  to  follow  these  instructions  and  solder  wires  on  to  the  motors.  We’ll  use   two   sets  of   two  different   colored  wires,   about  8”   long.     If   your   kit   has  Orange  and  Yellow    wires,  use  those  colors.  Strip  off  about  ¼”  of  the  insulation  off  one  end  of  each  wire.    Solder  the  stripped   end   of   each   color   to   each   motor.   If   you   set   each   motor   on   the   table   in   the   same  orientation,  we  will  want  to  solder  the  same  color  wire  to  the  top  of  each  motor.  This  way,  when  we   connect   them   to   the   motor   controller,   both   motors   will   turn   the   same   direction   when  connected  the  same  way.    Remember  to  get  a  good,  solid  solder  joint  but  be  careful  not  to  melt  any  of  the  plastic  on  the  motor.      If  you  have  never  soldered  before,  the  copper  tab  that  you  are  soldering  to  is  mounted  in  plastic  so  be  quick,  or   the  heat   from  the  soldering   iron  will   travel  down  the  copper  wire  and  melt   the  plastic.   Nope,   not   good.       Here’s   a   video   on   soldering   if   you   are   a   newby.  http://www.youtube.com/watch?v=QL86gO9mfT8     Soldering   is   fun,   and   as   a   Porpoiseinian,  soldering  will  become  second  nature.     If   you’ve  not  mastered   it,  practice  a   few   times  on  wires  before  attempting  this  on  your  car.          Now  place  the  encoder  disks  onto  the  motor  shaft.  They  are  installed  on  the  opposite  side  of  the  motor  to  where  the  wires  are  soldered  in.    The  encoders  are  installed  onto  the  shaft  so  that  the  end  of  the  shaft  is  even  with  the  surface  of  the  disk.  You  will  later  adjust  them  to  align  with  the  sensor  that  we  use  to  read  the  encoder  slots.      Now  mount  the  motors  to  the  base.    We  use  two  of  the  ‘T’s,  two  long  screws  and  two  nuts  per  motor.  The  nuts  shoud  be  on  the  inside.  Install  the  motor  with  the  wires  to  the  outside  of  the  base  and  align  the  encoders  over  the  cross  shaped  hole.    Here  is  what  it  should  look  like:                                          

     

Page 41: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

41

Now  we  will   install  the  caster  to  the  underside  of  the  chassis.  You  will  need  eight  small  screws,  four  brass  standoffs  and  the  caster.    Attach  the  standoffs  with  four  screws  to  the  chassis  at  the  rear  of  the  Land  Shark.    Do  not  tighten  the  screws  all  the  way  yet.    Now  use  the  four  remaining  screws  to  attach  the  caster.    Once  all  the  screws  are  in,  you  can  align  and  tighten  all  the  screws.      We  will  use  the  battery  pack  on  the  Land  Shark  to  power  the  motors  and  add  a  9  Volt  battery  to  power   the   Arduino  microprocessor.     The  motors   run   on   6   Volts.     The   Arduino  microprocessor  likes  between  7  volts  and  12  Volts.  The  standard  9  Volt  battery  will   run   the  Arduino   for  a   long  time,  as  long  as  we  don’t  try  to  run  a  lot  of  LED  or  any  motors  directly  off  the  Arduino.    We  want  to  allocate  as  much  space  on  the  chassis  for  various  sensors  and  other  devices  that  we  attach   to   the   Land   Shark,   so   lets  mount   the   6V   and  9V  battery   boxes   on   the  underside  of   the  chassis.  Place  two  velcro  tabs  on  the  battery  box  and  one  on  the  9V  battery.  Remove  the  backing  from   the  mating   velcro   tab  and  place   the   two  velcro   surfaces   together.   Press   the   larger  6   volt  battery  holder  on  the  back  end  of  the  Land  Shark  with  the  wires  towards  the  front.    Mount    the  9V  battery  crosswise  on  the  Chasis  with  the  negative  terminal  towards  the  front.      Install  the  wheels  on  the  right  and  left  motors.    You  will  have  some  extra  hardware,  place  it  into  one  of  the  small  bags  in  case  we  use  them  later.      I  always  make  a  small  box  or  bag  of  miscellaneous  parts,  even  odds  and  ends  that  I  may  find  on  the  floor.    You  never  know  when  you  will  need  something.    

Land  Shark  Bottom  View  (Note  the  right  and  left  wheels  appear  reversed  in  the  bottom  view)      

Right  Wheel

Left  Wheel

6V  Battery

9V   Battery

Front Wheel

Page 42: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

42

To mount the Arduino and the Motor Controller, cut eight 1” strips of double sided tape. Make the strips double thickness by pressing two pieces together. You now have four, 1” long, double thick pieces of double sided tape. Stick two pieces to the bottom of the motor driver, which is the square circuit board with the black heat sink, and line up the forward motor driver holes with the two holes in the chassis, with the heat sync facing to the rear. Press the motor driver to the top side of the chasis. Stick two tape pieces to the underside ends of the Arduino board and mount it to the top left side of the chasis as shown. Apply two rows of double sided tape to the back of the breadboard (white plastic panel with numerous holes). and mount it against the on/off switch forward of the Arduino computer.  

 Land  Shark  Top  View  (right  and  left  wheels  are  in  their  normal  top  down  view)    Install the breadboard with LEDs and Range Finder at the front of the Land Shark close to the 9V DC switch as shown in the above photo. This will allow room for an additional breadboard on the right side of the Land Shark if desired. The nine volt switch, located at the forward left corner of the chasis, will need to have a hole drilled for it. Very carefully drill a hole in left forward chasis, large enough to fit the switch shaft. Insert it, with the switch lever facing straight up, and screw on the retaining bolt. Installing the white toggle switch: Bring the positive red wire from the 6V battery box, up through the rectangular switch hole in the chassis. Solder it to one pole of the white toggle switch.

Motor

Controller

Bread-­‐board

1      0

1      0

Right  Wheel

Left  Wheel

Optical  

Encoder

Mot

or

Cont

rolle

r

Page 43: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

43

Remove one metal end of a red jumper wire, strip it back ¼” and solder it to the other pole of the white toggle switch. Push the rectangular toggle switch into the rectangular hole with the vertical line symbol (meaning close or engage the circuit) facing to the back of the Land Shark.    

   

You are now done with the mechanical assembly of the Land Shark.

Page 44: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

44

Assembled Landshark

Land Shark Top View

Land Shark Bottom View

Page 45: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

45

Wiring the Land Shark It is important to understand the power and signal flow of the Land Shark. The arrows indicate unidirectional or bidirectinal flow. Red indicates DC power is being supplied. When the Personal Computer is connected to the aLand Shark you do not need the 9v DC power and it should be switched off. After the Land Shark is disconnected from the Personal Computer, you should turn on the 9V DC switch. This will provide power to the Arduino which in turn will source power to the breadboard.

Land Shark Power and Signal Block Diagram

 

Personal))Computer)

with%%Arduino%

Interac0ve%Development%Environment%

%%%%

Arduino)Computer)

)))))))%%%%%

%Motor)

Controller)%

%%

)))))))Breadboard%

6V%DC%Ba;ery%

9V%DC%Ba;ery%

Le?%Motor%

Le?%Motor%

Op0cal%Encoder%

Range%Finder%

Switched%Pwr% Switched%Pwr%

Signal%

Drive%

Drive%

USB)Pwr%Signal%

Pwr%

Signal%

Signal%

Yellow%Range%Indicator%

Signal%

Green%Le?%Turn%Signal%

Pwr%

Signal%

Red%Right%Turn%Signal%

Signal%

Page 46: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

46

Make sure that all connections are in accordance with the Land Shark wiring diagram below. Step-by-step instructions follow this schematic diagram.

 Land  Shark  wiring  diagram

       

Vin$$Gnd$$Gnd$$ $$$$$$2$$$$$$$$$$$$$$$$$$3$$ $$$$$$4$$$$$$$$$$$$$$$$$$$5$$ $$$$$6$$$$$$$$$$$$$$$$$$$7$

Arduino(Computer($ $$$$$10$$$$$$$$$$$$$$$$$11$$$$

$ $$5V$$$$$$$$$$$$$Gnd$$$$$$$$$$$$$$$$$$$8$$ $$$$$$$9$$ $$$12$$ $$$$13$$$$$$$$$$$$$$$22$

$$Gnd$$$$$$$$$Vcc$Pin$1$ENA $ $L1$Pin$2$IN1 $ $L2$Pin$3$IN2$Pin$4$IN3$Pin$5$IN4 $ $R1$Pin$6$ENB $ $R2$Motor(Controller$

9V$DC$Ba=ery$

4AA$Cells$6V$Ba=ery$

3$Rt$Enc$$$$Op1cal((4$LD$Enc$$$Encoder$1$+5V$2$Gnd$

1$Vcc$$$$$$$$$Range$4$Gnd$$$$$$$$Finder(2$Trigger$3$Echo$

470$Ω$Resistor$

470$Ω$Resistor$

+$

LED$1$

+$

1$

+$

1$

Page 47: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

47

You   will   notice   that   we   are   using   separate   power   for   the   motor   controller   and   the   Arduino.  Because  of  this,  we  need  to  create  a  ground  reference  for  both  circuits.    Use  a  4”  length  of  Green  wire  and  strip  off  ¼”  from  one  end.        Start  by  connecting/taking   the  black  6V  battery  wire   thru   the  center   round  hole   in   the  chassis,  and  connecting   it   to   the  center,  GND,   terminal  of   the  motor  controller.  This   is   the   three  screw  terminal.    (GND  means  “Ground”  or  the  negative  side  of  any  battery,  generally  identified  by  being  black.)      Connect  a  green  wire  from  the  GND  terminal  of  the  Motor  Controller  to  a  GND  pin  of  the  Arduino  Mega  2560.  Next,  solder  the  red  wire  from  the  6V  battery  pack  to  the  aft  on/off  switch.    Solder  a  red  wire  to  the  other  side  of  the  switch  and  connect  to  the  left,  VCC  5V,  terminal  of  the  motor   controller.     Next,   connect   the   green   wire   from   the   motors   to   the   OUT1   and   OUT4  (forward)   terminals.     Connect   the   orange  wires   from   the  motors   to   the   OUT2   and  OUT3   (aft)  terminals.    Each  motor  should  have  two  wires  to  their  respective  sides  of  the  motor  controller.    Connect  the  9V  battery  black  wire  to  one  of  the  Arduino  Ground  pins.    Solder  a  red  wire  to  the  forward  on/off  switch.    Solder  a  red  wire  to  the  other  side  of  the  switch  and  connect  to  the  Vin  pin  on  the  Arduino  Mega  2560.    Now   we   are   going   to   install   the   ribbon   cable   between   the   Arduino,   motor   controller   optical  isolator  and  the  breadboard.      We  will   use   the   ENA   to   control   the   speed   of   our  motors.     It   will   be   connected   to   an   Arduino  Analog  (PWM)  Pin.  The  IN1  and  IN2  can  be  connected  to  any  standard  Digital  Pin  on  the  Arduino.    Your  kit  comes  with  some  Male  to  Female  cables.    We  will  need  to  connect  up  6  pins  from  the  motor   controller   to  our  Arduino.     Find  your  bundle  of   cables  and   tear  off   six   cables   from  your  bundle.  When  you  do  this,  start  the  bundle  with  the  blue  wire,  and  end  it  with  the  brown  wire.    You  will  now  have  a  cable  bundle  going  from  blue  to  brown,  and  which  will  contain  6  wires  with  pins  at  one  end  and  sockets  at  the  other.    The  sockets  will  plug  onto  the  motor  controller  and  the  pins  will  plug  into  the  I/O  Pins  on  the  Arduino.    The  motor  controller  comes  with  jumpers  on  the  ENA  and  ENB  pins.    We  will  be  removing  these  jumpers.    Connect  the  ribbon  cable  Motor  Controller  to  the  Arduino,  PWM  digital  input  pins  2-­‐7  as  follows:  In  our  bundle,  6  wires  starting  with  Brown  and  going  thru  to  Blue.  Check  that   the  ribbon  cable  connects  the  Blue  wire  to  ENA,  Green  to  IN1,  Yellow  to  IN2,  Orange  to  IN3,  Red  to  IN4  and  Brown  to  ENB.    On  the  other  end  connect  the  Blue  to  Pin  2,  Green  to  Pin  3,  Yellow  to  Pin  4,  Orange  to  Pin  5,  Red  to  Pin  6  and  Brown  to  Pin  7.    Make  sure  the  Ground  wire  is  connected  between  the  motor  controller  and  the  arduino.                    

Page 48: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

48

 

     Connect   the   4   remaining   pins   to   the   Optical   Encoder   (OE).   Connect   the   OE   pin   1   (left)   to   the  breadboard  +  bus.  Connect  the  OE  pin  2  (second  from  left)  to  the  breadboard  -­‐  bus.  Connect  the  OE  pin  3  (third  from  left)  to  the  Arduino  digital  input  pin  10.  Connect  the  OE  pin  4  (fourth  from  left)  to  the  Arduino  digital  input  pin  11.      Now  we  will  connect  the  Range  Finder  to  the  Arduino  Mega  2560.    Connect  the  5V  pin  of  the  Arduino  to  the  +  bus  of  the  breadboard.  Connect  the  PWM  Gnd  pin  to  the  –  bus  of  the  breadboard.    Connect   the   breadboard   +   bus   to   the   Range   Finder   pin   1   Vcc   (far   left   looking   aft)   and   the  breadboard  –  bus  to  the  Range  Finder  pin  4  Gnd  (far  right).  Connect  the  Arduino  PWM  pin  8  to  the  Breadboad  Range  Finder  pin  2  Trigger  (second  from  left  looking  aft).  Connect  the  Arduino  PWM  pin  9  to  the  the  Breadboad  Range  Finder  pin  3  Echo  (third  from  left  looking  aft).            

22"

8""""9""" """"""""12""13"Gnd"

Arduino"Computer"

Vin"Gnd"Gnd"5V"

Posi8ve"Bus"

Nega8ve"Bus"

470"Ω"Resistor"

LED"

Range"Finder"1" 2" 3" 4"

1"="Vcc"2"="Trigger"3"="Echo"4"="Gnd"

Page 49: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

49

Unit V – Motors, Buttons & Potentiometers This  unit  focuses  on  improving  your  understanding  and  application  of  various  types  of  actuators.    An  actuator  is  a  device  the  activates  or  causes  motion.    Actuators  can  be  mechanical  in  nature  or  electrical;  since  we  are  mostly  dealing  with  electronics  at  the  present  moment,  the  actuators  we  will  discuss  all  transmit  an  electrical  signal  to  start  or  end  motion.    Three  types  that  you  will  learn  about  are  buttons,  motors,  and  servo  motors.

                   

Motors  The  motor  controller  is  a  dual  H-­‐Bridge.    An  H-­‐bridge  is  a  device  that  is  commonly  used  to  control  DC  motors.    If  we  look  at  an  H-­‐Bridge  we  will  see  that  it  has  two  halves,  each  half  connects  to  one  lead  of  our  motor.  The  ENA  or  ‘Enable  A’  enables  power  to  the  entire  bridge.  IN1  selects  one  leg  of  the  motor  to  be  at  +V  or  Ground.    IN2  selects  the  other  leg  of  the  motor  to  be  at  +V  or  Ground.    If   IN1  and  IN2  are  both  set  the  same,  the  motor  is  turned  off.     If   IN1  is   low  and  IN2  is  high,  the  motor   turns   in   one   direction.     If   IN1   is   high   and   IN2   is   low,   the   motor   turns   in   the   opposite  direction.  

       

 

             

Page 50: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

50

Motor Control Test Sketch  First  we  need  to  tell  the  program  what  all  our  I/O  Pins  are:    

/* Define pins to motor functions */ const int ENA = 2; // This is the right motor enable const int IN1 = 3; const int IN2 = 4; const int IN3 = 5; const int IN4 = 6; const int ENB = 7; // This is the left motor enable

Next, in setup we will initialize the I/O and set our initial speed to zero:

void setup() { // All motor control pins are outputs pinMode(ENA, OUTPUT); pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT); pinMode(ENB, OUTPUT); // Speed = 0 analogWrite(ENA, 0); analogWrite(ENB, 0); // Set direction to forward digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW); }

Last, in the loop, I turn the speed up for 1 second then off for 5 seconds.

void loop(){ // Motors on analogWrite(ENA, 255); //send speed 255 to the right motor analogWrite(ENB, 255); //send speed 255 to the left motor delay(1000); // go forward 1 second // Motors off analogWrite(ENA, 0); analogWrite(ENB, 0); delay(5000); // go stop 5 second }

Page 51: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook    -­‐  Semester1.                                                                                                                                                                                                                                                                                        

51

Connect  your  Land  Shark  to  your  PC.  Load,  compile  and  save  the  above  “Motor  Control  Test”  sketch  into  the  Arduino  Integrated  Development  Environment.  Upload  the  file  to  the  Land  Shark  and  run  the  program.    Remember  if  you  have  batteries  loaded  and  the  power  switch  “on”,  the  motor  controller  turned  “on”,  as  soon  as  the  sketch  is  loaded  the  Land  Shark  will  drive  for  1  second.    Get  ready  or  turn  off  the  motor   controller.     Now   lift   the   Land   Shark’s   wheels   off   the   table   and   turn   on   the  motor  controller.   If   nothing   happens   after   several   seconds,   check   your   wiring.   If   the   motors   run  backwards  either  change  your  program,  or  the  wires  to  the  motors.     If  one  motor  runs  forward  and  the  other  backwards,  change  the  wires  to  the  motor  that  runs  backwards.    Now  we  want  to  run  the  Land  Shark  on  the  floor.     If  we  unplug  the  USB  cable  and  turn  on  the  motor  controller  power,  you  will  see  nothing  happens.  This  is  because  we  have  been  running  our  Arduino  power  from  the  USB  cable.    We  now  need  to  add  a  battery  to  our  Land  Shark.    In  your  kit  you  should  have  a  9  Volt  Battery  clip  and  a  coaxial  power  connector  that  plugs  into  your  Arduino.    You  need  to  solder  the  red  wire  to  the  center  connector  and  the  black  to  the  outside.    Remember  to  place  the  sleeve  over  the  wires  before  soldering.    Before  connecting  to  your  Arduino,  connect  the  battery  to  the  clip  and  test  with  a  volt  meter.    This  is  to  make  sure  that  the  center  conductor  is  connected  to  the  positive  side  of  the  battery.    Connect  up  the  battery  to  the  Arduino  and  place  the  Land  Shark  on  the  floor.    Now  turn  on  the  power  to  the  motor  controller  and  watch  your  Land  Shark  take  off!    It   is  now  your   job   to  create  more  Arduino  C++  code   to  make  your  Land  Shark  do  other   things.    Maybe  you  can  start  by  having  your  Land  Shark  go  forward  for  a  bit,  then  reverse.    Now  try  and  make  your  Land  Shark  do  a  figure  eight.    Try  adjusting  the  ‘speed  settings’  to  see  what  the  lowest  number   is   that   allows   the   Land   Shark   to  move.     Start   off   driving   in   a   direction   for   very   short  amounts   of   time   until   you   can   calculate   how   fast   the   car   travels.     You   don’t   want   it   always  crashing  into  obstacles.    Later,  we  will  add  sensors  to  allow  the  Land  Shark  to  sense  when  to  stop  or  turn,  and  thus  prevent  crashing,  but  for  now,  practice  safe  programming  and  use  short  delays.    

   Practice  problems:    1. Make  a  sketch  that  uses  subroutines  to  initialize  the  motor  pins  and  motor  control.    Make  the  

setup()  and  loop()  call  these  routines.  2. Make  a  sketch  that  has  a  subroutine  for  each  of  the  following:    Forward,  Backwards,  Right,  

Left  and  Stop.  Call  several  of  these  from  the  loop().  3. Make  a  sketch  that  has  a  subroutine  for  each  of  the  following:    Forward,  Backwards,  Right,  

Left  and  Stop.    Write  these  routines  to  accept  a  speed  variable  to  adjust  the  speed  of  the  motors  for  watch  routine.    Call  several  of  these  from  the  loop().  

Page 52: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook                                                                                                                                                                                                                                                                                        

52

Land Shark Turn Signals and Range Check Land Shark “Motor Test #1” Adding Tabs (subroutines) to your main program In this example we will use the main program to control the speed and duration of motor movements. We will create a Tab called “Motors” which will be used to convert motor movement commands to configure the Land Shark Motor Control Board output to the right and left motors. For eaxample, the Motor(128, 255) command will set the right motor speed to 128 and the left motor speed to 255. Delay(1000) will issue this command for one second. Each Main program has an Initialize, Setup and Loop section.

• Initialize: Constants are normally declared such as “int LED = 13” which defines the value of LED to be an integer equal to 13. However, in our Motor Test #, we are not declaring any values.

• Setup: Performed once to declare subroutines (Tabs) and other initial conditions. • Loop: This is the part of the program that runs/repeats continuously. This sketch makes our

Land Shark go forward for 1 second, spin right, spin left, go backwards, forward, and stops for 5 seconds.

Load the following into the Arduino IDE: // Land Shark Motor Test#1: Using subroutines void setup(){ setupMotors(); // Motors is the tab that sets up the Motor Control Board } void loop(){ Motor(128,128); delay(1000); /*The first number is the speed of the right wheel, the second number is the speed of the left wheel. The numbers range from 0 (stop) to 255 (full speed). In this case, both wheels are turning at half speed (128 is ½ of 256). Delay is the amount of time, in milliseconds, the motors will execute the command. 1,000 milliseconds equals 1 second. */ Motor(128,255); delay(1000); //The right wheel is half speed, the left is full speed, LS will turn to the right Motor(255,128); delay(1000); //The right wheel is full speed, the left is half speed, LS will turn to the left Motor(-255,-255); delay(1000); //The right and left wheels are full speed reverse, the LS will go backwards

Page 53: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook                                                                                                                                                                                                                                                                                        

53

Motor(255,255); delay(1000); //The right wheel is full speed, the left is full speed, LS will turn go straight Motor(0,0); delay(5000); //The right wheel is at 0 speed, the left is at 0 speed, LS will stop } Creating the Tab (subroutine) Now click on the downward pointing triangle on the right hand side of the Arduino IDE screen. Select “New Tab” and entrer the name “Motors” at the bottom of the screen. The Motors tab will open as a blank screen. Paste the following instructions into the screen. /* Motors tab Define pins to motor functions */ const int ENA = 2; const int IN1 = 3; const int IN2 = 4; const int IN3 = 5; const int IN4 = 6; const int ENB = 7; // Initialize code for the motors in a setup routine: void setupMotors() { // All motor control pins are outputs pinMode(ENA, OUTPUT); pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT); pinMode(ENB, OUTPUT); // Speed = 0 analogWrite(ENA, 0); analogWrite(ENB, 0); } /* We are going to start off by making a subroutine to handle controlling the motors. To make this routine useful, we must design it first. We have two motors and each can go from -255 to +255. Remember that 255, either positive or negative, is the maximum value we can assign to the motor power. The motor direction is the positive or negative. First, we will define the parameters that we will pass to our routine. We designed this routine to allow us to pass the speed for the left and right motor as follows: */

Page 54: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook                                                                                                                                                                                                                                                                                        

54

void Motor(int Right, int Left) { analogWrite(ENA, abs(Right)); // Set Speed, remove direction analogWrite(ENB, abs(Left)); if(Right < 0) { // Right Reverse digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); }else{ // Right Forward digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); } if(Left < 0) { // Left Reverse digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH); }else{ // Left Forward digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW); } } /* Everything up to here we’ve done before, but this is a little different. We’ll make one routine to handle both motors. Like we talked about before, we want to pass the routine a speed of between -255 and +255 for each motor. The first thing is we accept two variables, int’s to our routine, Right and Left. Then to get the speed, we take the absolute value using the abs() function of them and write them out to our motor controllers. analogWrite(ENA, abs(Right)); // Set Speed, remove direction analogWrite(ENB, abs(Left)); Next we check to see if Right is negative, and if so, we tell that motor to go backwards, if not (else) we tell that motor to go forward. We do the exact same thing for the Left motor. */

Page 55: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook                                                                                                                                                                                                                                                                                        

55

Motor(128,255); Which way will the car turn, left or right?

Motor(-255,-255); Which way will the car go? For the next three programs, uou will utilize the Arduino Mega 2680 and the Breadboard installed on the Land Shark in Unit IV.

22"

8""""9""" """"""""12""13"Gnd"

Arduino"Computer"

Vin"Gnd"Gnd"5V"

Posi8ve"Bus"

Nega8ve"Bus"

470"Ω"Resistor"

LED"

Range"Finder"1" 2" 3" 4"

Page 56: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook                                                                                                                                                                                                                                                                                        

56

/* Land Shark Motor Test #2: Using subroutine Blink Right and Turn Right The additional commands are highlighted in red */ //Initialize the output pin to light the green LED will be pin 13 int ledrt = 13; void setup(){ // Initialize the digital pin as an output. You must declare ledrt (13) to be an OUTPUT pinMode(ledrt, OUTPUT); setupMotors(); } void loop(){ Motor(128,128); // Flash the right LED 10 times to indicate a right turn for(int i=0; i< 10; i++) { // This “for loop” will execute 10 times digitalWrite(ledrt, HIGH); // turn the LED on (HIGH is the voltage level) delay(100); // turn on the red LED for 0.1 seconds each time // digitalWrite(ledrt, LOW); // turn the LED off by making the voltage LOW delay(100); // turn off the red LED for 0.1 seconds each time } Motor(128,255); // The LS is turning to the right because left wheel is faster digitalWrite(ledrt, HIGH); // turn the LED on steady by making the voltage HIGH delay(2000); // turn on the green LED for 2 seconds one time digitalWrite(ledrt, LOW); // turn the LED off by making the voltage LOW Motor(255,128); Motor(-255,-255); delay(1000); Motor(255,255); delay(1000); Motor(0,0); delay(5000); } /* This sketch makes our Land Shark go forward for 1 second, turn right, turn left, go backwards, forward, and stops for 5 seconds. The red light will blink for two seconds and then be on steady for two seconds */

Page 57: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook                                                                                                                                                                                                                                                                                        

57

/* Land Shark Motor Test #3 Blink Right, Turn Right and Left The additional commands are highlighted in blue */ int ledrt = 13; int ledlft = 12; void setup(){ // initialize the digital pin as an output. pinMode(ledrt, OUTPUT); pinMode(ledlft, OUTPUT); Serial.begin(9600); setupMotors(); } void loop(){ Motor(128,128); for(int i=0; i< 10; i++) { digitalWrite(ledrt, HIGH); // turn the LED on (HIGH is the voltage level) delay(100); digitalWrite(ledrt, LOW); // turn the LED off by making the voltage LOW delay(100); } Motor(128,255); digitalWrite(ledrt, HIGH); // turn the LED on by making the voltage HIGH delay(2000); digitalWrite(ledrt, LOW); // turn the LED off by making the voltage LOW Motor(255,128); digitalWrite(ledlft, HIGH); // turn the green LED on by making the voltage HIGH delay(2000); digitalWrite(ledlft, LOW); // turn the green LED off by making the voltage LOW Motor(-255,-255); delay(1000); Motor(255,255); delay(1000); Motor(0,0); delay(5000); } /* This sketch makes our Land Shark go forward for 1 second, spin right, spin left, go backwards, forward, and stops for 5 seconds. */

Page 58: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook                                                                                                                                                                                                                                                                                        

58

/* Land Shark Motor Test#4 Check Range, Turn Right and Left, The additional commands are highlighted in green. Install a 100 ohm resistor and yellow LED behind the Range Finder on the Land Shark breadboard. Connect the Arduino pin 22 to the resistor. */ int ledrt = 13; int ledlft = 12; const int trigger = 8; const int echo = 9; // Pin 13 has an LED connected on most Arduino boards, give it a name: int led = 22; void setup(){ // initialize the digital pin as an output. pinMode(ledrt, OUTPUT); pinMode(ledlft, OUTPUT); Serial.begin(9600); pinMode(trigger, OUTPUT); pinMode(echo, INPUT); // initialize the digital pin as an output. pinMode(led, OUTPUT); setupMotors(); } void loop(){ Motor(128,128); for(int i=0; i< 10; i++) { Range(); } Motor(128,255); digitalWrite(ledrt, HIGH); // turn the LED on by making the voltage HIGH delay(2000); digitalWrite(ledrt, LOW); // turn the LED off by making the voltage LOW Motor(255,128); digitalWrite(ledlft, HIGH); // turn the LED on by making the voltage HIGH delay(2000); digitalWrite(ledlft, LOW); // turn the LED off by making the voltage LOW Motor(-255,-255); delay(1000); Motor(255,255); delay(1000); Motor(0,0); delay(5000); } /* This sketch makes our Land Shark go forward for 1 second, turn right, turn left, go backwards, forward, and stops for 5 seconds. */

Page 59: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook                                                                                                                                                                                                                                                                                        

59

void Range(){ long duration, inches; digitalWrite(trigger, LOW); delayMicroseconds(2); digitalWrite(trigger, HIGH); delayMicroseconds(5); digitalWrite(trigger, LOW); duration = pulseIn(echo, HIGH); inches = duration / 74 / 2; Serial.print("The range = "); Serial.print(inches); Serial.println(" inches"); delay(200); if (inches < 20) blink(); else digitalWrite(led, LOW); // turn the LED off by making the voltage LOW } void blink() { digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) }

Page 60: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook                                                                                                                                                                                                                                                                                        

60

Plotting a Course  In   the   last   lesson  we   controlled   the  motor  with   inline   code.     If   we  wanted   to  move   the   Land  Shark,  we  entered  a  list  of  commands  that  controlled  the  motors.    That  is  perfectly  OK  for  testing  things  out,  but  we  are  just  building  a  base  for  more  complicated  programming.    If  we  just  keep  on  going  this  way  we  will  end  up  with  a  very  long  program  that  is  not  easy  to  follow.  Good  code  writers   always   try   to  write   short   concise   code.     They  also   try   to   reuse   segments  of  code  that  they  have  already  written.    We  can  copy  segments  or  “routines”  from  one  program  to  another,  and   then  use   them  over  and  over.    We  can  also  put  our   common  routines   into  a   file,  save  them  and  then   include  them  into  other  programs  when  we  need  them.    What  you  will  be  doing  is  creating  a  “library”  for  yourself,  and  you  can  use  that  library  for  thousands  of  different  designs  that  you  will  create  as  a  programmer.    To  create  a  file  that  is  automatically  included  we  add  another  tab  to  our  program.  To  get  started,  open  the  Arduino  IDE.    Now  click  on  the  down  arrow  on  the  right  side  of  the  window,  just  below  the  Serial  Monitor.  Click  on  New  Tab  and  enter  the  file  name:  motors.    This  will  create  a  new  file  and  tab  for  you  to  put  your  code  into.  

   

             

Page 61: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook                                                                                                                                                                                                                                                                                        

61

Within   this   file   we   will   put   everything   to   do   with   our   motors.   To   start,   we   will   write   the  definitions  of  the  I/O  Pins:  

// Define pins to motor functions const int ENA = 2; const int IN1 = 3; const int IN2 = 4; const int IN3 = 5; const int IN4 = 6; const int ENB = 7;  

Then  our  initiation  code  for  the  motors  in  a  setup  routine:      void setupMotors() { // All motor control pins are outputs pinMode(ENA, OUTPUT); pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT); pinMode(ENB, OUTPUT); // Speed = 0 analogWrite(ENA, 0); analogWrite(ENB, 0); }

We  are  going  to  start  off  by  making  a  subroutine  to  handle  controlling  the  motors.    To  make  this  routine  useful,  we  must  design  it  first.    We  have  two  motors  and  each  can  go  from  -­‐255  to  +255.    Remember   that   255,   either   positive   or   negative,   is   the   maximum   value   we   can   assign   to   the  motor   power.     The   motor   direction   is   the   positive   or   negative.   First,   we   will   define   the  parameters   that  we  will   pass   to  our   routine.  We  designed   this   routine   to   allow  us   to   pass   the  speed  for  the  left  and  right  motor  as  follows:        

void Motor(int Left, int Right) { analogWrite(ENA, abs(Left)); // Set Speed, remove direction analogWrite(ENB, abs(Right)); if(Left < 0) { // Left Reverse digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); }else{ // Left Forward digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); } if(Right < 0) { // Right Reverse digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH); }else{ // Right Forward digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW); } }

Page 62: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook                                                                                                                                                                                                                                                                                        

62

 Everything  up  to  here  we’ve  done  before,  but  this  is  a  little  different.    We’ll  make  one  routine  to  handle   both   motors.     Like   we   talked   about   before,   we   want   to   pass   the   routine   a   speed   of  between  -­‐255  and  +255  for  each  motor.    The  first  thing   is  we  accept  two  variables,   int’s  to  our  routine,   Left   and   Right.     Then   to   get   the   speed,   we   take   the   absolute   value   using   the   abs()  function  of  them  and  write  them  out  to  our  motor  controllers.    

 analogWrite(ENA, abs(Left)); // Set Speed, remove direction analogWrite(ENB, abs(Right));

Next  we  check  to  see  if  Left  is  negative,  and  if  so,  we  tell  that  motor  to  go  backwards,  if  not  (else)  we  tell  that  motor  to  go  forward.    We  do  the  exact  same  thing  for  the  Right.      All  this  was  done  in  the  Motors  tab.    Now  switch  back  to  the  Land  Shark  (main)  tab.    We  need  to  insert  our  setup()  and  loop()  routines.     In  the  setup()  routine  we  will  call  our  new  setupMotor()  routine  to  initialize  our  I/O  for  the  motor  controller.    In  the  loop()  section  we  will  call  our  Motor()  routine.    Here  is  an  example  of  what  you  can  do:  

 /* Land Shark Motor Test Using subroutines */ void setup(){ setupMotor(); } void loop(){ Motor(255,255); delay(1000); Motor(-255,255); delay(1000); Motor(255,-255); delay(1000); Motor(-255,-255); delay(1000); Motor(255,255); delay(1000); Motor(0,0); delay(5000); }

This   sketch  makes  our   Land  Shark  go   forward   for  1   second,   spin   right,   spin   left,   go  backwards,  forward,  and  stops  for  5  seconds.    It’s  now  time  to  test  our  code.    Upload  the  sketch  to  your  Land  Shark  by  clicking  the  RUN  Button.  Disconnect  the  USB  cable  after  the  upload  is  finished,  plug  in  a  battery,  move  to  an  open  space  and  turn  on  the  motor  controller  board.    You  should  also  press  the  reset  button  if  you  want  to  make  sure  you  sketch  starts  from  the  beginning.    

Page 63: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook                                                                                                                                                                                                                                                                                        

63

Measuring the Speed of the Land Shark Each  wheel/motor  has  a  slotted  disk  and  a  sensor  to  allow  us  to  see  how  fast  or  far  that  axle  has  turned.  On  one  side  of  the  slotted  disk  is  an  Infrared  LED  and  on  the  other  side  is  a  Infrared  detector.  As  the  disk  turns  the  beam  of  light  is  allowed  through  or  blocked.  We  can  use  the  Arduino  to  count  these  interruptions  or  pulses.    The  disk  has  20  slots  and  if  we  count  each  transition  from  high  to  low  and  low  to  high  we  get  40  counts.  The  wheels  are  just  about  2.6"  in  diameter.  This  gives  us  a  circumference  of  just  over  8".  This  means  we  can  accurately  move  the  car  within  .2"  or  about  5  mm.    We  have  already  hooked  up  the  sensor  board  on  our  Land  Shark  so  now  we  need  a  little  programming:  //  Tack  input  pins  #define  rTach  10  #define  lTach  11    //  Variable  to  store  the  Tach  reading  and  counts  values  int  rLast  =  0;  int  lLast  =  0;  int  rCount  =  0;  int  lCount  =  0;    void  setup()  {      pinMode(rTach,  INPUT);      pinMode(lTach,  INPUT);      Serial.begin(9600);  }  We  define  the  pins  that  are  connected  to  the  Tachometer  Sensor  Board.  Then  in  setup()  we  program  these  pins  as  inputs.  Note  that  I  also  setup  the  Serial  for  monitoring  at  9600.    void  loop()  {      int  rCurrent  =  digitalRead(rTach);      if(rCurrent  !=  rLast)      {          rCount++;          rLast  =  rCurrent;      }      int  lCurrent  =  digitalRead(lTach);      if(lCurrent  !=  lLast)      {          lCount++;          lLast  =  lCurrent;      }  

Page 64: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook                                                                                                                                                                                                                                                                                        

64

   Serial.print("Right:  ");      Serial.print(rCount);      Serial.print(",  Left:  ");      Serial.println(lCount);  }    In  loop()  we  check  the  right  sensor  to  see  if  it  has  changed  and  if  so  we  increment  the  counter.  Note  that  we  count  whenever  the  input  changes  so  we  will  get  both  edges  of  the  pulse.  With  the  40  slot  disk  we  will  get  40  pulses.  We  also  do  the  same  for  the  left  sensor.  After  that  we  print  the  counter  values  to  the  serial  monitor.    Upload  this  sketch  and  you  should  see  the  values  change  if  you  turn  a  wheel.      

Speed  Test    We  have  done  the  Tachometer  and  also  introduced  you  to  the  Tachometer  with  PCI  (Pin  Change  Interrupt).  That  is  all  fine  and  dandy  but  what  we  really  need  is  the  current  speed.  So  that  is  today's  lesson.    To  start  we  need  to  decide  how  often  we  want  to  check  the  speed.  For  this  example  we  will  use  our  scheduled  task  method  and  check  every  1/10th  of  a  second.  //  Last  times  for  scheduling  tasks  unsigned  long  LastSpeedTime;  const  int  SpeedTime  =  100;    void  Speed()  {      if((LastSpeedTime  +  SpeedTime)  <  millis())      {          LastSpeedTime  =  millis();          //  Calculate  current  speed  (every  .1  seconds)    If  you  remember  scheduled  tasks  you  will  recognize  the  above  code.  We  see  if  the  current  time  (millis())  is  greater  than  last  time  plus  our  .1  second  value.  If  so,  we  set  the  Last  time  with  the  current  time  and  execute  our  code,  else  we  just  return.    With  this  we  will  read  the  interrupt  counters  for  each  wheel  ten  times  a  second.  I  move  both  of  the  current  counters  into  a  temporary  storage.      I  use  an  array  to  store  the  last  10  samples.  I  always  insert  the  latest  value  into  the  end,  at  cell  9  but  first  I  shift  all  the  values  down  one  and  overwrite  cell  0.          r_tmp_cnt  =  r_int_cnt;          l_tmp_cnt  =  l_int_cnt;          for(int  i=1;i<10;i++)          {  

Page 65: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook                                                                                                                                                                                                                                                                                        

65

           rSpdAvg[i-­‐1]  =  rSpdAvg[i];              lSpdAvg[i-­‐1]  =  lSpdAvg[i];          }    We  have  one  problem,  the  counter  only  counts  to  255  and  then  wraps  to  0,  an  8  bit  counter.  You  can  use  the  following  code  with  two  if-­‐else  statements  like  this:          if(r_lst_cnt  >  r_tmp_cnt)          {              rSpeed  =  (r_tmp_cnt  +  256)  -­‐  r_lst_cnt;          }          else          {              rSpeed  =  r_tmp_cnt  -­‐  r_lst_cnt;          }          if(l_lst_cnt  >  l_tmp_cnt)          {              lSpeed  =  (l_tmp_cnt  +  256)  -­‐  l_lst_cnt;          }          else          {              lSpeed  =  l_tmp_cnt  -­‐  l_lst_cnt;          }    I  do  the  same  for  left  and  right,  check  to  see  if  the  last  reading  is  greater  then  the  current  one,  meaning  it  has  wrapped,  and  if  so  I  adjust  the  current  by  adding  255  to  our  current  reading  and  then  subtracting  the  last  count.  If  not,  I  just  subtract  the  last  count  from  the  last  sample.  There  must  be  100  ways  to  do  this,  and  here  is  a  more,  maybe  cryptic  way,  but  it  will  teach  you  a  new  method  of  checking  and  assigning  values  based  on  a  condition.    rSpdAvg[9]  =  (r_lst_cnt  >  r_tmp_cnt)?((r_tmp_cnt  +  256)  -­‐  r_lst_cnt):(r_tmp_cnt  -­‐  r_lst_cnt);  lSpdAvg[9]  =  (l_lst_cnt  >  l_tmp_cnt)?((l_tmp_cnt  +  256)  -­‐  l_lst_cnt):(l_tmp_cnt  -­‐  l_lst_cnt);    The  next  thing  to  do  is  calculate  the  average  of  the  last  10  samples.  We  can  change  this  to  less  if  we  want  to  average  over,  for  example  .5  seconds  rather  than  1  second,  but  here  we  are  going  for  the  whole  second.          r_lst_cnt  =  r_tmp_cnt;          l_lst_cnt  =  l_tmp_cnt;          r_tmp_cnt  =  0;          l_tmp_cnt  =  0;          for(int  i=0;i<10;i++)          {              r_tmp_cnt  +=  rSpdAvg[i];              l_tmp_cnt  +=  lSpdAvg[i];          }          rSpeed  =  r_tmp_cnt  /  10;          lSpeed  =  l_tmp_cnt  /  10;  

Page 66: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook                                                                                                                                                                                                                                                                                        

66

 First  we  save  our  temporary  count  to  our  last  counter  variable  for  the  next  time  around.  Now  to  calculate  the  average,  we  add  all  the  cells  of  the  array  and  then  divide  by  the  number  of  cells.    We  loop  through  all  the  cells,  adding  them  to  our  temporary  variable  and  finally  we  do  the  divide  and  put  the  results  in  rSpeed  and  lSpeed.  I  also  display  the  current  speed          displaySpeed();      }  }    Now  we  need  to  call  this  as  often  as  possible,  and  it  will  do  some  work  if  needed  and  print  out  our  current  speed.  I  am  using  the  skeleton  of  our  Land  Shark  Sketch  so  here  is  the  main  code  page:  #include  <arduino.h>    #include  "Land_Shark_PID_Ping.h"    void  setup()  {      setupDisplay();      setupHeartBeat();      setupTach();  /*      setupMotor();      setupRoute();      setupPing();      displayTemplate();    */  }    void  loop()  {      Speed();      HeartBeat();  /*      checkPing();      Route();    */  }    You  will  notice  there  are  calls  to  many  routine  commented  out,  we  will  enable  these  later  on.      

Page 67: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook                                                                                                                                                                                                                                                                                        

67

Buttons

   

The  push  button  is  used  to  input  to  a  digital  input  into  our  Arduino.    The  push  button  has  four  pins  but  the  pins  on  each  side  are  connected  and  the  switch  connects  both  sides  together.    Here  is  the  diagram:        

   Not  that  pins  1  and  2  are  connected  internally  and  so  are  pins  3  and  4.    To  prepare  for  these  lessons,  you  may  want  to  watch  this  video.  Tutorial 02 for Arduino: Buttons, PWM, and Functions (20 min 08 sec) http://www.youtube.com/watch?v=_LCCGFSMOr4                    

Page 68: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook                                                                                                                                                                                                                                                                                        

68

Here  is  our  test  setup:              

     

 You  can  get  this  sketch  from  the  IDE  Examples  under  Digital  and  called  Button.  

 

Digital PWM Pin 2

Page 69: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook                                                                                                                                                                                                                                                                                        

69

We  start  off  like  normal  by  defining  some  pins  for  the  switch  and  the  LED.  We  also  create  a  variable  to  store  the  current  button  state.    

const int buttonPin = 2; // the number of the pushbutton pin const int ledPin = 13; // the number of the LED pin int buttonState = 0; // variable for reading the pushbutton status

 In  setup()  we  setup  our  Pin  for  the  LED  as  an  output  and  the  button  pin  for  input.    

void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); }

 In  our  loop()  we  first  read  the  input  into  our  button  variable.    Next  we  have  an  if()  statement  that  tests  the  value  of  the  button  variable.    If  the  button  is  open  or  not  pushed,  then  the  10K  resistor  pulls  up  the  input  pin  and  we  get  a  HIGH.    So  if  the  button  is  not  pushed  the  LED  will  be  lit.    When  the  button  is  pressed,  it  pulls  the  input  to  ground  and  we  read  in  a  LOW.    Note  here  that  we  wired  our  switch  different  than  the  example,  so  we  expect  our  results  are  reversed.          

void loop(){ // read the state of the pushbutton value: buttonState = digitalRead(buttonPin); // check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (buttonState == HIGH) { // turn LED on: digitalWrite(ledPin, HIGH); } else { // turn LED off: digitalWrite(ledPin, LOW); } }

 With  this  example  there  is  something  we  may  not  see.    When  we  push  the  button  it  does  not  actually  switch  directly  from  high  to  low.    If  we  look  close,  we  would  see  the  switch  actually  close  and  open  several  times  before  it  stabilizes.    Because  we  are  just  lighting  an  LED  this  is  fine,  our  eyes  cannot  see  the  LED  flicker.    Later  we  will  want  to  use  the  button  to  modify  the  function  of  our  programs  as  this  ‘bounce’  may  cause  difficulty  because  the  Arduino  will  think  we  are  pressing  the  button  many  times.    Let’s  modify  this  sketch  to  print  out  the  sensorValue  to  our  Serial  Monitor.    Then  we  can  see  what  is  actually  happening.  You  will  see  the  modifications  in  GREEN.    

Page 70: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook                                                                                                                                                                                                                                                                                        

70

const int buttonPin = 2; // the number of the pushbutton pin const int ledPin = 13; // the number of the LED pin int buttonState = 0; // variable for reading the pushbutton status void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); // initialize serial communications at 9600 bps: Serial.begin(9600); } void loop(){ // read the state of the pushbutton value: buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (buttonState == HIGH) { // turn LED on: digitalWrite(ledPin, HIGH); } else { // turn LED off: digitalWrite(ledPin, LOW); } // print the results to the serial monitor: Serial.print("button = " ); Serial.println(buttonState); }

As  you  see,  the  modifications  were  done  in  GREEN  for  ease  in  modifying  your  sketch.    Upload  this  and  open  the  Serial  Monitor.    You  should  see  a  stream  of  button  status  messages.  When  you  press  the  button  you  will  see  the  status  change  from  1  to  0  and  back  a  few  times  before  settling  on  0.    The  same  happens  when  you  release  the  button.    As  you  have  probably  guessed,  we  have  a  solution  for  this.    In  the  IDE,  under  the  examples  in  the  Digital  section,  there  is  a  sample  debounce  and  here  it  is:    

// constants won't change. They're used here to // set pin numbers: const int buttonPin = 2; // the number of the pushbutton pin const int ledPin = 13; // the number of the LED pin // Variables will change: int ledState = HIGH; // the current state of the output pin int buttonState; // the current reading from the input pin

Page 71: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook                                                                                                                                                                                                                                                                                        

71

int lastButtonState = LOW; // the previous reading from the input pin // the following variables are long's because the time, measured in miliseconds, // will quickly become a bigger number than can be stored in an int. long lastDebounceTime = 0; // the last time the output pin was toggled long debounceDelay = 50; // the debounce time; increase if the output flickers

 First  we  define  the  LED  and  Button  pins.    We  also  create  variables  for  the  LED  state,  Button  state  and  one  more  which  is  the  Last  Button  state.    Last  state,  as  it  sounds,  is  where  we  store  the  last  known  state  of  the  button.    Then  there  are  two  more  variables  that  are  used  for  debouncing,  “debounceDelay”  is  how  long  we  want  to  wait  after  we  first  see  the  button  change  before  we  verify  that  it  is  really  changed.    Then  there  is  “lastDebounceTime”  where  we  store  the  actual  time  when  we  detect  the  last  change  of  the  button.    

void setup() { pinMode(buttonPin, INPUT); pinMode(ledPin, OUTPUT); }

 In  setup  we  just  set  the  LED  pin  for  output  and  the  button  for  input.    

void loop() { // read the state of the switch into a local variable: int reading = digitalRead(buttonPin); // check to see if you just pressed the button // (i.e. the input went from LOW to HIGH), and you've waited // long enough since the last press to ignore any noise: // If the switch changed, due to noise or pressing: if (reading != lastButtonState) { // reset the debouncing timer lastDebounceTime = millis(); } if ((millis() - lastDebounceTime) > debounceDelay) { // whatever the reading is at, it's been there for longer // than the debounce delay, so take it as the actual current state: buttonState = reading; } // set the LED using the state of the button: digitalWrite(ledPin, buttonState); // save the reading. Next time through the loop, // it'll be the lastButtonState: lastButtonState = reading; }

Page 72: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook                                                                                                                                                                                                                                                                                        

72

We  loop  till  there  is  no  more  change  to  the  button  and  the  difference  between  the  current  time  and  lastDebounceTime  is  greater  than  debounceDelay.    This  means  that  the  button  is  stable  for  at  least  the  amount  of  time  that  we  think  is  enough  to  say  the  button  status  is  valid.    

               Practice  problems:    1. Make  a  sketch  that  requires  a  press  of  the  button  for  the  LED  to  change  state.    Press  it  again  

and  the  LED  changes  again.  Push  on,  Push  off.  2. Make  a  sketch  that  requires  a  press  of  the  button  twice  for  the  LED  changes  state.    Press  it  

again  twice  and  the  LED  changes  again.    Push  “push  on”,  Push  “push  off”.  3. Make  a  sketch  that  requires  a  press  of  the  button  for  at  least  three  seconds  to  turn  on  the  

LED.    After  the  LED  has  been  on  for  10  seconds  it  turns  itself  off.    If  the  button  is  pressed  again  during  that  time,  it  will  extend  the  time  the  LED  stays  on.  

4. Make  a  sketch  that  requires  a  press  of  the  button  for  at  least  three  seconds  to  turn  on  the  LED.    After  the  LED  has  been  on  for  10  seconds  it  turns  itself  off.    If  the  button  is  pressed  again  during  that  time,  it  will  not  extend  the  time  the  LED  stays  on.  

5. Setup  two  buttons  and  two  LEDs.    Make  a  sketch  that  determines  which  button  is  pressed  first  and  lights  the  LED  for  that  button  and  stays  lit  for  5  seconds.  

6. Setup  two  buttons  and  two  LEDs.    Make  a  sketch  that  determines  which  button  is  pressed  first  and  lights  the  LED  for  that  button  and  stays  lit  for  5  seconds.    If  you  have  ever  watched  the  TV  show  Jeopardy,  you  will  know  that  you  are  creating  real  world  applications  with  your  Arduino.    Congratulations!  

7. Using  the  Sketch  in  #6,  keep  track  of  how  many  wins  each  side  has,  and  when  one  side    reaches  10,  start  flashing  that  LED  until  the  Arduino  is  reset.  

   

Page 73: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook                                                                                                                                                                                                                                                                                        

73

Potentiometer The  Arduino  Mega2560  has  16  analog  inputs.    So  far  we  have  only  used  the  Digital  inputs.    The  digital  input  is  either  a  1  or  a  0.    If  the  input  is  high  or  5  volts,  it  is  read  in  as  a  1.    If  the  input  is  low  or  0  volts,  it  is  read  in  as  a  0.    Actually  there  is  a  threshold  of  about  1  volt  that  if  the  input  is  above  that,  the  input  is  a  1,    and  below  you  get  a  0.   To prepare for the next sections you may want to view this video. Tutorial  04  for  Arduino:  Analog  Inputs  (18  min  17  sec) http://www.youtube.com/watch?v=js4TK0U848I  With  the  Analog  input  we  actually  read  the  values  that  range  between  0  and  5  volts.    When  we  read  an  analog  input  we  get  back  a  10  bit  value,  ranging  from  0  to  1023.  We  use  the  analogRead()  function  to  read  the  range  of  analog  values.    For  our  first  experiment  with  analogRead(),    we  will  need  an  analog  voltage.    We  will  use  a  10K  potentiometer.      

                                         

The  potentiometer  has  three  pins,  typically  labeled  1,  2  and  3.    The  two  outside  pins,  1  and  3,  are  each  the  end  of  a  resistor,  and  the  center  is  a  wiper  that  moves  from  one  end  of  the  resistor  to  the  other  as  we  turn  the  potentiometer.    If  we  turn  the  pot  to  full  right,  the  reading  from  the  center  pin  to  the  right  hand  pin  should  be  about  0  ohms.    Doing  the  same  from  the  center  pin  to  the  left  hand  pin  should  be  about  10K.    As  we  turn  the  potentiometer  shaft,    the  values  will  change  in  proportion  to  the  position  of  the  shaft  until  we  reach  the  other  end.      At  this  point,  the  values  are  reversed.      We  will  connect  the  center  pin  to  the  Arduino  analog  input  0  (A0),  pin  1  to  +5  Volts  and  pin  3  to  GND.    

       

Page 74: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook                                                                                                                                                                                                                                                                                        

74

Here  is  an  example  of  how  to  connect  up  the  pot  and  an  Arduino.    The  red  wire  is  +5,  the  black  wire  is  GND  and  the  yellow  wire,  the  wiper,  is  connected  to  A0.  

     

   Here  is  an  example  sketch  that  comes  with  the  Arduino  IDE  called  AnalogInput.    In  this  sketch  the  value  of  the  analog  input  us  used  to  set  the  delay  of  a  blinking  LED.    First  we  define  a  few  pins  on  the  Arduino  and  a  storage  variable  for  the  reading.    We  are  using  A0  for  the  analog  input  and  pin  13  for  the  LED.    Pin  13  is  connected  to  the  onboard  LED  of  the  Arduino,  so  no  wiring  is  required  for  the  LED.    

int sensorPin = A0; // select the input pin for the potentiometer int ledPin = 13; // select the pin for the LED int sensorValue = 0; // variable to store the value coming from the sensor

 Next  in  setup  we  define  the  ledPin  as  an  output.  

void setup() {

Page 75: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook                                                                                                                                                                                                                                                                                        

75

// declare the ledPin as an OUTPUT: pinMode(ledPin, OUTPUT); }

Finally,  in  the  loop  we  read  the  analog  signal,  turn  on  the  LED,  wait  the  number  of  milliseconds  times  the  value  of  our  analog  signal,  turn  off  the  LED,  wait  the  number  of  milliseconds  times  the  value  of  our  analog  signal,  then  repeat.      

void loop() { // read the value from the sensor: sensorValue = analogRead(sensorPin); // turn the ledPin on digitalWrite(ledPin, HIGH); // stop the program for <sensorValue> milliseconds: delay(sensorValue); // turn the ledPin off: digitalWrite(ledPin, LOW); // stop the program for for <sensorValue> milliseconds: delay(sensorValue); }

 Practice  problems:    1. Make  a  sketch  that  adjusts  the  frequency  of  the  LED  from  ten  times  per  second  to  once  per  

10  seconds  with  the  pot.  2. Make  a  sketch  that  lights  the  LED  when  the  pot  is  at  a  certain  position  or  higher,  and  if  off,  

when  the  pot  is  below  that  position.  3. Make  a  sketch  that  lights  the  LED  when  the  pot  is  at  a  certain  position,  and  off  all  other  times.  4. Add  a  speaker.    Make  a  sketch  that  adjusts  the  frequency  (tone)  of  a  speaker  with  the  pot.  5. Add  another  pair  of  resistors  as  a  divider  and  connect  to  A1.    Make  a  sketch  that  compares  

the  input  of  the  pot  and  the  other  resistors,  and  lights  the  LED  when  they  are  about  equal.  

Servo Motors                  The  Hobby  Servo  is  a  device  that  provides  us  with  motion  control  and  is  easy  to  control  with  the  Arduino.  The  servos  come  in  all  different  sizes,  power,  and  use  a  common  control  method.    They  were  originally  designed  for  Radio  Control  airplanes  and  are  now  used  in  all  remote  controlled  hobby  devices  and  robots.    

Page 76: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook                                                                                                                                                                                                                                                                                        

76

 The  Hobby  Servo  contains  a  motor,  gears,  shaft,  shaft  position  sensor  and  drive  electronics.  Most  Hobby  Servos  run  on  6  Volts  DC  but  operate  just  fine  on  5  Volts  also.    If  we  look  at  the  specs  then  you  will  see  they  are  a  little  slower  and  cannot  push  as  hard  on  5  Volts  as  with  6  Volts.    The  servo  requires  +  power,  ground  and  a  control  signal.    We  send  a  signal  over  the  control  line  to  tell  the  servo  what  position  we  want  the  shaft  to  be  in.    The  electronics  use    a  shaft  sensor  to  detect  the  current  position  of  the  shaft.    Then  the  electronics  power  the  motor  to  correct  any  error  in  the  current  position  compared  to  the  requested  position.    Once  the  shaft  sensor  detects  that  the  shaft  is  in  the  requested  position,    the  electronics  hold  the  motor  in  that  position.    To prepare for the next sections you may want to view this video. (17 min 04 sec) Tutorial  05  for  Arduino:  Motors  and  Transistors http://www.youtube.com/watch?v=5bHPKU4ybHY  On  our  project  we  will  be  using  two  different  size  servos.  The  first  is  a  Micro  Servo  that  is  very  small  and  provides  9  Grams  of  torque.  This  servo  will  be  used  to  allow  us  to  scan  up  to  180°  with  our  sensors.  The  second  servo  is  a  little  larger  and  we  will  use  to  control  our  ‘gate’  that  will  hold  our  submarine,  the  Sea  Squid,  in  place  when  it  is  docked  to  the  Sea  Hawk.      The  control  signal  to  the  servo  requires  a  special  pulse  to  be  repeated  at  a  minimum  of  once  every  40ms,  but  typically  every  10  to  20ms.    This  pulse  has  a  width  of  1  to  2ms.    At  1ms  the  shaft  is  positioned  at  0°.    With  a  pulse  of  2ms  the  shaft  will  be  at  180°.    When  the  pulse  is  at  1.5ms,  the  shaft  will  be  at  the  90°  position.                  Example  of  a  Pulse  Width  Modulated  (PWM)  Signal  This  pulse  is  called  Pulse  Width  Modulation  or  PWM  signal  for  short.    The  Arduino  has  many  PWM  pins  and  we  can  use  the  Analog  Write  command  to  control  them.    The  only  problem  is  the  Analog  Write  creates  a  PWM  signal  that  goes  from  0%  to  100%  duty  cycle  that  is  not  compatible  with  the  servo  control  line.    Luckily  there  is  a  library  included  with  the  Arduino  IDE  that  supports  the  Hobby  Servo.    The  library  is  the  Servo  library  and  we  include  it  using  the  following:   #include <Servo.h>  First  we  will  need  to  connect  the  servo  to  our  Arduino.    For  this  ‘test’  we  will  use  the  Micro  Servo  and  we  can  power  it  from  the  Arduino.    Because  the  servo  has  a  motor  that  can  draw  a  lot  of  current,  when  we  actually  use  the  servo  to  do  work,  we  will  want  to  power  it  from  a  separate  power  supply  and  not  from  the  Arduino.    The  regulator  on  the  Arduino  can  only  supply  about  500  mA  for  the  Arduino  and  everything  you  connect  to  it.    

Page 77: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook                                                                                                                                                                                                                                                                                        

77

Make  sure  that  the  Arduino  is  not  powered  up  and  is  first  disconnected  from  the  computer  before  connecting  your  wires  as  follows.    Connect  the  Black  (Brown)  wire  of  the  servo  to  the  GND  pin  of  the  Arduino.    Connect  the  Red  wire  from  the  servo  to  the  5V  pin  of  the  Arduino.    The  sample  sketch  used  pin  9  as  the  control  signal  to  the  servo,    so  we  will  connect  the  Orange  (Yellow)  wire  from  the  servo  the  pin  9  of  the  Arduino.  

 Example  of  connecting  a  servo  to  the  Arduino  using  the  Solderless  Breadboard.  

 This  is  the  example  code  that  is  included  with  the  Arduino  IDE,  and  can  be  loaded  by  selecting  File  àExamples  à  Servo  à  Sweep.    Connect  the  Arduino  to  your  computer  and  load  the  sketch  and  click  RUN.    After  the  sketch  loads,  the  servo  should  start  sweeping  back  and  forth.    If  all  works  as  expected,  then  we  can  modify  the  values  in  the  sketch.    The  example  moves  the  servo  arm  from  0°  to  180°  and  then  back  to  0°.    Try  changing  it  to  go  from  45°  to  135°.    You  can  also  change  the  delay  and  make  the  servo  move  faster  or  slower.      

Page 78: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook                                                                                                                                                                                                                                                                                        

78

If  you  want,  you  can  wire  up  the  second  servo  and  control  the  both  of  them  at  one  time.    You  will  have  to  add  in  the  “attach”  command  for  the  second  servo.    You  will  also  have  to  wire  both  servos  to  a  power  source  other  than  the  Arduino.    Remember  to  connect  the  Arduino  to  the  common  ground  of  the  servo  power  supply  as  it  is  required  as  a  reference  for  the  control  signal.  

  Practice problems: 1. Adjust the servo motor to alternate in 1 degree, 5 degree and 90 degree increments 2. Make the servo motor complete 180 degrees then stop. 3. Make the servo motor complete 90 degrees then return to the start. Then travel 180 degrees.          

Page 79: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook                                                                                                                                                                                                                                                                                        

79

One  of  the  cool  things  about  being  part  of  PORPOISE  ROBOTICS  at  school,  is  that  you  and  the  school  administration  can  contact  local  engineering  firms  as  well  as  the  Navy,  and  invite  them  to  speak  on  the  fascinating  careers  open  to  young  engineers.    Many  companies  also  have  internships.    Here,  Kevin  Bowen,  a  Senior  Engineering  Fellow  from  Raytheon,  gives  a  class  to  Porpoiseinians  about  his  work  in  maritime  autonomous  systems.    After  class  the  students  further  discuss  career  opportunities.            

The PORPOISE ROBOTICS staff is comprised of engineers, school administrators, robotic club mentors, and even a technical Academy Award winning film maker. Whether you are in a regular PORPOISE class, or are home shcooled, reach out to your local community of engineers and invite them in. They can be wonderful mentors and advisors on pursuing your engineering goals.

Page 80: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook                                                                                                                                                                                                                                                                                        

80

A Synopsis of SEMESTER 2. Down to the Sea in Ships.

 

 Unit VIII: Marine Environments.

Using surface and undersea vehicle systems to explore, research, and responsibly utilize our maritime environment. Unit IX: Naval Architecture and the Engineering Process. You will create a robotic surface vessel or submarine using the

Systems Engieering process used by industry. Create a mission statement and functional block diagram

Create quantifiable and measurable requirements Create a subsystem diagram and allocate requirements to subsystems Develop a Preliminary Design that meets the requirements and functions Report on the Preliminary Design for permission to proceed

Create a detailed design with analysis and performance design. Use a trade study to select components and configuatation (Use CAD drawings) Report on cost and time to complete project. Procure components, fabricate and/or assemble your subsystem. Test subsystems. Integrate subsystems and test the system Report on results, successes and failures and future improvements

Page 81: SEMESTER 1 Introduction to Mechatronics - Perfect · PDF fileSEMESTER 1 Introduction to Mechatronics ... Unit I – Introduction To Mechatronics Muchofthedesign,development,andcreationofrobots

Porpoise  Robotics.    Student  Classbook                                                                                                                                                                                                                                                                                        

81

Unit X : Building the Sea Hawk surface boat. Unit XI : Building the Porpoise submersible. Unit XII: Test and Evaluation. Critical thinking. Unit XIII: Students are the Next Generation of Pioneers.

A look at careers in Exploration, Industry, the Navy and DOD.

Thank  you  for  your  interest  in  PORPOISE  Robotics.  

 You  have  had  an  opportunity  to  read  the  first  five  units.    The    “Table  of  Contents”  list  seven  more  units  comprising  the  1st  semester  and  totals  106  pages.        Semester  two  continues  through  another  six  units  of  Systems  Engineering  where  students  will  build  their  own  autonomous  surface  craft  or  submarine.      Access  to  the  text  for  units  6  thru  13  is  available  as  we  work  with  you  to  create  a  PORPOISE  program  at  your  school  or  organization.      If  you  have  questions  on  those  later  units,  please  ask.        We  look  forward  to  working  with  you  to  bring  PORPOISE  STEM  Robotics  to  you  school.    

CONTACT FOR INFORMATION David Grober, President (310) 951-1110 [email protected] Kevin Bowen, Vice President for Technology and Education (858) 997-4792 [email protected] Jennifer Fox, Education Corordinator, (206) 714-5116 [email protected]