While low cost and small size are 2 of the key selling features of Raspberry Pi development boards, the feature that stands out most to me is the uses of the GPIO headers. These headers are able to detect electrical signals from a variety of sources, allowing you to use your Raspberry Pi for almost anything.
How do you detect electrical signals with a Raspberry Pi? Electrical signals can be detected by utilizing any of the 3.3V pins. The pins read with a binary system of high (3.3V) or low (0V).
GPIO Pin Basics
The 40 GPIO (General Purpose Input/Output) pins are a signature part of every Raspberry Pi model since the 1B model. There are 4 primary types of pins available in the header, 2 5V power pins, 2 3.3V power pins, a few ground pins, and several input/output pins.
There are a few things to keep in mind when connecting to the GPIO pins. First is the identification of the pins. If you look at the image above you will notice that the pins or not ordered sequentially. You will also need to set the input/output pin that you are sending the signal through to “input mode” so you do not damage the board.
What is more important to note, which is not on the chart above, is the amperage limit of the GPIO pins. The output of any one pin is maxed at 16ma and the total output of all the pins together is 50ma according to the documentation. The input for the GPIO pins is capped at 16ma each with no total cap. Each pin can be set via the software to receive somewhere between 2ma and 16ma in 2ma increments. Keep this in mind when setting up your connection so that you do not damage your Raspberry Pi when trying to detect a signal.
The maximum input voltage for the GPIO pins is 3.3 V. exceeding this can also damage your Raspberry Pi. The voltage point at which the electrical signal is detected is 1.8V.
You can use a variety of circuits to adjust the amperage and voltage that comes into your Raspberry Pi to make sure that your board is safe and will still be able to detect the signal you want it to read.
Now that we’ve covered the hardware side of detecting an electrical signal, we need to discuss the software side. Raspberry Pi’s can handle a wide variety of programming languages. The most common used for the GPIO is Python.
Detecting An Electrical Signal in Python
First, a file will need to be created. The can be done with the command:
sudo nano ~filename.py~
Then we will write the code for our program.
#import the GPIO package
import RPi.GPIO as GPIO
#set pin 4 to input and the initial value to zero
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
#set program to always be listening
#check for the signal
if GPIO.input(4) == GPIO.HIGH:
#display "Signal Detected!" when the signal is received
Once you have written you code, you can run the file by typing the following in the console:
$ python3 ~filename.py~
Now whenever pin 4 receives a signal, your screen will start printing “Signal Detected!” over and over until the signal stops.
Detecting An Electrical Signal in Scratch 2
Scratch 2 is a visual code editor developed by MIT to help youth learn the principles of coding without having to write any actual code. If you are nervous about writing your own code, you can repurpose the Button block.
As long as you have the latest Raspbian release, you should already have Scratch 2 installed. The first step in setting up Scratch 2 on your Raspberry Pi is adding the GPIO blocks by clicking More Blocks -> Add an Extention -> Pi GPIO. This should cause two new blocks to appear, one of which is the Button block.
The Sequence of bocks you will want to use will look something like this:
What this code is doing is looking for a button on pin 10. When the button is pressed, the program will say “Pressed”. In our case, instead of using a button, the electrical signal will trigger the program to say “Pressed” while it is being received. If the signal is not being received, the output will be “Not pressed”.
This is definitely not the cleanest method to detect an electrical signal with a Raspberry Pi, but if you are uncomfortable writing code then this may be the option you want to go with.
Can a Raspberry Pi measure voltage?
A Raspberry Pi can not directly measure voltage but the beauty of the Raspberry Pi is the number of sensors it can connect to. You can purchase a voltage sensor like this one from Amazon and connect it to your Raspberry Pi.
Can a Raspberry Pi measure current?
Natively, a Raspberry Pi is unable to measure electrical current, especially given the low amperage threshold of the GPIO pins. Instead, you can connect a current sensor like this one to your Raspberry Pi to gather the data that you want.
How much power can the GPIO pins output?
Any single GPIO pin can output 16mA. that multiplied by the 3.3V gives a total of 52.8mW. The entire array of pins can output a maximum of 51mA or 168.3mW. While this may not seem like a lot of power, you need to keep in mind that the pins are not designed for running motors or other power-hungry devices. Instead, the pins are used to control other boards that handle higher wattage.