Raspberry Pi GPIO Pinout: What Each Pin Does on Pi 4, Earlier Models

The best thing about all Raspberry Pis, including the Raspberry Pi 4, is that they can be used to build all sorts of awesome contraptions, from robots to retro game consoles to fart detectors. Most of the sensors, motors, lights, and other peripherals that make these projects possible connect to the Pi’s set of GPIO (General Purpose Input Output) pins. These pins provide a direct connection to the system-on-chip (SoC) at the heart of the Pi, allowing the Pi to communicate with external components and add-ons known as HATs (Hardware Attached on Top). All Pi models since 2014’s Raspberry Pi B+ had 40 GPIO pins, but the Pi Zero and Zero 2 W have 40 holes where you can solder pins or wires. Don’t worry if you don’t have a soldering iron. We have a list of the best soldering irons for you.
This guide has been updated to reflect the new features of the Raspberry Pi 4. The Raspberry Pi 4 still comes with 40 GPIO pins, but some additional I2C, SPI, and UART connections are available.
No matter what you build, you’ll need to know the Raspberry Pi’s GPIO pinout, a map, and a description of what each pin can do. Some pins provide voltage and logic, others are ground, and still others connect to different kinds of interfaces. All of these are described below.
General purpose input/output (GPIO) pins
GPIO is the most basic yet accessible aspect of the Raspberry Pi. GPIO pins are digital. That is, it can have two states, off or on. They can have a direction (input, output, respectively) from which they receive or transmit current, and programming languages such as Python, JavaScript, and node-RED can be used to control the state and direction of the pins.
The operating voltage of the GPIO pin is 3.3v and the maximum current is 16mA. This means that you can safely power one or two LEDs (Light Emitting Diodes) from a single GPIO pin via a resistor (see resistor color code). However, for things that require more current, such as DC motors, you should use external components to avoid damaging the GPIO.
To control GPIO pins in Python, first import a library of pre-written code.The most common libraries are RPi.GPIO It has been used to create thousands of projects since the early days of the Raspberry Pi.These days, a new library called GPIO zero was introduced to provide easier entry for those unfamiliar with Python and basic electronics. Both of these libraries are pre-installed on the Raspberry Pi OS.
GPIO pins have multiple names. The first and most obvious reference is the “physical” location on the GPIO. Starting from the top left of the GPIO, there is a physical pin 1, which means the pin closest to where the micro SD card is inserted, providing 3v3 power. To the right of that pin is physical pin 2, which provides 5v power. Then the pin numbers increase as you move down each row, going from pin 1 to pins 3, 5, 7, and finally pin 39. You can quickly see that each pin from 1 to 39 in this column follows an odd order. And in the column starting at pin 2 it goes 4, 6, 8, etc. until it reaches 40. Follows an even sequence. Physical pin numbering is the most basic way to find pins, but many tutorials written for the Raspberry Pi follow a different numbering sequence.
The Broadcom (BCM) pin numbering (aka GPIO pin numbering) looks confusing to the average user. GPIO17, 22, and 27 follow each other with little thought of logical numbering. BCM pin mapping refers to GPIO pins that are directly connected to the Raspberry Pi’s system-on-chip (SoC). Essentially, it links directly to the Pi’s brain to connect the sensors and components you use in your project.
Most Raspberry Pi tutorials use this reference. This is because this is the officially supported pin numbering scheme from the Raspberry Pi Foundation. Therefore, it is a best practice to start learning using the BCM pin numbering scheme. Also note that the BCM and GPIO pin numbering refer to the same scheme. For example, GPIO17 is the same as his BCM17.
Certain GPIO pins also have alternate functions that allow interfacing with different types of devices using I2C, SPI, or UART protocols. For example, GPIO3 and GPIO 4 are also SDA and SCL I2C pins used to connect devices using the I2C protocol.To use these pins with these protocols, the Raspbian OS[設定]The interface must be enabled using the Raspberry Pi configuration application found in the menu.
I2C, SPI, and UART: Which Should You Use?
Below we discuss the specific differences between I2C, SPI, and UART, but if you’re wondering which one you should use to connect to your particular device, check the spec sheet . For example, one small LED screen might require SPI and another might use I2C (although very few use UART). Reading the documentation that comes with the product (if it does) will usually tell you which Pi pins to use.
Raspberry Pi 4 users should note that more I2C, SPI, and UART pins are now available. These additional interfaces are activated using the device tree overlay and can provide four additional SPI, I2C and UART connections.
I2C – Inter Integrated Circuit
I2C is a low-speed two-wire serial protocol for connecting devices using the I2C standard. Devices using the I2C standard have a master-slave relationship. There can be multiple masters, but each slave device must have a unique address. This address is obtained by the manufacturer from NXP (previously known as Philips Semiconductors). This means that a single I2C connection can communicate with multiple devices, as each device is unique and can be detected by the user and computer using Linux commands such as i2cdetect.
As mentioned earlier, I2C has two connections, SDA and SCL. They work by sending data to and receiving data from the SDA connection at a controlled rate via the SCL pin. I2C is a quick and easy way to add a variety of components to your project, such as analog-to-digital converters for use in LCD/OLED screens, temperature sensors, photoresistors, and more. While it can prove to be a little more difficult to understand than standard GPIO pins, the knowledge gained from learning I2C will help you understand how to connect precision sensors for use in the field.
The Raspberry Pi has two I2C connections on GPIOs 2 and 3 (SDA and SCL), one for I2C0 (master), and physical pins 27 and 28, which connect the Pi to a compatible HAT (top-mounted hardware) I2C pins that allow you to communicate with add-on boards. .
SPI – Serial Peripheral Interface
SPI is another protocol for connecting compatible devices to the Raspberry Pi. It is similar to I2C in that there is a master-slave relationship between the Raspberry Pi and the device connected to it.
SPI is typically used to send data over short distances between a microcontroller and components such as shift registers, sensors, or even SD cards. Data is synchronized using the clock (SCLK on GPIO11) from the Master (Pi) and data is sent from the Pi to the SPI component using his MOSI (GPIO GPIO10) pin. MOSI stands for Master Out Slave In. When the component needs to respond to the Pi, it sends the data back using the MISO pin (GPIO9) representing Master In Slave Out.
UART – Universal Asynchronous Receiver/Transmitter
UART pins (transmit GPIO14, receive GPIO15), commonly known as “serial”, provide console/terminal login for headless setups. This means connecting to the Pi without a keyboard or pointing device. Usually the easiest way to do a headless Raspberry Pi setup is to control the Pi via network or direct USB connection (for the Pi Zero).
However, if you don’t have a network connection, you can also control the Headless Pi from a computer running Terminal Console using a serial cable or USB to serial board. UART is very reliable and allows access to the Pi without the need for additional equipment. Don’t forget to enable the serial console in the Raspberry Pi configuration application. You probably don’t want to do this, but there is UART support if you need it.
ground (GND)
Ground is commonly called GND, gnd, or -, but they all mean the same thing. GND is where all voltages can be measured and completes the electrical circuit. This is our zero point, by connecting a component such as an LED to power and ground, it becomes part of the circuit and current flows through the LED to produce light.
When building circuits it is always wise to make the ground connection first before applying power to prevent problems with sensitive components. The Raspberry Pi has 8 ground connections along the GPIO and each of these ground pins is connected to his 1 ground connection. Therefore, the choice of which ground pin to use is determined by personal preference or convenience in connecting components.
5v
The 5v pin provides direct access to 5v power from the mains power adapter, using less power than the Raspberry Pi itself uses. The Pi can be powered directly from these pins and can also power other his 5v devices. If you use these pins directly, be careful and check the voltages before connecting them as they bypass safety features such as voltage regulators and fuses that are there to protect the Pi. Bypassing these with higher voltages can render the Pi inoperable. Especially when 5v and 3.3v are connected together, yes we do it once and we know it will kill the Raspberry Pi in seconds.
3v3
The 3v pin is there to provide a stable 3.3v for powering components and testing LEDs. In practice, you rarely incorporate this pin into your build, but it does have a special use. When connecting an LED to a GPIO, you first need to make sure the LED is wired correctly and lit. By connecting the long leg, anode of the LED to a 3.3v pin through a resistor and the short leg, cathode to one of the ground (GND) pins, you can verify that the LED is lit and working. increase. This eliminates hardware failures from your project and allows you to start building your project with confidence.
More information: Raspberry Pi how-to
Read more: Why Every Tech Geek Should Own a Raspberry Pi
More information: How to set up a headless Raspberry Pi
Read more: 25+ Linux commands every Raspberry Pi user should know