{"id":536,"date":"2025-10-14T09:29:36","date_gmt":"2025-10-14T07:29:36","guid":{"rendered":"https:\/\/avlos.gr\/wpress\/?p=536"},"modified":"2025-10-14T09:41:54","modified_gmt":"2025-10-14T07:41:54","slug":"dc-motor-encoder","status":"publish","type":"post","link":"https:\/\/avlos.gr\/wpress\/en\/dc-motor-encoder\/","title":{"rendered":"DC motor &#8211; Encoder"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">DC motor &#8211; Encoder<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this article we will make a simple construction to understand the operation of the encoder in a DC motor.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The materials we will need to implement our construction are<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/translate.google.com\/website?sl=el&amp;tl=en&amp;nui=1&amp;u=https:\/\/www.arduino.cc\/en\/reference\/board\" target=\"_blank\" rel=\"noreferrer noopener\">an Arduino Uno<\/a>&nbsp;,<\/li>\n\n\n\n<li>a DC power supply for the motor,<\/li>\n\n\n\n<li>an&nbsp;&nbsp;engine drive&nbsp;<a href=\"https:\/\/translate.google.com\/website?sl=el&amp;tl=en&amp;nui=1&amp;u=https:\/\/www.handsontec.com\/dataspecs\/module\/7A-160W%2520motor%2520control.pdf\" target=\"_blank\" rel=\"noreferrer noopener\">controller&nbsp;<\/a>&nbsp;,<\/li>\n\n\n\n<li>and a DC motor with an encoder adapted to its construction.<\/li>\n\n\n\n<li>an Arduino UNO Shield, blank perforated board<\/li>\n\n\n\n<li>some connection materials, thin wires in different colors<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">All the necessary connections will be made on the blank board.&nbsp;The board wiring I present below is a good guide to construction.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the photo below you can see the DC motor I chose to use.&nbsp;As you can see it has a built-in encoder.&nbsp;It is up to you whether you want to use it as a simple DC motor or if you want to use the encoder or finally use the motor and the encoder together.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"690\" height=\"524\" src=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/10\/motor_2.jpg\" alt=\"\" class=\"wp-image-537\" style=\"width:338px;height:auto\" srcset=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/10\/motor_2.jpg 690w, https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/10\/motor_2-300x228.jpg 300w\" sizes=\"auto, (max-width: 690px) 100vw, 690px\" \/><figcaption class=\"wp-element-caption\">DC motor with encoder<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Engine<br>JGB37-520 DC motor with mechanical gearbox and encoder.<br>It is mainly used in variable speed cars, self-balancing cars (with two wheels), racing cars, monitoring car, robots and other related constructions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><br>The speed encoder operates using the&nbsp;<a href=\"https:\/\/translate.google.com\/website?sl=el&amp;tl=en&amp;nui=1&amp;u=https:\/\/el.wikipedia.org\/wiki\/%CE%91%CE%B9%CF%83%CE%B8%CE%B7%CF%84%CE%AE%CF%81%CE%B1%CF%82_Hall\" target=\"_blank\" rel=\"noreferrer noopener\">Hall<\/a>&nbsp;effect&nbsp;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Engine wiring:<br>1, red: motor power supply + (polarity to white reverses motor drive)<br>2, black: encoder power supply &#8211;<br>3, yellow: signal line, (A)<br>4, green: signal line, (B )<br>5, blue: encoder power supply +<br>6, white: motor power supply &#8211; (alternating polarity with red reverses motor drive)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In our first construction we will use only the engine encoder to understand its operation.&nbsp;As you can see in the image below, the motor power cables, which are Red and White, are not connected.&nbsp;Only the encoder cables are connected.&nbsp;We will rotate the motor shaft by hand and monitor the signals generated.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"635\" height=\"801\" src=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/10\/motor_1.jpg\" alt=\"\" class=\"wp-image-538\" style=\"width:341px;height:auto\" srcset=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/10\/motor_1.jpg 635w, https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/10\/motor_1-238x300.jpg 238w\" sizes=\"auto, (max-width: 635px) 100vw, 635px\" \/><figcaption class=\"wp-element-caption\">Encoder connection<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;An encoder works by observing changes in the magnetic field generated by a magnet attached to the motor shaft.&nbsp;Here you will find the principle of operation of the sensor based on the&nbsp;<a href=\"https:\/\/translate.google.com\/website?sl=el&amp;tl=en&amp;nui=1&amp;u=https:\/\/el.wikipedia.org\/wiki\/%CE%91%CE%B9%CF%83%CE%B8%CE%B7%CF%84%CE%AE%CF%81%CE%B1%CF%82_Hall\" target=\"_blank\" rel=\"noreferrer noopener\">HALL<\/a>&nbsp;effect&nbsp;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;As the motor rotates the encoder outputs will be activated periodically.&nbsp;When the magnet is rotated clockwise, output &#8220;A&#8221; will be activated first and when rotated counterclockwise, output &#8220;B&#8221; will be activated first.&nbsp;This way you know exactly when the motor shaft rotates.&nbsp;This can be very convenient for cases where we need to control the forward or backward movement of a DC motor.<br>The following diagram shows the phase difference between outputs A and B of the encoder.&nbsp;We will programmatically exploit this phase difference to control the motor movement.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"324\" height=\"219\" src=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/10\/DC_motor_3.jpg\" alt=\"\" class=\"wp-image-539\" srcset=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/10\/DC_motor_3.jpg 324w, https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/10\/DC_motor_3-300x203.jpg 300w\" sizes=\"auto, (max-width: 324px) 100vw, 324px\" \/><figcaption class=\"wp-element-caption\">Encoder diagram<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;The following figure shows the wiring for two identical engines on an Arduino UNO board. Other equipment will be connected to this board later. We will use in our example only the encoder from one engine.<br>It can be seen that there are two connectors for each motor, one for Motor A and one for encoder A.&nbsp;In this&nbsp;example we will only use the Encoder A<br>connector. Four connectors are connected to this connector. The two ends are for powering the encoder, GND and VCC. The two middle ones are connected one to pin D2 and the other to pin D10. Note here that pins D2 and D3 are the only ones used to create interrupts on the&nbsp;<a href=\"https:\/\/translate.google.com\/website?sl=el&amp;tl=en&amp;nui=1&amp;u=https:\/\/components101.com\/microcontrollers\/arduino-uno\" target=\"_blank\" rel=\"noreferrer noopener\">Arduino UNO<\/a>&nbsp;board.&nbsp;We will encounter this when writing the program.<\/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.jpg\" alt=\"\" class=\"wp-image-540\" style=\"width:280px;height:auto\" srcset=\"https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/10\/note_2.jpg 482w, https:\/\/avlos.gr\/wpress\/wp-content\/uploads\/2025\/10\/note_2-227x300.jpg 227w\" sizes=\"auto, (max-width: 482px) 100vw, 482px\" \/><figcaption class=\"wp-element-caption\">Board wiring<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;Let&#8217;s write a very simple program to understand how a coder works and how to read the coder outputs.<br>To read the encoder, we will connect the encoder output pins to the Arduino terminals 2 and 10.&nbsp;Pin 2 is an interrupt pin.&nbsp;The encoder power cables will be connected to the Arduino 5V and GND.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>\/\/Author :  Manolis Aristovoulidis\n\n\/\/I spin the motor by hand and measure and print\n\/\/ the speed and direction of the movement\n\/\/positive numbers represent clockwise movement,\n\/\/negative numbers represent counterclockwise movement\n\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\nvoid setup()\n{\n  Serial.begin(57600);          \/\/Initialize the serial port\n  EncoderInit();                \/\/Initialize the module\n}\n\nvoid loop()\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  }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the monitoring of the serial port we observe the following results.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">From&nbsp;<a href=\"https:\/\/avlos.gr\/wpress\/download\/motordc_encoder\/\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>&nbsp;you can download the program for the construction<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>Direction:Forword   Pulses:0\nDirection:Forword   Pulses:0\nDirection:Forword   Pulses:0\nDirection:Forword   Pulses:3\nDirection:Forword   Pulses:17\nDirection:Forword   Pulses:38\nDirection:Forword   Pulses:63\nDirection:Forword   Pulses:85\nDirection:Forword   Pulses:102\nDirection:Forword   Pulses:112\nDirection:Forword   Pulses:117\nDirection:Forword   Pulses:112\nDirection:Forword   Pulses:98\nDirection:Forword   Pulses:86\nDirection:Forword   Pulses:74\nDirection:Forword   Pulses:64\nDirection:Forword   Pulses:53\nDirection:Forword   Pulses:42\nDirection:Forword   Pulses:33\nDirection:Forword   Pulses:25\nDirection:Forword   Pulses:17\nDirection:Forword   Pulses:8\nDirection:Forword   Pulses:0\nDirection:Backword  Pulses:-3\nDirection:Backword  Pulses:-17\nDirection:Backword  Pulses:-39\nDirection:Backword  Pulses:-54\nDirection:Backword  Pulses:-67\nDirection:Backword  Pulses:-79\nDirection:Backword  Pulses:-88\nDirection:Backword  Pulses:-92\nDirection:Backword  Pulses:-97\nDirection:Backword  Pulses:-89\nDirection:Backword  Pulses:-77\nDirection:Backword  Pulses:-66\nDirection:Backword  Pulses:-56\nDirection:Backword  Pulses:-45\nDirection:Backword  Pulses:-35\nDirection:Backword  Pulses:-27\nDirection:Backword  Pulses:-19\nDirection:Backword  Pulses:-12\nDirection:Backword  Pulses:-6\nDirection:Backword  Pulses:0\nDirection:Backword  Pulses:0\nDirection:Backword  Pulses:0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When it starts the program correlates the wheelSpeed \u200b\u200b() function with the interrupt that will occur in pin 2. This interrupt will occur whenever the pulse in pin 2 rises (from 0 to 5V) whereupon the wheelSpeed \u200b\u200b() function will be executed. The signal line A of the encoder is connected to pin 2.<br>The first thing the wheelSpeed \u200b\u200b() function does is read the value of pin 10, ie the signal line B of the encoder. If its value is 0 (LOW) it means that the signal A precedes the B, that is, the movement is forward. At the same time it increases the value of the pulses by one unit.<br>In the other case where B precedes A the Direction variable becomes false and the pulse value decreases by one unit.&nbsp;For this reason we have negative values \u200b\u200bfor the pulses when the motion is reversed.<br>The loop executed by the program (ie the loop function) every 100 msec sends the direction and pulse values \u200b\u200bto the serial port and resets the pulses.<br>So as we turn the engine by hand, the values \u200b\u200bwe see in the table below are generated.&nbsp;These are snapshots of the engine condition per 100 msec.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>DC motor &#8211; Encoder In this article we will make a simple construction to understand the operation of the encoder in a DC motor. The materials we will need to implement our construction are All the necessary connections will be&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-536","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\/536","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=536"}],"version-history":[{"count":3,"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/posts\/536\/revisions"}],"predecessor-version":[{"id":545,"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/posts\/536\/revisions\/545"}],"wp:attachment":[{"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/media?parent=536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/categories?post=536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/avlos.gr\/wpress\/wp-json\/wp\/v2\/tags?post=536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}