{"id":1822,"date":"2015-06-27T10:45:24","date_gmt":"2015-06-27T10:45:24","guid":{"rendered":"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=1822"},"modified":"2015-06-27T13:59:50","modified_gmt":"2015-06-27T13:59:50","slug":"moving-your-diy-robot-using-arduino-and-servos","status":"publish","type":"post","link":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/moving-your-diy-robot-using-arduino-and-servos\/","title":{"rendered":"Moving Your DIY Robot using Arduino and Servos"},"content":{"rendered":"\n<!-- Facebook Like Button v1.9.6 BEGIN [http:\/\/blog.bottomlessinc.com] -->\n<iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href=https%3A%2F%2Finspiredtoeducate.net%2Finspiredtoeducate%2Fmoving-your-diy-robot-using-arduino-and-servos%2F&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" style=\"border:none; overflow:hidden; width:450px; height: 30px; align: left; margin: 2px 0px 2px 0px\"><\/iframe>\n<!-- Facebook Like Button END -->\n<p><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/06\/sumobotjr.png\"><img loading=\"lazy\" src=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/06\/sumobotjr.png\" alt=\"Sumobot Bot JR\" width=\"500\" height=\"375\" class=\"alignnone size-full wp-image-1818\" srcset=\"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/06\/sumobotjr.png 500w, https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/06\/sumobotjr-300x225.png 300w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p>\nInterested in building your own Arduino robot? For me, it has been a fun project to do with my kids.  We built our first the robot using the <a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/pi-bot-a-great-kit-for-learning-arduino-robotics\/\" title=\"Pi-Bot: A Great Kit for Learning Arduino Robotics!\">PiBot robotics kit<\/a>.  Additional iterations utilized Legos, Sumobot Jr, cardboard, and other materials around the house.  It&#8217;s fun to invent your own chassis and designs. Using some of the ideas from this blog post,<br \/>\nyou&#8217;ll be able to build your own DIY robot.  Hope you enjoy the journey.\n<\/p>\n<p>\nIn future blog posts, we&#8217;ll show ways to control your robot using a Wifi connection, Raspberry Pi, <a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/droidscript-building-android-apps-using-javascript\/\" title=\"DroidScript: Building Simple Android apps using JavaScript\">Droidscript<\/a> and Android.  This blog post will focus on controlling the wheels of the robot using continuous rotation servos and<br \/>\na simple communication protocol.\n<\/p>\n<p>\nHere&#8217;s a components that you&#8217;ll need to get started:\n<\/p>\n<ul>\n<li><a href=\"https:\/\/www.google.com\/search?safe=off&#038;output=search&#038;tbm=shop&#038;q=arduino+uno&#038;oq=arduino+uno&#038;gs_l=products-cc.3..0l10.2926.6516.0.6882.17.11.3.3.3.0.101.869.10j1.11.0....0...1ac.1.64.products-cc..1.16.831.HzJdAhRW2rk\" target=\"_blank\">Arduino UNO<\/a><\/li>\n<li><a href=\"https:\/\/www.google.com\/search?safe=off&#038;output=search&#038;tbm=shop&#038;q=arduino+uno&#038;oq=arduino+uno&#038;gs_l=products-cc.3..0l10.2926.6516.0.6882.17.11.3.3.3.0.101.869.10j1.11.0....0...1ac.1.64.products-cc..1.16.831.HzJdAhRW2rk#safe=off&#038;tbm=shop&#038;q=mini+Breadboard+and+wires\" target=\"_blank\">Bread board<\/a><\/li>\n<li>Pack for 4 AA batteries<\/li>\n<li>4 AA&#8217;s batteries<\/li>\n<li><a href=\"http:\/\/www.amazon.com\/PARALLAX-900-00008-Continuous-Rotation-Robotics\/dp\/B00B886KSE\/ref=sr_1_2?ie=UTF8&#038;qid=1435373604&#038;sr=8-2&#038;keywords=parallax+continuous+rotation+servos\" target=\"_blank\">2 continuous rotation servos<br \/>\n<\/a>\n<\/li>\n<li>Your robot chassis.  A <a href=\"http:\/\/sumobotkit.com\/\"  target=\"_blank\">Sumo bot Jr.<\/a> is a great way to get started if you don&#8217;t have an original design.<\/li>\n<\/ul>\n<p>\nThis post will focus on programming the servo&#8217;s with Arduino&#8217;s programming environment.  For sample instructions on building a Sumo Bot Jr, check out the following video.  In this post, we&#8217;ll assume that you have put together your robot chassis and your servos, breadboard, battery pack and Arduino have been connected to your chassis.\n<\/p>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/0Q3hrKUwxDM\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/06\/servoRobot.png\"><img loading=\"lazy\" src=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/06\/servoRobot.png\" alt=\"Servo Robot\" width=\"600\" height=\"397\" class=\"alignnone size-full wp-image-1829\" srcset=\"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/06\/servoRobot.png 600w, https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-content\/uploads\/2015\/06\/servoRobot-300x198.png 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<ol>\n<li> Connect the GND pin on the Arduino to the ground line of the bread board.  The ground line is marked with a blue stripe.<\/li>\n<li> Connect the black wires of the servos to the ground line.<\/li>\n<li> Connect the red wires of the servos to the voltage line of the bread board.   The voltage line is marked with a red stripe. <\/li>\n<li> Connect the white wire of the left servo to pin 9 of the Arduino.  This wire will act as a signal wire between the servo and the Arduino.<\/li>\n<li> Connect the white wire of the right servo to pin 10 of the Arduino.<\/li>\n<li> Connect the black wire of the battery pack to the ground line.<\/li>\n<li> Connect the red wire of the battery pack to the voltage line.<\/li>\n<li> Install 4 AA batteries into the battery pack.<\/li>\n<\/ol>\n<p>\nAt this point, we&#8217;re ready to install some Arduino code into the Arduino. Copy the following Arduino sketch and upload into your Arduino.  To learn more about uploading sketches using the Arduino IDE, check out the following video:\n<\/p>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/qHeF3hOn684\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p>Here&#8217;s another tutorial on setting up your Arduino and uploading sketches:<br \/>\n<a href=\"https:\/\/www.arduino.cc\/en\/Main\/Howto\" target=\"_blank\">https:\/\/www.arduino.cc\/en\/Main\/Howto<\/a><\/p>\n<p><code><br \/>\n#include <Servo.h> <\/p>\n<p>Servo leftServo;<br \/>\nServo rightServo;  <\/p>\n<p>int LEFT_FORWARD_VALUE = 180;<br \/>\nint LEFT_BACK_VALUE = 0;<br \/>\nint STOP_VALUE = 90;<br \/>\nint RIGHT_FORWARD_VALUE = 0;<br \/>\nint RIGHT_BACK_VALUE = 180;<br \/>\nint LEFT_SERVO_PIN = 9;<br \/>\nint RIGHT_SERVO_PIN = 10;<\/p>\n<p>void setup()<br \/>\n{<br \/>\n  Serial.begin(9600);<br \/>\n  leftServo.attach(LEFT_SERVO_PIN);<br \/>\n  rightServo.attach(RIGHT_SERVO_PIN);<br \/>\n  stop();<br \/>\n}<br \/>\n\/\/================================================================================<br \/>\nvoid forward()<br \/>\n{<br \/>\n    leftServo.write(LEFT_FORWARD_VALUE);<br \/>\n    rightServo.write(RIGHT_FORWARD_VALUE);<br \/>\n}<br \/>\n\/\/================================================================================<br \/>\nvoid stop()<br \/>\n{<br \/>\n    leftServo.write(STOP_VALUE);<br \/>\n    rightServo.write(STOP_VALUE);<br \/>\n}<br \/>\n\/\/================================================================================<br \/>\nvoid back()<br \/>\n{<br \/>\n    leftServo.write(LEFT_BACK_VALUE);<br \/>\n    rightServo.write(RIGHT_BACK_VALUE);<br \/>\n}<br \/>\n\/\/================================================================================<br \/>\nvoid left()<br \/>\n{<br \/>\n    leftServo.write(LEFT_BACK_VALUE);<br \/>\n    rightServo.write(RIGHT_FORWARD_VALUE);<br \/>\n}<br \/>\n\/\/================================================================================<br \/>\nvoid right()<br \/>\n{<br \/>\n    leftServo.write(LEFT_FORWARD_VALUE);<br \/>\n    rightServo.write(RIGHT_BACK_VALUE);<br \/>\n}<br \/>\n\/\/================================================================================<br \/>\nvoid loop()<br \/>\n{<br \/>\n  if (Serial.available() > 0) {<br \/>\n    int inByte = Serial.read();<\/p>\n<p>    switch (inByte) {<br \/>\n    case 'w':    forward(); break;<br \/>\n    case 's':    back(); break;<br \/>\n    case 'a':    left(); break;<br \/>\n    case 'd':    right(); break;<br \/>\n    case ' ':    stop(); break;<br \/>\n    }<br \/>\n  }<br \/>\n}<br \/>\n<\/code><\/p>\n<h3>\nHow does this code work?<br \/>\n<\/h3>\n<p>\nWe start by importing the &#8220;Servo&#8221; header and declaring the left and right<br \/>\nservos.\n<\/p>\n<p><code><br \/>\n#include <Servo.h> <\/p>\n<p>Servo leftServo;<br \/>\nServo rightServo;<br \/>\n<\/code><\/p>\n<p>\nThe continuous rotation servo has a simple protocol for controlling rotational<br \/>\nmotion using the frequency of voltage pulses. In the Arduino framework, the following<br \/>\ncode stops the rotation of the servo.\n<\/p>\n<p>leftServo.write(90);<\/p>\n<p>To make the servo spin forward, use the following code:    <\/p>\n<p>leftServo.write(180);<\/p>\n<p>To make the servo spin backward, use the following code:    <\/p>\n<p>leftServo.write(0);<\/p>\n<p>With these ideas in mind, we define the following constants for the left and right servos. <\/p>\n<p><code><br \/>\nint LEFT_FORWARD_VALUE = 180;<br \/>\nint LEFT_BACK_VALUE = 0;<br \/>\nint STOP_VALUE = 90;<br \/>\nint RIGHT_FORWARD_VALUE = 0;<br \/>\nint RIGHT_BACK_VALUE = 180;<br \/>\n<\/code><\/p>\n<p>We also define the constants for the Arduino digital pins.<\/p>\n<p>int LEFT_SERVO_PIN = 9;<br \/>\nint RIGHT_SERVO_PIN = 10;<\/p>\n<p>In the following code, we setup the serial port, attach the left and right servos,<br \/>\nand send the stop command. <\/p>\n<p><code><br \/>\nvoid setup()<br \/>\n{<br \/>\n  Serial.begin(9600);<br \/>\n  leftServo.attach(LEFT_SERVO_PIN);<br \/>\n  rightServo.attach(RIGHT_SERVO_PIN);<br \/>\n  stop();<br \/>\n}<br \/>\n<\/code> <\/p>\n<p>The following functions are used to move the robot forward, backward, left, and right.<br \/>\nThere&#8217;s also a function to stop movement.<\/p>\n<p><code><br \/>\n\/\/================================================================================<br \/>\nvoid forward()<br \/>\n{<br \/>\n    leftServo.write(LEFT_FORWARD_VALUE);<br \/>\n    rightServo.write(RIGHT_FORWARD_VALUE);<br \/>\n}<br \/>\n\/\/================================================================================<br \/>\nvoid stop()<br \/>\n{<br \/>\n    leftServo.write(STOP_VALUE);<br \/>\n    rightServo.write(STOP_VALUE);<br \/>\n}<br \/>\n\/\/================================================================================<br \/>\nvoid back()<br \/>\n{<br \/>\n    leftServo.write(LEFT_BACK_VALUE);<br \/>\n    rightServo.write(RIGHT_BACK_VALUE);<br \/>\n}<br \/>\n\/\/================================================================================<br \/>\nvoid left()<br \/>\n{<br \/>\n    leftServo.write(LEFT_BACK_VALUE);<br \/>\n    rightServo.write(RIGHT_FORWARD_VALUE);<br \/>\n}<br \/>\n\/\/================================================================================<br \/>\nvoid right()<br \/>\n{<br \/>\n    leftServo.write(LEFT_FORWARD_VALUE);<br \/>\n    rightServo.write(RIGHT_BACK_VALUE);<br \/>\n}<\/p>\n<p>\/\/================================================================================<br \/>\n<\/code> <\/p>\n<p>\nIn the loop function which is called repeatedly, the Arduino waits for a character<br \/>\nfrom the serial port.  If the Arduino receives a &#8220;w&#8221;, the program sends a forward command.<br \/>\nIf the Arduino receives a &#8220;s&#8221;, the program sends a backward command.<br \/>\nIf the Arduino receives a space, the program sends a stop command.\n<\/p>\n<p><code><br \/>\nvoid loop()<br \/>\n{<br \/>\n  if (Serial.available() > 0) {<br \/>\n    int inByte = Serial.read();<\/p>\n<p>    switch (inByte) {<br \/>\n    case 'w':    forward(); break;<br \/>\n    case 's':    back(); break;<br \/>\n    case 'a':    left(); break;<br \/>\n    case 'd':    right(); break;<br \/>\n    case ' ':    stop(); break;<br \/>\n    }<br \/>\n  }<br \/>\n}<br \/>\n<\/code> <\/p>\n<p>\nIn the Arduino IDE, press CTRL+SHIFT+M to open the serial port monitor.  This window is used to send bytes to the Arduino.   Try typing &#8220;w&#8221; and press enter.   The robot should move forward. Try typing space and press enter.  The robot should stop.<\/p>\n<p>\nCongrads! You&#8217;ve built your first Arduino robot!\n<\/p>\n<p><strong>Top Stories on InspiredToEducate.NET<\/strong><\/p>\n<p><strong>Learning To Code<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=1319\">Easy Data Visualization with Google Charts and JavaScript<\/a><\/li>\n<li><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=795\">Learn to Build Your Own Conversational Bot using ChatScript<\/a><\/li>\n<li><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=1285\">10 Free Resources for Learning JavaScript and HTML5<\/a><\/li>\n<li><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=1238\">17 Fun Tools To Teach Kids To Code by @ChrisBetcher<\/a><\/li>\n<li><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=623\">Benefits of Teaching Kids To Code That No One Is Talking About<\/a><\/li>\n<li><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=1349\">Easy Recipes for Building Android Apps using MIT App Inventor<\/a><\/li>\n<li><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=738\">12 Steps To 3D Print Your Minecraft Creations<\/a><\/li>\n<li><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=849\">How to Build Your Mobile App using HTML<\/a><\/li>\n<li><a href=\"http:\/\/inspiredtoeducate.net\/inspiredtoeducate\/?p=1079\" target=\"_blank\">Simple Minecraft Programming Using ScriptCraftJS<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n\n<!-- Facebook Like Button v1.9.6 BEGIN [http:\/\/blog.bottomlessinc.com] -->\n<iframe src=\"http:\/\/www.facebook.com\/plugins\/like.php?href=https%3A%2F%2Finspiredtoeducate.net%2Finspiredtoeducate%2Fmoving-your-diy-robot-using-arduino-and-servos%2F&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light\" scrolling=\"no\" frameborder=\"0\" allowTransparency=\"true\" style=\"border:none; overflow:hidden; width:450px; height: 30px; align: left; margin: 2px 0px 2px 0px\"><\/iframe>\n<!-- Facebook Like Button END -->\n","protected":false},"excerpt":{"rendered":"<p>Interested in building your own Arduino robot? For me, it has been a fun project to do with my kids. We built our first the robot using the PiBot robotics kit. Additional iterations utilized Legos, Sumobot Jr, cardboard, and other materials around the house. It&#8217;s fun to invent your own [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,24,18,16,23,11,8],"tags":[],"_links":{"self":[{"href":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts\/1822"}],"collection":[{"href":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/comments?post=1822"}],"version-history":[{"count":14,"href":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts\/1822\/revisions"}],"predecessor-version":[{"id":1837,"href":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/posts\/1822\/revisions\/1837"}],"wp:attachment":[{"href":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/media?parent=1822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/categories?post=1822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/inspiredtoeducate.net\/inspiredtoeducate\/wp-json\/wp\/v2\/tags?post=1822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}