Search Code

Course Lesson 1 of 10: Raspberry Pi Pico 4WD Smart Car Kit from SunFounder by Robojax

This lesson is part of: Raspberry Pi Pico 4WD Smart Car

Course Lesson 1 of 10: Raspberry Pi Pico 4WD Smart Car Kit from SunFounder by Robojax

Introduction

Welcome to the first lesson of the Raspberry Pi Pico 4WD Smart Car Kit course from SunFounder, presented by Robojax. This course is designed to take you from unboxing a complete robotics kit to fully controlling a four-wheel-drive car using MicroPython. This isn't just about assembling a toy; it's a comprehensive educational journey that teaches you the fundamentals of microcontrollers, sensors, motor control, and wireless communication.

By the end of this course, you will be able to build and program a versatile smart car that can be used for a variety of projects. The skills you learn here are directly applicable to real-world robotics and automation tasks.

Here are just a few practical projects you could build with this kit:

  • Autonomous Obstacle Avoidance Robot: Program the car to navigate a room on its own, using its ultrasonic sensor to detect and avoid walls and furniture.
  • Line-Following Delivery Bot: Set up a track and have the car follow a line to simulate a mail or parts delivery system within a workshop or office.
  • Edge-Detecting Safety Robot: Use the cliff detection feature to create a robot that can safely patrol the edge of a table or counter without falling off.
  • Hand-Gesture Controlled Vehicle: Implement the "follow hand" feature to create a robot that responds to your movements without a remote, a great introduction to proximity-based control.
  • Remote-Controlled Surveillance Car: Use the mobile app to drive the car around your home while it streams sensor data, acting as a fun, mobile security camera platform.

What You Will Learn in This 10-Lesson Course

This course is structured to build your knowledge progressively. Each lesson focuses on a specific aspect of the kit, from basic setup to advanced features. Here is a breakdown of what you will achieve:

  • Lesson 1 (This Lesson): Introduction to the kit's components and features, and setting up your computer for the first time.
  • Lesson 2: Installing the necessary software and learning ten basic Python commands to prepare you for coding.
  • Lesson 3: A deep dive into the function of each key component and how to test them individually.
  • Lesson 4: A step-by-step guide to physically assembling the smart car kit.
  • Lesson 5: Your first real movement code, learning how to make the car drive forward, backward, and turn.
  • Lesson 6: Programming the "Don't Push Me" feature, which uses the cliff sensors to detect edges and back the car away.
  • Lesson 7: Implementing line tracking so the car can follow a pre-defined path.
  • Lesson 8: Creating a "follow hand" mode where the car tracks and follows your hand's movements.
  • Lesson 9: Combining all the features into a comprehensive obstacle avoidance program.
  • Lesson 10: Connecting the car to your mobile device via the app for full wireless control.

Hardware and Components

In this lesson, we open the box and examine the components that come with the SunFounder Raspberry Pi Pico 4WD Smart Car Kit. Understanding what each part does is crucial for the programming lessons ahead. Let's take a look at what's inside the package (in video at 04:32).

The kit includes:

  • Raspberry Pi Pico Microcontroller: The "brain" of the car. This board is based on the RP2040 chip and features a Micro-USB connector for programming and power. It has a push button for boot selection and numerous GPIO pins that you will use to connect to sensors and motors (in video at 08:01).
  • Main Control Board: A custom expansion board that connects to the Pico and provides easy hookups for the motors and sensors.
  • 4x DC Motors and 4x Wheels: These provide the four-wheel drive (4WD) capability, giving the car power and maneuverability.
  • HC-SR04 Ultrasonic Sensor: Used for obstacle avoidance. It measures distance by sending out ultrasonic waves and timing how long they take to bounce back.
  • 3-Channel Grayscale Line Sensor: This sensor is used for line tracking. It can differentiate between a dark line and a light surface, allowing the car to follow a path.
  • 2-Channel Photo Interrupter: This is part of the cliff detection system, used to sense when the car is about to drive off an edge.
  • 8-bit RGB Lights: Addressable LEDs that can be programmed to display a wide range of colors for visual feedback.
  • Servo Motor: Used to pan the ultrasonic sensor left and right to scan for obstacles.
  • 18650 Battery Case and Hardware: To power the car wirelessly.

Software Setup and Installation

Before we can start programming, we need to set up our development environment. This course uses Thonny, an Integrated Development Environment (IDE) that is perfect for beginners learning MicroPython. It is available for Windows, Mac, and Linux (in video at 07:03).

Installing Thonny

To get started, download the latest version of Thonny from the official website and follow the installation prompts. It's a straightforward process. Once installed, you can open it and select your preferred language. The course recommends using the "Raspberry Pi" mode, which you can select when you first open the program or change later in the settings (in video at 10:56).

Installing MicroPython on the Pico

The Raspberry Pi Pico needs to have the MicroPython firmware installed to understand Python code. Here’s how to do it in Thonny (in video at 14:42):

  1. Connect your Pico to your computer using a Micro-USB cable. To enter bootloader mode, hold down the BOOTSEL button on the Pico while plugging in the USB cable (in video at 13:10).
  2. In Thonny, click on the bottom-right corner and select "Install MicroPython...".
  3. Choose "Raspberry Pi Pico" as the target device and click "Install". Thonny will then flash the MicroPython firmware onto your board.

Uploading the Car's Library Files

The smart car kit relies on a set of pre-written library files that handle the low-level controls for the motors, sensors, and LEDs. You will need to download the course files from the link provided in the video description and extract the ZIP folder (in video at 12:13).

Once extracted, you'll find a folder containing the necessary library files. To upload these to your Pico:

  1. In Thonny, navigate to the extracted folder and select the three library files (e.g., Pico4WD.py, rdp.py, and ws.py).
  2. Right-click on the selected files and choose "Upload to /". This will copy them to the root directory of your Raspberry Pi Pico (in video at 16:37).

Code Explanation

To verify that everything is working correctly, we write a simple "blink" test. This is the "Hello World" of hardware programming. While the transcript provides a live example, you will write this code yourself in Thonny. This test confirms that the Pico is communicating with your computer and that you can control its output pins.

The core of the test code is shown below. This is a user-configurable snippet where you can change the behavior of the LED:

from machine import Pin, Timer

# User-configurable: The pin number for the onboard LED
led_pin = 25

# User-configurable: The frequency of blinking in Hertz (times per second)
blink_frequency = 2.5

led = Pin(led_pin, Pin.OUT)
timer = Timer()

def blink(timer):
    led.toggle()

timer.init(freq=blink_frequency, mode=Timer.PERIODIC, callback=blink)

Here's how you can customize this code:

  • led_pin: This variable defines which pin your LED is connected to. On the Raspberry Pi Pico, the onboard LED is typically connected to pin 25. You can change this number if you want to connect an external LED to a different GPIO pin.
  • blink_frequency: This is the speed at which the LED blinks. A value of 2.5 means the LED will turn on and off 2.5 times per second. You can experiment with different values:
    • Set it to 1 for a slow, visible blink.
    • Set it to 10 for a fast blink.
    • Set it to 50 or 60 and the blinking becomes so fast that it appears to be constantly on to the human eye (in video at 19:49).

To run this code, simply click the "Run" button (green play icon) in Thonny. You should see the onboard LED on your Pico start to blink. To stop it, click the "Stop" button (red square icon).

Live Project Demonstration

In the video, instructor Ahmad Shamshiri demonstrates the full process of setting up the software and running the blink test. He shows how to install Thonny, flash the MicroPython firmware, and upload the necessary library files. He then writes the blink code and experiments with different frequencies, showing how the perceived behavior of the LED changes from a distinct blink to a solid light as the frequency increases (in video at 15:17).

This successful test is a critical milestone. It confirms that your Raspberry Pi Pico is functioning correctly, the MicroPython firmware is installed, and you are ready to move on to the more complex lessons involving motor control and sensors.

Chapters

  • [00:00] Course Introduction and Overview
  • [01:13] Course Syllabus and Features
  • [04:32] Unboxing the Smart Car Kit
  • [07:03] Downloading and Installing Thonny IDE
  • [08:01] Introduction to the Raspberry Pi Pico
  • [12:13] Downloading and Extracting Course Files
  • [14:42] Installing MicroPython on the Pico
  • [15:17] Uploading Library Files to the Pico
  • [17:30] Writing and Testing the Blink Code
  • [19:49] Experimenting with Different Blink Frequencies
No code attached.

Resources & references

No resources yet.

Files📁

No files available.