Anth's Computer Cave Projects

Build a ULN2003 relay-driver circuit.

Today we are building a relay-driver circuit to allow a Raspberry Pi or Arduino to activate the relay we built last week.

A ULn2003-based relay-driver circuit. Picture: Anthony Hartup
The relay-driver circuit AAIMI will use to activate relays.

This relay-driver circuit uses the 3.3V from the Raspberry Pi's GPIO pins to activate 12V for the relay. The relay circuit then uses that 12V to switch the appliance power on or off.

We've covered building relay drivers with individual NPN transistors before, but this new driver is much simpler to build. The driver circuit uses a ULN2003 transistor array that can power up to seven 5V, 12V, or 24V relays

A ULN2003 transistor array.

You can buy five of these for a couple of bucks on Ebay.

You'll need a PCB board, some screw terminals and an 8-pin header.

A PCB board, screw terninals and pin headers.

These are all available cheaply on Ebay.

The relays will connect to the screw terminals and the Raspberry Pi or Arduino GPIO pins will connect to the pin header.

The circuit

With the notched end of the chip facing top, the GND is the lower left pin and the COMMON pin is on the lower right. Solder the GND to the left lower terminal block as well as the lower pin on the pin header. Solder the COMMON pin to the right pin of the lower terminal block and also to the lower pin on the right-hand terminals

A ULn2003-based relay-driver circuit. Picture: Anthony Hartup

Starting from the top, the left hand pins are IN1, IN2, etc. Solder these to their corresponding pin headers. The right-hand pins are OUT1, OUT2 and so on. Solder these to their corresponding screw terminals on the right.

Testing the unit

To test your driver, you should replace the Raspberry Pi with a 3.3V battery or power supply for the initial test. That way if you have made a mistake in your circuit you won't fry your Pi. Once you know the circuit works you can try it with the Pi.

Below is the circuit diagram showing one of our relays from last week connected to the driver.

A ULn2003-based relay-driver circuit. Picture: Anthony Hartup

You can see that the relay connects between the 12V+ COMMON terminal and one of the output terminals, in this case Out1. To add another relay you can once again connect one side to the COMMON terminal and the other side to Out2.

Plug your 12V power supply to the GND and positive terminal. Connect the GND from a 3.3V power supply to the lower-left GND header pin. Connect the positive wire from the 3.3V power supply to the upper-left header pin, IN1.

Turn on your 12V power supply but leave the 3.3V supply off. Nothing should happen, the relay should not activate.

Now switch the 3.3V power supply on. You should hear the relay activate and your appliance should switch on.

You can now try it with your Pi or Arduino.

A circuit diagram for a relay driver. Picture created with CircuitDraw by Anthony Hartup.

This time GPIO 4 on the Raspberry Pi connects to the top pin, IN1. Connect one of the Raspberry Pi GND pins to the GND pin header on the relay driver.

This time when the Pi sends 3.3V to the input pin, the ULN2003 allows the 12V power to flow through the relay.

The code

We'll try the relay with some Python code on a Raspberry Pi

The code below merely switches the relay on and off every three seconds. Copy-and-paste it into a file and name it You can run it by typing sudo python in your Raspberry Pi Terminal.

To stop the program press CONTROL+C


#Relay test
#Switches an AC relay on and at three second intervals.
#By Anthony Hartup

import RPi.GPIO as GPIO
import time

#Set GPIO 4 as the pin for your relay
relay_pin = 4

GPIO.setup(relay_pin, GPIO.OUT)

#Turn relay on
def on():
    GPIO.output(relay_pin, True)
    print("Lights on!")

#Turn relay off
def off():
    print("Lights off!")
    GPIO.output(relay_pin, False)

    #This will turn the relay on and off at three second intervals.
    #You can stop it with CONTROL+c
    while True:
except KeyboardInterrupt:
    #Applies when you press CONTROL+c to stop the program.
    #This resets the GPIO pins before the program closes.



Previous: Build a Relay Circuit to Control AC Power Outlets and Lights

Next: Build and operate a temperature sensor.



Leave a comment on this article

Search the Cave with AaimiClip