{"id":548,"date":"2025-10-14T09:56:56","date_gmt":"2025-10-14T07:56:56","guid":{"rendered":"https:\/\/avlos.gr\/wpress\/?p=548"},"modified":"2025-10-14T09:56:57","modified_gmt":"2025-10-14T07:56:57","slug":"control-dc-motor","status":"publish","type":"post","link":"https:\/\/avlos.gr\/wpress\/en\/control-dc-motor\/","title":{"rendered":"Control DC Motor"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Control DC motor\u00a0<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this construction we will drive the motor using a controller and we will measure its speed, as we did in the application &#8220;DC Motor &#8211; Encoder&#8221;.&nbsp;To do this we will add another circuit, that of the motor power supply, and a few lines of code to the program we wrote for our previous construction entitled &#8220;DC Motor &#8211; Encoder&#8221;.<br>Secondly we will use an infrared remote control to give driving commands to the engine.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The materials for construction are the same.&nbsp;Here we will focus more on the controller for driving the engine, and we will say a few words about the PWM configuration (Pulse Width Modulation) and how we will implement it in the Arduino UNO.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u00a0PWM<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The PWM configuration is a periodic square waveform that differs from the percentage of time it is ON or Hight in a period. This ON state is called the Duty Cycle and we measure it as a percentage of the total duration of a period.<br>In the figure below we see six different forms of a PWM. The green vertical lines determine the duration of a cycle or the number of cycles per unit time.<br>In the first line we see that the state of the pulses is 0 for the entire duration of their period. Then we say that the Duty Cycle is 0%. In this case the power provided by the pulse is zero.<br>In the second line a small part of the period, 25% is ON. In this case the power provided by the pulse is 25% of its capacity.<br>Finally in the last line the pulse is ON throughout its duration and the power it provides is 100% of its capacity.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Arduino UNO in a very simple way can provide PWM configuration on pins 3, 9, 10, 11 with a frequency of 490 Hz while on pins 5 and 6 with a frequency of 980 Hz.&nbsp;In this application we are not interested in the pulse frequency, we are only interested in the Duty Cycle.<br>The analogWrite (pin, value) command, which has nothing to do with analog output, generates a PWM pulse on the pin we have defined.&nbsp;The variable value takes values \u200b\u200bfrom 0 to 255. For the value 0 it creates a pulse with Duty Cycle 0% and for the value 255 it creates a pulse with Duty Cycle 100%.&nbsp;Thus the value that the variable will have is determined by the relation value = Duty Cycle * 255.<br>For example to make a pulse with Duty Cycle 20% the value of the variable is value = 0.2 * 255 = 51.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"414\" height=\"452\" src=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/10\/DC_motor_6.jpg\" alt=\"\" class=\"wp-image-550\" style=\"width:218px;height:auto\" srcset=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/10\/DC_motor_6.jpg 414w, https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/10\/DC_motor_6-275x300.jpg 275w\" sizes=\"auto, (max-width: 414px) 100vw, 414px\" \/><figcaption class=\"wp-element-caption\">PWM configuration<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Controllers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The controller we use here is 7A \/ 160W.\u00a0It can drive two motors with voltage from 7 to 24V.\u00a0The following figure shows the interface points of the controller and the following table shows the functions of these interface points.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"652\" height=\"502\" src=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/10\/DC_motor_4.jpg\" alt=\"\" class=\"wp-image-551\" style=\"width:387px;height:auto\" srcset=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/10\/DC_motor_4.jpg 652w, https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/10\/DC_motor_4-300x231.jpg 300w\" sizes=\"auto, (max-width: 652px) 100vw, 652px\" \/><figcaption class=\"wp-element-caption\">Interconnection points<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"844\" height=\"418\" src=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/10\/DC_motor_5.jpg\" alt=\"\" class=\"wp-image-552\" style=\"width:367px;height:auto\" srcset=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/10\/DC_motor_5.jpg 844w, https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/10\/DC_motor_5-300x149.jpg 300w, https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/10\/DC_motor_5-768x380.jpg 768w\" sizes=\"auto, (max-width: 844px) 100vw, 844px\" \/><figcaption class=\"wp-element-caption\">Functions of connection points<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">We use the Label 1.2 points to connect a 12V battery.<br>Points 3 and 4 to connect the motor power supply (white and red cable)<br>Point 7 to connect the PWM pulse from the Arduino UNO pin<br>Points 8 and 9 to connect the IN1 and IN2 select direction as shown in the picture with my notes.<br>And the + 5V and GND points located before 7 and after 9 as shown in the controller image to connect the power supply to the digital controller circuit.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"482\" height=\"636\" src=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/10\/note_2-1.jpg\" alt=\"\" class=\"wp-image-554\" style=\"width:385px;height:auto\" srcset=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/10\/note_2-1.jpg 482w, https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/10\/note_2-1-227x300.jpg 227w\" sizes=\"auto, (max-width: 482px) 100vw, 482px\" \/><figcaption class=\"wp-element-caption\">Notes on construction<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The following table (Control Logic Table) for the engine we have connected, shows us the movements that the engine will perform depending on the values \u200b\u200bthat will have the inputs IN1 and IN2.\u00a0You can see their use in the program in the functions goForword () and goBackword ().<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Programming<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">We are now coming to the program that will start our construction.<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>\/\/ Author Manolis Aristovoulidis\n\/\/ This program drives a 12V DC motor using a controller\n\/\/ uses an Infrared remote control to give instructions for movement\n\/\/ measures and \"prints\" the speed and direction of movement.\n\n\n#include &lt;IRremote.h>\n\nconst int RECV_PIN = 12;\nIRrecv irrecv(RECV_PIN);\ndecode_results results;\n\n\/\/ Motor A connections\nconst int enA = 6;\nconst int in1 = 5;\nconst int in2 = 4;\n\n\/\/motor A encoder\nconst byte encoder_pinA = 2;    \/\/A pin -> the interrupt pin 2\nconst byte encoder_pinB = 10;    \/\/B pin -> the digital pin 10\n\nint pulses;                   \/\/the number of the pulses\nboolean Direction;              \/\/the rotation direction\n\n\/\/ Set the speed (0 = off and 255 = max speed)\nint motorSpeed = 0;\n\n\nvoid setup() {\n     \/\/ Motor control pins are outputs\n\n      Serial.begin(57600);          \/\/Initialize the serial port\n      EncoderInit();                \/\/Initialize the module\n      \n      pinMode(enA, OUTPUT);\n      pinMode(in1, OUTPUT);\n      pinMode(in2, OUTPUT);\n      irrecv.enableIRIn();\n}\n\nvoid loop() {\n    if (irrecv.decode(&amp;results)){\n        \/\/Serial.println(results.value, HEX);\n        irrecv.resume();\n        \n        switch (results.value) {\n          case 0xFF18E7:  \/\/key UP\n              if(motorSpeed &lt;= 200){\n                motorSpeed +=50;                  \n              }\n              if(motorSpeed >=0){\n                \/\/ move forword\n                goForword();\n              }else{\n                \/\/move backword\n                goBackword();\n              }\n          analogWrite(enA, abs(motorSpeed)); \n          break;\n          \n          case 0xFF4AB5:  \/\/key DOWN\n              if(motorSpeed > -250){\n                motorSpeed -=50;                   \n              }\n              if(motorSpeed >=0){\n                \/\/ move forword\n                goForword();\n              }else{\n                \/\/move backword\n                goBackword();\n              }\n          analogWrite(enA, abs(motorSpeed)); \n          break;\n          case 0xFF38C7:  \/\/key OK\n              \/\/stop moving\n              stopMove();\n              \n          break;\n          default:\n          \/\/ statements\n          break;\n       }\n  }\n\n    Serial.print(\"Direction:\");\n      if(Direction){\n        Serial.print(\"Forword   \");\n      }else{\n        Serial.print(\"Backword  \");\n      }\n    Serial.print(\"Pulses:\");\n    Serial.println(pulses);\n    pulses = 0;\n    delay(100);\n}\n\nvoid EncoderInit()\n{\n  Direction = true;         \/\/default -> Forward\n  pinMode(encoder_pinB,INPUT);\n  attachInterrupt(digitalPinToInterrupt(encoder_pinA), wheelSpeed, RISING );\n}\n\nvoid wheelSpeed()\n{\n  int val = digitalRead(encoder_pinB);\n  if(val == LOW ){\n      Direction = false; \/\/Reverse\n      pulses--;\n    }else{\n      Direction = true;  \/\/Forward\n      pulses++;\n    }\n  }\n\nvoid goForword(){\n    digitalWrite(in1, HIGH);    \/\/1,0 ->Forward \n    digitalWrite(in2, LOW);\n}\n\nvoid goBackword(){\n    digitalWrite(in1, LOW);    \/\/0,1 ->Backword \n    digitalWrite(in2, HIGH);\n}\n\nvoid stopMove(){\n    digitalWrite(in1, LOW);    \/\/1,0 ->Forward \n    digitalWrite(in2, LOW);\n    motorSpeed = 0;\n    analogWrite(enA, abs(motorSpeed)); \n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the Loop () of the program there is a switch &#8211; case which leads the program according to the command given by the remote control.&nbsp;From the remote control we give three commands, up arrow to increase forward speed, down arrow to reduce speed and finally reverse the movement and finally &#8220;OK&#8221; to reset the motor speed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Moving forward is done with the goForword () function<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Moving backwards is done with the goBackword () function<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The motor is stopped with the stoMove function ()<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Finally the speed is defined by the command analogWrite (enA, abs (motorSpeed)) which produces the PWM pulse on the pin enA with Duty Cycle according to the value of the variable motorSpeed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can download the program\u00a0\u00a0<a href=\"https:\/\/avlos.gr\/wpress\/download\/%CE%AD%CE%BB%CE%B5%CE%B3%CF%87%CE%BF%CF%82-%CE%BA%CE%B9%CE%BD%CE%B7%CF%84%CE%AE%CF%81%CE%B1-dc\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the video that follows you see the construction in operation.&nbsp;<a href=\"https:\/\/www.youtube.com\/watch?v=4HFExAJxDxY\" target=\"_blank\" rel=\"noreferrer noopener\">video<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Control DC motor\u00a0 In this construction we will drive the motor using a controller and we will measure its speed, as we did in the application &#8220;DC Motor &#8211; Encoder&#8221;.&nbsp;To do this we will add another circuit, that of the&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29,75],"tags":[73],"class_list":["post-548","post","type-post","status-publish","format-standard","hentry","category-home","category-robotics","tag-robotics"],"_links":{"self":[{"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/posts\/548","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/comments?post=548"}],"version-history":[{"count":5,"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/posts\/548\/revisions"}],"predecessor-version":[{"id":557,"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/posts\/548\/revisions\/557"}],"wp:attachment":[{"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/media?parent=548"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/categories?post=548"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/tags?post=548"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}