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/07-servo_hat
IntroductionIn this guide, we will explore how to set up and control both continuous and positional servo motors and LED intensity with PWM using the Astrial board equipped with a Adafruit PWM/Servo HAT. By following this tutorial, you'll be able to connect and control servo motors and electronic components using PWM through the Astrial board, bringing your projects to life.
Hardware setupLet's start by preparing the necessary hardware components:
- First connect the PWM / Servo Hat to the GPIO header of your CM4 board. You might need to solder the hat to its pin extension in order to make it work properly.
- Connect the LED to channel 0 of the Hat with the 220Ω resistor.
- Connect the continuous servos to channel 1 to 4.
- Connect the positional servo to channel 5.
With the hardware in place, let's move on to configuring the software:
- Install Python packages: open a terminal on the Astrial board and run the following command to install the required Python package:
pip3 install PCA9685-driver==1.2.0
- Create the Python script: create a Python script using the code provided in the Code section of this article.
- Copy the custom driver: we provide a custom version of the Adafruit hat driver that works with Astrial. Download it from the GitHub repository of this tutorial and copy it into the Astrial.
- Run the python script: finally, you are ready to run the script
python3 example.py
This script will make the LED blink smoothly for 4 times, then the continuous servo will rotate back and forth at maximum speed and finally the positional servo motor will rotate from 0° to 180° twice.
Comments