Como controlar a velocidade e direção do motor DC usando um joystick e Arduino

How to control DC motor speed and direction using a joystick and Arduino

Just as the name suggests, DC motor controllers control the speed and direction of a DC motor. To change the direction of the motor, however, the power it receives must be reversed. And to vary the speed of the DC motor, a pulse width modulation (PWM) signal or wave must be applied to it.

As the pulse width increases, the average voltage applied to the motor will also increase – and vice versa. This means that the speed of the DC motor varies with the pulse width.

PMW has become a commonly used method for doing just that. But another option that's gaining popularity is controlling the speed and direction of a DC motor using a joystick. How it works: When the joystick is in the center position, the DC motor stops. When the joystick is moved up or down, the motor rotates in the same direction – forward or backward.

Additionally, the further from center the joystick is pushed (in any direction), the faster the engine speed in that same direction. So users can control the speed of the DC motor in this way.

The joystick control method for DC motors is currently used in a number of different applications, including:

1. Remote controlled (RC) toys such as planes, helicopters, boats, cars, etc.
2. Video camera cranes
3. Industrial Jogg Controllers
4. Robotic arms or for robotic vehicles
5. Surveillance Camera Controllers

There are also many other applications, where the DC motor that drives the load is controlled by a joystick. In some, only the direction of the motor is changed (such as in RC toys), while in other applications, both the direction and speed are varied (such as in video camera cranes, jog controls, etc.).

The project below demonstrates the use of a joystick to control the speed and direction of a DC motor. It uses a two-axis resistive joystick using an Arduino NANO development board to control an L298 motor driver.

Circuit Diagram

The circuit is built using three main building blocks with an Arduino NANO board, a two-axis resistive joystick, and an L298 motor driver:

  • The joystick has five interface pins: Vcc, GND, X, Y and button. The Vcc pin receives a 5 V supply from the Arduino board and the GND pin is connected to the board's ground.
  • The X and Y pins are analog outputs connected to the Arduino analog pins A0 and A1. The button pin is not used here.
  • The PWM output of Arduino pins D5 and D6 is connected to the motor driver's IN1 and IN2 input pins. They drive the motor through the motor driver.
  • The 12 V DC motor is connected to the OUT1 and OUT2 output of the motor driver.
  • Both the Arduino and the motor driver receive 12V external power.

Circuit functioning and operation:

Circuit functioning and operation:

  • Initially, when the joystick is in the center or rest position, the engine is stopped. As the joystick is gradually moved upward, the motor begins to run at a slow speed in a clockwise (forward) direction. As the joystick is moved upward from the center, the engine speed increases. When the joystick is as high as possible, the motor reaches its maximum forward speed.
  • As the joystick returns to the center (rest) position, engine speed begins to decrease and stops.
  • Similarly, when the joystick is moved down, the motor starts running counterclockwise (reverse). As the joystick moves away from the center, the engine speed increases until it reaches that point. When the joystick is lowered to the maximum, the engine reaches its maximum speed in reverse.
  • When the joystick is moved all the way to the left or right, the motor runs forward or backward at full speed.

Next, let's review the circuit in action:

  • Moving the joystick toward the center or rest position always slows down the motor. It will stop completely when it is in the center position.
  • When the joystick is moved up or down, its internal resistance (the potentiometer) increases or decreases. Essentially, this increases or decreases the analog output voltage to pin X.
  • The Arduino will read the analog voltage and convert it to a digital value, which ranges from 0 to 1023 depending on whether the joystick moves all the way up or all the way down.
  • When the joystick is in the center position, the Arduino receives a value of about 510. When it is moved up, the value gradually increases from 510 to 1023 maximum. Similarly, when the joystick is moved down, the value decreases from 510 to 0 maximum.
  • Based on these values, the Arduino generates PWM on pins D5 and D6. When the joystick moves up, the PWM value gradually increases from 0 to 255 (0 – 100%) on pin D5 (and the motor speed accelerates forward). When the joystick moves down, the PWM value increases at pin D6 (and the motor speed accelerates in the reverse direction).
  • Similarly, moving the joystick left or right will increase or decrease the analog output on pin Y. The Arduino will read the analog voltage and convert it to a digital value.
  • When the joystick is moved to the right, the value will be greater than 750. As a result, the Arduino will supply 100% PWM signal to pin D5 (and the motor will advance at full speed). When the joystick is moved to the left, the value will be less than 250. Now the Arduino will give 100% PWM signal to pin D6 (and the motor will run in reverse at full speed).
  • Motor speed increases and decreases as the joystick is moved. It will also change direction depending on whether the joystick is moved up or down.

The operation of this circuit depends on the program that is downloaded into the internal memory (FLASH) of the Arduino ATMega328 microcontroller. This program was written in C language, using the Arduino IDE software. It also uses the Arduino “DC_Motor” library that I developed (and is available on this website, Garagem de Engenheiros ).

Software program:

#include
DC_Motor dcm(5,6,1);
int engine_speed,set_speed;
null configuration
{

}

empty loop
{
int x_value,y_value;
x_value = analog reading (A0);
if((x_value>=490) && (x_value<=530)) dcm.motor_speed_zero ;
if(x_value>550)
{
set_speed = map(x_value,550,1020,10,100);
dcm.run_motor(1,set_speed);
}
if(x_value<470)
{
set_speed = map(x_value,470,0,10,100);
dcm.run_motor(0,set_speed);
}
y_value = analog reading (A1);
if(y_value>750)
{
dcm.jogg_set_speed(1.100);
}
if(y_value<250)
{
dcm.jogg_set_speed(0,100);
}
}

Related Content

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.