This post shows how to control a DC motor speed and direction of rotation using Arduino uno, joystick and L293D motor driver.
In this project, we will focus on the more advanced L298N Motor Driver and see Arduino DC Motor Control using L298N Motor Driver using PWM technique. Also read DC MOTOR CONTROL WITH ARDUINO AND L293D. A Brief Note on L298N Motor Driver. L298N Motor Driver IC is a 15-lead high voltage, high current Motor Driver IC with two full bridge drivers.
The joystick (PS2 Joystick) consists of two potentiometers (one for the X-axis and the other for the Y-axis) and a push-button. With one potentiometer we can control the speed and rotation direction of a DC motor.
The L293D quadruple half-H drivers chip allows us to drive 2 motors in both directions, with two PWM outputs from the Arduino we can easily control the speed as well as the direction of rotation of one DC motor. (PWM: Pulse Width Modulation).
Related topic:
Arduino DC motor speed and direction control with L293D
Components Required:
- Arduino UNO board
- 12V DC motor
- Joystick board
- L293D motor driver IC
- 12V source
- Breadboard
- Jumper wires
DC motor control with Arduino and joystick circuit:
Circuit schematic diagram is shown below.
(All grounded terminals are connected)
L293d Motor Driver Ic
The L293D driver has 2 VCCs: VCC1 is +5V (comes from the Arduino board) and VCC2 is +12V (same as motor nominal voltage). Pins IN1 and IN2 are the control pins where:
L293d Motor Driver Circuit Schematic
IN1 | IN2 | Function |
L | H | Direction 1 |
H | L | Direction 2 |
L | L | Fast motor stop |
H | H | Fast motor stop |
L293d Motor Driver Circuit Pwm Input
As shown in the circuit diagram we need only 3 Arduino terminal pins: A0 (analog pin), pin 9 and pin 10. Pins 9 and 10 are PWM signal outputs, at any time there is only 1 active PWM, this allows us to control the direction of rotation as well as the speed by varying the duty cycle of the active PWM signal. The active PWM pin decides the motor direction of rotation (one at a time, the other output is logic 0).
The joystick board has 5 terminals: GND, +5V, VRX, VRY and SW where:
GND and +5V are power supply pins
VRX is the X-axis potentiometer output
VRY is the Y-axis potentiometer output
SW is the push button terminal (the other terminal is connected to GND).
The output of the X-axis potentiometer is connected to Arduino analog pin A0, Y-axis potentiometer output can be used to control the DC motor. The switch pin (SW) is not used in this example.
Using the X-axis and the Y-axis potentiometers we can control two DC motors independently.
Arduino code:
The resolution of the Arduino UNO ADC is 10 bits which means the digital output value is between 0 and 1023.
The PWM signals has a resolution of 8 bits which means the duty cycle can vary between 0 and 255.
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 | // Arduino joystick DC motor speed and rotation direction control #define joystick A0 #define pwm2 10 intmotor_control; voidsetup(){ pinMode(pwm2,OUTPUT); motor_control=analogRead(joystick); if(motor_control>255){ analogWrite(pwm1,(motor_control-256)); else digitalWrite(pwm1,0); } digitalWrite(pwm1,0); } |
Arduino L293d Stepper Motor Driver Circuit
The video below shows a simple hardware circuit of the project: