{"id":551,"date":"2016-03-21T19:42:39","date_gmt":"2016-03-22T00:42:39","guid":{"rendered":"http:\/\/adaming.com\/blog\/?p=551"},"modified":"2016-03-21T19:42:39","modified_gmt":"2016-03-22T00:42:39","slug":"motor-hookup","status":"publish","type":"post","link":"http:\/\/adaming.com\/blog\/index.php\/archive\/motor-hookup\/","title":{"rendered":"Motor Hookup"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/LA6YyI6LZJE\" width=\"420\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>Once I have Bluetooth communication, it&#8217;s time to make this thing move. \u00a0I&#8217;ve set it up with an Ardumoto motor shield which can allow for a maximum of 2A of power.<\/p>\n<ul>\n<li><a href=\"http:\/\/cdn.sparkfun.com\/datasheets\/Robotics\/DG01D.pdf\">Motor physical specifications<\/a><\/li>\n<\/ul>\n<p>Using this code that I slapped together with a few tutorials, I can control the wheels via Bluetooth commands. \u00a0Now I noticed that the wheels wouldn&#8217;t turn when I had set the speed to 255\/2. \u00a0This was due to the USB plug, having a maximum output of 500mA x 5V = 2.5W. \u00a0That is not enough power, so to increase the amount of available power, the barrel jack should be used to supplement with 4x AA alkaline batteries.<\/p>\n<ul>\n<li>Next steps: \u00a0order new parts for more power!<\/li>\n<\/ul>\n<blockquote><p>#include &lt;SoftwareSerial.h&gt;<br \/>\nSoftwareSerial BT(4, 5);<br \/>\n\/\/ creates a &#8220;virtual&#8221; serial port\/UART<br \/>\n\/\/ connect BT module TX to D4<br \/>\n\/\/ connect BT module RX to D5<br \/>\n\/\/ connect BT Vcc to 5V, GND to GND<\/p>\n<p>\/\/ Clockwise and counter-clockwise definitions.<br \/>\n\/\/ Depending on how you wired your motors, you may need to swap.<br \/>\n#define CW 0<br \/>\n#define CCW 1<\/p>\n<p>\/\/ Motor definitions to make life easier:<br \/>\n#define MOTOR_A 0<br \/>\n#define MOTOR_B 1<\/p>\n<p>\/\/ Pin Assignments \/\/<br \/>\n\/\/ Don&#8217;t change these! These pins are statically defined by shield layout<br \/>\nconst byte PWMA = 3; \/\/ PWM control (speed) for motor A<br \/>\nconst byte PWMB = 11; \/\/ PWM control (speed) for motor B<br \/>\nconst byte DIRA = 12; \/\/ Direction control for motor A<br \/>\nconst byte DIRB = 13; \/\/ Direction control for motor B<\/p>\n<p>void setup()<br \/>\n{<br \/>\nsetupArdumoto(); \/\/ Set all pins as outputs<\/p>\n<p>\/\/ set digital pin to control as an output<br \/>\npinMode(13, OUTPUT);<br \/>\n\/\/ set the data rate for the SoftwareSerial port<br \/>\nBT.begin(9600);<br \/>\n\/\/ Send test message to other device<br \/>\nBT.println(&#8220;Hello from Arduino&#8221;);<br \/>\n}<\/p>\n<p>char a; \/\/ stores incoming character from other device<\/p>\n<p>void loop()<br \/>\n{<\/p>\n<p>if (BT.available())<br \/>\n\/\/ if text arrived in from BT serial&#8230;<br \/>\n{<br \/>\na=(BT.read());<br \/>\nif (a==&#8217;1&#8242;)<br \/>\n{<br \/>\ndigitalWrite(13, HIGH);<br \/>\nBT.println(&#8220;LED on&#8221;);<br \/>\n}<br \/>\nif (a==&#8217;2&#8242;)<br \/>\n{<br \/>\ndigitalWrite(13, LOW);<br \/>\nBT.println(&#8220;LED off&#8221;);<br \/>\n}<br \/>\nif (a==&#8217;?&#8217;)<br \/>\n{<br \/>\nBT.println(&#8220;Send &#8216;1&#8217; to turn LED on&#8221;);<br \/>\nBT.println(&#8220;Send &#8216;2&#8217; to turn LED on&#8221;);<br \/>\n}<br \/>\n\/\/ you can add more &#8220;if&#8221; statements with other characters to add more commands<\/p>\n<p>if (a==&#8217;q&#8217;)<br \/>\n{<br \/>\n\/\/ Drive motor A (and only motor A) at various speeds, then stop.<br \/>\ndriveArdumoto(MOTOR_A, CCW, 255); \/\/ Set motor A to CCW at max<br \/>\ndelay(3000); \/\/ Motor A will spin as set for 1 second<br \/>\ndriveArdumoto(MOTOR_A, CW, 255); \/\/ Set motor A to CW at half<br \/>\ndelay(3000); \/\/ Motor A will keep trucking for 1 second<br \/>\nstopArdumoto(MOTOR_A); \/\/ STOP motor A<br \/>\n}<br \/>\nif (a==&#8217;w&#8217;)<br \/>\n{<br \/>\n\/\/ Drive motor B (and only motor B) at various speeds, then stop.<br \/>\ndriveArdumoto(MOTOR_B, CCW, 255); \/\/ Set motor B to CCW at max<br \/>\ndelay(3000); \/\/ Motor B will spin as set for 1 second<br \/>\ndriveArdumoto(MOTOR_B, CW, 255); \/\/ Set motor B to CW at half<br \/>\ndelay(3000); \/\/ Motor B will keep trucking for 1 second<br \/>\nstopArdumoto(MOTOR_B); \/\/ STOP motor B<br \/>\n}<br \/>\nif (a==&#8217;e&#8217;)<br \/>\n{<br \/>\n\/\/ Now spin both!<br \/>\ndriveArdumoto(MOTOR_A, CW, 255); \/\/ Motor A at max speed.<br \/>\ndriveArdumoto(MOTOR_B, CW, 255); \/\/ Motor B at max speed.<br \/>\ndelay(3000); \/\/ Drive forward for a second<br \/>\nstopArdumoto(MOTOR_A); \/\/ STOP motor A<br \/>\nstopArdumoto(MOTOR_B); \/\/ STOP motor B<br \/>\n}<br \/>\nif (a==&#8217;r&#8217;)<br \/>\n{<br \/>\n\/\/ Now go backwards at half that speed:<br \/>\ndriveArdumoto(MOTOR_A, CCW, 255); \/\/ Motor A at max speed.<br \/>\ndriveArdumoto(MOTOR_B, CCW, 255); \/\/ Motor B at max speed.<br \/>\ndelay(3000); \/\/ Drive forward for a second<br \/>\nstopArdumoto(MOTOR_A); \/\/ STOP motor A<br \/>\nstopArdumoto(MOTOR_B); \/\/ STOP motor B<br \/>\n}<\/p>\n<p>}<br \/>\n}<\/p>\n<p>\/\/ driveArdumoto drives &#8216;motor&#8217; in &#8216;dir&#8217; direction at &#8216;spd&#8217; speed<br \/>\nvoid driveArdumoto(byte motor, byte dir, byte spd)<br \/>\n{<br \/>\nif (motor == MOTOR_A)<br \/>\n{<br \/>\ndigitalWrite(DIRA, dir);<br \/>\nanalogWrite(PWMA, spd);<br \/>\n}<br \/>\nelse if (motor == MOTOR_B)<br \/>\n{<br \/>\ndigitalWrite(DIRB, dir);<br \/>\nanalogWrite(PWMB, spd);<br \/>\n}<br \/>\n}<\/p>\n<p>\/\/ stopArdumoto makes a motor stop<br \/>\nvoid stopArdumoto(byte motor)<br \/>\n{<br \/>\ndriveArdumoto(motor, 0, 0);<br \/>\n}<\/p>\n<p>\/\/ setupArdumoto initialize all pins<br \/>\nvoid setupArdumoto()<br \/>\n{<br \/>\n\/\/ All pins should be setup as outputs:<br \/>\npinMode(PWMA, OUTPUT);<br \/>\npinMode(PWMB, OUTPUT);<br \/>\npinMode(DIRA, OUTPUT);<br \/>\npinMode(DIRB, OUTPUT);<\/p>\n<p>\/\/ Initialize all pins as low:<br \/>\ndigitalWrite(PWMA, LOW);<br \/>\ndigitalWrite(PWMB, LOW);<br \/>\ndigitalWrite(DIRA, LOW);<br \/>\ndigitalWrite(DIRB, LOW);<br \/>\n}<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Once I have Bluetooth communication, it&#8217;s time to make this thing move. \u00a0I&#8217;ve set it up with an Ardumoto motor shield which can allow for a maximum of 2A of power. Motor physical specifications Using this code that I slapped together with a few tutorials, I can control the wheels via Bluetooth commands. \u00a0Now &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/adaming.com\/blog\/index.php\/archive\/motor-hookup\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Motor Hookup&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"http:\/\/adaming.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/551"}],"collection":[{"href":"http:\/\/adaming.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/adaming.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/adaming.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/adaming.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=551"}],"version-history":[{"count":2,"href":"http:\/\/adaming.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/551\/revisions"}],"predecessor-version":[{"id":553,"href":"http:\/\/adaming.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/551\/revisions\/553"}],"wp:attachment":[{"href":"http:\/\/adaming.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/adaming.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=551"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/adaming.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}