You are on page 1of 12

Driving relays with stm32f407vgtx microcontroller using uln2003

relay driver
In this tutorial I am going to teach you how to interface relays with stm32f407vgtx
microcontrollers. Stm32 microcontrollers works on 3.3 volt TTL logic. Whereas electrical relays
minimum take +5 volts to make a regular connection. We must need an external circuit to drive
relays with stm32 microcontrollers. This post is about teaching you what must be used with stm32
microcontroller to driver multiple relays with it. There are number of methods on how to drive
high loads with microcontrollers. I enlisted those methods in another tutorial. If you are interested
in knowing through which ways we can drive high loads with microcontrollers than just click the
below button to take the tutorial.
1.2 Driving/Running and Controlling High Power loads with MCU
Sometimes we want to drive heavy loads with microcontrollers. By heavy loads I mean motors,
fans, AC's, bulbs and other high voltage and current sources. Since we all know that
microcontrollers can output/source +3.3 volts to +5 volts and 25 mA to 40 mA through their
input/output pins. This voltage and current is not enough to drive high power loads motors, fans
and bulbs etc. There are few methods and electronic components which can handle much greater
loads (currents/voltages). We can interface these components with microcontrollers and can drive
and handle high power loads. In this tutorial i am going to explain these methods and about
electronic components that can be interfaced with microcontrollers in order to handle heavy
(current-voltage) loads.

1.2.1 Methods/Techniques to drive heavy loads with Microcontrollers


We can use following electronic components to drive high power loads with microcontrollers.

 Transistors
 optocoupler
 Relays
 Mosfets
 SSR(Solid State Relay)

The upper list starts from low power control electronic component (Transistor) to high power
control (SSR-Solid State relay). These electronic components can be interfaced with any vendor
of microcontrollers Altera, Atmel, Cypress Semiconductor, Maxim Integrated, EPSON
Semiconductor, Freescale Semiconductor, Infineon, Intel, Microchip Technology, National
Semiconductor, NXP Semiconductors, Panasonic, Parallax, Silicon Laboratories, Silicon Motion,
Sony, STMicroelectronics, Texas Instruments, Toshiba. I interfaced the above components in
many circuits with different microcontroller’s e.g Arduino, Pic, Stm32, 8051, Picaxe, Avr,
Atmega, Arm and LPC series etc to control high power loads. The above components can be used
with mini computers or development boards such as BeagleBone, Raspberry Pi, Olimex and Xilinx
FPGA boards.
1.2.2 Driving heavy loads with Microcontrollers and Transistors.
A transistor is a semiconductor device used to switch and amplify electronic signal and electronic
power. It has three terminals Base, Emitter and Collector. A small voltage applied at base makes
the current to flow from collector to emitter (NPN) or from emitter to collector (PNP). There are
two common types of transistors PNP and NPN. This tutorial does not go in depth of electronic
components rather it only shows the method through which we can control heavy loads.

Transistor mean
Generally transistor can handle currents up to 600 mills Amperes and voltage up to 20 volts. The
ratings can be little bit high or low. The maximum power that a transistor can handle is 12 watts
to 18 watts. This power is enough to control loads that falls below 18 watt power such as dc toy
motors and dc bulbs etc. But still this power is not enough to control the loads that consumes
continuous current of 1 amperes.

 A typical connection between transistor and microcontroller is shown on the left side.
 A small dc toy motor is controlled through microcontroller.
 NPN transistor is used in the circuit.
 Diode against the motor is fly back diode to protect circuit from any back emf by the
motor.
Transistor can be arranged together for high current gain. Most popular configuration is
Darlington configuration in which a high current gain is achieved by small voltage switch.
Many commercial IC’s are available in market that contain the transistors build in side them.
Such as ULN2003 and ULN2803 contains Darlington transistor array inside them.

Darlington circuit configuration and ULN2003 IC pin out is shown on the right hand side.
ULN2003 contains 7 Darlington configuration in side it. It’s easy to use the IC instead of
making the Darlington configuration by our own. IC consumes less space and less connections
are required.

Many more configurations can be made using transistors for current gain. But for this tutorial the
Darlington is best suited. ULN2003 can control 500 mA at 50 volts. Thus a Darlington
configuration increases power control to 25 watts.
Driving heavy loads with Microcontrollers and Optocoupler.
An Optocoupler/opt isolator or photo coupler is an electronic component that separates two circuits
from each other using a light as medium. The two circuits are independent but can be controlled
by light source. A small voltage applied a one side activates the other circuit. Optocoupler is
composed of a led diode and a photo cell. When voltage is applied across the led diode it
illuminates and showers light on photo cell. Photo cell resistance decreases upon receiving light
so it starts conducting.

A general optocoupler circuit with connections to microcontroller is shown on the left hand side. A motor is driven by the
microcontroller. Optocuplers are used in many circuits to isolate the circuit from the other. This way danger of high spikes is
eliminated. If there is a high spike it will only blow off the optocoupler at the remain circuit will remain safe. It is generally u
high valuable circuit cores. To isolate them with high current regulation circuits.
Opt couplers can handle more power than transistors. Such as FOD3180 optocoupler that can handle continuous 2 Amperes o
current and 10 volts to 20 volts. DC Opt couplers can control power up to 40 watts to 60 watts.
Unlike transistors which are not useful in switching AC loads. Opt couplers can be used to control heavy AC loads. This will
discussed in SSR (Solid State relays) topic.
Some projects that I created using opt couplers and microcontrollers to control loads etc. Projects contains free source code an
circuit diagrams.
Driving heavy loads with Microcontrollers and Relays.

Relay Pinout
Relays are mechanical switches that open and close when voltage is applied across its specified pins. Relays consists of a coil
NC (Normally Close), NO (Normally Open) and Common line. During ideal state (No Power) the common pin is shorted wit
NC (Normally Close) pin. Making a path of current flow between common and normally close line.
But when a sufficient voltage is applied across the coil of relay the common pin shorts with the NO (Normally Open) pin. Ma
path of current flow between common and normally open pin. The path is a straight mechanical connection between wires. A
relay is shown on left hand side. Relay's comes in many packages such as single pole single through, double pole and double
through (DBDT), Single pole double trough (SPST) etc.

Relays can directly be controlled with microcontroller pins. Small +3 volt and +5 volt relays can directly be controlled trough
microcontrollers GPIO (General purpose input/output) pins. But some relays coils require more current to switch the output c
In order to supply them sufficient voltage they are derived through transistors. Relays can handle 10-15 Amperes of current a
110-220 volts. We can easily control a 500 watt load with standard relays.

Figure: Relay driving heavy loads with microcontroller and transistor


Figure: Relay driving heavy loads with microcontroller and uln2003
In the above pictures a relay is controlled with a single transistor and uln2003 IC containing
Darlington transistor array circuit. Relays can control Dc as well as Ac loads. Relay is the oldest
mechanical method to switch heavy loads e.g. bulbs and fans.

In general Transistors
Transistors are best suitable with microcontrollers for driving relays. But if we have number of
relays in our project than using transistor for each relay is not the best choice. Individual transistors
not only make our circuit bigger but also messy. Luckily we have number of IC’s available in
market which contain transistors combinations in them. These IC’s are small in size and consumes
less space when compared to stand alone single transistor. ULN2003 and ULN2803 are popular
transistors combination IC’s available in market. ULN2003 has 7 transistor channels and
ULN2803 has 8 transistor channels. Each channel is composed of a Darlington pair of 2 transistors.
In Darlington configuration current amplified by the first transistor in further amplified by the
second transistor. We can conclude that each channel of ULN2003 and ULN2803 are high current
gain amplifiers. In this project I am going to use ULN2003 Darlington pair IC. ULN2003 in this
project will work as relay driver.

ULN2003 as Relay Driver


Individual Darlington pair configuration in ULN2003 ic is given on the right side. 3.3 volts from the output pins of stm3
to the base of first transistor. First transistor emitter is input to second transistor base. So when first transistor is switched
will also become switched on. Current will start flowing from 'Out' to ground. Diode at the 'COM' pin is working as a fly
the circuit from any back emf generated by the relay coils. ULN2003 can handle loads requiring 50 volts and 500 mA of
Project Circuit
I am going to switch on and off four relays with stm32 microcontroller. Four buttons are used as
input to stm32 microcontroller. These four buttons corresponds to four output relays. Pressing
each button will change the state of its corresponding relay. For example if the relay is on
pressing its corresponding button will change its state to off. We can say that buttons are
toggling the state of relays on each press.

Microcontroller used in the project is stm32f103c8t6. Its build on cortex m3 core. Port-A pins
0,1,2,3 of stm32 microcontroller are used as output pins. Pin#0 of port-a is connected to input 1
of ULN2003 relay driver. Pin#1 of port-a is connected to input 3, pin#2 is connected to input 5
and pin#3 is connected to pin#7 of ULN2003 relay driver.

Input buttons are connected to port-b pins 6, 7, 8, 9. Stm32 microcontroller has built in pull up
and pull down resistors on each individual gpio pin. We can enable and disable them in code. For
our button inputs I enabled the pull up resistors associated with each gpio (Port-B Pin#6, 7, 8, 9).
One side of input buttons is connected to microcontroller pins and the other side is commonly
grounded.

Relays which i am using in the project activates on +9 volts. I connected the relay coil one end
with ULN2003 output and the other +12 volts. Com pin of relay is grounded with the load
power. NC pin of relay is connected with load and other end of load is connected with positive
lead of load power. No pin of relay is left untouched. Ground pin of ULN2003 is grounded with
relay 12 volt power supply and with stm32 ground. Note ULN2003 GND must be commonly
grounded with stm32 power supply and ULN2003 relay power supply.
Project Code
Stm32cubemx is used for microcontroller configuration. Input and output pins are declared in it. Pull up
and pull down resistor on input pins are also activated in stm32cubemx. After the configuration and settings
project code is created and imported for keil uvision arm 5 mdk ide. Below fig is the final stm32 settings
in stm32cubemx.

Code is written and compiled in keil uvision ide. Stm32 HAL libraries are used in the project.
Main logic and crux of the code is in the while 1 loop. The statement
if (HAL_GPIO_ReadPin(GPIOB,B6_Pin)==GPIO_PIN_RESET) //Check for input button
press is checking if the button is pressed or not. The statement utilizes stm32 HAL libraries for
checking the state of the input button. Port-B Pin#6 is checked in the above code.
GPIO_PIN_RESET means if the pin is at low voltage (0 volts). When the button is pressed the
ground appears on this pin. Since the button other end is connected to stm32 ground. If the above
statement is true the next statement is executed.
HAL_GPIO_TogglePin (GPIOA, A0_Pin);
Above statement executes right after the if statement IF if statement is true. It toggles the state of
relay. This statement also utilizes stm32 HAL libraries. The statement toggles the state of port-a
pin#6.
Project code:
#include”main.h”
Next to two braces

Next to RCC_HCLK_DIV1;
NEXT TO rest:

#end if ..Finish
Making the above circuit on bread board is hard for a diy project. If you are making a diy project
on stm32 relay driving. I suggest you to use a pre-assembled relay board in your project. There
are numerous relay boards available in market containing different number of relays. 2 channel,
4 channel and 8 channel relay boards are most commonly available in market. These boards have
ULN2003 or ULN2803 relay driver IC’s installed on them. They also have status led's on them.
Led's depicts the status of relays. Terminal blocks for each relay are also very help full in making
connections with loads.
The code above can be used with the relay board. Connections are same. Only you have to make
relay power and stm32 power grounds common.
Relay boards also comes with optocouplers and single transistor circuits. The
code in this project can be used with any of the relay board containg
ULN2003, ULN2803, optocouplers or individual transistors as relay drivers.

You might also like