In the previous blog post, "Joystick controlled DC motor with Arduino and TIP122", we showed how to control the speed of a DC motor using a joystick and the TIP122 transistor. However, we only utilized the x-axis of the joystick for controlling the speed. In this Arduino joystick example, we will show how to control both the speed and direction of a DC motor using a joystick, Arduino, and the L298N H-Bridge motor controller.
Here Arduino Mega, an open-source microcontroller platform based on easy-to-use hardware and software will be used. It is widely used for various applications including robotics, automation, and control systems. In these applications, the use of a joystick is a popular input device for controlling the motion of the system. A joystick can provide two-axis control, with one axis controlling speed and the other axis controlling direction.
One way to control a DC motor using an Arduino and joystick is by using Arduino L298N DC motor controller. The L298N is a dual H-Bridge motor driver that allows for control of the speed and direction of two DC motors. The L298N can be connected to the Arduino and joystick to provide a complete motor control system.
The first step in using a joystick to control a DC motor is to read the joystick's analog inputs with the Arduino. The x-axis of the joystick is used to control the speed of the motor while the y-axis is used to control the direction. The joystick readings are then mapped to a range of PWM values that are used to control the speed of the motor.
Circuit Diagram
The L298N motor controller is connected to the Arduino and the DC motor. The PWM signals from the Arduino is used to control the speed of the DC motor. The direction of the motor is controlled by the y-axis input from the joystick, which determines whether the L298N drives the motor forward or backward.
The following shows Arduino Joystick schematic diagram including L298N motor driver IC(Integrated Circuit), diode bridge and DC motor.
In the Arduino Joystick schematic diagram, the x-axis and y-axis pins of the joystick are connected to Arduino Mega analog pins A0 and A1 respectively. The Vcc and ground pin of the joystick are connected to the +5V power supply and ground where the Arduino ground is also connected. The Arduino Mega pins 9, 7 and 6 are connected to the L298N Integrated Circuit(IC) pins ENA, IN1 and IN2. The output pins OUT1 and OUT2 are connected to the DC motor via the diode bridge as shown in the above circuit diagram.
Arduino Program for Joystick
The code for controlling DC motor using Joystick with Arduino is as follows:
/*
ee-diary.com : DC Motor control using JoyStick
*/
const int ENA = 9;
const int IN1 = 7;
const int IN2 = 6;
const int xPin = A0;
const int yPin =A1;
int xVal = 0;
int yVal = 0;
void setup () {
Serial.begin(9600);
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
digitalWrite(ENA,LOW); //initially stop DC motor
}
void loop() {
xVal = analogRead(xPin);
int xPWM = map(xVal, 0, 1023, 0, 255);
yVal = analogRead(yPin);
//Direction Control
if(yVal <= 300){
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
}
else{
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
}
//Speed Control
analogWrite(ENA, xVal);
Serial.print("PWM: ");
Serial.println(xVal);
Serial.print("direction: ");
Serial.println(yVal);
//delay(50);
}
In the setup function, we first initialize the serial communication at a baud rate of 9600. Then, we define the pins for the Enable, IN1, and IN2 pins of the L298N motor controller as outputs and set the ENA pin to LOW, which stops the motor.
In the loop function, we first read the x and y values from the joystick using the analogRead function and the xPin and yPin constants. The x value, representing the joystick's x-axis, is used to control the speed of the motor, while the y value, representing the joystick's y-axis, is used to control the direction of the motor.
The speed control section maps the x value from the joystick to a PWM signal with a range of 0 to 255 and writes this value to the ENA pin of the L298N motor controller. This controls the speed of the motor by varying the amount of power being supplied to it.
The direction control section uses an if-else statement to determine the direction of the motor based on the y value from the joystick. If the y value is less than or equal to 300, the IN1 pin is set to HIGH and the IN2 pin is set to LOW, causing the motor to rotate in one direction. If the y value is greater than 300, the IN1 pin is set to LOW and the IN2 pin is set to HIGH and causes the motor to rotate in the other direction.
Video demonstration
The following video demonstrates the speed and direction control of a DC motor using Joystick and Arduino.
In conclusion, the combination of an Arduino, Joystick and L298N can
provide a versatile and easy-to-use system for controlling the speed and
direction of DC motors. The use of a joystick for input, and the L298N
for motor control, provides a powerful and flexible platform for a wide
range of applications. It is also possible to control the speed and direction of the DC motor
using two potentiometer, but using a single joystick control is easy way
to build complete motor control system. If you are interested in learning more about how to control speed of dc motor using potentiometer, or using the arduino L298N for DC motor control, be sure to check out relevant blog posts and tutorials.