5.4 QR Code Control Light
5.4.1 Overview
In this project, we control the car light by QR codes. Herein, the AI vision module will recognize QR codes and read their content(color information) to light up the WS2812 in corresponding colors, including red, green, blue, black(off), and white. If you want to generate a brand-new QR code, please refer to the tutorial in 4.8 QR Code Recognition.
5.4.2 Code Flow

5.4.3 Test Code
from machine import I2C,UART,Pin
from Sengo1 import *
import time
from neopixel import myNeopixel
# Wait for Sengo1 to initialize the operating system. This waiting time cannot be removed to prevent the situation where the controller has already developed and sent instructions before Sengo1 has been fully initialized
time.sleep(3)
# Select UART or I2C communication mode. Sengo1 is I2C mode by default. You can change it by just pressing the mode button.
# 4 UART communication modes: UART9600(Standard Protocol Instruction); UART57600(Standard Protocol Instruction), UART115200(Standard Protocol Instruction); Simple9600(Simple Protocol Instruction)
#########################################################################################################
# port = UART(2,rx=Pin(16),tx=Pin(17),baudrate=9600)
port = I2C(0,scl=Pin(21),sda=Pin(20),freq=400000)
# Sengo1 communication address: 0x60. If multiple devices are connected to the I2C bus, please avoid address conflicts.
sengo1 = Sengo1(0x60)
err = sengo1.begin(port)
print("sengo1.begin: 0x%x"% err)
# Activate the QR code recognition algorithm
err = sengo1.VisionBegin(sengo1_vision_e.kVisionQrCode)
print("sengo1.VisionBegin(sengo1_vision_e.kVisionQrCode):0x%x"% err)
# Define the number of pin and LEDs connected to neopixel.
NUM_LEDS = 4
np = myNeopixel(NUM_LEDS, 13)
np.brightness(150) #brightness: 0 ~ 255
lastDetectionTime = 0
while True:
# Sengo1 does not actively return the detection and recognition results; it requires the main control board to send instructions for reading.
# The reading process: 1.read the number of recognition results. 2.After receiving the instruction, Sengo1 will refresh the result data. 3.If the number of results is not zero, the board will then send instructions to read the relevant information. (Please be sure to build the program according to this process.)
obj_num = sengo1.GetValue(sengo1_vision_e.kVisionQrCode, sentry_obj_info_e.kStatus)
# Sengo1 can only recognize and decode a QR code generated by no more than 10 characters. So when the returned result is not 0, it is only necessary to obtain and process the relevant data of the first result
# Get the running time
currentMillis = time.ticks_ms()
if obj_num:
lastDetectionTime = currentMillis
QRcodeStr = sengo1.GetQrCodeString()
if QRcodeStr == "Red":
np.fill(255,0,0)
np.show()
elif QRcodeStr == "Green":
np.fill(0,255,0)
np.show()
elif QRcodeStr == "Blue":
np.fill(0,0,255)
np.show()
elif QRcodeStr == "Black":
np.fill(0,0,0)
np.show()
elif QRcodeStr == "White":
np.fill(255,255,255)
np.show()
# If the QR code is not detected within 5 seconds, turn off the WS2812
if currentMillis - lastDetectionTime >= 5000:
lastDetectionTime = currentMillis
np.fill(0,0,0)
np.show()
5.4.4 Test Result
After uploading the code, the AI vision module will detect the captured image to determine if there is a QR code. If there is, the content of the QR code will be assigned to a variable, and the module will check whether it is the corresponding content. Then, the WS2812 will turn on in the color given by the QR code, including “Red”, “Green”, “Blue” and “White”, and it will turn off if QR code says “Black”. If the QR code is not detected within 5 seconds, the WS2812 will also turn off.