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.


The code value of the infrared remote control included in the car kit is shown in the table below.
| Keys | Shell prints key values |
|---|---|
| Up | 1 |
| Left | 4 |
| Right | 6 |
| Down | 9 |
| Sound | 5 |
| 1 | 16 |
| 2 | 17 |
| 3 | 18 |
| 4 | 20 |
| 5 | 21 |
| 6 | 22 |
| 7 | 24 |
| 8 | 25 |
| 9 | 26 |
3. About Code
Code path: Code -> 3.Robotics course -> 10.Infrared control.py
ximport timefrom machine import Pin, I2C, PWM, Timerfrom 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 connectedpixels = ws2812b(num_leds, 0)# Set all led offpixels.fill(0,0,0)pixels.show()# set buzzer pinBZ = PWM(Pin(22))BZ.freq(1000)#initialization irIr = ir()#initialization oledi2c=I2C(1, scl=Pin(15),sda=Pin(14), freq=100000)oled = SSD1306_I2C(128, 32, i2c)#define Timertim = Timer()times_ = 0def tick(timer): global times_ times_ = times_ + 1 if times_ > 100: times_ = 0#set timer frequency 20tim.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

After the code is downloaded, we can control robot car by infrared remote controller.
| Keys | Shell prints key values | OLED display | Effects |
|---|---|---|---|
| Up | 1 | Run | Car forward, and the bottom is forward with flowing light effect |
| Left | 4 | Left | Car rotates to left, and the bottom is counterclockwise with flowing light effect |
| Right | 6 | Right | Car rotates to right, and the bottom is clockwise with flowing light effect |
| Down | 9 | Back | Car back |
| Sound | 5 | Buzzer | Buzzer make a sound |
| 1 | 16 | Red | RGB lights red |
| 2 | 17 | Green | RGB lights green |
| 3 | 18 | Blue | RGB lights blue |
| 4 | 20 | Yellow | RGB lights yellow |
| 5 | 21 | Cyan | RGB lights cyan |
| 6 | 22 | Purple | RGB lights purple |
| 7 | 24 | White | RGB light white |
| 8 | 25 | White | RGB light become white with lower brightness |
| 9 | 26 | Black | RGB light off |