Controlling Speed and angle of DC Motor with encoder simultaneously

Hello :slight_smile:

I'm new to Arduino and I need some guidance on my project. My project is about controlling the speed of DC motor with encoder and its angle of rotation using potentiometers.

Project Description:

The project is about controlling the speed and also its angle to which the motor should oscillate back and forth to the set angle and speed. Two potentiometers are used, one for speed and the other for the angle.

Project Parts:

HD Hex motor (REV-41-1391)
Two potentiometers
L298N motor Driver
Arduino Mega 2560

Hope to get some help on the steps on how to achieve this :slight_smile:

Thank you

Start looking at example code for reading pots. Then look for example code for running motors, reading end switches...
..

Hello

I have achieved my first objective of the project which is to control the speed using potentiometers. However, I am not able to control the angle of rotation.

Any help will be appreciated :slight_smile:

Below is the code for controlling the speed:

//Speed Control Using Potentiometer//
int input1 = 52;        
int input2 = 53;
int PWM = 11;  
int speed1;
int speed_1;

void setup() {
  Serial.begin(9600);
  pinMode(52, OUTPUT);
  pinMode(53, OUTPUT);
  pinMode(9, OUTPUT);  
}

void clockwise(){
  digitalWrite(input1, LOW);            
  digitalWrite(input2, HIGH);
}

void anticlockwise(){
  digitalWrite(input1, HIGH);            
  digitalWrite(input2, LOW);
}

void loop() {
  clockwise();
  speed_1 = analogRead(A0);
  speed1 = map(speed_1, 0, 1023, 0, 255);
  analogWrite(PWM, speed1);           
  Serial.println("Pot value = ");
  Serial.print(speed_1 );
  Serial.println("PWM = ");
  Serial.print(speed1 );
}

Hi,
Can you post a copy of your circuit please?
What are you using to control the power to the motors?

If you want to control the angle of the motor shaft you will need to have a sensor/encoder to measure the shaft angle to begin with.

You need to tidy up your code too, your variable names are very confusing.
This is your code with some edits.

//Speed Control Using Potentiometer//
int motorcontrolPin1 = 52;
int motorcontrolPin2 = 53;
int potentiometerPin = A0;
int PWMoutPin = 11;
int speed1;
int speed_1;

void setup()
{
  Serial.begin(9600);
  pinMode(motorcontrolPin1, OUTPUT);
  pinMode(motorcontrolPin2, OUTPUT);
  pinMode(9, OUTPUT);
}

void clockwise()
{
  digitalWrite(motorcontrolPin1, LOW);
  digitalWrite(motorcontrolPin2, HIGH);
}

void anticlockwise()
{
  digitalWrite(motorcontrolPin1, HIGH);
  digitalWrite(motorcontrolPin2, LOW);
}

void loop()
{
  clockwise;
  speed_1 = analogRead(potentiometerPin);
  speed1 = map(speed_1, 0, 1023, 0, 255);
  analogWrite(PWMoutPin, speed1);
  Serial.print("Pot value = ");
  Serial.print(speed_1 );
  Serial.print("  PWM = ");
  Serial.println(speed1 );
}

Thanks.. Tom.. :slight_smile:

Hello

Thank you for the feedback :). I will improve on the variable names :).

For your information, I do not have the circuit diagram because I am building this project from scratch. Therefore, I am building and testing little by little and hence I do not have a fixed circuit diagram for this.

This is the parts I'm using:

HD Hex motor (REV-41-1391)
Two potentiometers
L298N motor Driver
Arduino Mega 2560

To control and power the motor I am using the L298N motor driver. From this particular driver, I have one pin connected to pin 52 of Arduino and the other pin of the driver to pin 53 of Arduino these pins are used to control the direction of rotation of the motor shaft. One pin from the L298N driver goes into pin 11 of Arduino which is for PWM. The motor positive and negative are connected to the dedicated connectors on the driver. The driver is powered up using a power supply which is set to 12V. The negative of the power supply is also connected to the common ground of the circuit.

The motor I am using has an encoder with it. Channel A and channel B of the encoder are connected to interrupt pin 2 and 3 of Arduino respectively. The encoder Vcc is connected to 5V from Arduino and the encoder ground is connected to the common ground of the circuit

For the potentiometers, one is connected to pin A0 to control the speed and the other to pin A1 to control the angle.

Hope this information has given you a fair idea about the connections.

Hi,
From what you have said in your post can you please draw a circuit to show how far you have come.

Thanks.. Tom.... :slight_smile:

For the circuit, a pencil and paper are usually good enough.
Take a pic and post it here.
While you are at it, snap a few pics of your project as well. That often helps us understand what you are doing.

However, I am not able to control the angle of rotation.

What progress have you made? Can you read the encoder counts?

How do you know the starting point for the motor? What type of angular control are you trying to achieve?

Hello everyone :slight_smile:

Thanks for helpful feedback :). Till today I am able to vary the speed using a potentiometer and also I am able to count pulses from one of the channels.

Below is the code :slight_smile: :

//Pulse Counting and Varying Speed Using Potentiometer

volatile unsigned long int count = 0;
int motorcontrolPin1 = 52;
int motorcontrolPin2 = 53;
int PWM = 9;
int speed_1;
int speed1;

void setup() {
 pinMode(2, INPUT); 
 pinMode(52, OUTPUT);
 pinMode(53, OUTPUT);
 pinMode(9, OUTPUT);
 digitalWrite(2, HIGH); //enable internal pullup resistor
 attachInterrupt(digitalPinToInterrupt(2), interruptName, RISING);
 Serial.begin(9600);
}

void clockwise()
{
  digitalWrite(motorcontrolPin1, LOW);
  digitalWrite(motorcontrolPin2, HIGH);
  
}

void loop() {
  clockwise();
  speed_1 = analogRead(A0);
  speed1 = map(speed_1, 0, 1023, 0, 255);
  analogWrite(PWM, speed1);           
  Serial.print("Pot value = ");
  Serial.println(speed_1 );
  Serial.print("PWM = ");
  Serial.println(speed1 );
  Serial.print("count = ");
  Serial.println(count );
  delay(100);
}

void interruptName()
{
  count = count+1;
}

I need help on how to set the angle of rotation (0deg to 360deg). using the other potentiometer and make the motor shaft rotate back and forth in that particular set angle using the potentiometer. Also, I should be able to vary the speed using the potentiometer connected to A0 anytime. This condition also applies to the setting of angle rotation.

Attached is the picture and circuit Diagram :):

Hope to get some response :slight_smile:

Hi,
Ops pics;



Can you post a picture of a hand drawn schematic please, Frittzy pictures do no shpw component names or wiring labels?

Tom... :slight_smile:

Also, I should be able to vary the speed using the potentiometer connected to A0 anytime.

Does not your current code do that?

speed_1 = analogRead(A0);
  speed1 = map(speed_1, 0, 1023, 0, 255);
  analogWrite(PWM, speed1);

I need help on how to set the angle of rotation (0deg to 360deg). using the other potentiometer and make the motor shaft rotate back and forth in that particular set angle using the potentiometer.

Forget about the potentiometer to get started. How may counts per revolution is your encoder reading showing? Make the motor rotate back and forth 180 degrees
by using the value of counts/revolution divided by 2.

Home position will be a major issue. What is your thinking about that?

Looks like the encoder does 7 basic counts per revolution, what is the gearbox ratio? 40:1, 20:1?

Hello :slight_smile:

I have attached the hand-drawn the circuit diagram as requested :).

Does not your current code do that?

Yes with the current code I am able to vary the speed :).

How may counts per revolution is your encoder reading showing?

The following are the specifications of the motor:

Gear Ratio - 40:1
Free Speed (RPM) - 150
Cycles per Rotation of the Encoder Shaft - 28 (7 Rises of Channel A)
Counts per Rotation of the Output Shaft - 1120 (280 Rises of Channel A)

Home position will be a major issue. What is your thinking about that?

No idea about this :neutral_face:

Forget about the potentiometer to get started. How may counts per revolution is your encoder reading showing? Make the motor rotate back and forth 180 degrees
by using the value of counts/revolution divided by 2.

I do not know much about how to achieve this :slightly_frowning_face:

Hope for some helpful responses :slight_smile:

Hi,
Thanks for the schematic;

Tom.... :slight_smile:

Hi,
Why have you got one side of the motor and Out2 to gnd, when the 298 is designed to take both motor wires so it can control direction?
Your current circuit cannot change the motors direction.
Are you using a 298 IC or a shield/module?

Thanks.. Tom... :slight_smile:

Why have you got one side of the motor and Out2 to gnd, when the 298 is designed to take both motor wires so it can control direction?
Your current circuit cannot change the motors direction.

I had wondered about disappearance of anticlockwise() from the code :confused:

There are many tutorials about the use of the LN298. Here's a pretty good one

Hi

Why have you got one side of the motor and Out2 to gnd, when the 298 is designed to take both motor wires so it can control direction?

Ya i made the mistake while drawing my schematic. The correct schematic is attached below :slight_smile: .

Also attached is the picture of L298N H-Bridge.

Any help regarding the control of angle of rotation will be highly appreciated :slight_smile: .

Thanks

Hi,
Thanks for the pics.



Tom.... :slight_smile:

Hello

For your information i have manually checked my encoder pulse of one channel ie channel A. I got 280 pulse for one revolution of the output shaft :).

The code is below:

volatile unsigned long int encoderValue=0;

void setup()
{
Serial.begin(9600);
pinMode(2,INPUT);
attachInterrupt(digitalPinToInterrupt(2),count,FALLING);
}

void loop()
{ 
Serial.print("Encoder Value="); 
Serial.println(encoderValue);
}

void count()
{
encoderValue++;
}

My aim is to achieve the angle of shaft rotation, need some help :).

With the correct wiring, can you make the motor run clockwise, stop, and run counter clockwise?

Can you make the motor run some number of counts and stop?

There are 280 counts for 360 degrees of rotation.

My aim is to achieve the angle of shaft rotation, need some help

What specifically do you need help with?

Post your best attempt at getting the motor to run 180 degrees (140 encoder counts)back and forth.

1 Like