# Controlling DC motor as Servo

**URL:** <https://forum.arduino.cc/t/controlling-dc-motor-as-servo/1167015>\
**Category:** Motors, Mechanics, Power and CNC\
**Created:** [September 10, 2023, 7:09am UTC](https://forum.arduino.cc/t/controlling-dc-motor-as-servo/1167015 "2023-09-10T07:09:49Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![vivek\_samani](https://avatars.discourse-cdn.com/v4/letter/v/a88e57/32.png) [@vivek\_samani](https://forum.arduino.cc/u/vivek_samani)\
**Post date:** [September 10, 2023, 7:09am UTC](https://forum.arduino.cc/t/controlling-dc-motor-as-servo/1167015/1 "2023-09-10T07:09:49Z")

</div>

Currently I am working on a dc geared motor which has a encoder (6 wires, 2 for powering encoder and 2 for powering motor and other 2 : en-A & en-B).

Motor:[https://robokits.co.in/motors/mega-torque-18v-250w-dc-motor/mega-torque-encoder-servo-motor/mega-torque-dc-planetary-geared-encoder-servo-motor-250w-25rpm-18vdc-650kgcm](https://robokits.co.in/motors/mega-torque-18v-250w-dc-motor/mega-torque-encoder-servo-motor/mega-torque-dc-planetary-geared-encoder-servo-motor-250w-25rpm-18vdc-650kgcm)

Motor Driver: [https://robu.in/product/cytron-20amp-6v-30v-dc-motor-driver-md20a/](https://robu.in/product/cytron-20amp-6v-30v-dc-motor-driver-md20a/)

My task is to make this dc motor work like a servo i.e. we give angle to it and it rotates accordingly the command feed to it like a typical servo does.

Please help me write such a arduino code for that

---

<div class="post-metadata">

**Author:** ![anon95813201](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@anon95813201](https://forum.arduino.cc/u/anon95813201)\
**Post date:** [September 10, 2023, 8:23am UTC](https://forum.arduino.cc/t/controlling-dc-motor-as-servo/1167015/2 "2023-09-10T08:23:01Z")

</div>

Have you googled "Arduino motor with encoder" ?

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [September 10, 2023, 9:43am UTC](https://forum.arduino.cc/t/controlling-dc-motor-as-servo/1167015/3 "2023-09-10T09:43:37Z")

</div>

> [@vivek\_samani](#):
>
> Please help me write such a arduino code for that

This short sentence translates to  
"can you please write the code for me so that I don't have to learn anything and just can use the code"

This is not the way how this forum works.  
**You** have to show **own** effort  
**You** have to show that **you climb up** the learning-curve

So a much better question is:

Can you please post links to a good tutorial where I can read about how to write code that

> [@vivek\_samani](#):
>
> we give angle to it and it rotates accordingly the command feed to it like a typical servo does.

best regards Stefan

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [September 10, 2023, 9:51am UTC](https://forum.arduino.cc/t/controlling-dc-motor-as-servo/1167015/4 "2023-09-10T09:51:53Z")

</div>

As soon as you have **specific** questions to whatever detail you will get helpful answers

The question could even be  
"If have googled for ...." and found a lot of links.  
Though as I don't know anything about it yet I'm unable to decide which link is worth reading and which not  
Can somebody with more experience give some suggestions what sources have a high probability to be helpful  
or  
can do a **quick cross-reading** of these links

- your link list

and post her/his estimation which links might be helpful?

> [@anon95813201](#):
>
> Have you googled "Arduino motor with encoder" ?

> **[Arduino Motor With Encoder](https://letmegooglethat.com/?q=Arduino+motor+with+encoder)**
>
> For those who think it's easier to annoy you than to Google 'Arduino motor with encoder' themselves.

best regards Stefan

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [September 10, 2023, 9:58am UTC](https://forum.arduino.cc/t/controlling-dc-motor-as-servo/1167015/5 "2023-09-10T09:58:14Z")

</div>

Hi, @vivek_samani

Have you been able to control the motor?  
Have you been able to read the encoder?  
In separate small bits of code to make sure you can read and control them, **BEFORE** trying anymore coding.

You need to develop your code in stages and then you will be able to understand how it works.  
Getting a full on all bells and whistles code from someone else will teach you very little and how to debug it.

Can you please tell us your electronics, programming, arduino, hardware experience?

Is this a school/college/university project?

Thanks.. Tom.. 😀 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [September 10, 2023, 10:00am UTC](https://forum.arduino.cc/t/controlling-dc-motor-as-servo/1167015/6 "2023-09-10T10:00:37Z")

</div>

Some additional information about RC-Servos.

Most RC-Servos work very different than what you described.

A RC-Servo is **permanently** supplied with power over a plus and Ground-wire  
and  
a the RC-Servo receives a **specialised** PWM-signal over a single wire and move the servo-horn according to the **pulse-length** to a position.

All the control is done **inside** the RC-Servo

What you are planning to build as DC-Servo-Motor where the motor-controller and encoder are **outside** of the Motor-housing and all controlling is done by an external microcontroller.

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [September 10, 2023, 5:31pm UTC](https://forum.arduino.cc/t/controlling-dc-motor-as-servo/1167015/7 "2023-09-10T17:31:25Z")

</div>

> [@vivek\_samani](#):
>
> make this dc motor work like a servo

Dronebotworkshop has a nice page and video on building a servo from a dc motor...  
[https://dronebotworkshop.com/custom-servo-motor/](https://dronebotworkshop.com/custom-servo-motor/)

---

<div class="post-metadata">

**Author:** ![vivek\_samani](https://avatars.discourse-cdn.com/v4/letter/v/a88e57/32.png) [@vivek\_samani](https://forum.arduino.cc/u/vivek_samani)\
**Post date:** [September 14, 2023, 11:48am UTC](https://forum.arduino.cc/t/controlling-dc-motor-as-servo/1167015/8 "2023-09-14T11:48:48Z")

</div>

Yes I have tried controlling the motor like a servo using the below code. Also I looked for may such videos but in my case the type of motor didn't matched but yes I tried to understand the idea from it and atleast wrote the code on my own.

**My code:-**

```arduino
int a=2;
int b=3;
int k;
int v;
int pp=9;
int d=10;
int pos=0
int angle;

void setup() {
  
Serial.begin(9600);
pinMode(a,INPUT);
pinMode(b,INPUT);
pinMode(pp,OUTPUT);
pinMode(d,OUTPUT);
}

void loop()
{  
  k=digitalRead(a);
  v=digitalRead(b);
  if(k>v)
{
pos++;
Serial.println("clk=");
angle=pos;
Serial.print(pos);
  }
  else if(v>k)
{
    pos--;
    Serial.println("anticlk=");
angle=pos;
Serial.print(pos);
  }
  else
{
    pos=0;
    Serial.print("no rotation=");
    angle=pos;
Serial.print(pos);
  }
  
angle=map(angle,0,8192,0,360);
}

```

Please help me with this to improve the flow of the code. The problem with our code is that the iteration doesn't even goes inside the first if condition itself. We have tried debugging it but end up with no solution. Please suggest some ideas for controlling such dc motors like a typical standard servo motor.

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [September 14, 2023, 12:07pm UTC](https://forum.arduino.cc/t/controlling-dc-motor-as-servo/1167015/9 "2023-09-14T12:07:57Z")

</div>

> [@vivek\_samani](#):
>
> ```arduino
> int a=2;
> int b=3;
> int k;
> int v;
> int pp=9;
> int d=10;
> 
> ```

Hello [vivek\_samani](https://forum.arduino.cc/u/vivek_samani)

In general, you should not use magic numbers. The I/O pins love to have a functional name.

Take some time and describe the desired function of the sketch in simple words and do this very simply.

Have a nice day and enjoy coding in C++.

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [September 14, 2023, 3:28pm UTC](https://forum.arduino.cc/t/controlling-dc-motor-as-servo/1167015/10 "2023-09-14T15:28:31Z")

</div>

> [@vivek\_samani](#):
>
> Yes I have tried controlling the motor like a servo using the below code

Then - accidently - You must have missed to mark that lines of code that control the motor.  
The lines of code that you have posted has only two lines of code that might be related to controlling the motor.

```arduino
  pinMode(pp, OUTPUT);
  pinMode(d, OUTPUT);

```

But I am unsure because I do not understand what the meaning of "pp" and "d" is.

"pp" and "d" is too short to be understandable.

If you want real help from the arduino-forum you should do these things:

1. pressing ctrl-T inside the arduino IDE for automatic formatting your code
2. giving all variables and all **self** -written functions self-explaining names
3. writing a code that does nothing more than making the motor run clockwise / counterclockwise and then post this code

> [@vivek\_samani](#):
>
> The problem with our code is that the iteration doesn't even goes inside the first if condition itself.

The reason might be that you don't drive the motor at all. If both inputs stay on LOW  
or  
if both inputs stay on HIGH  
the conditions

> [@vivek\_samani](#):
>
> `if(k>v)`

> [@vivek\_samani](#):
>
> `else if(v>k)`

never become true

best regards Stefan

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [September 14, 2023, 4:44pm UTC](https://forum.arduino.cc/t/controlling-dc-motor-as-servo/1167015/11 "2023-09-14T16:44:25Z")

</div>

Your is missing a semicolon here:

> [@vivek\_samani](#):
>
> ```arduino
> int d=10;
> int pos=0 <<<<<==== mising semicolon
> int angle;
> 
> ```

it might be that your latest code-version did not upload at all.

Here is **exactly** your code. Exactly in the sense of exact same logic but with additional debug-output that makes visible what you code is doing.

Here is a WOKWI-Simulation of your code

> **[DC-Servo-Motor-Testcode-001 - Wokwi ESP32, STM32, Arduino Simulator](https://wokwi.com/projects/375863349227983873)**
>
> Run IoT and embedded projects in your browser: ESP32, STM32, Arduino, Pi Pico, and more. No installation required!

best regards Stefan

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [September 14, 2023, 9:56pm UTC](https://forum.arduino.cc/t/controlling-dc-motor-as-servo/1167015/12 "2023-09-14T21:56:36Z")

</div>

> [@TomGeorge](#):
>
> Have you been able to control the motor?  
> Have you been able to read the encoder?  
> In separate small bits of code to make sure you can read and control them, **BEFORE** trying anymore coding.
> 
> You need to develop your code in stages and then you will be able to understand how it works.  
> Getting a full on all bells and whistles code from someone else will teach you very little and how to debug it.
> 
> Can you please tell us your electronics, programming, arduino, hardware experience?
> 
> Is this a school/college/university project?

Can you please post a copy of your circuit a picture of a hand drawn circuit in jpg, png?  
Hand drawn and photographed is perfectly acceptable.  
Please include ALL hardware, power supplies, component names and pin labels.

Thanks.. Tom.. 😀 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![vivek\_samani](https://avatars.discourse-cdn.com/v4/letter/v/a88e57/32.png) [@vivek\_samani](https://forum.arduino.cc/u/vivek_samani)\
**Post date:** [September 19, 2023, 7:47pm UTC](https://forum.arduino.cc/t/controlling-dc-motor-as-servo/1167015/13 "2023-09-19T19:47:58Z")

</div>

pp here I mean pwm (PWM pin on my motor driver)  
d here I mean direction (DIR pin om my motor driver)

---

<div class="post-metadata">

**Author:** ![vivek\_samani](https://avatars.discourse-cdn.com/v4/letter/v/a88e57/32.png) [@vivek\_samani](https://forum.arduino.cc/u/vivek_samani)\
**Post date:** [September 19, 2023, 7:51pm UTC](https://forum.arduino.cc/t/controlling-dc-motor-as-servo/1167015/14 "2023-09-19T19:51:17Z")

</div>

Yes I'm working for a robotics competition.  
I have an experience of working with arduino for more than a year now.

Well I was able to read the encoder

There are 4 wires coming out from the motor  
From my best of knowledge 2 for power and 2 for respective encoders A & B.

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [September 19, 2023, 8:45pm UTC](https://forum.arduino.cc/t/controlling-dc-motor-as-servo/1167015/15 "2023-09-19T20:45:50Z")

</div>

I have created an online-simulation of your code.

> **[DC-Servo-Motor-Testcode-001 - Wokwi ESP32, STM32, Arduino Simulator](https://wokwi.com/projects/375863349227983873)**
>
> Run IoT and embedded projects in your browser: ESP32, STM32, Arduino, Pi Pico, and more. No installation required!

you shoud run ths simulation to see what your code-logic does so far.  
Seeing printed what your code is doing should give you new ideas  
what you have to change in your code.

If you hoped for that your strategy can be post a little code to make other users write ready to use code.

It is very unlikely to happen. Yiu have to show **own effort** and ask **specific** questions.

Then you will get a lot of useful answers

best regards Stefan

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [March 17, 2024, 8:46pm UTC](https://forum.arduino.cc/t/controlling-dc-motor-as-servo/1167015/16 "2024-03-17T20:46:13Z")

</div>

Closed on March 17, 2024, 8:46pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
