Arduino DC motor control with joystick and L293D

This post shows how to control a DC motor speed and direction of rotation using Arduino uno, joystick and L293D motor driver.

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).

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:

IN1

IN2

Function

L

H

Direction 1

H

L

Direction 2

L

L

Fast motor stop

H

H

Fast motor stop

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.