0% found this document useful (0 votes)
16 views5 pages

Arduino Lab 1 - Motor Control ENGN1211 2023

1) The document describes an Arduino lab activity to control DC motors using an L293D H-bridge integrated circuit. Students will learn to control a single motor and then two motors together. 2) The activity involves wiring a motor to the H-bridge, then writing Arduino code to control the motor's direction via the H-bridge inputs. Functions are used to make the code reusable. 3) Students will duplicate the circuit and replace the battery power with an Arduino board. The code is updated to control the motor from the Arduino pins. An alternative higher-voltage battery power for the motor is also demonstrated.

Uploaded by

huh le
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

Arduino Lab 1 - Motor Control ENGN1211 2023

1) The document describes an Arduino lab activity to control DC motors using an L293D H-bridge integrated circuit. Students will learn to control a single motor and then two motors together. 2) The activity involves wiring a motor to the H-bridge, then writing Arduino code to control the motor's direction via the H-bridge inputs. Functions are used to make the code reusable. 3) Students will duplicate the circuit and replace the battery power with an Arduino board. The code is updated to control the motor from the Arduino pins. An alternative higher-voltage battery power for the motor is also demonstrated.

Uploaded by

huh le
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

ENGN1211 Discovering Engineering S1 2023

School of Engineering/ College of Engineering, Computing & Cybernetics

Arduino lab 1 –Motor Control –Week 4


Aims 1. To learn how to wire an H-bridge integrated circuit to control the movement of
a DC motor
2. To learn how to control motor movement via the Arduino
3. To learn how to make two motors work together
4. Learn to use function to make code more readable and reusable

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.

Activity 1. Controlling a single motor


To begin we will design a circuit to demonstrate how to use the H-bridge to drive a single motor.
The L293D is a dual H-bridge IC, meaning it contains two H-bridge circuits and can therefore be
used to control two motors, however we will start with one. This H-bridge is not the exact same
as the one in your rover kit, but the wiring is the same. You will need the parts listed in Table 1.
Table 1: Components required for activity 1

AA switched battery holder (with 4 batteries) DC motor


Breadboard small L293D H-bridge
Connecting wires
1) Create a new document in TinkerCAD and add the parts listed above. Use the search to find
the H-bridge.
2) Begin by positioning the H-bridge over the isolation ravine in the centre of the breadboard
with the notch at the top. This is done so that the two sides of the IC are electrically isolated
from each other. The pin layout for our H-bridge can be seen in Figure 1 and are outlined in
Table 2. The direction the motor will turn is controlled by the input provided to 1A and 2A. If
1A is set to HIGH (5V) and 2A LOW (Gnd) the motor will turn in one direction. If 1A is set to
LOW and 2A HIGH, the motor will rotate in the other direction.

The Australian National University 1


ENGN1211 S1 2023 – Arduino Lab 1, Week 4

Figure 1: Pin layout of the L293D H-bridge (source: https://raw.githubusercontent.com/OnionIoT/Onion-


Docs/master/Omega2/Kit-Guides/img/h-bridge-pinout.png)

Table 2: Pins of the L293D H-bridge

Label Pin Purpose


Vcc1 Pin 16 5V: This is the power supply for the chip
Heat sink and Pins 4,5,12, GND: connect these 4 pins to ground
ground and 13
Vcc2 Pin 8 5V: This is motor power
1,2EN and 3,4EN Pins 1 and 9 Enable pins for motor 1 (1,2EN) and motor 2 (3,4EN). These turn
the H-bridge off and on.
1A, 2A Pins 2 and 7 Inputs to control the switches for motor 1
3A, 4A Pins 10 and 15 Inputs to control the switches for motor 2
1Y, 2Y Pins 3 and 6 Outputs for motor 1, paired to 1A and 2A
3Y, 4Y Pins 11 and 14 Outputs for motor 2, paired to 3A and 4A
3) We will now wire up our motor as seen in Figure 2. In this first example we will only use one motor,
therefore pins 10, 11, 14 and 15, the inputs and outputs for motor 2, will not be connected. Similarly
pin 9, the enable pin for the second motor, will not be used.
a) Begin by connecting pins 4, 5, 12 and 13 to the negative power rails.
b) Connect the two negative power rails on either side of the board.
c) Connect pin 1 to the positive power rail (HIGH). This enables the motor (turns one side of the H-
bridge on).

The Australian National University 2


ENGN1211 S1 2023 – Arduino Lab 1, Week 4

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

The Australian National University 3


ENGN1211 S1 2023 – Arduino Lab 1, Week 4

Activity 2. Controlling the motor using the Arduino


We will now use an Arduino script to control the motor. We will also use the 5V power supply from the
Arduino (rather than the battery pack) to power both the motor and the H-bridge.
You can duplicate a simulation in TinkerCAD. In the list of 'Your designs', mouse over the design you
want to duplicate. A red cog will appear in the top right of the design. Click on this and select
'Duplicate'. This will open a new copy of your circuit design.
Duplicate activity 1 and make the following changes to your circuit from activity 1.
1) Delete the battery pack and add an Arduino Uno R3. Connect the 5V power supply of the Arduino to
the positive power rail. Connect the negative power rail to the GND on the Arduino (the pin above pin
13 or near to the 5V supply).
2) Connect the enable pin for the motor (H-bridge pin 1) to pin 9 on the Arduino (and disconnect the
enable pin from the positive power rail).
3) Connect the inputs for the motor (1A and 2A) to pins 3 and 4 on the Arduino. You are now ready to
write your script.
4) Write a script within the code editor (make sure it is in ‘Text’ mode) to turn the motor first in one
direction and then the opposite. Then bring the motor to a stop. You will need to make use of some
of the functions used in last week's workshop such as pinMode and digitalWrite.

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

The Australian National University 4


ENGN1211 S1 2023 – Arduino Lab 1, Week 4

supply) to the Arduino 5V power supply. Note the increase in motor speed compared to the speed
with the 5V power supply.

Activity 3. Controlling two motors


As previously mentioned, the L293D is a dual H-bridge IC. We will now look at how it can be used to
control two motors.
1) Using the information provided in Figure 1 and Table 2, connect the second motor to the circuit. This
will involve wiring the inputs for the second motor (3A and 4A), connecting the motor to the outputs
(3Y and 4Y), and connecting the enabler for the second motor (3,4EN).
2) Once you have two motors wired into the circuit, adapt your code to account for the second motor.
This will involve three new outputs.
3) You might like to create a routine. Turn both motors clockwise for a period, anticlockwise for a
period, stop for a period and then rotate in opposite directions for a period before repeating. Why
would you want to move the motors in opposite directions?
4) Simulate your circuit, and check that it operates as expected.

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

Activity 4. Using functions to control the motors


If you look at the script in activity 3 you may notice that there are lines that repeat or are very similar.
There are also actions that we expect the rover to need to preform regularly. These sections of the code
would make good functions!
1) Using the information about functions provided in the pre-lab reading, adapt your previous code to
contain two functions. One to send a motor in the forwards direction, and one to send the motor
backwards.
2) You will use this script again in future workshops so be sure to save your simulation file with a
suitable file name.
3) What other functions could be useful for the rover?

The Australian National University 5

You might also like