You are on page 1of 12

Robotics Lab LAB 2

Motor Speed Control using Motor Driver.

Objectives:

- Using Arduino I/O commands.


- To produce PWM signals to control motor driver
- To get familiarized with Arduino libraries

Introduction
Motor speed and direction control is a fundamental aspect of robotics
and automation, playing a pivotal role in determining the movement,
precision, and functionality of robotic systems. Whether it's in industrial
automation, mobile robots, or even hobbyist projects, the ability to
precisely manage the speed and direction of motors is the cornerstone of
achieving tasks efficiently and with precision. By modulating the
rotational speed and steering of motors, we unlock a world of
possibilities, from the precise positioning of robotic arms to the
navigation of autonomous vehicles. In this realm of control, we delve
into the technologies and methodologies that empower us to manipulate
motors with finesse, turning them into the obedient workhorses of our
mechanized world.

Direction of a DC Motor:

To change the direction of a DC motor, you need to reverse the polarity of the voltage applied
to the motor. This can be accomplished in several ways, depending on your application and the
available components. Here are some common methods for reversing the direction of a DC
motor:

A
Robotics Lab LAB 2

1. H-Bridge Circuit:
An H-bridge is a popular and versatile circuit for changing the direction of a DC motor. It uses
a combination of transistors or relays to control the voltage polarity to the motor.
- Depending on the logic level applied to the control pins of the H-bridge, you can make the
motor rotate clockwise, counterclockwise, or stop it.

2. DPDT (Double-Pole, Double-Throw) Switch:


You can use a DPDT switch to manually reverse the direction of the motor. By connecting the
motor to the switch and toggling the switch position, you can change the polarity of the voltage
applied to the motor.

3. Relay Circuit:
A relay is an electromagnetic switch that can be used to reverse the direction of the motor.
Connect the motor to the common terminal of the relay and use the normally open (NO) and
normally closed (NC) contacts to change the motor's direction.

4. Electronic Motor Driver/Controller:


Many motor driver boards or modules are available that make it easy to control DC motors,
including changing their direction. These controllers often accept input signals, such as PWM
(Pulse Width Modulation), to control motor speed and direction.

5. Microcontroller or Arduino:
If you're using a microcontroller like Arduino, you can control the motor direction by writing
code that controls the GPIO pins connected to the motor driver. For example, by changing the
state of specific pins, you can reverse the motor's direction.

H Bridge:
Circuit containing 4 switching elements + 4 catch diodes
Switching elements are usually bipolar transistors or MOSFETs
Catch diodes are used to prevent short circuiting
Path of current controlled by switches
Robotics Lab LAB 2

Potential paths and switch combinations


Robotics Lab LAB 2

Incorrect path

L298N Dual H-Bridge Motor Driver


This dual bidirectional motor driver, is based on the very popular L298 Dual H-Bridge Motor
Driver Integrated Circuit. The circuit will allow you to easily and independently control two
motors of up to 2A each in both directions.It is ideal for robotic applications and well suited for
connection to a microcontroller requiring just a couple of control lines per motor. It can also be
interfaced with simple manual switches, TTL logic gates, relays, etc. This board equipped with
power LED indicators, on-board +5V regulator and protection diodes.
Robotics Lab LAB 2

L298n Specifications
● Input Voltage: 3.2V~40Vdc.
● Driver: L298N Dual H Bridge DC Motor Driver
● Power Supply: DC 5 V - 35 V
● Peak current: 2 Amp
● Operating current range: 0 ~ 36mA
● Control signal input voltage range :
● Low: -0.3V ≤ Vin ≤ 1.5V .
● High: 2.3V ≤ Vin ≤ Vss.
● Enable signal input voltage range :
● o Low: -0.3 Vin 1.5V (control signal is invalid).
● o High: 2.3V Vin Vss (control signal active).
● Maximum power consumption: 20W (when the temperature T = 75 °C).
● Storage temperature: -25 °C ~ +130 °C.
● On-board +5V regulated Output supply (supply to controller board i.e. Arduino).
● Size: 3.4cm x 4.3cm x 2.7cm

Schematic Diagram:
Robotics Lab LAB 2

Board Dimension & Pins Function:


Robotics Lab LAB 2

Connection Examples:
Controlling 2-DC Motor with +5V Arduino onboard Power Supply:
Below is the circuit connection use the on-board +5V power supply from Arduino board, and
should be done without the 5V Enable Jumper on (Active 5V). This connection can drive two 5V
DC motors simultaneously.
Robotics Lab LAB 2
Sketch Listing:
// Definitions Arduino pins connected to input H Bridge
int IN1 = 4;
int IN2 = 5;
int IN3 = 6;
int IN4 = 7;
void setup()
{// Set the output pins
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
}
void loop()
{// Rotate the Motor A clockwise digitalWrite(IN1, HIGH);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
delay(2000);
// Motor A
digitalWrite(IN1, HIGH);
digitalWrite(IN2, HIGH);
delay(500);
// Rotate the Motor B clockwise
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
delay(2000);
// Motor B
digitalWrite(IN3, HIGH);
digitalWrite(IN4, HIGH);
delay(500);
// Rotates the Motor A counter-clockwise
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
delay(2000);
// Motor A
digitalWrite(IN1, HIGH);
digitalWrite(IN2, HIGH);
delay(500);
// Rotates the Motor B counter-clockwise
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
delay(2000);
// Motor B
digitalWrite(IN3, HIGH);
digitalWrite(IN4, HIGH);
delay(500);
}

The below provided Arduino code is designed to control a DC motor using an L298N H-Bridge
motor driver, a potentiometer for setting the motor speed, and a push-button to reverse the
motor's direction.

/* Arduino DC Motor Control - PWM | H-Bridge | L298N - Example 01


by Dejan Nedelkovski, www.HowToMechatronics.com
*/
#define enA 9
#define in1 6
#define in2 7
#define button 4
Robotics Lab LAB 2
int rotDirection = 0;
int pressed = false;
void setup() {
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(button, INPUT);
// Set initial rotation direction
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
}
void loop() {
while(1){
pressed=digitalRead(button);

// If button is pressed - change rotation direction


if (pressed == true) {
int potValue = analogRead(A0); // Read potentiometer value
int pwmOutput = map(potValue, 0, 1023, 0 , 255); // Map the potentiometer value from 0 to 255
analogWrite(enA, pwmOutput); // Send PWM signal to L298N Enable pin
// Read button - Debounce
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
rotDirection = 1;
delay(20);
}
// If button is pressed - change rotation direction
if (pressed == false) {
int potValue = analogRead(A0); // Read potentiometer value
int pwmOutput = map(potValue, 0, 1023, 0 , 255); // Map the potentiometer value from 0 to 255
analogWrite(enA, pwmOutput); // Send PWM signal to L298N Enable pin
// Read button - Debounce
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
rotDirection = 0;
delay(20);
}
}
}

The result is that you can use the potentiometer to control the motor speed, and pressing the
push-button reverses the motor's direction.
Robotics Lab LAB 2

Lab Assignments Marks: 20

1- Write a code to program Arduino to run DC Motor in a clockwise and counterclockwise


direction with variable speed. Speed should be provided by PWM pin using analogWrite
command.
Note: Paste your code in the manual:

2- Write a code to program Arduino to run DC Motor in a clockwise and counterclockwise


direction with variable speed. Speed should be provided by PWM pin using analogWrite
command and speed reference is obtain by potentiometer attached to the analog pjn of
the Arduino. Also direction is obtain by the selector switch attached to the digital pin.
Note: Paste your code in the manual:
Robotics Lab LAB 2

OBSERVATIONS AND COMMENTS


Robotics Lab LAB 2

You might also like