yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
Pico Robot Car📖
0.Introduction
  • 0.1 Description of Pico Robot
  • 0.2 Interface Description
1.Assembly steps
  • Assembly video
2.Development environment construction
  • 2.1 Update firmware
  • 2.2 Build environment
  • 2.3 Download code Startup automatically
  • 2.4 Import library file
3.Basic course
  • 3.1 Control LED light
  • 3.2 Onboard temperature sensor
  • 3.3 Passive buzzer
  • 3.4 Breathing light
  • 3.5 Marquee light
  • 3.6 OLED display
  • 3.7 Battery power display
  • 3.8 Servo control
4.Advanced course
  • 4.1 Light sensor
  • 4.2 Sound sensor
  • 4.3 Tracking sensor
  • 4.4 Ultrasonic sensor
  • 4.5 IR control display
5.Robot course
  • 5.1 Car forward
  • 5.2 Car movement
  • 5.3 Singing dancing
  • 5.4 Car tracking
  • 5.5 Edge detection
  • 5.6 Ultrasonic avoiding
  • 5.7 Ultrasounic following
  • 5.8 Light follow
  • 5.9 Voice control car
  • 5.10 Infrared control
  • 5.11 Bluetooth control
6.Battery and charge
  • Battery and charge
Download
  • APP(iOS/Android)
  • Code
  • Instructions Manual
  • Appendix
Download ZIP

Welcome to Pico Robot Car repository

5.8 Light follow

5.8 Light Follow

In Section 4.1, we use the photosensitive sensor to detect the light intensity and display the result on the OLED. In this section, we use this light intensity result to achieve light tracking.

Note that the search light is affected by the ambient light, please use it in a place where the indoor light does not change much or is dark.

The motor speed is affected by the battery power. The routine is when the battery power is high (the power value is above 26000), if the battery power is low, it needs to be charged in time or the motor speed should be modified.

1. Learning Objectives

1. Learn the motor, OLED, photosensitive sensor, and programmable RGB lights of the Raspberry Pi Pico 2/Pico board and the car expansion board to realize the car light search.

2. Understand the realization of the car's light seeking.

2. the use of hardware

This course uses the motor, OLED, photosensitive sensor, and programmable RGB lights of the Pico 2/Pico board and the car expansion board. The car is placed in an open space without obstacles in front of it. Please connect the jumper cap to the Light pin header before running, have a light source ready (flashlight or your phone's camera light).

image-20220302165728937

image-20220302122626662

In the program, we read the values ​​of the two photosensitive sensors, compare the two values, and judge the position of the light source, thereby controlling the car to search for light.

3. program analysis

Complete program location: Pico Robot Supporting Materials -> Appendix -> Course code -> 3.Robot course -> 8. Lighting follow.py

x
import time
from machine import Pin, I2C, PWM, ADC
from pico_car import SSD1306_I2C, pico_car, ws2812b
​
Motor = pico_car()
Motor.Car_Stop()
num_leds = 8  # Number of NeoPixels
# Pin where NeoPixels are connected
pixels = ws2812b(num_leds, 0)
pixels.fill(0,0,0) 
pixels.show()
#Light1 -> GP27
#Light2 -> GP26
light1 = machine.ADC(27)
light2 = machine.ADC(26)
#initialization oled
i2c=I2C(1, scl=Pin(15),sda=Pin(14), freq=100000)
oled = SSD1306_I2C(128, 32, i2c)
light_down = 0.9
light_up = 1.1
​
while True:
    #get value
    LightS1 = light1.read_u16()
    LightS2 = light2.read_u16()
    print("light1 is %d"%(LightS1) )
    print("light2 is %d"%(LightS2) )
    #Display sound on OLED
    oled.text('Light1:', 0, 0)
    oled.text(str(LightS1), 60, 0)
    oled.text('Light2:', 0, 10)
    oled.text(str(LightS2), 60, 10)
    #Control action
    if LightS1 > (LightS2*light_down) and LightS1 < (LightS2*light_up):
        Motor.Car_Run(120,120)
        for i in range(num_leds):
            pixels.set_pixel(i,150,150,150)
        pixels.show()
    elif LightS2 > (LightS1*light_down) and LightS2 < (LightS1*light_up):
        Motor.Car_Run(120,120)
        for i in range(num_leds):
            pixels.set_pixel(i,150,150,150)
        pixels.show()
    elif LightS1 > (LightS2*light_up) or LightS2 < (LightS1*light_down):
        Motor.Car_Run(120,0)
        pixels.fill(0,0,0) 
        pixels.set_pixel(0,150,0,150)
        pixels.set_pixel(1,150,0,150)
        pixels.set_pixel(2,150,0,150)
        pixels.set_pixel(7,150,0,150)
        pixels.show()
    elif LightS2 > (LightS1*light_up) or LightS1 < (LightS2*light_down):
        Motor.Car_Run(0,120)
        pixels.fill(0,0,0) 
        pixels.set_pixel(3,150,0,150)
        pixels.set_pixel(4,150,0,150)
        pixels.set_pixel(5,150,0,150)
        pixels.set_pixel(6,150,0,150)
        pixels.show()
    else:
        Motor.Car_Stop()
    oled.show()
    oled.fill(0)
    time.sleep(0.01)
​

from pico_car import SSD1306_I2C, pico_car, ws2812b

Using pico_car's SSD1306_I2C, pico_car, ws2812b, encapsulates the motor driver, RGB lights, and OLED libraries.

import time

The "time" library. This library handles everything time related, from measuring it to inserting delays into programs. The unit is seconds.

from machine import Pin, I2C, ADC, PWM

The machine library contains all the instructions that MicroPython needs to communicate with Pico and other MicroPython-compatible devices, extending the language of physical computing, using the Pin, PWM, ADC and I2C libraries here.

Motor= pico_car()

Initialize the motor drive.

pixels = ws2812b(num_leds, 0)

Initialize RGB lights, we have 8 RGB lights, here num_leds is set to 8.

pixels.fill(0,0,0)

Set all lights to 0,0,0, that is, turn off all lights, the parameters are (red, green, blue), and the color brightness is 0-255.

pixels.show()

Display the set lights.

pixels.set_pixel(0,150,0,150)

Set the first headlight to purple.

i2c=I2C(1, scl=Pin(15),sda=Pin(14), freq=100000)

Set the IIC 1 pin to SCL 15, SDA 14, and the frequency to 100000.

oled = SSD1306_I2C (128, 32, i2c)

Initialize the size of the OLED to 128*32, and pass in the IIC parameters set earlier.

Motor.Car_Run(120,120)

To control the car to move forward, the speed is set to 120, the parameters are (left motor speed, right motor speed), and the speed range is 0-255.

Motor.Car_Stop()

Control the car to stop.

light1 = machine.ADC(27)

Initialize ADC port 27, a total of two photosensitive sensors, and set pins 27 and 26 respectively.

oled.text (str (LightS1), 60, 0)

Convert the photosensitive value into a string and display it at the 60,0 position of the OLED.

oled.show ()

Display the set OLED content.

oled.fill (0)

Clear the settings and prepare for the next display.

LightS1 = light1.read_u16()

The light1.read_u16() function is used to detect the value of the sound sensor and assign it to the variable LightS1.

4. Experimental phenomenon

After the program is downloaded, we can see that the first line of the OLED displays the value of photosensitive sensor 1, and the second line displays the value of photosensitive sensor 2. At the same time, the Shell will also print the value of the photosensitive sensor. Use the flashlight to move left and right in front of the car, you can Control the left and right movement of the car.

image-20220302172725024

Because the photosensitive sensor is easily affected by ambient light, please use it in a place where the indoor light does not change much or is dark. If the light-seeking effect is not good, you can adjust the detection range appropriately, that is, the variables light_down and light_up.

Ultrasounic following
Voice control car

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文