Using digital out to switch and display a relay status

For switching a relay, we only need to use one pin on the Raspberry Pi and have its mode set to OUTPUT and turn this high or low to open or close a relay. Most relays need 5V to be able to switch, so we will be using a 2N3904 NPN transistor. With this transistor, we are able to switch a 5V lane with 3.3V so we can switch a relay.

A word of caution: Switching with mains voltages can be very dangerous. Only do this when you are absolutely sure what you are doing. Also, when beginning with relays, make sure you use a mechanical relay as these are able to switch any load up to the maximum rated on the relay.

In our schematic, we will only be switching the relay. This is done for safety reasons ...

Get Raspberry Pi 3 Projects for Java Programmers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.