L298N Motor Driver with Arduino Codes, Schematics:
we will learn how to control DC motors using the L298N motor driver and the Arduino board. PWM DC Motor Control PWM, or pulse width modulation is a technique which allows us to adjust the average value of the voltage that’s going to the electronic device by turning on and off the power at a fast rate. The average voltage depends on the duty cycle, or the amount of time the signal is ON versus the amount of time the signal is OFF in a single period of time.
Why we use Motor Driver?
So depending on the size of the motor, we can simply connect an Arduino PWM output to the base of transistor or the gate of a MOSFET and control the speed of the motor by controlling the PWM output.
The low power Arduino PWM signal switches on and off the gate at the MOSFET through which the high power motor is driven.
H-Bridge DC Motor Control:
On the other hand, for controlling the rotation direction, we just need to inverse the direction of the current flow through the motor, and the most common method of doing that is by using an H-Bridge.
Alternatives for DC Motor Driver:
1. L298N.
2. L293D.
3. MX1508
L298N Driver:
The L298N is a dual H-Bridge motor driver which allows speed and direction control of two DC motors at the same time. The module can drive DC motors that have voltages between 5 and 35V, with a peak current up to 2A.The module has two screw terminal blocks for the motor A and B, and another screw terminal block for the Ground pin, the VCC for motor and a 5V pin which can either be an input or output.
Next, the Input 1 and Input 2 pins are used for controlling the rotation direction of the motor A, and the inputs 3 and 4 for the motor B. Using these pins we actually control the switches of the H-Bridge inside the L298N IC. If input 1 is LOW and input 2 is HIGH the motor will move forward, and vice versa, if input 1 is HIGH and input 2 is LOW the motor will move backward. In case both inputs are same, either LOW or HIGH the motor will stop. The same applies for the inputs 3 and 4 and the motor B.
Arduino Robot Car Control using L298N Motor Driver:
All we need is 2 DC Motors, the L298N motor driver, an Arduino board and a joystick for the control. As for the power supply, I chose to use three 3.7V Li-ion batteries, providing total of 11V. I made the chassis out of 3 mm tick plywood, attached the motors to it using metal brackets, attached wheels to the motors and in front attached a swivel wheel.
We will add a little tolerance and consider the values from 470 to 550 as center. So if we move the Y axis of joystick backward and the value goes below 470 we will set the two motors rotation direction to backward using the four input pins. Then, we will convert the declining values from 470 to 0 into increasing PWM values from 0 to 255 which is actually the speed of the motor.
L298N Motor Driver with Arduino Codes, Schematics:
/* www.androiderode.com*/ #define enA 9 #define in1 4 #define in2 5 #define enB 10 #define in3 6 #define in4 7 int motorSpeedA = 0; int motorSpeedB = 0; void setup() { pinMode(enA, OUTPUT); pinMode(enB, OUTPUT); pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); pinMode(in3, OUTPUT); pinMode(in4, OUTPUT); } void loop() { int xAxis = analogRead(A0); // Read Joysticks X-axis int yAxis = analogRead(A1); // Read Joysticks Y-axis if (yAxis < 470) { // Set Motor A backward digitalWrite(in1, HIGH); digitalWrite(in2, LOW); // Set Motor B backward digitalWrite(in3, HIGH); digitalWrite(in4, LOW); //motorSpeedA = map(yAxis, 470, 0, 0, 255); motorSpeedB = map(yAxis, 470, 0, 0, 255); } else if (yAxis > 550) { // Set Motor A forward digitalWrite(in1, LOW); digitalWrite(in2, HIGH); // Set Motor B forward digitalWrite(in3, LOW); digitalWrite(in4, HIGH); motorSpeedA = map(yAxis, 550, 1023, 0, 255); motorSpeedB = map(yAxis, 550, 1023, 0, 255); } else { motorSpeedA = 0; motorSpeedB = 0; } if (xAxis < 470) { int xMapped = map(xAxis, 470, 0, 0, 255); motorSpeedA = motorSpeedA - xMapped; motorSpeedB = motorSpeedB + xMapped; if (motorSpeedA < 0) { motorSpeedA = 0; } if (motorSpeedB > 255) { motorSpeedB = 255; } } if (xAxis > 550) { int xMapped = map(xAxis, 550, 1023, 0, 255); motorSpeedA = motorSpeedA + xMapped; motorSpeedB = motorSpeedB - xMapped; if (motorSpeedA > 255) { motorSpeedA = 255; } if (motorSpeedB < 0) { motorSpeedB = 0; } } if (motorSpeedA < 70) { motorSpeedA = 0; } if (motorSpeedB < 70) { motorSpeedB = 0; } analogWrite(enA, motorSpeedA); // Send PWM signal to motor A analogWrite(enB, motorSpeedB); // Send PWM signal to motor B }