Friday 21 March 2014

ULN2803 interfaced with a Raspberry Pi

One of the issues with using an ARM based system or a micro-controller such as an Arduino or a Microchip is using the I/O pins to drive any real load, anything other than an LED seems to cause issues. So what do we do? In the case of a Raspberry Pi we have outputs that are only sitting at 3.3v, and conversations on forums seem to point to you not pulling more than 50mA from any one pin. I'd recommend only pulling a few mA if possible, and if you used 8 outputs at 50mA each, that would be 400mA, which just won't work well, something will fry. Anyway, introducing the ULN2803 (link to datasheet). What is it? It's called a "Darlington Array" which consists of a bunch of Darlington pairs, which is two transistors back to back per channel.
(Schematic from datasheet)

It has 8 inputs and 8 outputs arranged neatly so the package is easy to use and works well on a breadboard.
 (Schematic from datasheet)

What do they do? You can attach a secondary supply of a voltage between 0-30VDCs and pull a current of up to 500mA per output (you can gang them together for increased current too). What does this mean? Lots of POWER! Traditionally you would use an NPN transistor, and use a current limiting resistor that you had to determine its value based on the beta characteristics of the transistor, and use diodes to prevent kick back from relays, yadda yadda. This package includes a limiting resistor as well as diodes to prevent fly-back from those pesky collapsing electromagnetic fields on coils. Maybe that was a dramatization, but here is a video demonstration to enjoy:

 

In the video I complain that the mA's I pulled through are lower than what I calculated. I looked into it more, and it turns out that I can't read - I thought one of the ceramic resistors was 5 ohms, but it was actually 15. So the reading on the multimeter was actually (Vcc - Vbe) / (22ohm + 15ohm) which came out to around 120 mA. I originally thought the ohms would be 27, not 37.

I also prepared a schematic that doesn't show anything too exciting, however, it shows a byte of LED's being driven directly by the GPIO, as well as having a parallel connection to the ULN2803 which is driving more LED's. It isn't terribly exciting, but you can see that a different supply voltage is used for the 2nd set of LED's, and a relay could easily be dropped in, and the voltage could be different as well. 
(Made with Fritzing)

One other thing to pay attention to is that the voltage is being supplied to the components and not sourced (grounding it to the outputs on the ULN2803).
I've also seen some discussion about putting a limiting resistor on the inputs of the ULN2803, and I haven't come to any real solid best practice, so I did the lazy thing and did nothing. 
I would recommend fusing the power supply connection to your chip, and also placing a bypass capacitor between the + and - on the chip: 0.1uF should be fine (104 ceramic code).
Also check out the ULN2003, which is similar, except it has 7 inputs and outputs. These chips can supply small motors and steppers too!

I hoped that helped!