{"id":1167,"date":"2015-08-25T15:07:06","date_gmt":"2015-08-25T19:07:06","guid":{"rendered":"http:\/\/blog.agilityfeat.com\/?p=1167"},"modified":"2020-11-09T23:23:15","modified_gmt":"2020-11-09T23:23:15","slug":"using-arduino-to-control-dc-motor-speed-and-direction","status":"publish","type":"post","link":"http:\/\/34.200.113.64\/en\/blog\/2015\/08\/using-arduino-to-control-dc-motor-speed-and-direction\/","title":{"rendered":"Using Arduino to Control DC Motor Speed and Direction"},"content":{"rendered":"<p><script src=\"https:\/\/s3-us-west-2.amazonaws.com\/blogs-scripts\/util\/code-scripts-1.js\"><\/script><\/p>\n<p><script src=\"https:\/\/platform.vine.co\/static\/scripts\/embed.js\"><\/script><\/p>\n<p>This is the first in a series of posts I&#8217;m going to be writing in order to achieve the goal of having a <strong>WebRTC controlled webcam<\/strong> through an Arduino.<\/p>\n<p>The final build will consist of a webcam attached to a DC motor which will be controlled by an Arduino board. When complete we&#8217;ll be able to steer the camera left and right, and both the Arduino and the camera feed will be managed remotely using WebRTC to handle communications.<\/p>\n<p>At AgilityFeat we pride ourselves in being jacks of all trades, and in this tutorial we&#8217;ll go full DIY hardware hackers by building an initial prototype of the motor control needed for this project, which will allow us to handle both speed and direction of a 12 Volt DC motor via USB (and through our Arduino). The result should look like this:<\/p>\n<p><script src=\"https:\/\/s3-us-west-2.amazonaws.com\/blogs-scripts\/vine\/vine-2015-08-006.js\"><\/script><\/p>\n<p>Before we get started, here&#8217;s a list of the parts we&#8217;ll need:<\/p>\n<p><video src=\"http:\/\/media.giphy.com\/media\/xTiTnFkZN8ID9VfT5S\/giphy.mp4\" autoplay loop><\/video><\/p>\n<ul>\n<li>Arduino (or Arduino compatible microcontroller)<\/li>\n<li>2x BDX53B Transistors or TIP120 Darlington Transistors<\/li>\n<li>2x 220 Ohm Resistors<\/li>\n<li>12VDC Coil DPDT Relay<\/li>\n<li>9V Battery Connector<\/li>\n<li>9V Battery<\/li>\n<li>Hookup Wire<\/li>\n<li>12VDC Motor<\/li>\n<\/ul>\n<p>The gist of what we&#8217;re going to do is to use the DPDT Relay to change the lead of the motor we will allow current to flow to, one of the transistors will enable us to switch on and off the relay&#8217;s coil (to control direction), the other transistor will allow the current flow to the motor, finally the Arduino will drive the transistors.<\/p>\n<p>Here&#8217;s a schema to better visualize what I just described:<\/p>\n<p><a href=\"\/wp-content\/uploads\/2015\/09\/Arduino-Motor-Schema.png\"><img loading=\"lazy\" src=\"\/wp-content\/uploads\/2015\/09\/Arduino-Motor-Schema.png\" alt=\"Schema for a build of a DC Motor speed and direction controller using an Arduino and a relay\" style=\"max-width:600px; max-height:480px;\" width=\"600\" height=\"480\"\/><\/a><br clear=\"left\"\/><\/p>\n<p>Since this is a prototype I&#8217;ll go ahead and solder what needs soldering using hookup wire and using alligator wires where solders aren&#8217;t needed. The final product will be mounted on a PCB, so be sure to check back for updates on the continuation of this project to see the final build.<\/p>\n<h2>Wiring a DPDT Relay Switch for Reversing Polarity<\/h2>\n<p>There are many ways we can drive the direction in which a DC motor spins; this is the same as saying controlling the polarity or the current flow to the motor.<\/p>\n<p>The option I&#8217;ve chosen for this build is one of the simplest, as well as the most cost-effective, both in regards to time and money.<\/p>\n<p>DPDT relays can be found in most hardware or electronics stores. The main difference between a regular relay switch and a DPDT relay is the ability to control polarity in current flow. <\/p>\n<p>DPDT relays have 8 pins or leads, two for the coil, two which are \u00abnormally closed\u00bb, two which are \u00abcommon\u00bb, and two which are \u00abnormally open\u00bb.<\/p>\n<p>In order to harness the polarity switching power of the DPDT relay, we are going to use some hookup wire and solder the pins like so:<\/p>\n<p><a href=\"https:\/\/agilityfeat.com\/wp-content\/uploads\/2015\/09\/IMG_0536.png\"><img loading=\"lazy\" src=\"\/wp-content\/uploads\/2015\/09\/IMG_0536.png\" alt=\"Picture of a DPDT relay switch wired to allow polarity reversal\" style=\"max-width:600px; max-height:385px;\" width=\"600\" height=\"385\"\/><\/a><br clear=\"left\"\/><\/p>\n<p>It&#8217;s that simple. Just solder a bridge from the coil input down to the left-most \u00abcommon\u00bb, then bridge in a cross the \u00abnormally closed\u00bb pins to the opposite \u00abnormally open\u00bb pins.<\/p>\n<p>Since we&#8217;re going to be using the Arduino to activate the relay, we need to make sure that we normalize the voltage output from the Arduino analog pins as well as protecting the Arduino from any high voltage feedback that may occur.<\/p>\n<p>To do this we will proceed to hookup our transistors and resistors to the relay in the following fashion:<\/p>\n<p><a href=\"https:\/\/agilityfeat.com\/wp-content\/uploads\/2015\/09\/IMG_0537.png\"><img loading=\"lazy\" src=\"\/wp-content\/uploads\/2015\/09\/IMG_0537.png\" alt=\"Picture showing how to hook up collector leads from transistors to polarity reversal relay switch\" style=\"max-width:600px; max-height:376px;\" width=\"600\" height=\"376\"\/><\/a><br clear=\"left\"\/><\/p>\n<p>The collector leads are to be hooked up one to the remaining coil lead, and the other to the remaining common lead.<br clear=\"left\"\/><\/p>\n<p><a href=\"https:\/\/agilityfeat.com\/wp-content\/uploads\/2015\/09\/IMG_0540.png\"><img loading=\"lazy\" src=\"\/wp-content\/uploads\/2015\/09\/IMG_0540.png\" alt=\"Picture showing how to hook up emitter leads from transistors on DPDT relay switch\" style=\"max-width:480px; max-height:720px;\" width=\"480\" height=\"720\"\/><\/a><br clear=\"left\"\/><\/p>\n<p>The emitter leads from both transistors should be soldered together. I had to add a little electrical tape to avoid shorting out the emitter and collector leads on one of the transistors, but aside from that I had no other snags in hooking up the leads this way.<\/p>\n<p>From there on you just have to solder the resistors to the remaining base leads on the transistors.<br clear=\"left\"\/><\/p>\n<h2>Hooking It All Up Together <\/h2>\n<p>All that&#8217;s left on the hardware side of this project is for us to assemble what&#8217;s left, so we have something like this:<\/p>\n<p><a href=\"https:\/\/agilityfeat.com\/wp-content\/uploads\/2015\/09\/IMG_0542.png\"><img loading=\"lazy\" src=\"\/wp-content\/uploads\/2015\/09\/IMG_0542.png\" alt=\"Picture showing how to hook up reversibler elay switch to Arduino pins\" style=\"max-width:480px; max-height:640px;\" width=\"480\" height=\"640\"\/><\/a><br clear=\"left\"\/><\/p>\n<p>Make sure you plug both resistors, individually, to analog pins on the Arduino board. In this case I plugged the \u00abdirection\u00bb lead to PIN 8 and the \u00abenable\u00bb lead to PIN ~11. The \u00ab~\u00bb next to the PIN tells us that it is a pin with PWM capabilities, hooking up the \u00abenable\u00bb lead to a PWM capable PIN is required since turning on and off the motor in rapid succesion is how we will control its speed.<br clear=\"left\"\/><\/p>\n<p><a href=\"https:\/\/agilityfeat.com\/wp-content\/uploads\/2015\/09\/IMG_0545.png\"><img loading=\"lazy\" src=\"\/wp-content\/uploads\/2015\/09\/IMG_0545.png\" alt=\"Picture showing how to hook up reversible relay switch emitters to ground pole\" style=\"max-width:600px; max-height:605px;\" width=\"600\" height=\"605\"\/><\/a><br clear=\"left\"\/><\/p>\n<p>Both emitter leads shall be hooked up to both the Arduino and battery ground leads.<br clear=\"left\"\/><\/p>\n<p><a href=\"https:\/\/agilityfeat.com\/wp-content\/uploads\/2015\/09\/IMG_0546.png\"><img loading=\"lazy\" src=\"\/wp-content\/uploads\/2015\/09\/IMG_0546.png\" alt=\"Picture showing how to connect battery to reversible relay switch\" style=\"max-width:600px; max-height:450px;\" width=\"600\" height=\"450\"\/><\/a><br clear=\"left\"\/><\/p>\n<p>The battery&#8217;s positive lead must be hooked up to our relay&#8217;s coil input.<br clear=\"left\"\/><\/p>\n<p><a href=\"https:\/\/agilityfeat.com\/wp-content\/uploads\/2015\/09\/IMG_0547.png\"><img loading=\"lazy\" src=\"\/wp-content\/uploads\/2015\/09\/IMG_0547.png\" alt=\"Picture showing how to connect DC motor to reversible relay switch and Arduino\" style=\"max-width:600px; max-height:900px;\" width=\"600\" height=\"900\"\/><\/a><br clear=\"left\"\/><\/p>\n<p>Lastly, we connect the DC motor leads, individually, to both of the \u00abnormally open\u00bb pins on the relay.<br clear=\"left\"\/><\/p>\n<h2>Code to Drive DC Motor with Arduino<\/h2>\n<p>Our hardware is all setup, great, now what?<\/p>\n<p>Well, we need to tell the Arduino what to do.<\/p>\n<p>For now I&#8217;m going to keep it to a minimum and simply show you the code necessary to drive the motor back and forth periodically. Laundry machine style. In part two of this series I&#8217;ll guide you through setting up a way to control this rig remotely.<\/p>\n<p>Here&#8217;s the code snippet which is self-explanatory:<\/p>\n<pre><code class=\"javascript\">\/\/pin 8 = direction\r\n\/\/pin 11 = enable\r\n\r\nvoid setup() {\r\n  pinMode(8, OUTPUT); \/\/set direction pin as output\r\n  pinMode(11, OUTPUT); \/\/set enable pin as output\r\n}\r\n\r\nvoid loop() {\r\n\r\n  \/\/start off going forward at 50% throttle\r\n  digitalWrite(8, HIGH); \/\/forward\r\n  analogWrite(11,30); \/\/~12% PWM\r\n  delay(2000);\r\n  \r\n  \/\/and stop for a while\r\n  digitalWrite(11, LOW); \/\/turn enable pin off\r\n  delay(1000);\r\n  \r\n  \/\/now lets go backwards\r\n  digitalWrite(8, LOW); \/\/backward\r\n  analogWrite(11,30); \/\/~12% PWM\r\n  delay(2000);\r\n  \r\n  \/\/and stop for a while\r\n  digitalWrite(11, LOW); \/\/turn enable pin off\r\n  delay(1000);\r\n\r\n}<\/code><\/pre>\n<p>And that&#8217;s basically it, you should now have something that looks like this:<\/p>\n<p><script src=\"https:\/\/s3-us-west-2.amazonaws.com\/blogs-scripts\/youtube\/youtube-2015-08-001.js\"><\/script><\/p>\n<p>Be sure to check for updates in the following week as I&#8217;ll be posting Part II, where we&#8217;ll code a NodeJS server and a front-end client to control this build remotely via WebRTC.<\/p>\n<p>Happy Coding!<\/p>","protected":false},"excerpt":{"rendered":"<p>This is the first in a series of posts I&#8217;m going to be writing in order to achieve the goal of having a WebRTC controlled webcam through an Arduino. The final build will consist of a webcam attached to a DC motor which will be controlled by an Arduino board. When complete we&#8217;ll be able [&hellip;]<\/p>","protected":false},"author":11,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":""},"categories":[4,57,116],"tags":[148,117],"jetpack_featured_media_url":"","yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v15.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Using Arduino to Control DC Motor Speed and Direction - AgilityFeat Panama Software Test Center<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/agilityfeatpanama.com\/blog\/2015\/08\/using-arduino-to-control-dc-motor-speed-and-direction\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using Arduino to Control DC Motor Speed and Direction - AgilityFeat Panama Software Test Center\" \/>\n<meta property=\"og:description\" content=\"This is the first in a series of posts I&#8217;m going to be writing in order to achieve the goal of having a WebRTC controlled webcam through an Arduino. The final build will consist of a webcam attached to a DC motor which will be controlled by an Arduino board. When complete we&#8217;ll be able [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"http:\/\/agilityfeatpanama.com\/blog\/2015\/08\/using-arduino-to-control-dc-motor-speed-and-direction\/\" \/>\n<meta property=\"og:site_name\" content=\"AgilityFeat Panama Software Test Center\" \/>\n<meta property=\"article:published_time\" content=\"2015-08-25T19:07:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-11-09T23:23:15+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/34.200.113.64\/wp-content\/uploads\/2015\/09\/Arduino-Motor-Schema.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"5 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/34.200.113.64\/#website\",\"url\":\"https:\/\/34.200.113.64\/\",\"name\":\"AgilityFeat Panama Software Test Center\",\"description\":\"AgilityFeat Panama offers customized, multilevel web and mobile software testing for a variety of industries.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/34.200.113.64\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"http:\/\/agilityfeatpanama.com\/blog\/2015\/08\/using-arduino-to-control-dc-motor-speed-and-direction\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"\/wp-content\/uploads\/2015\/09\/Arduino-Motor-Schema.png\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/agilityfeatpanama.com\/blog\/2015\/08\/using-arduino-to-control-dc-motor-speed-and-direction\/#webpage\",\"url\":\"http:\/\/agilityfeatpanama.com\/blog\/2015\/08\/using-arduino-to-control-dc-motor-speed-and-direction\/\",\"name\":\"Using Arduino to Control DC Motor Speed and Direction - AgilityFeat Panama Software Test Center\",\"isPartOf\":{\"@id\":\"https:\/\/34.200.113.64\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/agilityfeatpanama.com\/blog\/2015\/08\/using-arduino-to-control-dc-motor-speed-and-direction\/#primaryimage\"},\"datePublished\":\"2015-08-25T19:07:06+00:00\",\"dateModified\":\"2020-11-09T23:23:15+00:00\",\"author\":{\"@id\":\"https:\/\/34.200.113.64\/#\/schema\/person\/c34873e9bee22a4aab19b8a0e800aad0\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/agilityfeatpanama.com\/blog\/2015\/08\/using-arduino-to-control-dc-motor-speed-and-direction\/\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/34.200.113.64\/#\/schema\/person\/c34873e9bee22a4aab19b8a0e800aad0\",\"name\":\"Jean\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/34.200.113.64\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"http:\/\/0.gravatar.com\/avatar\/?s=96&d=mm&r=g\",\"caption\":\"Jean\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"http:\/\/34.200.113.64\/en\/wp-json\/wp\/v2\/posts\/1167"}],"collection":[{"href":"http:\/\/34.200.113.64\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/34.200.113.64\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/34.200.113.64\/en\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"http:\/\/34.200.113.64\/en\/wp-json\/wp\/v2\/comments?post=1167"}],"version-history":[{"count":1,"href":"http:\/\/34.200.113.64\/en\/wp-json\/wp\/v2\/posts\/1167\/revisions"}],"predecessor-version":[{"id":1319,"href":"http:\/\/34.200.113.64\/en\/wp-json\/wp\/v2\/posts\/1167\/revisions\/1319"}],"wp:attachment":[{"href":"http:\/\/34.200.113.64\/en\/wp-json\/wp\/v2\/media?parent=1167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/34.200.113.64\/en\/wp-json\/wp\/v2\/categories?post=1167"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/34.200.113.64\/en\/wp-json\/wp\/v2\/tags?post=1167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}