Reader small image

You're reading from  Raspberry Pi Pico DIY Workshop

Product typeBook
Published inMay 2022
PublisherPackt
ISBN-139781801814812
Edition1st Edition
Right arrow
Authors (2):
Sai Yamanoor
Sai Yamanoor
author image
Sai Yamanoor

Sai Yamanoor is an embedded systems engineer working for a private startup school in the San Francisco Bay Area, where he builds devices that help students achieve their full potential. He completed his undergraduate work in mechatronics engineering from Sri Krishna College of Engineering and Technology, Coimbatore, India and his graduate studies in mechanical engineering at Carnegie Mellon University, Pittsburgh PA. His interests, deeply rooted in DIY and open software and hardware cultures, include developing gadgets and apps that improve the quality of life, Internet of Things, crowdfunding, education, and new technologies. In his spare time, he plays with various devices and architectures, such as the Raspberry Pi, Arduino, Galileo, Android devices and others. Sai has earlier published a book titled Raspberry Pi Mechatronics Projects.
Read more about Sai Yamanoor

Srihari Yamanoor
Srihari Yamanoor
author image
Srihari Yamanoor

Srihari Yamanoor is a mechanical engineer, working on medical devices, sustainability, and robotics in the San Francisco Bay Area. He completed his undergraduate studies in mechanical engineering from PSG College of Technology, Coimbatore, India and graduate studies in mechanical engineering at Stanford University. He is certified in SolidWorks, simulation, sustainable design, PDM as well as in quality and reliability engineering and auditing. His has a wide range of interests, from DIY, crowdfunding, AI, travelling, photography to gardening and ecology.
Read more about Srihari Yamanoor

View More author details
Right arrow

Chapter 9: Let's Build a Robot!

In this chapter, we will have some fun building a robot using a Raspberry Pi Pico. We will discuss programming the Raspberry Pi Pico using MicroPython. We will also discuss building a robot using an off-the-shelf robotics kit. We will get started by reviewing the components of our robot by testing them; then, we will discuss building a line-following robot and an obstacle avoiding robot.

Figure 9.1 – Kitronik Autonomous Robotics Platform for Raspberry Pi Pico

This can be a fun weekend project that could be further enhanced to participate in robotic contests. We will give you some ideas toward the end of this chapter.

Figure 9.2 – Line-following robot using the Kitronik kit

The topics discussed in this chapter include the following:

  • Installing the prerequisites
  • Controlling the LEDs
  • Motor selection and control
  • Testing the sensors
  • Testing the robot

Technical requirements

The following hardware is recommended for this chapter:

The code samples for this chapter are available at https://github.com/PacktPublishing/Raspberry-Pi-Pico-DIY-Workshop/tree/main/chapter_09.

Code in Action videos for this chapter can be viewed at https://bit.ly/3kNCFkM.

In the next section, we will install the required library for our robot.

Installing the prerequisites

Before we get started, we need to install the Thonny IDE, along with installing MicroPython on the Raspberry Pi Pico.

MicroPython Installation

We recommend installing MicroPython on your Pico. If you are not familiar with the installation process, you can follow the installation process in Chapter 1, Getting Started with the Raspberry Pi Pico.

The required library for the Kitronik Robotics kit is available for download as a ZIP file from its repository at https://bit.ly/3K35aoM.

Once the contents of the files are extracted, we can install the library via the Thonny IDE as follows:

  1. In the Thonny IDE, go to the folder containing the extracted library files, as shown in the following screenshot, where you can navigate to the location of the files:

Figure 9.3 – Locating the extracted files in the Thonny IDE

  1. Now, right-click on PicoAutonomousRobotics.py, and upload it to the Pico, as shown in the...

Controlling the LEDs

In this section, we will get started with testing the RGB LEDs on the Kitronik Robotics kit. There are four RGB LEDs on the chassis (highlighted in Figure 9.6). The LEDs could be used to provide a visual indication of the robot's action. We will light them up in a circular pattern.

Figure 9.8 – The LED location on the robotics platform

The code sample discussed in this section is rgb_led_test.py:

  1. In the Thonny IDE, create a file called main.py and save it to your Pico, as shown in the following figure, where the dialog appears when you click on the Save button.

Figure 9.9 – Save main.py to Raspberry Pi Pico

  1. Now, let's discuss the code meant to drive the LEDs in a circular pattern. The first step is to import the requisite modules. We are going to need the KitronikPicoRobotBuggy class from the library we installed earlier, along with the time module:
    from PicoAutonomousRobotics...

Motor selection and control

In this section, we will discuss the various motor options available to build a robot. For a typical desktop robot, the various motor options include the following:

  • Direct Current (DC) motors
  • Stepper motors
  • Servo motors

Let's discuss the applications of these motors.

DC motors

DC motors are ideal for wheeled robots. The following figure shows the DC motors used to drive the wheels of our robot. DC motors are widely used in toys because of their low cost and their small size. The DC motors used in our robot come with an in-built gearbox to reduce the revolutions per minute (rpm) and make it suitable for a wheeled robot.

DC motors are typically operated using an H-bridge circuit. This enables the speed and direction of travel to be controlled.

Figure 9.11 – DC motors on the robot

Stepper motors

Stepper motors are ideal for positioning applications. For example, stepper motors can be...

Testing the sensors

In this section, we will test the ultrasonic sensor and the line-following sensor that come with the robotics kit. We will start by testing the ultrasonic sensor.

Ultrasonic sensor

Ultrasonic sensors are generally used in obstacle-avoidance applications. They use the time of flight principle to measure the distance between objects. The sensor transmits a sound signal at a known frequency in the ultrasonic spectrum. The sound signal bounces off the surface of obstacles back to the sensor. The time elapsed since the transmission of the signal is used to calculate the distance between the sensor and the object. The distance between objects is calculated as follows:

Speed = Distance / Time

Distance = (Speed of Sound x Time) / 2

We divide by two because the sound has to travel twice between the obstacle and the sensor.

You can learn about ultrasonic sensors at https://bit.ly/3xeqpS2.

The ultrasonic sensor can be installed on both the front and...

Testing the robot

Now that we have tested the components of our robot, it is time to take it for a spin. The first step is to install the wheels of the robot (as shown in the following figure):

Figure 9.23 – Installing wheels on the robot

The code samples for the line-following and obstacle-avoidance robots are available for download along with this chapter as line_following.py and obstacle_avoidance.py. We tested the line-following robot using a mat from Kitronik (https://bit.ly/3GAn0xM), as shown in the following figure:

Figure 9.24 – Line-following robot on a map

Robotics contests

Where do we go from here? If building robots interests you, we recommend checking out contests organized by your local robotics club. There are other types of robots, such as maze-solving robots and sumo wrestling robots, for example. Some clubs even organize robotic sumo wrestling competitions. Our favorite contest is a Micromouse contest...

Summary

In this chapter, we discussed building a robot using an off-the-shelf Kitronik Robotics kit. We discussed the individual components of the robot, including testing the LEDs, motor selection, and DC and servo motor control. We discussed drivers meant for driving the motors and interfacing the sensors. We also discussed testing the line-following robot and the obstacle-avoidance robot. This was followed by a review of how to take this project forward.

In the next chapter, we will discuss building TinyML applications on the Pico.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Raspberry Pi Pico DIY Workshop
Published in: May 2022Publisher: PacktISBN-13: 9781801814812
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

Authors (2)

author image
Sai Yamanoor

Sai Yamanoor is an embedded systems engineer working for a private startup school in the San Francisco Bay Area, where he builds devices that help students achieve their full potential. He completed his undergraduate work in mechatronics engineering from Sri Krishna College of Engineering and Technology, Coimbatore, India and his graduate studies in mechanical engineering at Carnegie Mellon University, Pittsburgh PA. His interests, deeply rooted in DIY and open software and hardware cultures, include developing gadgets and apps that improve the quality of life, Internet of Things, crowdfunding, education, and new technologies. In his spare time, he plays with various devices and architectures, such as the Raspberry Pi, Arduino, Galileo, Android devices and others. Sai has earlier published a book titled Raspberry Pi Mechatronics Projects.
Read more about Sai Yamanoor

author image
Srihari Yamanoor

Srihari Yamanoor is a mechanical engineer, working on medical devices, sustainability, and robotics in the San Francisco Bay Area. He completed his undergraduate studies in mechanical engineering from PSG College of Technology, Coimbatore, India and graduate studies in mechanical engineering at Stanford University. He is certified in SolidWorks, simulation, sustainable design, PDM as well as in quality and reliability engineering and auditing. His has a wide range of interests, from DIY, crowdfunding, AI, travelling, photography to gardening and ecology.
Read more about Srihari Yamanoor