Controlling servo motors and LEDs using Python
Having successfully connected the servo motor and LED to our Raspberry Pi, we’ll start writing the Python control code. To facilitate this, we will be using the GPIO Zero Python library, a powerful tool for Raspberry Pi GPIO programming. Our first step in this process will be to set up a Python virtual environment so that we can develop our code.
Setting up our development environment
Just like we did in Chapter 2, we will use a Python virtual environment for our development. As there are libraries that only work with the root installation of Python, we will use system packages in our Python virtual environment. To do this, follow these steps:
- On our Raspberry Pi 5, open a Terminal application.
- To store our project files, create a new directory by running the following command:
mkdir Chapter3
- Then, navigate to the new directory:
cd Chapter3
- Create a new Python virtual environment for our project:
python -m venv ch3-env --system-site-packages
- With this command, we’ve created a new Python virtual environment called
ch3-env
and enabled access to the system site packages. With our new Python virtual environment created, we can source into it with the following command:source ch3-env/bin/activate
- Install the extra packages that are required for our code with the following command:
pip install requests
- The
requests
library in Python simplifies making HTTP requests to web servers. We will use therequests
library when we pull weather data from the web. With therequests
library installed, we may close the Terminal by running the following command:exit
With our project folder created, our Python virtual environment set up and activated, and the requests
package installed, we may now start writing code. We will start by controlling the servo motor through Python code using the Terminal.
Using GPIO Zero to control a servo
GPIO Zero is a Python library for controlling the GPIO pins on the Raspberry Pi. It was created in 2016 by Ben Nuttall and Dave Jones of the Raspberry Pi Foundation. GPIO Zero provides a user-friendly and high-level interface, making it easier to work with GPIO, including controlling LEDs, buttons, servos, and more. It comes pre-installed with the latest Raspberry Pi operating system.
The Servo
class is a part of GPIO Zero and provides a way to control a servo motor. To test our servo motor’s connection to our Raspberry Pi, follow these steps:
- Open a Terminal window and navigate to our project folder by running the following command:
cd Chapter3
- Source our
ch3-env
virtual environment with the following command:source ch3-env/bin/activate
- Open a Terminal window in our
ch3-env
virtual environment and launch Python with the following command:python
- Then, enter the following code to import the
Servo
class and create an object calledservo
. After that, initialize it with the PIN we connected our Servo to in Figure 3.3:from gpiozero import Servo servo = Servo(14)
- The
Servo
class provides several useful methods to control a servo motor. To set the servo to the minimum position, run the following command:servo.min()
- After executing this command, we should notice that our servo motor has pivoted completely to one side. To move our servo motor to the middle position, we can use the following Python command:
servo.mid()
- After executing this command, we should observe that our servo motor has moved to the mid position. For the final test, we’ll move our servo motor to the maximum position:
servo.max()
- We should observe that our servo motor moves to the maximum position (we shouldn’t be alarmed if the motor doesn’t move as far to the maximum position as it does to the minimum position as we will calibrate the motor later in this chapter). To close our servo connection, run the following command:
servo.close()
Why is the servo motor jittering?
We may observe jittering from our SG90 servo motor when it’s controlled through GPIO Zero. A multitude of factors may contribute to this issue, ranging from the power provided to the servo via the GPIO port, to potential mechanical problems with the servo, or even software-related issues within the library. Although an investigation into these causes falls outside the scope of this project, we must acknowledge the potential for motor jittering. A straightforward solution involves closing the connection to the servo using the servo.close()
command after setting its position.
With our servo motor tested, we can focus on the LED. In the next section, we will write some code to control the status of our LED using the GPIO library.
Using GPIO Zero to control an LED
The LED
class is a part of the GPIO Zero library and provides a simple interface to control an LED. We can use this class to control our LED. To test the connection of our LED, follow these steps:
- First, open a new Terminal window and navigate to our project folder with the following command:
cd Chapter3
- Then, source our
ch3-env
virtual environment with the following command:source ch3-env/bin/activate
- Launch Python by running the following command:
python
- Enter the following code to import the
LED
class and create an object calledled
. Once you’ve done this, initialize it with the PIN we connected our LED to in Figure 3.9:from gpiozero import LED led = LED(25)
- The
LED
class provides several useful methods to control an LED. To turn on the LED, type the following:led.on()
- After executing this command, we should notice that our LED has turned on. For the next test, we’ll turn our LED off by running the following Python command:
led.off()
- After executing this command, we should observe that our LED has turned off. For the final test, we’ll blink our LED:
led.blink()
- We should observe that our LED starts blinking. To stop this and turn the LED off, run the following command:
led.off()
Should we encounter issues during testing, there could be several potential causes:
- Incorrect wiring: Incorrect wiring is among the most common problems. It’s crucial to verify our connections and ensure we’ve wired the correct GPIO pin according to our Python script.
- Issues with the power supply: Power supply inadequacies might also lead to issues. While the Raspberry Pi’s GPIO pins may not supply adequate power for certain servos, especially under load, causing the servo to act unpredictably, our SG90 servo and LED shouldn’t face this issue, given they have a lower power demand.
- Software: Software-related issues could also pose problems. Keeping the Raspberry Pi OS and GPIO Zero library up to date is an essential preventative measure.
- Components: Issues could lie within the components themselves. By testing them with a known, functioning device, we can either confirm or rule out this possibility.
Now that we’ve tested our servo and LED components alongside the corresponding code, we can construct the stand that will house our project.