{"id":28,"date":"2006-02-08T05:22:44","date_gmt":"2006-02-08T10:22:44","guid":{"rendered":"http:\/\/www.tigoe.com\/pcomp\/code2\/category\/arduinowiring\/28"},"modified":"2007-08-06T23:37:10","modified_gmt":"2007-08-07T04:37:10","slug":"dc-motor-control","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/arduinowiring\/28\/","title":{"rendered":"DC Motor Control"},"content":{"rendered":"<p>This program controls a DC motor attached to the microcontroller via a transistor or relay. Adaptation to make the motor speed change by Ryan Holsopple.<\/p>\n<p>A simple version is at the bottom of the page<\/p>\n<p><!--more--><\/p>\n<pre>\n\/*\n  DC motor control\n  by Ryan Holsopple\n  \n  modification of Tom Igoe's DC motor code   \n  I added function to start motor turning in one direction\n  I raised the threshold for my pot to work\n  **r.holsopple\n  \n  Created 8 Feb. 2006\n\n*\/\n\n\nint sensorValue = 0;     \/\/ the variable to hold the analog sensor value\nint sensorPin = 0;       \/\/ the analog pin with the sensor on it\nint motorPin = 9;        \/\/ the digital pin with the motor on it\nint threshold = 300;     \/\/ the theshold of the analog sensor below which the motor should turn on\nint forwardPin = 7;\nint backwardPin = 8;\n\n\/\/ prototype of the function that changes the motor's speed:\nvoid changeMotorSpeed();\n\n\/\/function to begin motor turning in one direction\nvoid forwardDirection();\n\nvoid setup() {\n \/\/ declare the inputs and outputs:\n pinMode(motorPin, OUTPUT);\n pinMode(forwardPin, OUTPUT);\n pinMode(backwardPin, OUTPUT);\n pinMode(sensorPin, INPUT);\n}\n\nvoid loop() {\n  \/\/  read the analog sensor\n  sensorValue = analogRead(sensorPin);\n  \/\/  determine whether its above or below the threshold\n  if (sensorValue &gt; threshold) {    \/\/ it's above the threshold\n    \/\/ turn off the motor\n    digitalWrite(motorPin, LOW);\n  } \n  else {\n    \/\/start motor turning forward\n    forwardDirection();\n    \/\/ change the speed of the motor based on the sensor value:\n    changeMotorSpeed();\n  }\n}\n\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\nvoid forwardDirection(){\n  digitalWrite (forwardPin, HIGH);\n  digitalWrite (backwardPin, LOW);\n}\nvoid changeMotorSpeed() {\n  analogWrite(motorPin, sensorValue);\n}\n\n<\/pre>\n<p>The simple version:<\/p>\n<pre>\n\nint motorPin = 9;  \/\/ where the transistor's base is attached\nint analogPin = 0;  \/\/ Analog input number for the potentiometer\n\nint analogValue = 0;  \/\/ variable for the analog reading\n\nvoid setup()  {\npinMode(motorPin, OUTPUT);\n\n}\n\nvoid loop() {\n  analogValue = analogRead(analogPin);\n  \n  if (analogValue &gt; 512) {\n  \/\/ turn the motor on\n  digitalWrite(motorPin, HIGH);\n  } else {\n  \/\/ turn the motor off\n  digitalWrite(motorPin, LOW);\n  }\n\n}\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This program controls a DC motor attached to the microcontroller via a transistor or relay.  Adaptation to make the motor speed change by Ryan Holsopple.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-28","post","type-post","status-publish","format-standard","hentry","category-arduinowiring"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/28","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=28"}],"version-history":[{"count":0,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/28\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=28"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=28"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=28"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}