For a more in-depth guide, we've created a detailed tutorial hosted on GitHub. In this tutorial, you'll find step-by-step instructions, code examples, and additional resources to help you better understand and implement the concepts discussed here. Whether you're a beginner or looking to deepen your knowledge, this comprehensive guide covers everything you need.
You can access the GitHub repository with the full code here: https://github.com/System-Electronics/astrial_tutorials/tree/main/02-rgb_led_blink_waveshare
PremiseIn this tutorial we are going to use the three Waveshare carrier boards provided in the components section.
These boards have all the same pinouts and the steps required to make the RGB LED blink are the same hence we are going to refer to the boards simply as CM4.
The CM4 PoE Board can be powered like the other boards via the USB-C port but also via the ethernet port by using a PoE injector (like the one in the image below).
- Connect the Astrial module: connect the Astrial’s ground to any CM4’s ground pin, and Astrial’s VCC to any CM4’s 5V pin. For this tutorial we have used pins 2 and 6 as 5V and GND respectively.
- Connect the RGB led: connect the led’s ground pin (the longest pin) to any Astrial’s ground pin. For this tutorial we have used pin 20. Connect the red, green, and blue pins of the led to pins 22, 18 and 16, corresponding to GPIOs 25, 24 and 23 respectively.
Take a look at the schematics and the image below to have a clear view of the connections.
- Install the python packages: use the pip command line tool to install the required library to control the GPIO pins of your board. Open a terminal window and run the following command:
pip3 install gpiod==2.2.0
- Create the python script: create a python script that implements the code provided in the Code section of this article.
- Launch the python script: we are finally ready to launch our python script. From the terminal, run the following command:
python3 rgb_led_blink.py
If you have done everything correctly, you should see the RGB LED changing color at regular intervals.
How to find the pin mappingTo control the GPIO pins from the Raspberry Pi 40-pin connector, follow these steps to trace the mapping:
Locate the Raspberry Pi GPIO Pin Number: Use the Raspberry Pi pinout diagram to identify the GPIO pin number associated with your chosen physical pin on the connector. For example: Pin 16 on the Raspberry Pi connector corresponds to GPIO 23. Pin 18 corresponds to GPIO 24. Pin 22 corresponds to GPIO 25.
- Locate the Raspberry Pi GPIO Pin Number: Use the Raspberry Pi pinout diagram to identify the GPIO pin number associated with your chosen physical pin on the connector. For example: Pin 16 on the Raspberry Pi connector corresponds to GPIO 23. Pin 18 corresponds to GPIO 24. Pin 22 corresponds to GPIO 25.
Understand the Mapping: Each GPIO on the CM4 carrier corresponds to a specific GPIO chip (gpiochipX) and line number. This mapping is determined by the DTS configuration. Use the following table to find the associated GPIO chip and line for your desired GPIO:
- Understand the Mapping: Each GPIO on the CM4 carrier corresponds to a specific GPIO chip (gpiochipX) and line number. This mapping is determined by the DTS configuration. Use the following table to find the associated GPIO chip and line for your desired GPIO:
You can find the complete table in our GitHub tutorial.
Determine the Path to the GPIO Pin: Using the table above, trace the GPIO to its /dev/gpiochipX representation: GPIO23 → /dev/gpiochip1 line 1, GPIO24 → /dev/gpiochip1 line 2, GPIO25 → /dev/gpiochip1 line 3
- Determine the Path to the GPIO Pin: Using the table above, trace the GPIO to its /dev/gpiochipX representation: GPIO23 → /dev/gpiochip1 line 1, GPIO24 → /dev/gpiochip1 line 2, GPIO25 → /dev/gpiochip1 line 3
Drive the GPIO Pin: Once you have the gpiochip and line, you can control the pin using Python's gpiod library, as shown in the example script.
- Drive the GPIO Pin: Once you have the gpiochip and line, you can control the pin using Python's gpiod library, as shown in the example script.
Comments