Reader small image

You're reading from  Robotics at Home with Raspberry Pi Pico

Product typeBook
Published inMar 2023
Reading LevelBeginner
PublisherPackt
ISBN-139781803246079
Edition1st Edition
Languages
Concepts
Right arrow
Author (1)
Danny Staple
Danny Staple
author image
Danny Staple

Danny Staple builds robots and gadgets as a hobbyist, makes videos about his work with robots, and attends community events such as PiWars and Arduino Day. He has been a professional Python programmer, later moving into DevOps, since 2009, and a software engineer since 2000. He has worked with embedded systems, including embedded Linux systems, throughout the majority of his career. He has been a mentor at a local CoderDojo, where he taught how to code with Python. He has run Lego Robotics clubs with Mindstorms. He has also developed Bounce!, a visual programming language targeted at teaching code using the NodeMCU IoT platform. The robots he has built with his children include TankBot, SkittleBot (now the Pi Wars robot), ArmBot, and SpiderBot.
Read more about Danny Staple

Right arrow

Sensing Distances to Detect Objects with Pico

Our robot is starting to move around independently. We spent the last chapter preparing mount points to add sensors, including distance sensors. We can use these sensors to detect how far objects are from the robot, and by adding more than one, we can see which direction is closest. This sense will allow the robot to respond to the real world and drive around a room without much manual control.

In this chapter, we will learn more about these sensors and their limitations. Then, we will attach the sensors to the robot and learn more about the communication protocol used to talk to them. Next, we will wire the sensors into Raspberry Pi Pico and get data. Finally, we will tie multiple sensors together with motor control to make the robot avoid obstacles while driving.

In this chapter, we’re going to cover the following main topics:

  • How distance sensing works
  • Soldering headers and attaching them to the robot
  • An...

Technical requirements

This chapter requires the following:

  • The robot build from previous chapters
  • 2 x Pimoroni VL53L1X distance sensor modules
  • 2 x five-way single row 2.54-mm (included with the modules)
  • 8 x male-to-female jump wires with a 2.54-mm DuPont connector
  • 2 x M2 nuts and M2 x 6-mm bolts
  • A suitable screwdriver for driving the bolts, and a spanner for holding the nuts
  • A soldering station with a soldering iron, solder, tip-cleaning brass, and soldering stand
  • A flat work area with good lighting, free of interruptions or being nudged
  • The code from previous chapters
  • A Raspberry Pi Pico code editor such as Mu or Thonny
  • A USB micro cable

You can find the code for this chapter at https://github.com/PacktPublishing/Robotics-at-Home-with-Raspberry-Pi-Pico/tree/main/ch-08.

How distance sensing works

Before diving into connecting and programming distance sensors, we should examine how they operate. Chapter 7, Planning and Shopping for More Devices, evaluated options and chose optical (light-based) distance sensors. We will be focusing on this type for the remainder of this chapter.

Many distance sensors operate using a principle known as time of flight. The following diagram demonstrates this:

Figure 8.1 – Optical distance sensor operation

The preceding diagram shows pictures of robots with sensors and the returned light. On the top left, a single beam is emitted (shown as a cone), hits an object, and its reflection (shown as a dashed line) hits the sensor (the blue box), which detects it. The time between emitting the beam and receiving the response is the time of flight used to calculate the distance. At the top right of the diagram, both sensors are active. However, the left sensor detects a closer object in its beam...

Soldering headers and attaching them to the robot

Before we can start to use or wire the sensors, we will need to solder headers onto them and then bolt them so they face forward on the robot.

Soldering headers

I recommend using a spare breadboard for soldering these, as you did with Raspberry Pi Pico and the motor controller earlier in Chapter 4, Building a Robot around Pico. The following photo shows me soldering them:

Figure 8.2 – Soldering the distance sensor headers

The photo on the left of the preceding figure shows the sensors, the headers, and a breadboard to aid soldering. Place the headers long pins in the breadboard holes and the sensors on top. Pimoroni designed these sensor modules to hold the board on the header for easy soldering.

In the right photo, one sensor has had the header soldered in, and I am soldering the other. The headers should be facing back from the sensor so that the wiring will not be in the sensor beam.

With...

Introduction to I2C communication

You encountered I2C communication in earlier chapters. Chapter 1, discussed how I2C is a data bus that carries address information, allowing a primary device such as Raspberry Pi Pico to reach multiple devices on a single bus. We learned then that Raspberry Pi Pico has two hardware I2C buses. I2C (or I2C) is an acronym for Inter-Integrated Circuit.

In Chapter 7, Planning and Shopping for More Devices, we saw how we would be using I2C devices both for VL53L1X distance sensors along with an IMU.

How exactly does this bus work? Chapter 1, also mentioned that I2C has two wires – a Serial Clock line (SCL) and a Serial Data line (SDA). The following picture shows how devices send signals through them:

Figure 8.4 – I2C signals on the wire

The preceding diagram shows two graphs representing I2C signals. The horizontal axis is time, and the vertical axis when high is logic one, with low being logic zero. As shown...

Communicating with a single distance sensor

Each distance sensor requires only four wires; however, we will also improve the power system. We will then get into the code needed to read data from a system.

Wiring the distance sensors

We start our wiring by looking at a block diagram of our robot, as we saw previously in Chapter 7. Look at the following diagram:

Figure 8.5 – The robot block diagram with distance sensors

The preceding diagram shows the robot block diagram with the additional VL53L1x distance sensors connected via I2C to Raspberry Pi Pico. The new parts have a thick double outline.

We need the schematic to get into the details of the connections, as shown in the following figure:

Figure 8.6 – The schematic with distance sensors

In the preceding figure, I’ve shown a close-up schematic of the distance sensors connected to Raspberry Pi Pico. We connect a sensor to each I2C bus, along with 3v3...

Connecting two distance sensors

We have wired in two sensors, each on a separate set of pins. Create the read_2_sensors.py file. The imports look identical:

import time
import board
import busio
import adafruit_vl53l1x

When we come to set up the sensors, we first need to set up two I2C buses on the different pins and then use them:

i2c0 = busio.I2C(sda=board.GP0, scl=board.GP1)
i2c1 = busio.I2C(sda=board.GP2, scl=board.GP3)
vl53_l = adafruit_vl53l1x.VL53L1X(i2c0)
vl53_r = adafruit_vl53l1x.VL53L1X(i2c1)

We can also apply the same configuration settings for both sensors:

vl53_l.distance_mode = 1
vl53_l.timing_budget = 100
vl53_r.distance_mode = 1
vl53_r.timing_budget = 100

The main loop starts in the same way, with both sensors going into ranging mode:

vl53_l.start_ranging()
vl53_r.start_ranging()
while True:

And in this case, we will check for ready data from both sensors before printing:

    if vl53_l.data_ready and vl53_r.data_ready:...

Building a wall avoider with Raspberry Pi Pico

Two distance sensors and independent motor control with some code are the ingredients needed to avoid obstacles. Let’s start by putting the distance sensors in the shared robot library.

Preparing the robot library

Like we have with other aspects of the robot, we’ll start by building the distance sensors set up in the robot.py file. At the top of this file, the imports now include busio and adafruit_vl53l1x libraries:

import board
import pwmio
import pio_encoder
import busio
import adafruit_vl53l1x

We can then set up our left and right distance sensors. Insert the following below the encoder setup and above the stop function:

i2c0 = busio.I2C(sda=board.GP0, scl=board.GP1)
i2c1 = busio.I2C(sda=board.GP2, scl=board.GP3)
left_distance = adafruit_vl53l1x.VL53L1X(i2c0)
right_distance = adafruit_vl53l1x.VL53L1X(i2c1)

Save this file and be sure to upload it to the CIRCUITPY volume.

We will use robot.py in the...

Troubleshooting

These steps should get you up and running if you have any problems:

  • If the robot complains about importing modules, ensure you have uploaded the code and libraries for this chapter and previous ones.
  • If the robot is getting too close before turning, try increasing the too_close variable.
  • With any behavior using motors, ensure the batteries are fresh. Low power can cause stalling motors and malfunctioning sensors.
  • The sensors will not detect obstacles that are above or below them. This limitation means the robot will drive into and get stuck on low obstacles or under high ones.

Your robot now avoids walls and objects.

Summary

In this chapter, we learned about distance sensors. We looked at how the sensors operate and how to attach them.

We learned more about the I2C bus and then saw how to electrically connect these VL53L1X distance sensors.

We then looked at the operating modes of the VL53L1X sensor and wrote code to get readings from one. Finally, we finished with a behavior to avoid walls using this sensor.

In the next chapter, we will gain remote control of our robot by adding Bluetooth.

Exercises

You can use these exercises to practice more of the concepts learned in this chapter:

  • Could you write code to follow an object at a fixed distance? If it’s further away, could you drive it forward, and if it’s too close, back it up a little?
  • Try different materials in front of the robot, such as glass, black fabric, thin paper, and thick paper. Observe which are detected and how they affect the distance detected. For example, what happens if you aim the robot at a mirror?
  • Try observing the distance measurements in different light conditions, such as room light, darkness, and full sunlight.

Additional reading

The following sources of additional reading can deepen your understanding of these sensors:

For an alternative technique using ultrasonic distance sensors, refer to Learn Robotics Programming – Second Edition by Danny Staple.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Robotics at Home with Raspberry Pi Pico
Published in: Mar 2023Publisher: PacktISBN-13: 9781803246079
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.
undefined
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 $15.99/month. Cancel anytime

Author (1)

author image
Danny Staple

Danny Staple builds robots and gadgets as a hobbyist, makes videos about his work with robots, and attends community events such as PiWars and Arduino Day. He has been a professional Python programmer, later moving into DevOps, since 2009, and a software engineer since 2000. He has worked with embedded systems, including embedded Linux systems, throughout the majority of his career. He has been a mentor at a local CoderDojo, where he taught how to code with Python. He has run Lego Robotics clubs with Mindstorms. He has also developed Bounce!, a visual programming language targeted at teaching code using the NodeMCU IoT platform. The robots he has built with his children include TankBot, SkittleBot (now the Pi Wars robot), ArmBot, and SpiderBot.
Read more about Danny Staple