RGB Lights

RGB LightsIntroductionQuick StartSimulated breathing lightWS2812 Driverrepresents binary 0Additional knowledge

 

Introduction

In this section, we will learn how to use code to control the RGB light on the YAHBOOM K230

image-20250401111815127

Quick Start

We open CanMV IDE and connect K230 to the computer.

Press Ctrl + N to create a new code and delete all automatically generated code content

image-20250401112115029

Copy the following code and paste it into the IDE [Source Code/02.Basic/02.1_rgb.py]

Simulated breathing light

We can also combine the time module and the math module to simulate a better-looking breathing light effect.

The code is as follows [Source Code/02.Basic/02.2_better_rgb.py]

WS2812 Driver

This part is relatively complex and requires some basic knowledge. Skipping this part will not affect the subsequent use of K230.

  1. Basic hardware setup :
  1. Color setting principle :
  1. Data transmission principle (show function) : The communication protocol of WS2812B LED is quite special:

represents binary 0

timing_buf[pos] = 0b10000000 # 100xxxxx

Internal Process:

  1. In set_led, data is stored as: [0, 255, 0]
  2. show()The function converts this data into a time series signal
  3. Send to LED via SPI interface
  4. The LED displays the corresponding color after receiving the signal

Because the WS2812B LED uses a special single-wire communication protocol to receive data, the functions in YbRGB show()actually convert ordinary RGB values into timing signals that the LED can understand.

Additional knowledge

The RGB of the RGB lamp is different from the RGB displayed on the computer screen?

This is because there is a fundamental difference between the display principles of WS2812B RGB LED and computer monitors:

How the display works:

Working principle of WS2812B LED: