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.10 Infrared control

5.10 Infrared Control

Note: The infrared receiver and remote controller will be affected by light, please run this code in an indoor environment without sunlight to reduce the interference of sunlight on the infrared sensor.

Note: Motor speed is affected by battery power. For this course, when the battery power is high (the power value is above 26000), if the battery power is not enough, we need to charge battery in time or modify the motor speed in the code.

1. Learning Objectives

In this course, we will learn how to control robot car by infrared remote controller.

2. About Hardware

We need use the motor, OLED, infrared receiver, RGB light, and buzzer on Pico robot. And we also need use the infrared remote controller.

image-20220302165728937

image-20220321093338725

The code value of the infrared remote control included in the car kit is shown in the table below.

KeysShell prints key values ​​
Up1
Left4
Right6
Down9
Sound5
116
217
318
420
521
622
724
825
926

3. About Code

Code path: Code -> 3.Robotics course -> 10.Infrared control.py

​x
import time
from machine import Pin, I2C, PWM, Timer
from pico_car import SSD1306_I2C, ir, 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)
# Set all led off
pixels.fill(0,0,0)
pixels.show()
# set buzzer pin
BZ = PWM(Pin(22))
BZ.freq(1000)
#initialization ir
Ir = ir()
#initialization oled
i2c=I2C(1, scl=Pin(15),sda=Pin(14), freq=100000)
oled = SSD1306_I2C(128, 32, i2c)
#define Timer
tim = Timer()
times_ = 0
def tick(timer):
    global times_
    times_ = times_ + 1
    if times_ > 100:
        times_ = 0
#set timer frequency 20
tim.init(freq = 20,mode = Timer.PERIODIC,callback = tick)

while True:
    #get value
    value = Ir.Getir()
    time.sleep(0.01)
    if value != None:
        print(value)
        #display press
        if value == 1:
            i = 0
            while value == 1:
                value = Ir.Getir()
                Motor.Car_Run(255,255)
                if times_ > 1:
                    times_ = 0
                    if i == 0:
                        pixels.set_pixel(2,150,0,150)
                        pixels.set_pixel(3,150,0,150)
                        i = 1
                    elif i == 1:
                        pixels.set_pixel(2,0,0,0)
                        pixels.set_pixel(3,0,0,0)
                        pixels.set_pixel(1,150,0,150)
                        pixels.set_pixel(4,150,0,150)
                        i = 2
                    elif i == 2:
                        pixels.set_pixel(1,0,0,0)
                        pixels.set_pixel(4,0,0,0)
                        pixels.set_pixel(0,150,0,150)
                        pixels.set_pixel(5,150,0,150)
                        i = 3
                    elif i == 3:
                        pixels.set_pixel(0,0,0,0)
                        pixels.set_pixel(5,0,0,0)
                        pixels.set_pixel(6,150,0,150)
                        pixels.set_pixel(7,150,0,150)
                        i = 4
                    elif i == 4:
                        pixels.set_pixel(0,0,0,0)
                        pixels.set_pixel(5,0,0,0)
                        pixels.set_pixel(6,150,0,150)
                        pixels.set_pixel(7,150,0,150)
                        i = 5
                    elif i == 5:
                        pixels.set_pixel(6,0,0,0)
                        pixels.set_pixel(7,0,0,0)
                        i = 0
                    pixels.show()
            Motor.Car_Stop()
            oled.text('Run', 0, 0)
            oled.show()
            oled.fill(0)
        elif value == 4:
            i = 0
            while value == 4:
                value = Ir.Getir()
                Motor.Car_Left(130,130)
                if times_ > 1:
                    times_ = 0
                    if i == 0:
                        pixels.set_pixel(7,0,0,0)
                        pixels.set_pixel(0,150,0,150)
                        i = i + 1
                    else:
                        pixels.set_pixel(i-1,0,0,0)
                        pixels.set_pixel(i,150,0,150)
                        i = i + 1
                        if i == 8:
                            i = 0
                    pixels.show()
            Motor.Car_Stop()
            oled.text('Left', 0, 0)
            oled.show()
            oled.fill(0)
        elif value == 6:
            i = 8
            while value == 6:
                value = Ir.Getir()
                Motor.Car_Right(130,130)
                if times_ > 1:
                    times_ = 0
                    if i == 8:
                        pixels.set_pixel(7,150,0,150)
                        pixels.set_pixel(0,0,0,0)
                        i = i - 1
                    else:
                        pixels.set_pixel(i-1,150,0,150)
                        pixels.set_pixel(i,0,0,0)
                        i = i - 1
                        if i == 0:
                            i = 8
                    pixels.show()
            Motor.Car_Stop()
            oled.text('Right', 0, 0)
            oled.show()
            oled.fill(0)
        elif value == 5:
            while value == 5:
                value = Ir.Getir()
                BZ.duty_u16(500)
                BZ.freq(624)
            BZ.duty_u16(0)
            oled.text('Buzzer', 0, 0)
            oled.show()
            oled.fill(0)
        elif value == 9:
            i = 0
            while value == 9:
                value = Ir.Getir()
                Motor.Car_Back(255,255)
                if times_ > 1:
                    times_ = 0
                    if i == 0:
                        pixels.set_pixel(6,150,0,150)
                        pixels.set_pixel(7,150,0,150)
                        i = 1
                    elif i == 1:
                        pixels.set_pixel(6,0,0,0)
                        pixels.set_pixel(7,0,0,0)
                        pixels.set_pixel(0,150,0,150)
                        pixels.set_pixel(5,150,0,150)
                        i = 2
                    elif i == 2:
                        pixels.set_pixel(0,0,0,0)
                        pixels.set_pixel(5,0,0,0)
                        pixels.set_pixel(1,150,0,150)
                        pixels.set_pixel(4,150,0,150)
                        i = 3
                    elif i == 3:
                        pixels.set_pixel(1,0,0,0)
                        pixels.set_pixel(4,0,0,0)
                        pixels.set_pixel(2,150,0,150)
                        pixels.set_pixel(3,150,0,150)
                        i = 4
                    elif i == 4:
                        pixels.set_pixel(1,0,0,0)
                        pixels.set_pixel(4,0,0,0)
                        pixels.set_pixel(2,150,0,150)
                        pixels.set_pixel(3,150,0,150)
                        i = 5
                    elif i == 5:
                        pixels.set_pixel(2,0,0,0)
                        pixels.set_pixel(3,0,0,0)
                        i = 0
                    pixels.show()
            Motor.Car_Stop()
            oled.text('Back', 0, 0)
            oled.show()
            oled.fill(0)
        elif value == 16:
            while value == 16:
                value = Ir.Getir()
            for i in range(num_leds):
                pixels.set_pixel(i,255,0,0)
            pixels.show()
            oled.text('Red', 0, 0)
            oled.show()
            oled.fill(0)
        elif value == 17:
            while value == 17:
                value = Ir.Getir()
            for i in range(num_leds):
                pixels.set_pixel(i,0,255,0)
            pixels.show()
            oled.text('Green', 0, 0)
            oled.show()
            oled.fill(0)
        elif value == 18:
            while value == 18:
                value = Ir.Getir()
            for i in range(num_leds):
                pixels.set_pixel(i,0,0,255)
            pixels.show()
            oled.text('Blue', 0, 0)
            oled.show()
            oled.fill(0)
        elif value == 20:
            while value == 20:
                value = Ir.Getir()
            for i in range(num_leds):
                pixels.set_pixel(i,255,255,0)
            pixels.show()
            oled.text('Yellow', 0, 0)
            oled.show()
            oled.fill(0)
        elif value == 21:
            while value == 21:
                value = Ir.Getir()
            for i in range(num_leds):
                pixels.set_pixel(i,0,255,255)
            pixels.show()
            oled.text('Cyan', 0, 0)
            oled.show()
            oled.fill(0)
        elif value == 22:
            while value == 22:
                value = Ir.Getir()
            for i in range(num_leds):
                pixels.set_pixel(i,255,0,255)
            pixels.show()
            oled.text('Purple', 0, 0)
            oled.show()
            oled.fill(0)
        elif value == 24:
            while value == 24:
                value = Ir.Getir()
            for i in range(num_leds):
                pixels.set_pixel(i,255,255,255)
            pixels.show()
            oled.text('White', 0, 0)
            oled.show()
            oled.fill(0)
        elif value == 25:
            while value == 25:
                value = Ir.Getir()
            for i in range(num_leds):
                pixels.set_pixel(i,100,100,100)
            pixels.show()
            oled.text('White', 0, 0)
            oled.show()
            oled.fill(0)
        elif value == 26:
            while value == 26:
                value = Ir.Getir()
            for i in range(num_leds):
                pixels.set_pixel(i,0,0,0)
            pixels.show()
            oled.text('Black', 0, 0)
            oled.show()
            oled.fill(0)
        value = None

In the program of 5.7 Ultrasonic Follow, we introduced how to realize the simultaneous control of buzzer, RGB light, and car motion. In this program, we also did similar processing, and also added a timer interrupt Timer to realize RGB light. Toggle speed control.

We use while value == key value: to achieve the effect of pressing and releasing the remote control and then executing.

from pico_car import SSD1306_I2C, ir, pico_car, ws2812b

Using pico_car's SSD1306_I2C, ir, pico_car, ws2812b, it encapsulates the motor driver and RGB lamp, OLED, infrared remote control library.

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, PWM, Timer

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, Timer 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(2,150,0,150)

Set the third light 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.

oled.show ()

Display the set OLED content.

oled.fill (0)

Clear the settings and prepare for the next display.

oled.text ('Green', 0, 0)

Display 'Green' at the 0,0 position of the OLED.

Motor.Car_Run(255,255)

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

Motor.Car_Back(255,255)

Control the car to back up.

Motor.Car_Left(130,130)

Control the car to turn left.

Motor.Car_Right(130,130)

Control the car to rotate right.

Motor.Car_Stop()

Control the car to stop.

BZ = PWM(Pin(22))

Set IO22 as a PWM output pin to control the buzzer.

BZ.freq(1000)

Set the PWM frequency to 1000.

BZ.duty_u16(0)

When the value is 0, the sound is turned off, and when the value is 500, the sound is turned on.

tim = Timer ()

Initialize timer interrupt.

tick(timer)

The timing interrupt function uses the variable times_ for time control in the function to control the switching speed of RGB, etc.

tim.init(freq = 1000,mode = Timer.PERIODIC,callback = tick)

Set the timer interrupt function and frequency.

Ir = ir ()

Initialize the infrared remote control.

value = Ir.Getir ()

Read the infrared remote control value and assign it to the variable value.

4. Experimental Phenomenon

image-20220321095747956

After the code is downloaded, we can control robot car by infrared remote controller.

KeysShell prints key values ​​OLED displayEffects
Up1RunCar forward, and the bottom is forward with flowing light effect
Left4LeftCar rotates to left, and the bottom is counterclockwise with flowing light effect
Right6RightCar rotates to right, and the bottom is clockwise with flowing light effect
Down9BackCar back
Sound5BuzzerBuzzer make a sound
116RedRGB lights red
217GreenRGB lights green
318BlueRGB lights blue
420YellowRGB lights yellow
521CyanRGB lights cyan
622PurpleRGB lights purple
724WhiteRGB light white
825WhiteRGB light become white with lower brightness
926BlackRGB light off

 

Voice control car
Bluetooth control

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/中文