Controlling DC motor as Servo

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

Motor Driver: 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

Have you googled "Arduino motor with encoder" ?

1 Like

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

best regards Stefan

1 Like

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?

best regards Stefan

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.. :grinning: :+1: :coffee: :australia:

1 Like

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.

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

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:-

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.

Hello 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++.

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.

  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

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

never become true

best regards Stefan

Your is missing a semicolon here:

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

best regards Stefan

1 Like

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.. :grinning: :+1: :coffee: :australia:

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

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.

I have created an online-simulation of your code.

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

1 Like

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