Raspberry Pi Zero W GPIO Control

13/09/2022

This article introduces the GPIO control of Raspberry Pi Zero W, and uses LED to see the effect.

0.1 Raspberry Pi GPIO numbering method
Function Physical Pin
Left to right, top to bottom: left odd, right even: 1-40

raspberry pi

  • BCM
    The numbering focuses on the CPU registers, according to the GPIO register numbering of the BCM2835.

    wiringPi
    The numbering focuses on the implementation logic, and the extended GPIO ports are numbered from 0, which is convenient for programming. As shown in the WiringPi column.

raspberry pi zero w

When operating GPIO, you must first know which set of numbers to use.

1. Prepare
1.1 Hardware
Raspberry Pi Zero W
LED (3mm or 5mm)
1KΩ resistor
3 Dupont wires
Computer (I use Windows 7)

1.2 GPIO interface

raspberry pi 3

1.3 Wiring
First we connect the LED to the Raspberry Pi. The positive pole of the LED is connected in series with a 1KΩ resistor to the GPIO18 (pin12) of the Raspberry Pi, and the negative pole is grounded. (Note that I borrowed a picture from others below. In the picture, the positive pole of the LED is connected to pin11, and we are connected to pin12)

raspberry pi

2. Test
2.1 Connect PC and Pi Zero W
Use putty to connect the computer and Pi Zero W, see the reference document at the end of this article. Fill in raspberrypi.local for Host Name, port 22, username pi, and password raspberry.

Note: There is an empty text file named ssh in the boot partition. This ssh file is easy to be lost. If ssh cannot be logged in, first check whether ssh is lost.

2.2 Control GPIO directly with Shell commands
Expose GPIO18 from kernel space to user space
pi@raspberrypi:~ $ sudo echo 18 > /sys/class/gpio/export

> is the IO redirection symbol. IO redirection refers to changing the default device of linux standard input and output to point to a user-defined device. echo 18 > export is to write 18 to the export file.

After performing this operation, a gpio18 folder will be added to the /sys/class/gpio directory.

View the GPIO18 pin (in Liunx, the device is in the form of a file, and the pin is also a device)

pi@raspberrypi:~ $ cd /sys/class/gpio/gpio18

pi@raspberrypi:/sys/class/gpio/gpio18 $ ls

raspberry pi

  • Set GPIO18 to output mode
    pi@raspberrypi:/sys/class/gpio/gpio18 $ sudo echo out > direction

    Enter 1 into the value file, the GPIO outputs a high level, and the LED lights up
    pi@raspberrypi:/sys/class/gpio/gpio18 $ sudo echo 1 > value

    Enter 0 into the value file, the GPIO output is low, and the LED is off
    pi@raspberrypi:/sys/class/gpio/gpio18 $ sudo echo 0 > value

    back to home directory
    pi@raspberrypi:/sys/class/gpio $ cd ~

    Log out the GPIO18 interface
    pi@raspberrypi:~ $ sudo echo 18 > /sys/class/gpio/unexport

    2.3 Controlling GPIO with Shell Script
    Create a new script named ledonoff.sh.
    pi@raspberrypi:~ $ sudo nano ledonoff.sh

    The script writes the following:

    echo $1 > /sys/class/gpio/export

    echo out > /sys/class/gpio/gpio$1/direction

    echo 1 > /sys/class/gpio/gpio$1/value

    sleep 5 # delay 5 seconds

    echo 0 > /sys/class/gpio/gpio$1/value

    echo $1 > /sys/class/gpio/unexport

    Note: The shell script can pass in parameters, for example, $1 represents the first parameter, $2 represents the second parameter, and so on.

    Add executable permission for ledonoff.sh
    pi@raspberrypi:~ $ sudo chmod +x ledonoff.sh

    run script
    pi@raspberrypi:~ $ sudo ./ledonoff.sh 18

    Result of operation: LED is on and off for 5 seconds.

    2.4 Using Python to control GPIO through PRI.GPIO command
    The most convenient way to control GPIO with Python is to use a python class library, such as RPi.GPIO integrated in the Raspberry Pi system itself.

    Enter the command in putty's ssh terminal:

    Enter the python interactive interface
    pi@raspberrypi:~ $ python

    >>> This is the python prompt.

    Import the python class library RPi.GPIO, named and aliased as GPIO
    >>> import RPi.GPIO as GPIO

    After the introduction, you can use the functions of the GPIO module.

    Set the BOARD encoding method, based on BCM
    Raspberry Pi 3 GPIO is divided into the following three encoding methods: physical pin BOARD encoding, BCM encoding, and wiringPi encoding.

    >>> GPIO.setmode(GPIO.BCM)

    output mode
    >>> GPIO.setup(18,GPIO.OUT)

    GPIO18 outputs high level, the LED lights up
    >>> GPIO.output(18,GPIO.HIGH)

    GPIO18 output low level, LED off
    >>> GPIO.output(18,GPIO.LOW)

    Clean up after use
    >>> GPIO.cleanup()

    Exit the python interface
    >>> Ctrl+D

    2.5 Controlling GPIO with Python script
    Create a new script named blinky.py.
    pi@raspberrypi:~ $ sudo nano blinky.py

    The script writes the following:

    import RPi.GPIO as GPIO

    import time

    GPIO.setmode(GPIO.BCM)

    GPIO.setup(18,GPIO.OUT)

    while True:

    GPIO.output(18,GPIO.HIGH)

    time.sleep(1)

    GPIO.output(18,GPIO.LOW)

    time.sleep(1)

    GPIO.cleanup()

    Description: The loop body below while True needs to be indented, just use the space or Tab (but not mixed) keys to indent.

    Add executable permission to blinky.py
    pi@raspberrypi:~ $ sudo chmod +x blinky.py

    Run the Python script
    pi@raspberrypi:~ $ sudo python blinky.py

    LED blinks.

    stop running
    Use Ctrl+C to break the loop.

    3. Control GPIO in C language with wiringPi GPIO
    The Raspberry Pi kernel has been compiled with a built-in gpio driver. We often use some library functions written by third parties to complete specific operations. The more common operation library functions are:

    Python GPIO
    Python GPIO has been integrated into the Raspberry Pi kernel, which is recommended in the official Raspberry Pi information and easy to use. python GPIO is a small python library that can help users complete raspberry related IO port operations, but python GPIO library does not yet support bus interfaces such as SPI, I2C or 1-wire.

    Common C language libraries are:

    wiringPi 
    wiringPi is suitable for those who have a foundation in C language and have been exposed to microcontroller or embedded development before contacting Raspberry Pi. The API function of wiringPi is very similar to arduino, which also makes it popular. The author gives a lot of instructions and sample codes, which also include UART devices, I2C devices, and SPI devices, etc.

    BCM2835 C Library 

BCM2835 C Library can be understood as related underlying drivers implemented in C language. The driver library of BCM2835 C Library includes GPIO, SPI and UART, etc. You can learn BCM2835 C Library to familiarize yourself with BCM2835-related register operations. If you have the opportunity to develop a linux driver on the Raspberry Pi, or develop your own python or PHP extension driver, you can find a lot of "inspiration" from the BCM2835 C Library.

3.1 WiringPi GPIO installation
WiringPi is a GPIO control library function applied to the Raspberry Pi platform. WiringPi complies with GUN Lv3. wiringPi is developed in C or C++ and can be packaged by other languages ​​such as python, ruby ​​or PHP.

wiringPi includes a set of gpio control commands, which can be used to control the GPIO pins of the Raspberry Pi. Users can use gpio commands to control or query GPIO pins through shell scripts.

wiringPi installation
update list:

pi@raspberrypi:~ $ sudo apt-get update

Update software:

pi@raspberrypi:~ $ sudo apt-get upgrade

Install:

pi@raspberrypi:~ $ sudo apt-get install wiringpi

test
wiringPi includes a set of gpio commands, which can be used to control various interfaces on the Raspberry Pi. The following commands can be used to test whether wiringPi is installed successfully.

pi@raspberrypi:~ $ gpio -v

raspberry pi zero

  • View the GPIO diagram

pi@raspberrypi:~ $ gpio readall

raspberry pi 3

3.2 Writing code
Create a new source program named led_blink.c
pi@raspberrypi:~ $ sudo nano led_blink.c

The content is as follows
#include

int main(void) {

wiringPiSetup();

pinMode (1, OUTPUT);

for(;;) {

digitalWrite(1, HIGH); delay(500);

digitalWrite(1, LOW); delay (500) ;

}

}

Explanation: Looking at the picture in the previous section, the GPIO18 pin of the BCM number is 1 in the wiringPi number.

3.3 Compile and run
compile
pi@raspberrypi:~ $ gcc led_blink.c -o led_blink -l wiringPi

-l wiringPi means to dynamically load the wiringPi shared library.

run
pi@raspberrypi:~ $ sudo ./led_blink

Use Ctrl+C to break the loop.

4. Control GPIO in C language with BCM2835 C Library
4.1 Download and install

raspberry pi zero

  • download bcm2835-1.56.tar.gz
    pi@raspberrypi:~ $ wget //www.airspayce.com/mikem/bcm2835/bcm2835-1.56.tar.gz

    unzip
    pi@raspberrypi:~ $ tar xvzf bcm2835-1.56.tar.gz

    configure compilation
    Go to the compressed directory:

    pi@raspberrypi:~ $ cd bcm2835-1.56

    Execute the configuration command:

    pi@raspberrypi:~/bcm2835-1.56 $ ./configure

    pi@raspberrypi:~/bcm2835-1.56 $ make

    perform inspection
    pi@raspberrypi:~/bcm2835-1.56 $ sudo make check

    Install the bcm2835 library:
    pi@raspberrypi:~/bcm2835-1.56 $ sudo make install

    4.2 Writing code
    Create a new source program named blink_led.c
    pi@raspberrypi:~/bcm2835-1.56 $ cd ~

    pi@raspberrypi:~ $ sudo nano blink_led.c

    The content is as follows
    #include

    #define PIN RPI_GPIO_P1_12

    int main(int argc, char **argv) {

    if (!bcm2835_init())

    return 1;

    bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_OUTP);

    while (1) {

    bcm2835_gpio_write(PIN, HIGH);

    bcm2835_delay(500);

    bcm2835_gpio_write(PIN, LOW);

    bcm2835_delay(500);

    }

    bcm2835_close();

    return 0;

    }

    Note: The numbering method of GPIO is different. The physical interface number of the PCB board is used, and the led is connected to the 12-pin of the Raspberry Pi Zero W board.

    4.3 Compile and run
    compile
    pi@raspberrypi:~ $ gcc blink_led.c -o blink_led -l bcm2835

    -l bcm2835 means to dynamically load the bcm2835 shared library

    run
    sudo ./blink_led

    Use Ctrl+C to break the loop.