Mechatronics System Design - MECA 443 Lab Experiment 3 - Position Control Using An Encoder
Mechatronics System Design - MECA 443 Lab Experiment 3 - Position Control Using An Encoder
ABSTRACT
The aim of this experiment is to control a DC motor using an encoder. In this experiment, the degrees of
rotation as well as the direction of rotation of the DC motor are set by the user. The system should work
as follows: The user inputs a value that corresponds to a distance, i.e., 3 cm. The motor runs clockwise
and travels the distance entered by the user before it stops.
Keywords: DC motor, Motor drive, Mechanical encoder, Arduino Nano.
1. INTRODUCTION
The aim of this experiment is to perform an essential step during the initialization process
which is homing. In this experiment, a limit switch will be used to set a reference for the
system. The speed of the motor is set in the program. The system works as follows. When the
system is turned on, the motor starts rotating until it reaches a limit switch that is placed at
one end of the system. When the limit switch is reached, the motor stops. This sets a
reference point for the system. At this point, the motor starts running at the opposite
direction until it hits another limit switch which stops the motor to indicate that the maximum
traveling distance was reached.
DC motor:
12 volt DC motor is a rotary motor which can
convert the direct current into mechanical
energy or convert mechanical energy into DC
power. It means that the 12 volt DC motor can
1 FIGURE 3- DC motor
interconvert electric energy and mechanical energy. When it is operated as a DC
motor, electric energy is converted into mechanical energy. [2]
Mechanical encoder:
A mechanical encoder is an electromechanical
device that converts position or movement into
a signal or signals that can be read by an
electronic circuit or processor system... [3]
Motor drivers:
Motor drivers acts as an interface between the motors and
the control circuits. ... So the function of motor drivers is
to take a low-current control signal and then turn it into a
higher-current signal that can drive a motor. [4]
2
FIGURE 6- FLOWCHART
3
3.1 Simulation
3
6
3.2 Components
TABLE 1 shows the components numbering and naming used in the circuit.
Component’s Number Component’s Name
1 NI ELVIS
2 NANO Arduino
3 DC motor
4 Motor driver
5 mechanical encoder
TABLE 1: COMPONENTS USED IN THE CIRCUIT
3.4 Wiring
o Connect the two limit switches to pin 2, 3 in the Arduino NANO
o Take common ground between the Arduino and motor driver and the switches
o Connect the DC Motor to the motor drive
o Connect IN1 to pin 4 and IN2 to pin 5
4
4. Tinkercad stimulation
5. CONCLUSION
In brief, we reached our objective to use an encoder to control the position of a DC
motor. Where, the degrees of rotation as well as the direction of rotation of the DC motor are
set by the user. So that, the user inputs a value that corresponds to a distance and we
measured the number of ticks through the given distance so the motor will stop when the
distance is reached. This is done using various materials like 12V DC motor, mechanical
encoder, Arduino NANO, motor drive.
REFERENCES
[1] http://www.tecoelectric.com/blog/12-volt-dc-motors-definition-and-structure/
[2] https://www.arrow.com/en/categories/electromechanical-encoders/mechanical-encoders
[3] https://www.arduino.cc/en/pmwiki.php?n=Main/ArduinoBoardNano
[4] https://sproboticworks.com/blog/choosing-the-right-motor-driver
APPENDIX
int count=0;
int outputA = 6;
int outputB = 5;
int motor1 = 2;
int motor2 = 3;
int currentstateA;
int currentstateB;
int previousstateA;
float distance = 2.5;
5
int finalcount = 25;
void setup()
{
pinMode(motor1,OUTPUT);
pinMode(motor2,OUTPUT);
pinMode(outputA,INPUT);
pinMode(outputB,INPUT);
Serial.begin(9600);
previousstateA = digitalRead(outputA);
void loop()
{
currentstateA = digitalRead(outputA);
if(currentstateA != previousstateA)
{
currentstateB = digitalRead(outputB);
if(currentstateA != currentstateB)
count++;
else
count--;
Serial.println(count);
}
if (count < finalcount)
{
analogWrite(motor1, 0); //right
analogWrite(motor2, 150);
}
else{
analogWrite(motor1, 0); //stop
analogWrite(motor2, 0);
}
previousstateA=currentstateA;
}