DC Motor With Encoder Using Arduino Uno

by | Apr 1, 2023 | Arduino, Basic Coding, Coding

Introduction of the Project

Building a DIY DC motor with Encoder using Arduino Uno is an exciting project that can be accomplished by anyone interested in robotics or electronics. DC motors are widely used in various applications due to their simplicity, low cost, and high efficiency. An encoder is a device used to measure a motor shaft’s rotation accurately. By combining a DC motor with an encoder, you can achieve precise control of the motor’s speed and position.

Arduino Uno is a popular microcontroller board that can be easily programmed using Arduino software. It provides a simple and user-friendly interface to interact with various electronic components, including DC motors and encoders. With the help of an Arduino Uno, you can build a DIY DC motor with an encoder and control its rotation speed and position with just a few lines of code.

By following the step-by-step instructions and coding examples, you can learn how to wire and program your DIY DC motor with encoder using Arduino Uno. This project is an excellent opportunity to develop your skills in electronics, programming, and mechanical engineering. It can also serve as a foundation for more complex robotics projects, such as building a robot arm or a mobile robot.

Supplies

To build this Arduino project, we will alter the voltage or current from the power supply, and accordingly, the encoder will change the DC motor’s speed. In order to join DC Motor with Encoder using Arduino Uno, we will require the following components:

Components

We have created our project using the TinkerCad website. But if you are doing this project physically, then you will also need some basic tools, such as a soldering iron, wire cutters, and pliers.

Circuit Diagram

Steps To Connect DC Motor With Encoder Using Arduino Uno

Step 1: Gather all the required components in one place.

Step 2: Plug the H – Bridge Motor Driver into the BreadBoard.

DC Motor with Encoder:

Step 3: Connect the Motor Negative Terminal of it to the Output 2 pin of the H-Bridge.

Step 4: Connect the Motor Positive Terminal of it to the Output 1 pin of the H-Bridge.

Step 5: Connect the Encoder Ground terminal of it to the GND pin of the Arduino.

Step 6: Connect the Channel B terminal of it to the 3-number pin of the Arduino.

Step 7: Connect the Channel A terminal of it to the 2-number pin of the Arduino.

Step 8: Connect the Encoder Power terminal of it to the 5V pin of the Arduino.

LED:

Step 9: Connect the Cathode terminal of it to the GND pin of the Arduino.

Step 10: Connect the Anode terminal of it to the positive of the Power supply thru a resistor.

H – Bridge Motor Driver:

Step 11: Connect the Power1 terminal of it to the 5V pin of the Arduino.

Step 12: Connect the Power2 terminal of it to the Positive Terminal of the Power Supply.

Step 13: Connect both the Ground terminals of it to the GND pin of the Arduino.

Step 14: Connect both the Input Terminals of it to the 5 & 6 number pin of the Arduino, respectively.

Step 15: Connect the Enable 1 & 2 Terminals of it to the 9-number pin of the Arduino.

Power Supply:

Step 16: Connect the Negative terminal of it to the GND pin of the Arduino.

Source Code

int input1 = 5;
int input2 = 6;
int enablePin = 9;
int LED = 13;
int encoderPinA = 2;
int encoderPinB = 3;
int encoderPos = 0;
float ratio = 360./188.611/48.;
float Kp = 30;
float targetDeg = 360;
void setup()
{
pinMode(encoderPinA, INPUT);
attachInterrupt(0, encoderA, CHANGE);


pinMode(encoderPinB, INPUT);
attachInterrupt(1, encoderB, CHANGE);


pinMode(LED, OUTPUT);
pinMode(input1, OUTPUT);
pinMode(enablePin, OUTPUT);
}
void loop()
{
float motorDeg = float(encoderPos) * ratio;
float error = targetDeg - motorDeg;
float control = Kp * error;


digitalWrite(enablePin, 255);
motor((control>=0)? HIGH : LOW, min(abs(control), 255));
}
void encoderA()
{
encoderPos += (digitalRead(encoderPinA) == digitalRead(encoderPinB))? 1 : -1;
}
void encoderB()
{
encoderPos += (digitalRead(encoderPinA) == digitalRead(encoderPinB))? -1 : 1;
}
void motor(bool dir, int val)
{
digitalWrite(input1, dir);
digitalWrite(LED, dir);
analogWrite(input2, dir?(255 - val):val);
}

Explanation of the Code

1. The first thing we did is we initialize some variables of the components to the pin number of the Arduino to which they are connected, such as the encoder and other inputs.

2. After that, we initialized the float ratio, in which 360 represents 1 turn, 188.611 is the gear ratio, and 48 is the motor shaft.

3. In the setup function, we have configured the pins for Input and Output purposes.

4. In the loop function, we are calculating the motor degree, error, and control and using functions to produce the desired output.

5. We have used conditional statements to get the values. If the statement is true, the first one will be considered; else, the second one will be considered.

6. Finally, we are changing the encoder position wrt the encoder pin.

Output

On switching ON the Arduino, we will be able to control the speed of the DC motor by altering the Voltage or current from the Power supply, as shown in the figure.

DC Motor With Encoder Using Arduino Uno

In summary, building a DIY DC motor with encoder using Arduino Uno is a fun and educational project that can introduce you to the exciting world of robotics and electronics. With the right components and tools, you can create a functional motor system that can be controlled with precision and accuracy.

You May Also Like To Create…

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *