Day 24: Controlling a relay with the Raspberry Pi

In order to control the circuit presented on Day 23 with the Raspberry Pi, I used the L293D motor driver.

The relay requires 40 mA and the GPIO can provide up to 16 mA. Using the L293D motor is a little overkill since it can handle up to 600 mA. I believe that the ideal solution would be using a transistor as shown in this article but I don’t have one now.

It is extremely important to use the protection diode in the proper position. I have been told that the 1N4148 is more suitable for this because it has a better response time. By now, I only have a 1N4001. I have read in some blogs people saying they use the 1N4001 without problems, so I gave it a chance.


I used the GPIO #17 pin to activate the relay through the L293D. I connected +5V from a external power source to the line in the top of the image  and its ground to the line in the bottom of the image.

As usual, I used my PiEater library to control the GPIO from my desktop computer. Since this is just a quick test, I added a new check box in my previous Truck Driver program:


And, voilà!

Green  Red


Day 23: Experimenting with a relay

Although the L293D is very handy, it has two important constraints: It is limited to 600 mA and it causes the voltage to drop.

The relay below has a 5V 40mA coil. It can deal with a charge up to 1A at 30V. It has the size of a 14-pin IC. Its lower part has a drawing explaining what every pin is for.

Relay_top Relay_bottom

In the following circuit, a 330 Ohms resistor is connected to the common pin, a green LED is connected to the normally closed pin and a red LED is connected to the normally open pin. The power provided is 5V.

Relay_GreenLed Relay_RedLed

As expected, the green LED lights when the coil is turned off and the red LED lights when the coil is energized.