Arduino Lab 1 - Motor Control ENGN1211 2023
Arduino Lab 1 - Motor Control ENGN1211 2023
Introduction
Today we will look at using the L293D H-bridge to control two DC motors. We will also
experiment with using functions in our scripts.
d) Wire the motor to the output pins of the h-bridge for motor 1 (pins 3 and 6). The direction of the
motor is not important.
e) Connect pin 2 to the positive power rail (HIGH) and pin 7 to the negative power rail (LOW). These
are the inputs to the motor.
f) Connect pin 16 to the positive power rail (5V). This is the power for the h-bridge. Also connect pin
8 to the positive power rail. This is the power for the motor.
g) Connect the positive power rails on either side of the board.
h) Take the time to check your circuit (it is important to get into this habit as with a real circuit you
can destroy components by connecting them incorrectly).
i) Finally connect to the battery. Connect the negative (Black) to the negative power rail (ground),
and the positive (red) to the positive power rail (5V).
j) Run the simulation and note which direction the motor turns.
Figure 2: Wiring for a single motor using the H-bridge and battery pack
4) Now disconnect the power source. Change pin 2 to LOW and pin 7 to HIGH. Reconnect the power.
Has the direction of rotation changed?
5) Now change pin 1 to be connected to the negative power rail. What happens to the motor?
This exercise has demonstrated the functionality seen in Table 3 below.
Table 3: Function of the H-bridge where H = high, L = low and X = does not matter
EN 1A 2A Function
H L H Turn right
H H L Turn left
L X X Motor stop
a) At the top of your script, you can declare your pins as constants. You will need to do this for Pin
9, our motor enabler, and pins 3 and 4, our motor outputs. Use variable names that make it easy
to track the purpose of each pin. For example:
const int enablePin1 = 9; //H-bridge enable pin 1,2 EN
b) Within the setup() of the script, use the pinMode(pin, mode) function to initialise our three
output pins. For example:
//1,2 EN Arduino digital pin 9 connects to H-bridge enable pin
pinMode(enablePin1, OUTPUT);
c) Set the pins to initial values using the digitalWrite(pin, value) function. Reference Table
3 to determine suitable values. This covers the setup function.
d) Within the loop() write code to change the direction of the motor by altering the value of the
output pins using digitalWrite()to alter the value of the pins. You can use the delay(ms)
function between commands. You may have to increase the delay to clearly see the directional
change in the motor.
e) After turning the motor in both directions, use the enable pin for the motor to bring it to a stop.
f) Start the simulation, and check your motor works as desired.
Alternative power supply: The H-bridge IC requires a 5V power supply, however the power supplied
to the motor can vary depending on the specifications of the motor. If within the specifications, the
power supply for the motor can be from a different supply for example a 9V battery. This will allow
the motor to rotate faster than with the 5V power source.
5) To demonstrate that an alternative power supply can be used to power the motors, alter your circuit
to run the H-bridge from the 5V Arduino power source and the motor from 9V battery. That is,
connect H-bridge pin 8 (motor power supply) to a 9V battery, and H-bridge pin 16 (chip power
supply) to the Arduino 5V power supply. Note the increase in motor speed compared to the speed
with the 5V power supply.
Discussion/ Further research: you can use pulse width modulation (PWM) to control the speed of the
motors. What effect would rotating the motors at different speeds have on the direction of travel for the
rover? For more information see: https://www.electronics-tutorials.ws/blog/pulse-width-
modulation.html