This tutorial is part of: SunFounder Rasberry Pi Pico Pico 4WD Smart Car Kit
Files related to this group will be displayed together. Links to other videos are below this article.
Course Lesson 8 of 10: Raspberry Pi Pico Smart Car – Lesson 8: Servo Motor and Object Following
This lesson from the Raspberry Pi Pico Smart Car course by Robojax explores the functionality of a servo motor and how to use it to create an object-following or "follow your hand" robot. The project goes beyond simple motor control, demonstrating how to combine a servo-driven ultrasonic sensor with intelligent Python logic to create a car that actively seeks out and follows an object within its range. This is a fundamental concept in robotics, enabling you to build interactive projects like a robot that follows a person, a smart pet that plays fetch, or an autonomous security camera that tracks movement. The lesson also covers a second, simpler project: a car that automatically approaches, pushes, and backs away from objects within a specific distance zone.
Hardware and Components
This project is based on the SunFounder Raspberry Pi Pico 4WD car kit. The key components used in this lesson are:
- Raspberry Pi Pico board
- 4WD Smart Car chassis with motors
- Servo motor (ideally a metal gear servo for durability)
- Ultrasonic distance sensor (HC-SR04)
- Motor driver board (part of the car kit)
- RGB LED (for visual feedback)
- Power source (battery pack for the car)
The servo motor is the core of this lesson's functionality. Unlike a standard DC motor that spins continuously, a servo motor is designed to rotate to and hold a specific angular position, typically between 0 and 180 degrees. This makes it perfect for precisely aiming the ultrasonic sensor. The quality of a servo is often determined by its gear material (plastic vs. metal) and its torque, which dictates its strength and durability.
How it works
The chart blow explins how the code works.
Wiring Guide
The wiring for this lesson is straightforward, as it builds upon the pre-assembled car kit. The critical connection is the servo motor, which must be connected to a specific GPIO pin on the Pico.
In the video, the instructor connects the servo to pin number 18 on the Raspberry Pi Pico. The ultrasonic sensor is mounted on top of the servo horn, allowing it to be rotated left and right to scan the area. The rest of the wiring for the motors, RGB LED, and power is assumed to be pre-connected from previous lessons in the course.
For a detailed visual guide, please refer to the wiring diagram below.
Code Explanation
The Python code for this lesson is built around a custom pico_4wd library, which simplifies complex hardware interactions. The user-configurable parts are located at the top of the script and control the car's behavior.
Here are the key variables you can adjust to customize your robot's "follow hand" behavior:
car.RADAR_REFERENCE = 25: This is the maximum distance in centimeters that the sensor will "see." Any object beyond this distance is ignored. You can increase this to make the car more sensitive or decrease it to require a closer object.car.RADAR_MAX_ANGLE = 45&car.RADAR_MIN_ANGLE = -45: These define the leftmost (45) and rightmost (-45) angles the servo will scan. This creates a 90-degree field of view in front of the car.car.RADAR_STEP_ANGLE = 10: This is the increment (in degrees) the servo uses to move between scan points. A smaller step (e.g., 5) gives a finer resolution but takes longer to complete a full scan. A larger step (e.g., 15) scans faster but with lower precision.MOTOR_FORWARD_POWER = 40: This sets the motor power (0-100) used when the car is moving forward toward the detected object. Adjust this to change the car's chasing speed.MOTOR_TURNING_POWER = 40: This sets the motor power used when the car is turning to face the object. A higher value will make the car turn more aggressively.
The main logic of the main() function is a continuous loop. It first calls car.radar_scan(), which returns a string of '1's and '0's representing whether an object was detected at each scan angle (a '1' means an object is within the reference distance). The code then analyzes this string to find the largest group of consecutive '1's, which indicates the center of the detected object. Based on where this center is in the scan field, the car is commanded to turn left, turn right, or move forward to align itself with the object and follow it.
Live Project Demonstration
The video provides a clear demonstration of the project in action. The instructor first runs a test script to show how the servo motor can be moved to precise angles like 90, 0, and -90 degrees. This is crucial for understanding the servo's range and for mounting the ultrasonic sensor correctly.
After mounting the sensor, the "follow hand" feature is showcased. As the instructor moves his hand in front of the car, the servo-mounted sensor scans left and right. The car's logic processes the sensor data and turns the wheels to steer toward the detected hand, effectively following it across the floor. The demonstration highlights how the car can track an object as it moves and adjusts its direction accordingly. A second project is also shown, where the car is programmed to approach and push an object if it is detected between 10 and 20 centimeters away, and to back off if the object gets too close (under 10 centimeters).
Key Takeaways
This lesson is an excellent introduction to using servo motors for creating interactive robotic behaviors. By combining a servo with a distance sensor, you can build a robot with a "sense of sight" that can actively track and respond to its environment. The ability to tune parameters like scan angles, step size, and motor power allows for fine-grained control over the robot's behavior, which is a critical skill for any robotics project.
Chapters
- [00:00] Introduction to Servo Motor and Object Following
- [01:48] Course Kit Features and Capabilities
- [03:08] Understanding Servo Motor Operation
- [06:08] Testing Servo Motor with Python Code
- [09:59] Explaining the "Follow Hand" Feature
- [10:49] Demonstrating the "Follow Hand" Functionality
- [15:37] Project: Object Pushing and Backing Off
- [18:40] Demonstration of Object Pushing Project
- [19:25] Conclusion and Next Steps
This tutorial is part of: SunFounder Rasberry Pi Pico Pico 4WD Smart Car Kit
- Course Lesson 1 of 10: Raspberry Pi Pico 4WD Smart Car Kit from SunFounder by Robojax
- Course Lesson 2 of 10: Basic Python to drive Raspberry Pi Pico 4WD Smart Car Kit
- Course Lesson 3 of 10: Assembling Raspberry Pi Pico 4WD Smart Car Kit from SunFounder by Robojax
- Course Lesson 4 of 10: Raspberry Pi Pico Expansion board for 4WD Smart Car Kit
- Course Lesson 5 of 10: Controlling RGB LED WS2812B Using Raspberry Pi Pico
- Course Lesson 6 of 10: Controlling DC Motors using Raspberry Pi Pico 4WD Smart Car Kit
- Lesson 7 of 10: Line Tracking, Cliff Detection Calibrate Greyscale Sensor Raspberry Pi Pico Car
- Course Lesson 9 of 10: Obstacle Avoidance using Raspberry Pi Pico 4WD Smart Car
- Course Lesson 10 of 10: Controlling Raspberry Pi Pico 4WD Smart Car Kit with mobile App
# Lesson 8/10 Pi Car Pico 4WD: Follow hand
# Download and resource page https://robojax.com/RJT630
import pico_4wd as car
car.RADAR_REFERENCE = 25
car.RADAR_MAX_ANGLE = 45
car.RADAR_MIN_ANGLE = -45
car.RADAR_STEP_ANGLE = 10
MOTOR_FORWARD_POWER = 40
MOTOR_TURNING_POWER = 40
def main():
while True:
radar_data = car.radar_scan()
if isinstance(radar_data, int):
continue
radar_data = [str(i) for i in radar_data]
radar_data = "".join(radar_data)
paths = radar_data.split("1")
length_list = []
for path in paths:
length_list.append(len(path))
if max(length_list) == 0:
car.move("stop")
else:
i = length_list.index(max(length_list))
pos = radar_data.index(paths[i])
pos += (len(paths[i]) - 1) / 2
delta = len(radar_data) / 3
if pos < delta:
car.move("right", MOTOR_TURNING_POWER)
elif pos > 2 * delta:
car.move("left", MOTOR_TURNING_POWER)
else:
#if radar_data[int(len(radar_data)/2-1)] == "0":
# car.move("stop")
#else:
car.move("forward", MOTOR_FORWARD_POWER)
try:
main()
finally:
car.move("stop")
car.set_light_off()
Common Course Links
- Purchase SunFounder Pico 4WD Smart Car Kit from AliExpress
- Purchase SunFounder Pico 4WD Smart Car Kit from eBay
- Amazon Canada: Purchase Pico 4WD Smart Car Kit by SunFounder
- Amazon France: Purchase Pico 4WD Smart Car Kit by SunFounder
- Amazon Germany: Purchase Pico 4WD Smart Car Kit by SunFounder
- Amazon Italy: Purchase Pico 4WD Smart Car Kit by SunFounder
- Amazon Japan: Purchase Pico 4WD Smart Car Kit by SunFounder
- Amazon Spain: Purchase Pico 4WD Smart Car Kit by SunFounder
- Amazon UK: Purchase Pico 4WD Smart Car Kit by SunFounder
- Amazon USA: Purchase Pico 4WD Smart Car Kit buy SunFounder
- Purchase Pico 4WD Smart Car Kit from SunFounder.com
- SunFounder Pico-4wd Car Kit Documentation
- Video Play List of 10 lesson on SunFounder Pico 4WD Smart Car Kit
Resources & references
No resources yet.
Files📁
No files available.