Question 9.5: You want to turn devices on and off that may not be suitable......

You want to turn devices on and off that may not be suitable for switching with a MOSFET.

Step-by-Step
The 'Blue Check Mark' means that this solution was answered by an expert.
Learn more on how do we answer questions.

Use a relay and small transistor.

Figure 9-5 shows how you can connect up a transistor and relay on breadboard. Make sure that both the transistor and diode are placed the right way around. The diode has a stripe at one end, and the transistor used here has one side flat and one curved.

To make this recipe, you will need:

• Breadboard and jumper wires (see “Prototyping Equipment” on page 380)

• 1kΩ resistor (see “Resistors and Capacitors” on page 380)

• Transistor 2N3904 (see “Transistors and Diodes” on page 381)

• 1N4001 diode (see “Transistors and Diodes” on page 381)

• 5V relay (see “Miscellaneous” on page 382)

• Multimeter

You can use the same LED blink program that you used in Recipe 9.1. If all is well, you’ll hear a click from the relay each time the contacts are closed. However, relays are slow mechanical devices, so don’t try to use them with PWM. It may damage the relay.

Discussion

Relays have been around since the early days of electronics and have the great advantage of being easy to use, plus they’ll work in any situation where a switch would normally work—for example, when you’re switching AC (alternating current) or in situations where the exact wiring of the device being switched is unknown.

If the relay contacts are asked to exceed their specifications, then the relay’s life will be shortened. There will be arcing, and the contacts may eventually fuse together. There is also the possibility of the relay becoming dangerously hot. When in doubt, overspecify the relay contacts.

Figure 9-6 shows how a relay is constructed.

A relay is essentially a switch whose contacts are closed when an electromagnet pulls them together. Since the electromagnet and switch are not connected electrically in any way, this protects the circuit driving the relay coil from any high voltages on the switch side.

The downside of relays is that they are slow to operate and will eventually wear out after many hundreds of thousands of operations. This means they are only suitable for slow on/off control, not for fast switching like PWM.

The coil of a relay requires about 50 mA to close the connections. Because a Raspberry Pi GPIO pin is only capable of supplying about 3 mA, you need to use a small transistor as a switch. You don’t need to use a high-power MOSFET like you did in Recipe 9.4, but instead you can just use a small transistor. This has three connections. The base (middle lead) is connected to the GPIO pin via a 1kΩ resistor to limit the current. The emitter is connected to GND, and the collector to one side of the relay. The other side of the relay is connected to 5V on the GPIO connector. The diode is used to suppress any high-voltage pulses that occur when the transistor rapidly switches the power to the relay’s coil.

Although relays can be used to switch 110V AC, this voltage is very dangerous and should not be used on a breadboard. If you want to switch high voltages, use Recipe 9.6 instead.

See Also

For switching DC using a power MOSFET, see Recipe 9.2.

Another way of controlling a relay is to use a PiFace interface board (Recipe 8.16).

Table A-3. Prototyping equipment
Description Suppliers
M-M jumper wires SparkFun: PRT-08431, Adafruit: 759
M-F jumper wires SparkFun: PRT-09140, Adafruit: 825
F-F jumper wires SparkFun: PRT-08430, Adafruit: 794
Half-sized breadboard SparkFun: PRT-09567 Adafruit: 64
Pi Cobbler Adafruit: 1105
Table A-4. Resistors and capacitors
270Ω 0.25W resistor Mouser: 293-270-RC
470Ω 0.25W resistor Mouser: 293-470-RC
1kΩ 0.25W resistor Mouser: 293-1k-RC
3.3kΩ 0.25W resistor Mouser: 293-3.3k-RC
4.7kΩ 0.25W resistor Mouser: 293-4.7k-RC
10 kΩ trimpot Adafruit: 356, SparkFun: COM-09806, Mouser: 652-3362F-1-103LF
Photoresistor Adafruit: 161, SparkFun: SEN-09088
220nF capacitor MCM: 31-0610, Mouser: 80-C322C224M5U5HA
Table A-5. Transistors and diodes
FQP30N06 N-channel MOSFET transistor SparkFun: COM-10213, Adafruit: 355
2N3904 NPN bipolar transistor SparkFun: COM-00521, Adafruit: 756
1N4001 diode SparkFun: COM-08589, Adafruit: 755
Table A-9. Miscellaneous
1200mAh LiPo battery Adafruit: 258
5V relay SparkFun: COM-00100
5V panel meter SparkFun: TOL-10285
Servo motor SparkFun: ROB-09065, Adafruit: 1449
5V 1A power supply Adafruit: 276
Low power 6V DC motor Adafruit: 711
0.1 inch header pins SparkFun: PRT-00116, Adafruit: 392
5 V 5-pin unipolar stepper motor Adafruit: 858
12 V, 4-pin bipolar stepper motor Adafruit: 324
Magician chassis with gearmotors SparkFun: ROB-10825
Tactile push switch SparkFun: COM-00097, Adafruit: 504
Miniature slide switch SparkFun: COM-09609, Adafruit: 805
Rotary encoder Adafruit: 377
4×3 keypad SparkFun: COM-08653
Piezo buzzer SparkFun: COM-07950, Adafruit: 160
9.5
9.6

Related Answered Questions

Question: 9.11

Verified Answer:

Assuming you have a 5V volt meter, you can use a P...
Question: 9.10

Verified Answer:

The way to do this is to use a technique called Ch...
Question: 9.9

Verified Answer:

Use PWM to control the power to each of the red, g...
Question: 9.8

Verified Answer:

Using the Tkinter user interface framework, write ...
Question: 9.7

Verified Answer:

Using the Tkinter user interface framework, write ...
Question: 9.1

Verified Answer:

Connect an LED (see “Opto-Electronics” on page 381...
Question: 9.3

Verified Answer:

Use a piezo-electric buzzer connected to a GPIO pi...
Question: 9.4

Verified Answer:

These high-power LEDs use far too much current to ...
Question: 9.6

Verified Answer:

Use a PowerSwitch Tail II (see “Modules” on page 3...