Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Internet of Things Programming Projects

You're reading from   Internet of Things Programming Projects Build exciting IoT projects using Raspberry Pi 5, Raspberry Pi Pico, and Python

Arrow left icon
Product type Paperback
Published in Jun 2024
Publisher Packt
ISBN-13 9781835082959
Length 458 pages
Edition 2nd Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Colin Dow Colin Dow
Author Profile Icon Colin Dow
Colin Dow
Arrow right icon
View More author details
Toc

Table of Contents (21) Chapters Close

Preface 1. Part 1: Setting Up the Raspberry Pi for IoT Development FREE CHAPTER
2. Chapter 1: Understanding the Raspberry Pi 3. Chapter 2: Harnessing Web Services with the Raspberry Pi 4. Chapter 3: Building an IoT Weather Indicator 5. Chapter 4: Building an IoT Information Display 6. Part 2: Building an IoT Home Security Dashboard
7. Chapter 5: Exploring the GPIO 8. Chapter 6: Building an IoT Alarm Module 9. Chapter 7: Building an IoT Button 10. Chapter 8: Creating an IoT Alarm Dashboard 11. Part 3: Creating a LoRa-Enabled IoT Monitoring Station
12. Chapter 9: Understanding LoRa 13. Chapter 10: Integrating LoRa with the Internet 14. Part 4: Building an IoT Robot Car
15. Chapter 11: Introducing ROS 16. Chapter 12: Creating an IoT Joystick 17. Chapter 13: Introducing Advanced Robotic Eyes for Security (A.R.E.S.) 18. Chapter 14: Adding Computer Vision to A.R.E.S. 19. Index 20. Other Books You May Enjoy

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:

  1. On our Raspberry Pi 5, open a Terminal application.
  2. To store our project files, create a new directory by running the following command:
    mkdir Chapter3
  3. Then, navigate to the new directory:
    cd Chapter3
  4. Create a new Python virtual environment for our project:
    python -m venv ch3-env --system-site-packages
  5. 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
  6. Install the extra packages that are required for our code with the following command:
    pip install requests
  7. The requests library in Python simplifies making HTTP requests to web servers. We will use the requests library when we pull weather data from the web. With the requests 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:

  1. Open a Terminal window and navigate to our project folder by running the following command:
    cd Chapter3
  2. Source our ch3-env virtual environment with the following command:
    source ch3-env/bin/activate
  3. Open a Terminal window in our ch3-env virtual environment and launch Python with the following command:
    python
  4. Then, enter the following code to import the Servo class and create an object called servo. After that, initialize it with the PIN we connected our Servo to in Figure 3.3:
    from gpiozero import Servo
    servo = Servo(14)
  5. 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()
  6. 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()
  7. 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()
  8. 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:

  1. First, open a new Terminal window and navigate to our project folder with the following command:
    cd Chapter3
  2. Then, source our ch3-env virtual environment with the following command:
    source ch3-env/bin/activate
  3. Launch Python by running the following command:
    python
  4. Enter the following code to import the LED class and create an object called led. 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)
  5. The LED class provides several useful methods to control an LED. To turn on the LED, type the following:
    led.on()
  6. 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()
  7. After executing this command, we should observe that our LED has turned off. For the final test, we’ll blink our LED:
    led.blink()
  8. 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.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image