0% found this document useful (0 votes)
4 views

MEC8063 Tutorial 5 Motors

Uploaded by

yulin lin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

MEC8063 Tutorial 5 Motors

Uploaded by

yulin lin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

MEC8063 Introduction to Mechatronics Design

Tutorial 5 – Motors

Objective: This tutorial is to gain experience in connecting motors to the Arduino with open loop control. The
basic H-bridge transistor configuration will be considered followed by microchip solutions.

Reading: It is best to read pages 531 – 548, 552 – 572 of “Introduction to mechatronic design” by J. Carryer, R.
Ohline and T. Kenny before commencing this tutorial (corresponding pages of international edition are 557 – 574,
578 - 598). You will also need to know about other types of motor and so should read pages 588 – 598, 600 – 619,
636 – 641 of this book in preparation for the exam (corresponding pages of international edition are 614 – 624,
626 – 645, 662 – 667).

1. Introduction

There are a range of possible motors available. For this tutorial, we will look at the operation of dc brushed
motors. Brushed motors are the easiest to implement as commutation is performed mechanically. Brushless
motors provided more reliability but as the commutation is performed electrically, this makes the driving circuitry
more complex.

For this simulation tutorial, we will look more closely at the basic circuits used to drive motors and progress to
dedicated chip solutions. Without going into too much detail about the electronic components, you should bear
in mind the following points:

• A transistor has 3 terminals – for a bipolar transistor these are collector, base and emitter whilst for a FET these
are drain, gate and source. They are essentially amplifiers controlled by the base or gate connection. We will use
these in saturation (turned fully on or off) and for our purposes can be thought of as an electronic switch.
• Bipolar transistors are current controlled (you send a current to the gate connection to control it). FETs are
voltage controlled (you set the base connection to a certain voltage level). We will only be concerned with
bipolar transistors for this tutorial.
• There are 2 basic types of bipolar transistor – NPN and PNP. NPN transitors are connected to the ground
connection and you need to supply their base with current to operate. PNP transistors are connect to the
voltage supply and you need to take current from their base.
• As bipolar transistors are current controlled and we are controlling them with a voltage output from the
Arduino, we will use resistors to limit the amount of current supplied to the base.
• In the tutorials, take careful note on where transistor terminals (C,B,E) are connected. To switch on the NPN, we
need to set the corresponding Arduino pin HIGH (as we are feeding current into it). To switch on the PNP, we
need to set the corresponding Arduino pin LOW (as we are taking current from the base).

You should read the corresponding chapters in the required reading on motor control and H bridges before
attempting this tutorial. The operational principle of various types of brushed and brushless motors can be found
in this recommended reading.

2. Simulation tutorials

2.1. Basic motor switch

We shall start with a basic on/off motor controller. Two options are given, as shown in figures 1 and 2. In both
cases, the resistor has a value of 5 kΩ, capacitor a nominal value of 5 nF and the voltage supply is set at 5V and
0.5A. There are several motors to choose from, your simulation will run into problems if you try to run the larger

John Hedley, School of Engineering, 2024. Page 1


MEC8063 Introduction to Mechatronics Design

motors. The actually specifications for these transistors are not given so we assume they are only suitable for
driving small loads (the Arduino itself has a current limit of 40 mA per pin and 200 mA in total across all pins).
Darlington transistors and FETs are a better option for loads requiring larger currents.

Note: If a MOSFET transistor is used, a pull-down resistor on the gate electrode helps to dissipate any stored
charge when the transistor is switched off.

On running the simulation, you should see a positive rpm speed given on the motor indicating it is turning (you
may or may not see the sprocket on the motor turn, this animation seems to be an intermittent fault with the
simulation which we will revisit later). Try switching the positive and negative leads on the motor, you should now
see the motor run in the reverse direction (as indicated by a negative rpm speed). Try both circuits and consider
the current flow in each.

// NPN basic motor control

#define motorLowPinA 7

void setup()
{
// Set digital pins used for motor control
pinMode(motorLowPinA, OUTPUT);
}

void loop()
{
// Turn on NPN transistor
digitalWrite(motorLowPinA, HIGH);
delay(100);
}

Figure 1: NPN transistor used for motor control.

// PNP basic motor control

#define motorHighPinA 10

void setup()
{
// Set digital pins used for motor control
pinMode(motorHighPinA, OUTPUT);
}

void loop()
{
// Switch on PNP transistor
digitalWrite(motorHighPinA, LOW);
delay(100);
}

Figure 2: PNP transistor used for motor control.

John Hedley, School of Engineering, 2024. Page 2


MEC8063 Introduction to Mechatronics Design

There are several points to note about driving a motor. To circumvent flyback (the voltage spike as the current is
switched off from the inductive load of the motor) a diode is connected (either across the motor or across the
transistor). This does not prevent current flowing through the motor (it is connected in reverse bias so it does not
short the motor current) but does provide a route to dissipate any back emf generated. We have also connected a
relatively large capacitor across the motor terminals. As motors draw large current, this can cause voltage spikes
in surrounding circuitry and a capacitor helps to mitigate against this effect.

2.2. Transistor H-bridge

If we now consider figure 23.12 of “Introduction to mechatronic design” by Carryer, you can see how a H-bridge
allows changing the current direction through the motor without the need of physically switching the cables.
Given the circuits shown in figures 1 and 2, have a go at designing a H-bridge circuit to allow for motor control in
both directions. Your corresponding Arduino program should control which transistors need to be on and off.
How would you program the Arduino for this? Have a go at building a H-bridge with a corresponding Arduino
program to control this circuit. Once you have given this a go, take a look at one possible solution in figure 3 (and
4) on the next page. Note that the H-bridge configuration also allows for breaking of the motor.

John Hedley, School of Engineering, 2024. Page 3


MEC8063 Introduction to Mechatronics Design

// Transistor H bridge motor control void loop()


{
#define motorHighPinA 10 // Set motor direction
#define motorLowPinA 7 if(reqDir>0)
#define motorHighPinB 9 {
#define motorLowPinB 8 // Drive motor anticlockwise
Serial.println("Running anticlockwise...");
int reqDir = 1; digitalWrite(motorHighPinA, LOW);
digitalWrite(motorLowPinA, LOW);
void setup() digitalWrite(motorHighPinB, HIGH);
{ digitalWrite(motorLowPinB, HIGH);
// Open comms channel }
Serial.begin(9600); else if(reqDir<0)
Serial.println(); {
Serial.println("Starting motor control program."); // Drive motor clockwise
Serial.println("Running clockwise...");
// Set digital pins used for motor control digitalWrite(motorHighPinA, HIGH);
pinMode(motorHighPinA, OUTPUT); digitalWrite(motorLowPinA, HIGH);
pinMode(motorLowPinA, OUTPUT); digitalWrite(motorHighPinB, LOW);
pinMode(motorHighPinB, OUTPUT); digitalWrite(motorLowPinB, LOW);
pinMode(motorLowPinB, OUTPUT); }
} else
{
// Break
Serial.println("Breaking...");
digitalWrite(motorHighPinA, LOW);
digitalWrite(motorLowPinA, LOW);
digitalWrite(motorHighPinB, LOW);
digitalWrite(motorLowPinB, LOW);
}
delay(100);
}

Figure 3: A transistor H-bridge followed by the corresponding control code. Serial monitor messages are used to
confirm correct program operation. You could move the base connections of the NPN transistors to the
corresponding pins on the Arduino for the PNP base connections (i.e. move Arduino pin 7 connection to 10 and
pin 8 connection to 9) as they always share the same logic (for example when 10 is HIGH then 7 is HIGH also). You
will need to reduce the resistor values to 500 Ω. This reduces the number of pins required from the Arduino and
simplifies the code as shown in figure 4.

John Hedley, School of Engineering, 2024. Page 4


MEC8063 Introduction to Mechatronics Design

// 2 pin transistor H bridge motor control void loop()


{
#define motorHighPinA 10 // Set motor direction
#define motorHighPinB 9 if(reqDir>0)
{
int reqDir = 1; // Drive motor anticlockwise
Serial.println("Running anticlockwise...");
void setup() digitalWrite(motorHighPinA, LOW);
{ digitalWrite(motorHighPinB, HIGH);
// Open COMMS channel }
Serial.begin(9600); else if(reqDir<0)
Serial.println(); {
Serial.println("Starting motor control program."); // Drive motor clockwise
Serial.println("Running clockwise...");
// Set digital pins used for motor control digitalWrite(motorHighPinA, HIGH);
pinMode(motorHighPinA, OUTPUT); digitalWrite(motorHighPinB, LOW);
pinMode(motorHighPinB, OUTPUT); }
} else
{
// Break
Serial.println("Breaking...");
digitalWrite(motorHighPinA, LOW);
digitalWrite(motorHighPinB, LOW);
}
delay(100);
}

Figure 4: A simplified transistor H-bridge controller.

John Hedley, School of Engineering, 2024. Page 5


MEC8063 Introduction to Mechatronics Design

2.3. L293D

H-bridges are now available as chips, such as the L293D which consists of four half bridges. Have a look at the
datasheet for this:

https://www.ti.com/document-viewer/L293D/datasheet/detailed_description#SLRS0084412

in particular the specifications (section 6.1) and the functional block diagram (section 8.2). From the
specifications, you can see that the chip can handle a motor supply voltage of up to 36V and a supply current of
600 mA per channel. An example of bidirectional control for the motor is shown in figure 10 and table 3 of the
datasheet (note that for the L293D, diodes are incorporated inside the chip and are therefore not needed
externally). Figure 5 below is a Tinkercad circuit with corresponding program using the L293D, in this example we
have used a 12V motor.

Note that we have chosen a larger motor in this case (the L293D can handle the power) and increased the power
supply to 12V (current set to 1.2A which is the maximum permissible for this chip using 2 outputs), I have set the
motor RPM to 612 (click on the motor to set this value) as this gives us more counts to work with for tutorial 7.
The Arduino program currently sets the speed to a maximum with a digitalWrite HIGH command.

Note: There is a voltage drop across the L293D chip so the actual voltage this supplies to the motor is 10.2 V (you
can check this using the Multimeter on Tinkercad). So the motor runs slower than 612 rpm. If you set the drive
voltage to 14.2 V this compensates for the drop and you can obtain 612 rpm (which puts 12V across the motor)
when fully on. If you use more than this the motor will run faster than the RPM specification which is OK for the
simulation but in practise would eventually burn out the motor.

When building the circuit, carefully consider each of the pin connections to the L293D. Pin 1 (EN1,2) switches on
the outputs to the chip, the control command for each half bridge is sent to pins 2 and 7 from the Arduino, the
output to the motor is pins 3 and 6, the logic supply voltage is pin 16 whilst the motor supply voltage is pin 8.
There are four ground pins, here connected via pin 4 although these also serve to act as a thermal path to remove
heat from the chip and so in practise you would connect all four ground pins (4,5,12,13) and probably also attach
a heat sink to the surface of the chip (at 12V and 1.2A, you have over 14.4W of power going through this chip so it
will get hot). Run the simulation and you should see the motor spin.

John Hedley, School of Engineering, 2024. Page 6


MEC8063 Introduction to Mechatronics Design

// Basic L293D motor control void loop()


{
#define motorPinA 11 // Set motor speed
#define motorPinB 6 if(reqSpeed>0)
{
int reqSpeed = 255; // Drive motor anticlockwise
Serial.print("Anticlockwise at: ");
void setup() Serial.println(reqSpeed);
{ digitalWrite(motorPinA, HIGH);
// Open COMMS channel digitalWrite(motorPinB, LOW);
Serial.begin(9600); }
Serial.println(); else if(reqSpeed<0)
Serial.println("Starting L293D motor driver..."); {
// Drive motor clockwise
// Set digital pins used for motor control Serial.print("Clockwise at: ");
pinMode(motorPinA, OUTPUT); Serial.println(-reqSpeed);
pinMode(motorPinB, OUTPUT); digitalWrite(motorPinB, HIGH);
} digitalWrite(motorPinA, LOW);
}
else
{
// If zero then brake using:
Serial.print("Braking for: ");
Serial.println(reqSpeed);
digitalWrite(motorPinA, HIGH);
digitalWrite(motorPinB, HIGH);
}

delay(100);
}

Figure 5: Using a L293D for motor control.

John Hedley, School of Engineering, 2024. Page 7


MEC8063 Introduction to Mechatronics Design

Rather than just switching the motor on, we can control its speed using the PWM signal, as done in the analogue
output signal of Tutorial 1. The Arduino connections used in figure 5 allow for PWM (this is signified by a ~ next to
the pin number) so we can replace digitalWrite with an analogWrite command. To give us more control, we will
also add a potentiometer so we can control speed as the program runs. We will use the map function to change
the potentiometer input to fully represent maximum clockwise and anticlockwise. As it is really difficult to set the
potentiometer to exactly the mid point (corresponding to zero speed), we shall assuming braking is required for
potentiometer values of approximately 0 (i.e. ±10). The required circuit is shown in figure 6 followed by the
Arduino program.

We have also attached an oscilloscope to the circuit. Have a look at the PWM signal as you change the
potentiometer setting. As you require more speed, the duty cycle of the pulses becomes larger (i.e. wider pulses)
until they become continuously on at maximum and minimum potentiometer settings. The ground (0V) level is
the x-axis on the oscilloscope so as you change the motor from anticlockwise to clockwise, you will see the PWM
signal go from positive to negative (you are changing the direction in which current flows through the motor).

Note: This program works but is not ideal, it allows you to instantly reverse the direction of the motor. In practise
this should not be done, i.e. you would not drive a car and then suddenly put it into reverse, you would stop the car
first. The same principle applies here, if you did wish to change motor direction you should first brake the motor to
a stop and then speed it up in the reverse direction. I’ll let you figure out the code for this.

You will need to keep a copy of this circuit as we will be using the encoder on the motor to accurately set motor
speed in tutorial 7.

Note: There is a problem with the simulation, the rotor sometimes appears to stop spinning (this usually happens
after a braking event) although the rpm still indicates the motor is working. This would not happen practically and
is just a fault with the simulation. We will revisit this issue in tutorial 7.

2.3.1. Exercise

The L293D chip allows for independent control of 2 motors. Create a copy of the circuit shown in figure 6 and see
if you can modify it so that 2 motors are being independently controlled from the one L293D chip. You will need
to add a second potentiometer to control the speed of this second motor.

John Hedley, School of Engineering, 2024. Page 8


MEC8063 Introduction to Mechatronics Design

// Motor speed controller void loop()


{
#define motorPinA 11 // Read potentiometer
#define motorPinB 6 int potentiometer = analogRead(speedPin);
#define speedPin A0 reqSpeed = map(potentiometer,0,1023,-255,255);

int reqSpeed = 1; // Set motor speed


if(reqSpeed>10)
void setup() {
{ // Drive motor anticlockwise
// Open COMMS channel Serial.print("Anticlockwise at: ");
Serial.begin(9600); Serial.println(reqSpeed);
Serial.println(); analogWrite(motorPinA, reqSpeed);
Serial.println("Starting L293D motor driver..."); digitalWrite(motorPinB, LOW);
}
// Set digital pins used for motor control else if(reqSpeed<-10)
pinMode(motorPinA, OUTPUT); {
pinMode(motorPinB, OUTPUT); // Drive motor clockwise
} Serial.print("Clockwise at: ");
Serial.println(-reqSpeed);
analogWrite(motorPinB, -reqSpeed);
digitalWrite(motorPinA, LOW);
}
else
{
// If zero then brake using:
Serial.print("Braking for: ");
Serial.println(reqSpeed);
digitalWrite(motorPinA, HIGH);
digitalWrite(motorPinB, HIGH);
}

delay(100);
}

Figure 6: Motor speed controller.

John Hedley, School of Engineering, 2024. Page 9


MEC8063 Introduction to Mechatronics Design

2.4. Breakout boards

Breakout boards are essentially just L293D chips (or equivalent) that have been mounted onto a circuit board with
additional circuitry, connections, heat sinks, etc, to make using the chips much easier for you. Breakout boards
and shields are very useful in practical work if you are just wanting to put together a project very quickly and are
happy paying a little extra for the parts. Figure 7 shows an example of such a board available of Tinkercad, this
board has several options for input – timed pulse (as shown), comms, analogue input.

// Popolu driver

#define motorPinA 10

int reqSpeed = 30;

void setup()
{
// Set digital pins used for motor control
pinMode(motorPinA, OUTPUT);
}

void loop()
{
// Length of pulse dictates motor
// speed and direction
analogWrite(motorPinA, reqSpeed);
delay(100);
}

Figure 7: A Popolu motor controller with a variety of input options.

2.5. Servo motors

Servo motors are simply dc motors with built in feedback. They are generally used to control small projects such
as remote control cars, etc. They are usually wired up for positioning (setting the rotor to a given angle) although
they can be configured to give continuous rotation if that is required. For servos, it is simple a case of supplying
power (usually from the Arduino unless multiple servos are being used) and a PWM signal which corresponds to a
given position – a servo.h header file is included to help configure the appropriate settings, see:

https://www.arduino.cc/reference/en/libraries/servo/

Figure 8 shows the servo example taken from Tinkercad (Starters – Arduino – Servo) with an oscilloscope added
to the PWM pin. As the simulation runs, you can see the pulse width change which then changes the position of
the rotor.

John Hedley, School of Engineering, 2024. Page 10


MEC8063 Introduction to Mechatronics Design

Figure 8: Servo motor control.

3. Practical exercises

IMPORTANT NOTE: When doing the following exercises, be very careful that conductive parts of connectors do
not unintentionally touch each other. For example you DO NOT want a voltage supply connector to touch a
ground connector – if V was 5V and R (resistance of the contact) was virtually zero then from Ohm’s Law V=IR
you would get a VERY LARGE current (you have created what is called a short) and will probably end off burning
something out together with a corresponding burning smell. Therefore be very careful where you place your
cables.

HEALTH & SAFETY NOTICE: It actually takes very little current to kill someone, around 100 mA. However this
current must pass through the body. You skin is fairly resistant, up to about 40V, so by using low voltages there
is no risk of being electrocuted. For using anything over 40V (or over 30V to be on the safe side) then you should
get professional advice on how to proceed safely. You also need to be careful with large currents. Although
these may not have entered the body, if they pass through a conductor located on you (for example a metal
ring) this would significantly heat up and cause burns to the skin. In this module we are using low currents
(unless you accidently create a short) and low voltages so there are no risks involved here but it is something
you should be aware of. If in doubt, ask a technician for advice.

For this practical lab, we will use a RB-Dfr-668 6V brushed micro motors with an integrated 120:1 gearbox and
Hall encoders. The wiring for this motor is shown in figure 9 and consists of a red and black connection (twisted
pair) to drive the motor (connections 1,2 in the figure) and a set of 4 wires for the Hall sensors (connections
3,4,5,6). We can ignore the Hall sensors for this particular tutorial. Placing a voltage across the drive connections
will operate the motor. Note that you typically cannot drive the motor directly from the Arduino as the Arduino
cannot supply enough current to drive the motor (unless the motor is very small).

John Hedley, School of Engineering, 2024. Page 11


MEC8063 Introduction to Mechatronics Design

Figure 9: Image of RB-Dfr-668 motor. Image from https://wiki.dfrobot.com/Micro_DC_Motor_with_Encoder-


SJ01_SKU__FIT0450.

3.1. Motor driver breakout board

For this practical, we will use the breakout board shown in figure 10 (in this case it is just an L298N chip wired up
so that you can connect to it easily). The required connections and Arduino program are shown in figure 11.
Connect Arduino pin 11 to IN1, pin 6 to IN2, ground to ground and ensure you keep the jumper between the +5V
and EN_A pin to enable the L298N. Note that the breakout board contains the required capacitors and therefore
you do not need to add additional ones across the motor. As with the Tinkercad L293D simulation, IN1 and IN2
determines motor direction (clockwise or anticlockwise), if both are set high braking occurs. Two motors can be
driven independently from this breakout board (as with any L293 chip).

Figure 10: Image of the L298N breakout board (image from Amazon).

// Basic driver program for L298N breakout board

byte IN1 = 11; // Control pin for first motor


byte IN2 = 6; // Control pin for first motor
int motorSpeed = 170; // Motor speed

void setup()
{
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
}

void loop()
{
analogWrite(IN1, motorSpeed);
digitalWrite(IN2, LOW);
delay(100);
}

Figure 11: Circuit diagram and Arduino program for driving a motor using the L298N breakout board.

John Hedley, School of Engineering, 2024. Page 12


MEC8063 Introduction to Mechatronics Design

This breakout board needs a minimum of 6V to operate so you will need to use a power supply or 9V battery to
test this circuit. Try adding serial communications to the program and control the speed of the motor by entering
the number 0 to 5 (corresponding to 0% to 100%) through the Serial Monitor on the Arduino IDE as done in
tutorial 1. Figure 12 shows one possible solution.

// Basic motor speed program for L298N breakout board


// Maximum speed set to 66% of 9V = 6V for these 6V motors

byte IN1 = 11; // Control pin for first motor


byte IN2 = 6; // Control pin for first motor
int motorSpeed = 0; // Zero speed to start with

void setup()
{
// Initialise motor drive pins
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);

// Set initial motor speed


analogWrite(IN1, motorSpeed);
digitalWrite(IN2, LOW);
delay(100);

// Initialise communications
Serial.begin(9600);
Serial.println("Enter Volts: 0 to 5");
}

void loop()
{
if (Serial.available() > 0) // If a value is waiting in the serial comms buffer
{
char ch = Serial.read(); // read character
motorspeed = (ch - '0') * 34; // change ASCII value to decimal and scale to 0-170
analogWrite(IN1, motorSpeed); // and set motor speed
digitalWrite(IN2, LOW);
}
}

Figure 12: Motor speed control using values input from the user. Note that these are 6V motors so if we use a 9V
supply, we risk burning out the motor. Hence speed is set to a maximum of 170 (rather than 255) to keep the
upper limit of the (average) voltage to 6V.

Note: Another way to use the L298N chip (either the chip directly or on the breakout board) is to use digital writes
for the motor pins (set to either HIGH or LOW accordingly) and put a PWM signal on the ENABLE pin so speed is
controlled by periodically enabling the chip.

When using a motor driver, it is important to know the current limitations of the driver as driving too large a
motor may burn out the driver circuitry. This driver chip will drive continuous currents up to 1A and can withstand
short current bursts up to 2A. The motor we are using will use a lot less than this. Is there a way to measure this
current?

John Hedley, School of Engineering, 2024. Page 13


MEC8063 Introduction to Mechatronics Design

3.2. Servo motor (note this may form part of the assignment so take a copy of the screenshot)

Disconnect the motor drive and connect a servo motor to the Arduino as you did in Figure 8. Alter this example
program to set the angle to a value corresponding to the ASCII value of the first few letters of your name (for
example for John, I would want to set the angle to be 74° (then pause for a short while), then to 111°, etc. Use a
PicoScope (timebase of 5 ms/div) to monitor the voltage on the control connection to the servo. Print back over
the serial monitor the current positions of the servo motor. Take a screenshot of your program, Serial Monitor
display, PicoScope output and group members’ names. Each group member should do this for their own name.
Note that if there are not enough USB connections on your PC, you may need to plug the Arduino into one PC and
the PicoScope into another PC (in which case you will need screenshots from both PCs).

3. Summary

We have looked at a variety of options to drive a motor and set its speed. However we still do not know if the
speed we want from the motor is actually being delivered. For this, we need to sense the rotor speed and ideally
correct for any errors between what we want and what is actually obtained. We will develop these aspects in a
later tutorial where we shall look at how rotor speed is measured and how to implement closed loop control.

John Hedley, School of Engineering, 2024. Page 14

You might also like