In the world of electronics, lighting up an LED is often the first task for those learning Arduino or other microcontrollers, similar to the “Hello World” in C programming. This time, we will also try to light up an LED using the GPIO of the Raspberry Pi.
1. Understanding the GPIO of Raspberry Pi B
GPIO (General-purpose input/output) is similar to the P0—P3 of the 8051 microcontroller, where the pins can be freely used by the programmer. Depending on the actual situation, the pins can serve as general input (GPI), general output (GPO), or both (GPIO), such as when functioning as a clock generator or chip select.
Since a pin can be used for input, output, or other special functions, there must be registers to select these functions. For input, the state of the pin can be determined by reading a specific register; for output, a register can be written to set the pin to high or low; and for other special functions, there are different registers to control them.
For beginners, understanding GPIO can be simplified to the external pins that come from the chip, which have at least two functions (input and output). What does output mean? For example, if we connect an LED, the CPU needs to set a pin to high to provide power to the LED, which is output. What does input mean? For instance, if we connect an infrared human sensor, the CPU needs to read the state from a pin; if it detects a person, that pin will go high, which is input.
The multiplexing of GPIO refers to some pins serving not only as regular input/output but also having functional roles beyond standard I/O, such as being used for JTAG debugging, serial TX, RX, etc. However, a pin can only serve one function at a time; multiplexing does not mean that a pin can simultaneously be used for input and output.
In other words, before using GPIO, we need to set the corresponding mode for the pin to function properly. After a reboot, it returns to its initial mode. Each pin set to a different mode has different functions. Some pins are fixed, such as 3.3V, 5.5V, and GND, which cannot be used as operable GPIO.
First, let’s take a look at the GPIO of Raspberry Pi B, which has 26 pins.
Raspberry Pi pins can be numbered in many ways: essentially, there is not much difference; different numbering methods correspond to different pin numbers.
Assuming we are using pin 15 in the P1 numbering (15 in the header section), named GPIO3 (the name may vary). If using BCM numbering, it is pin 22. If using WiringPi, it is pin 3. Using different numbering methods, the pin numbers may be different. As long as you refer to the correct table for operation, there should be no issues.
In the actual diagram, the board is numbered using the P1 numbering method. In Python GPIO, it is in BOARD mode.
Unless otherwise specified, we will default to the P1 numbering method. From top to bottom, from left to right (please rotate the image I posted 90 degrees clockwise to count QAQ), numbers 1-26.
By executing #gpio readall, you can accurately obtain the GPIO information of your board.
Let’s first take a look at what the initial mode of each pin is. Here we will look at the results. Later, there will be source code for reading using Python.
Here we can see that PIN8 and PIN10 default to serial mode. In the previous section, we did not set the mode during serial debugging, yet it worked normally because the board defaults to serial mode for pins 8 and 10 after booting. Pins 1, 2, 4, 6, 9, 14, 17, 20, and 25 are power, 3.3V, 5.5V, or GND. The others are default GPIO.IN, all in input mode. To light up the LED, we certainly need to set a pin to OUT mode.
2. Directly Lighting Up an LED
According to the table, PIN1 is the positive terminal for 3.3V power. PIN6 is 0V, which is the GND negative terminal. Does connecting an LED automatically make it light up? That’s smart! However, for safety, we should still connect a resistor of around 300 ohms.
3.3/300=0.011A=11ma. This is relatively safe. I won’t elaborate on high school physics here.
Required materials:
1. Breadboard (for easy connections without manual wiring)
2. Two male-to-female DuPont wires
3. One LED (reference voltage 3.0-3.2V) with a current of 5-20 ma. Note the positive and negative of the LED. How to distinguish? First: the longer pin is positive, the shorter pin is negative. Second: look inside the LED; the larger part is connected to the negative terminal, and the smaller part is connected to the positive terminal.
4. One resistor of about 300 ohms.
The wiring is as follows: PIN1 (3.3V positive) ——– resistor —— LED positive ——– LED negative ——– PIN6 (GND negative). The LED will light up.
3. Programming to Control the LED
There are two common libraries used to operate the GPIO of Raspberry Pi: one is the Python RPi.GPIO (https://pypi.python.org/pypi/RPi.GPIO), and the other is the C language WiringPi (http://wiringpi.com/).
1. Using RPI.GPIO
The default complete version of the IMG comes with the RPI.GPIO library and Python environment. We don’t need to install it. If you need to install it, use #sudo apt-get update to install Python #sudo apt-get install python to install pip and use pip to install rpi.gpio #sudo pip install rpi.gpio. This time, we need to modify the wiring scheme above because we want to program a pin, and we cannot operate the power pins. We choose to connect PIN3, keeping the rest unchanged. I won’t post an image here. The connection is as follows: PIN3 positive ——– resistor —— LED positive ——– LED negative ——– PIN6 negative. The syntax of Python is simple and easy to learn.
Create a text file and write:
With the above code, you will see your LED blinking once per second. (This script does not exit and does not clean resources upon exit). You can try modifying the sleep time, e.g., time.sleep(0.2) to change the blinking frequency.
At the same time, we can check the mode of PIN3.
It is found to have changed to GPIO.OUT, which is the effect of GPIO.setup(3, GPIO.OUT) in our script.
The Getmode.py script is as follows. I will not explain further (the power PIN cannot be operated, which will cause exceptions).
2. Using WiringPi to Operate
We will do the same work using WiringPi in C language.
Download the WiringPi library. (I won’t discuss the installation of git; the default complete img also includes it), clone from git #git clone git://git.drogon.net/wiringPi #cd wiringPi #./build
Wait for the compilation to complete; the Raspberry Pi B CPU at 700MHZ is relatively slow, so it takes a little while.
Once compiled, it will automatically install in the /usr/local/lib directory. You just need to use the header files and lib. If your system does not have this directory, refer to the INSTALL in the WiringPi directory for a solution. We will create a led.c file.
#include <wiringPi.h>
#include <unistd.h>
#include <stdbool.h>
int main()
{
// Initialize environment
wiringPiSetup();
// Set PIN3 to output mode, which corresponds to wiringPi; it should be 8 based on the previous diagram. Note this carefully.
pinMode(8,OUTPUT);
while(true)
{
sleep(1);
// Write high
digitalWrite(8,HIGH);
sleep(1);
// Write low
digitalWrite(8,LOW);
}
}
Here, it is particularly important to note that the number is no longer 3, but 8. This is because we are using WiringPi.
Start compiling this file with #gcc -o led led.c -lwiringPi, which means to generate a led binary file from led.c, linking with the WiringPi development library (the base library is linked by default; you don’t need to specify it). If -lwiringPi is not used, it will prompt linking errors due to undefined references.
After compiling, execute with administrator privileges #sudo ./led and you will see the LED blinking once per second again.
What device will we play with next? Is it a buzzer? Or a human infrared sensor? Or a camera? Or after the human infrared sensor detects, the buzzer will sound an alarm?
Or an internet camera, where the Raspberry Pi acts as a client, transmitting home video to your phone over the internet, allowing you to see the situation at home on your phone? Next, we will add motion detection to the camera. Once abnormal movement is detected, the buzzer will sound an alarm and report to the external cloud?
Combining the buzzer, human sensor, camera, internet cloud terminal, and mobile phone terminal will create a small security monitoring system.
Look at Xuezhongce: http://ce.kanxue.com
Look at Xue Forum: http://bbs.pediy.com/
—– WeChat ID: ikanxue —–
Xue Security Continuously focusing on security for 16 years, professionally serving you!