5.11 Bluetooth Control

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 the car via APP by Bluetooth.

We will integrate the previously learned tracking, ultrasonic obstacle avoidance, voice control, OLED control, buzzer control, programmable RGB lights, etc. All can be controlled via Bluetooth.

2. About Hardware

We need use all components on Pico robot.

Note: Please put the cart on the ground. It is normal for sound sensor and battery level to fluctuate when running motors, programmable RGB lights. When running the line inspection mode, it must be performed in an indoor environment without sunlight to reduce the interference of sunlight on the line inspection sensor, and the indoor light must be sufficient when the line inspection is performed, otherwise the program needs to be modified according to the method in section 5.4. When running the ultrasonic obstacle avoidance mode, you need to ensure that the obstacles are not too dense. If the obstacles are too dense, you can modify the program according to the method in Section 5.6. When running the voice control mode, it needs to run in a quieter environment. OLED can display up to 16 characters in one line, and only supports numbers and English display. Please make sure the battery power (the power value is above 26000) before use, otherwise it may run abnormally.

About tracking function:

Note: The tracking sensor will be affected by light, please run the program in an indoor environment without sunlight to reduce the interference of sunlight on the tracking sensor. And keep the indoor light enough when tacking.

About avoid function:

Note: The car can be placed on the ground, and the obstacles on the ground should not be too dense.

3. Download code and Bluetooth connection

3.1 Follow the assembly steps or our video to assemble the car, and connect the Bluetooth module and the OLED module, insert the jumper cap to the Voice pin header. As shown below.

image-20220302110753097

3.2 According to [Development Environment Setup]---[Import library file], import the library file of the robot into the Raspberry Pi Pico board.

3.3 Download the corresponding code to the Raspberry Pi Pico board.

Note: If you want the robot to automatically start the Bluetooth remote control function, please refer to [Development Environment Construction] --- [Download code/Startup automatically] to set the Bluetooth remote control code to start automatically when robot is turned on.

(Note: The Bluetooth control file name should be set to main.py)

image-20220305102551352

3.4 Unplug the Micro USB cable on the robot and restart the robot. At this point, we can see that the red light of the Bluetooth module is flashing.

image-20220305102645010

3.5 Android users search "YahboomRobot" inPlay Store to download APP. iOS users search "YahboomRobot”in AppStore to download APP.

3.6 After downloading the Bluetooth remote control APP, we need to install it.

During installation, if the phone prompts you to obtain location permission, you need to click "ALLOW" to obtain location permission.

3.7 Open the mobile phone Bluetooth and YahboomRobot APP, select the [smart car] --[PICO ROBOT], enter the Bluetooth connection interface, the Bluetooth will automatically connect when the phone is close to the robot.

If it didn’ t connect automatically, we need to click [Search for Bluetooth], after successful connection, The APP will jump to the control interface.

image-20220321111455748

image-20220321111519188

4.8 After the connection is successful, the red light of the Bluetooth module will keep on, and the APP interface will prompt “Bluetooth connect successful!” .

We can control the robot through APP. If the Bluetooth is disconnected, the red light will keep flashing again.

image-20220321105523443

image-20220302110924791

App function introduction:

1. Basic functions

image-20220321105606523

2. Music options

image-20220321105653793

3. Lamp options

image-20220321105717363

4. Lamp efficiency

image-20220321105735929

5. Mode selection

image-20220321105749922

5. About code

Code path: Code -> 3.Robotics course -> 11.Bluetooth control.py

In this program, the procedures of the previous courses are integrated, for example, the breathing light part is the procedure of Section 3.4, and the ultrasonic obstacle avoidance part is the procedure of Section 5.6. In this program, the timer interrupt Timer is also used to regularly report ultrasonic, sound, and battery data to the APP for display.

from pico_car import SSD1306_I2C, pico_car, ws2812b, ultrasonic

Using pico_car's SSD1306_I2C, ultrasonic, pico_car, ws2812b, encapsulates motor driver and RGB lights, OLED, and ultrasonic 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, PWM, Timer, UART, ADC

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, UART, ADC and I2C libraries here.

Motor = pico_car()

Initialize the motor drive.

Motor.Car_Run(255,255)

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

Motor.Car_Stop()

Control the car to stop.

Motor.Car_Back(255,255)

Control the car to back up.

Motor.Car_Run(0,255)

Control the car to turn left.

Motor.Car_Run(255,0)

Control the car to turn right.

Motor.Car_Left(255,255)

Control the car to turn left.

Motor.Car_Right(255,255)

Control the car to rotate right.

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.set_pixel(1,150,0,150)

Set the first light to purple.

pixels.show()

Display the set lights.

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.

ultrasonic = ultrasonic()

Initialize ultrasonic ranging.

distance = ultrasonic.Distance_accurate()

Assign the value returned by ultrasonic ranging to the variable distance .

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 ('Run', 0, 0)

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

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 = 0.5,mode = Timer.PERIODIC,callback = tick)

Set the timer interrupt function and frequency.

machine.ADC(28)

Initialize ADC port 28.

Sound.read_u16()/65535*200

Read the sound sensor value and convert it into a range of 0-100.

Quantity_of_electricity.read_u16()/65535*240

Read the power value and convert it into a range of 0-100.

uart = UART(0, 9600, bits=8, parity=None, stop=1, tx=Pin(16), rx=Pin(17))

Initialize Bluetooth serial port UART 0, pins TX 16, RX 17, baud rate 9600, data bits 8, stop bit 1, no parity bit.

uart.write('$DAT')

Send $DAT to APP via Bluetooth.

uart.any()

Read serial port data and return data when any data is received.

dat = uart.read(2)

Read two data and assign them to the variable dat, and make different actions by judging the value of this variable.

Tracing_1 = machine.Pin(2, machine.Pin.IN)

Initialize pin 2 as the pin of line tracking sensor 1 and set it as input.

Tracing_1.value()

The Tracing_1.value() function is used to detect the level of the corresponding port.

stroled_1 = stroled_1.replace("b'", "")

Replace the b' string in the variable stroled_1 with nothing, that is, remove the b' string.

6. Communication protocol

After the program download is complete, run the car according to the following protocol.

Trolley movementAgreement content
ForwardA#
BackB#
Turn LeftC#
Turn RightD#
Left-handedE#
Right RotationF#
stop0#
Music PlayAgreement Content
do1#
ri2#
mi3#
fa4#
his5#
the6#
xi7#
NoneO#
DO HIGH8#
do#W#1
ri#W#2
fa#W#3
su#W#4
la#W#5
Colorful lightsAgreement content
RedG#
GreenH#
BlueI#
YellowJ#
GreenK#
MagentaL#
CloseM#
Colorful lighting effectAgreement content
Water LampN#
MarqueeP#
Breathing LightQ#
CloseM#
Mode selectionProtocol content
Line Follower ModeS#
Ultrasonic Obstacle AvoidanceT#
Voice Control CarU#
Exit modeV#
OLED display (no more than 16 characters per line of data)Protocol content
first rowX#data
Second rowY#data
Third rowZ#data
Proactively report dataProtocol content
$DAT120,99,98#Ultrasonic distance: 120
 Sound value: 99
 Power: 98