Driving a relay

To drive higher current than 4mA with an Electric Imp (which can’t really drive anything), you need a relay. But to drive that relay you need a few other things too.

Here is a list of the items you need to drive a relay:

  • 1x Transistor (2N3904)
  • 1x Diode (1N4148
  • 1x Resistor (10k ohm)

In this example I am going to use Pin 9 on the Electric Imp to control the relay.

relay_bb

If we start with the power wires (red and blue), you may notice the positive is connected directly to the relay, this is because we are going to control it with the negative pin instead.

There are also a diode going across the pins for the coil, this is to protect the transistor from the voltage generated by the coil when it is released from having been pulled. The orientation of the diode is important, if it is the wrong way around it will act as a short instead.

Going back to the Electric Imp, we got a wire (yellow) connected to Pin 9, which when goes to a 10k ohm resistor, this resistor is to limit the current being drawn from the pin. The other pin of the resistor is then connected to the middle pin (base) on our 2N3904 NPN transistor.

The pin to the left (emitter) is connected to the negative rail (blue wire), this is where the negative side of the relay coil will go through.

On the right side of the transistor we got the pin (collector) which goes directly to the relay (pink wires).

All you need to do now is to set the pin to be an output pin with

hardware.pin9.configure(DIGITAL_OUT);

and then use

hardware.pin9.write(1);

and

hardware.pin9.write(0);

to turn it on and off.

Leave a Reply