In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you how to understand raspberry pie GPIO programming, the content is concise and easy to understand, it will definitely make your eyes shine. I hope you can get something through the detailed introduction of this article.
In addition to providing common network ports and USB interfaces, raspberry pie also provides a set of GPIO (General Purpose Input/Output) interfaces. This set of GPIO interfaces greatly expands the ability of raspberry pie. GPIO can not only realize communication, but also directly control electronic components, so that users can experience the fun of hardware programming.
Introduction to GPIO
On raspberry pie 3, the GPIO interface consists of 40 PIN. Each pin can be connected to an external device with a wire. You can attach the wire to the PIN by welding, or you can use the mother jumper to connect to the PIN.
Jumper
Among the 40 PIN, there are fixed output 5V (2, 4 PIN), 3.3 V (1, 17 PIN) and ground wire (Ground,6, 9, 14, 20, 25, 30, 34, 39). If the two ends of a circuit are connected between 5V and the ground wire, the circuit will get a voltage input of 5V. PIN numbers 27 and 28 are marked ID_SD and ID_SC. They are two special PIN. They belong to the ID EEPROM (Electrically Erasable Programmable Read-Only Memory) interface and are used to communicate with additional circuit boards that extend the raspberry pie function. Most other PIN programs program the numbering of GPIOX, such as GPIO14. In the raspberry pie operating system, the GPIO number 14 is used to refer to the PIN instead of the location number 8. There are some PIN that provide advanced port functions in addition to GPIO functions. For example, GPIO14 and GPIO15 can also act as UART ports. In addition, I2C and SPI ports can be found on GPIO.
GPIO pins of raspberry pie 3
In computers, high and low voltages are usually used to represent binary ones and zeros. The same is true of raspberry pie. GPIO represents data in the same way. The PIN of each GPIO can be in the input or output state. When in the output state, the system can pass 1 or 0 to the PIN. If it is 1, then the corresponding physical PIN outputs a high voltage of 3.3 V, otherwise a low voltage of 0 V is output. Accordingly, the PIN in the input state can detect the voltage on the physical PIN. If the voltage is high, the PIN will return 1 to the system, otherwise 0. It is using the above simple mechanism that GPIO realizes the interaction with physical circuits.
Control LED lamp
Let's first look at an example of GPIO output. We connect a series circuit between the GPIO21 and the ground wire. There is a LED lamp on the circuit and a 330ohm resistor to prevent short circuit. When the GPIO21 is at a high level, there will be an electric current passing through the circuit, thus lighting the LED lamp.
We use the bash command to control GPIO21. In Linux, external devices are often represented as files. Writing or reading characters to a file is equivalent to outputting or inputting characters to or from a device. The same is true of the GPIO port on the raspberry pie, whose representative file is located under / sys/class/gpio/. First, activate GPIO21:
Echo 21 > / sys/class/gpio/export
This command means to enter the character "21" into / sys/class/gpio/export. As you can see, after the command is executed, a directory representing GPIO21 is added under / sys/class/gpio/, and the directory name is gpio21. Next, we put GPIO21 in the output state:
Echo out > / sys/class/gpio/gpio21/direction
The file / sys/class/gpio/gpio21/direction is used to control the direction of GPIO21. We wrote the character "out" to represent the output. Finally, write 1 to GPIO21, leaving PIN at a high voltage:
Echo 1 > / sys/class/gpio/gpio21/value
As you can see, the LED light is on. If you want to turn off the LED light, simply write 0 to GPIO21:
Echo 0 > / sys/class/gpio/gpio21/value
After using GPIO21, you can delete the port:
Echo 21 > / sys/class/gpio/unexport
/ sys/class/gpio/gpio21 disappears.
GPIO between two raspberry pies
We can connect two raspberry pies in a GPIO way. The GPIO output of one raspberry pie will become the GPIO input of another raspberry pie. The connection is simple, requiring only two wires. One wire connects two raspberry pie ground wires, and the other connects the two PIN of raspberry pie:
We use the raspberry pie on the left to output and the raspberry pie on the right to input. The output process is similar to the example of controlling the LED lamp above. The GPIO21 in the first raspberry pie is ready to output:
Echo 21 > / sys/class/gpio/export
Echo out > / sys/class/gpio/gpio21/direction
In the second raspberry pie, get ready to read GPIO26:
Echo 26 > / sys/class/gpio/export
Echo in > / sys/class/gpio/gpio26/direction
When we write "in" to / sys/class/gpio/gpio26, we put the GPIO26 in the input state.
After that, in the first raspberry pie, you can change the output value to 1 or 0:
Echo 1 > / sys/class/gpio/gpio21/value
Echo 0 > / sys/class/gpio/gpio21/value
In the second raspberry pie, you can use the cat command to read the file and get the input value:
Cat / sys/class/gpio/gpio26/value
Because the cat command will be returned after reading once. To keep reading, we can call cat repeatedly with an infinite loop in bash:
While true; do cat / sys/class/gpio/gpio26/value; done
As the output of the first raspberry pie changes, so does the input of the second raspberry pie. We made a simple communication between two raspberry pies.
Finally, don't forget to delete the port after using GPIO.
UART programming
Computer data is a sequence of zeros and ones of many bits. Although GPIO can switch between 0 and 1, it cannot accurately split bits. For example, if we output a binary sequence 11000111 to the GPIO port, from the input point of view, it just enters 1 for a period of time, then becomes 0, and then becomes 1 again. The input can not accurately say how many bits of 1 are wrapped in a high-level input.
One solution is to use multiple PIN to communicate at the same time, each PIN representing one bit. When the input side is finished reading, notify the output side, let the output side send the following batch of data. This mode of communication is called parallel port transmission. And parallel port corresponds to serial port transmission. A PIN is still used for transmission, but the input side can know how long the one-bit data lasts. UART, I2C and SPI on GPIO are all serial communication.
UART differs from the other two in that both parties send or receive data at a predetermined rate. This mode of communication is called asynchronous communication. In synchronous communication such as I2C and SPI, additional connections are used to ensure the same rate on both sides. The connection and implementation of UART is very simple, and it has become the most popular way of serial communication. But the disadvantage of UART is that communication errors occur if the rates of the sender and receiver are different. The communication rate is called "baud rate" (baudrate), and the unit is the number of bits per second (bps).
The port of UART has at least three pins: RX, TX and ground wire. RX is responsible for reading and TX is responsible for output. If there are two UART ports, they are connected as follows:
In the case of raspberry pie 3, TX and RX are GPIO14 and GPIO15 pins. Therefore, we can connect the two raspberry pies as shown above, and then achieve UART communication between the two raspberry pies.
Here, we should pay attention to a little change in raspberry pie 3. The standard UART is used in raspberry pie 1 and 2, and the corresponding file in the operating system is / dev/ttyAMA0. In Raspberry pie 3, the new Bluetooth module uses the standard UART port to communicate with the raspberry pie. The external UART communication uses a simple Mini UART, and the corresponding file in the operating system is / dev/ttyS0. Because the baud rate of mini UART depends on the CPU clock rate, and the CPU frequency may fluctuate during operation, mini UART often brings unintended errors. There are generally two solutions. One is to turn off the Bluetooth module and let the external connection reuse the standard UART port. The other is to fix the CPU clock frequency so that mini UART can communicate at an accurate baud rate.
To close the Bluetooth module, you need to modify / boot/config.txt and add:
Dtoverlay=pi3-disable-bt
Restart after modification. After that, UART communication can be done through / dev/ttyAMA0.
If you adopt the second solution or modify / boot/config.txt, the above modification is as follows:
Core_freq=250
Dtoverlay=pi3-miniuart-bt
Restart after modification. After that, UART communication can be done through / dev/ttyS0.
Let's take the first solution as an example for UART communication. Set the baud rate:
Stty-F / dev/ttyAMA0 9600
Output text:
Echo "hello" > / dev/ttyAMA0
Read the text:
Cat / dev/ttyAMA0
If you use the second solution, you only need to change the / dev/ttyAMA0 above to / dev/ttyS0.
As you can see, UART can achieve more complex text communication.
Connect PC with UART
The general PC does not have exposed UART pins. In order to connect PC to raspberry pie through UART, we need a converter between USB and UART. One end of the converter is the USB interface, and the other end is the UART pin. We insert the USB end into the PC. The other end is connected to the UART pin of the raspberry pie in a UART-to-UART manner.
Once connected, you can use serial port operation software to communicate with raspberry pie on PC. Under Linux, the USB connection is represented as / dev/ttyUSB0. Of course, when there is only one USB device on the computer, the final number will be 0. On my Mac OSX, the USB connection is represented as / dev/cu.SLAB_USBtoUART. After that, you can manipulate the USB file for UART communication. Under Windows, there is also ready-made graphical software for serial communication.
Log in to raspberry pie with UART
We can also use UART to connect and log on to raspberry pie. Enter the raspberry pie settings:
Sudo raspi-config
In Interfacing Options- > Serial, it is allowed to log in through the serial port when booting.
After reboot, the raspberry pie will automatically push the baud rate of 115200 boot information to the UART port when it starts. On PC at the other end of UART, if you use Mac OSX, you can connect with the following command:
Screen / dev/cu.SLAB_USBtoUART 115200
If PC is a Linux system, you only need to change the USB device file to the corresponding device file. If it is a Windows system, you can also use graphical software. I won't repeat it here.
The above content is how to understand the GPIO programming of raspberry pie. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.