[Diao Ye learns programming] MicroPython hands-on (13) - RGB three-color lights on the control board

Knowledge points: What is a control board?
The control board is an open source intelligent hardware that popularizes STEAM maker education, artificial intelligence education, and robot programming education. It integrates ESP-32 high-performance dual-core chip, supports WiFi and Bluetooth dual-mode communication, and can be used as an IoT node to realize IoT applications. At the same time, the control board integrates OLED display, RGB lights, accelerometers, microphones, light sensors, buzzers, key switches, touch switches, and gold finger external expansion interfaces. It supports graphics and MicroPython code programming, which can realize intelligent robots, Smart control applications such as Maker Smart Works.

insert image description here
insert image description here
insert image description here

Control board hardware features:
ESP-32 main control
Processor: Tensilica LX6 dual-core processor (one core handles high-speed connection; one core independent application development)
main frequency: up to 240MHz clock frequency
SRAM: 520KB
Flash: 8MB
Wi-Fi standard: FCC/CE/TELEC/KCC
Wi-Fi protocol: 802.11 b/g/n/d/e/i/k/r (802.11n, speed up to 150 Mbps), A-MPDU and A-MSDU aggregation, support 0.4us Protection interval
Frequency range: 2.4~2.5 GHz
Bluetooth protocol: Compliant with Bluetooth v4.2 BR/EDR and BLE standards
Bluetooth audio: CVSD and SBC audio Low power consumption: 10uA
Power supply: Micro USB Power supply
Operating voltage: 3.3V
Maximum operating current: 200mA
maximum load current: 1000mA
Onboard
three-axis accelerometer MSA300, measuring range: ±2/4/8/16G
geomagnetic sensor MMC5983MA, measuring range: ±8 Gauss; accuracy 0.4mGz, electronic compass error ±0.5°
Light sensor
Microphone
3 full-color ws2812 lamp beads
1.3-inch OLED display, support 16*16 character display, resolution 128x64
passive buzzer
, support 2 physical buttons (A/B), 6 touch buttons,
support 1 alligator clip interface, Easy access to various resistive sensors
Expansion interface
20-channel digital I/O, (including 12-channel PWM, 6-channel touch input)
5-channel 12bit analog input ADC, P0~P4
1-channel external input alligator clip interface: EXT/GND
supports I2C, UART, SPI communication protocol

insert image description here
insert image description here
1. The onboard RGB-LED light
WS2812 is a low-power RGB three-color light integrated with a current control chip. R stands for red, G stands for green, and B stands for blue. It can display 256 levels of brightness and complete 16777216 kinds Full true color display of the color, using a special single-line communication method to control the color of the RGB light.

There are three RGB-LED lights that control the onboard WS2812 chip, and the light numbers are 0, 1, and 2 from left to right.

insert image description here
insert image description here

Numerical color matching in graphics programming

insert image description here

2. WS2812
is an intelligent external control LED light source integrating control circuit and light emitting circuit. Its appearance is the same as a 5050LED lamp bead, and each element is a pixel. The pixel contains an intelligent digital interface data latch signal shaping and amplifying drive circuit, and also includes a high-precision internal oscillator and a 12V high-voltage programmable constant current control part, which effectively ensures that the color of the pixel light is highly consistent. The data protocol adopts the single-line return-to-zero code communication method. After the pixel is powered on and reset, the DIN terminal receives the data transmitted from the controller. The 24bit data sent first is extracted by the first pixel and sent to the inside of the pixel. The remaining data is reshaped and amplified by the internal shaping processing circuit, and then forwarded and output to the next cascaded pixel through the DO port. Every time a pixel is transmitted, the signal is reduced by 24 bits. Pixels adopt automatic shaping and forwarding technology, so that the number of cascaded pixels is not limited by signal transmission, but only limited by signal transmission speed requirements.

Application fields
It has the advantages of low voltage drive, environmental protection and energy saving, high brightness, large scattering angle, good consistency, ultra-low power, and ultra-long life. The control circuit is integrated on the LED, the circuit becomes simpler, the volume is smaller, and the installation is easier. Main application areas, LED full-color luminous character light strings, LED full-color modules, LED full-color flexible light strips and hard light strips, LED guardrail tubes. LED point light source, LED pixel screen, LED special-shaped screen, various electronic products, electrical equipment marquee, etc.

insert image description here
Main features of WS2812
● Intelligent reverse connection protection, the reverse connection of the power supply will not damage the IC.
● IC control circuit and LED point light source share a power supply.
● The control circuit and RGB chip are integrated in a 5050-packaged component to form a complete external control pixel.
● Built-in signal shaping circuit, after any pixel receives a signal, it will be output after waveform shaping, so as to ensure that the line waveform distortion will not accumulate.
● Built-in power-on reset and power-off reset circuits.
● The three primary colors of each pixel can realize 256 levels of brightness display, complete the true color display of 16,777,216 colors, and the scanning frequency is not lower than 400Hz/s.
● Serial cascading interface, which can complete data receiving and decoding through one signal line.
● There is no need to add any circuit when the transmission distance between any two points does not exceed 5 meters.
● When the refresh rate is 30 frames per second, the cascade number is not less than 1024 points.
● Data sending speed can reach 800Kbps.
● Highly consistent light color, high cost performance.

insert image description here
3. The RGB color mode
is a color standard in the industry. It obtains a variety of colors by changing the three color channels of red®, green (G), and blue (B) and superimposing them with each other. Yes, RGB is the color that represents the three channels of red, green, and blue. This standard includes almost all the colors that human vision can perceive, and is one of the most widely used color systems.

RGB principle
RGB is designed from the principle of color light emission. Generally speaking, its color mixing method is like three lights of red, green and blue. When their lights are superimposed on each other, the colors are mixed, and the brightness But it is equal to the sum of the brightness of the three, the more the brightness is mixed, the higher the brightness is, that is, additive mixing. The superposition of red, green and blue lights, the brightest superimposed area of ​​the three colors in the center is white, and the characteristics of additive mixing: the more superimposed, the brighter. The three color channels of red, green, and blue are each divided into 256 levels of brightness. At 0, the "light" is the weakest—it is turned off, and at 255, the "light" is the brightest. When the three-color grayscale values ​​are the same, gray tones with different grayscale values ​​are produced, that is, when the three-color grayscales are all 0, it is the darkest black tone; when the three-color grayscales are all 255, it is the brightest white tone . RGB colors are called additive colors because you create white by adding R, G, and B together (that is, all light reflected back to the eye). Additive colors are used in lighting, televisions, and computer monitors. For example, displays produce color by emitting light from red, green, and blue phosphors. Most of the visible spectrum can be represented as a mixture of red, green, and blue (RGB) colors in varying proportions and intensities. If these colors overlap, yellow, blue and purple are produced.

RGB Format
The methods of encoding a color are collectively referred to as a "color space" or "gamut". In the simplest terms, the "color space" of any color in the world can be defined as a fixed number or variable. RGB (red, green, blue) is just one of many color spaces. With this encoding method, each color can be represented by three variables - the intensity of red, green, and blue. RGB is the most common scheme when recording and displaying color images. However, it lacked good compatibility with earlier black and white display systems. Therefore, a common practice adopted by many electronic and electrical manufacturers is to convert RGB to YUV color space to maintain compatibility, and then switch back to RGB format as needed in order to display color graphics on computer monitors.

insert image description here
RGB color space
The RGB color space has various implementation methods according to the different capabilities of the actual device system. As of 2006, the most common implementation is the 24-bit implementation, that is, 8 bits per channel for red, green, and blue, or 256 color levels. The color space based on such a 24-bit RGB model can represent 256×256×256 ≈ 16.7 million colors. Some implementations use 16 bits per primary color, enabling higher and more precise color densities within the same range. This is especially important in wide-gamut color spaces, since most of the commonly used colors are packed relatively tightly together. The RGB color space in printing technology mainly refers to the three-dimensional color space in the additive color method. By using the three primary colors of different intensities, red, green, and blue light to combine into different colors, it seems to say that if we usually Use a scanner to scan an image from a printed matter. The principle is that the scanner reads the brightness of the red, green, and blue colors on the image, and then converts these measurements into data. When the display receives these data, it can be set according to the program. The three primary colors of red, green, and blue are converted into the specified primary colors. In fact, there are many small color blocks of different colors among them. Because the pixels of these color blocks are very, very small and densely packed, our eyes cannot distinguish them.

Common colors and color values

insert image description here

4. Light up the RGB three-color light

from mpython import *

rgb[0] = (100, 0, 0)  # 设置为红色,半亮度
rgb[1] = (0, 100, 0)  # 设定为绿色,半亮度
rgb[2] = (0, 0, 255)  # 设置为蓝色,全亮度

rgb.write()

annotation:

First import the mpython module:

from mpython import *

After importing the mpython module, a NeoPixel object rgb will be created for control. To control the onboard RGB, you only need to operate on the rgb object.

Set color:

rgb[0] = (100, 0, 0)  # 设置为红色,半亮度

rgb[1] = (0, 100, 0)  # 设定为绿色,半亮度

rgb[2] = (0, 0, 255)  # 设置为蓝色,全亮度

rgb[n] = (r, g, b) can set the color of each pixel, n is the number of onboard RGB lights, and the first light is 0. r, g, b are color brightness values, the range value is 0~255.

rgb.fill(rgb_buf) can fill the color of all pixels, such as: rgb.fill((255,0,0)), all RGB lights are set to red, full brightness.

Output color to RGB lights:

rgb.write()

Template color selection for graphics programming

Click the color box to set the color of the lamp beads. We can light up one lamp individually or light up all of them. You can decide which one to use according to the situation.

insert image description here
insert image description here
Real object and simulation of RGB three-color lamp

insert image description here
insert image description here

5. RGB rainbow running water lamp
Use the delay module to control the lighting time of the RGB lamp, and then change the color of the lamp by changing the RGB value of the RGB lamp, so as to achieve the effect of the rainbow running water lamp.

from mpython import *
import time

rgb[0] = (100, 0, 0)  
sleep(0.05)
rgb[1] = (0, 100, 0)
sleep(0.05)
rgb[2] = (0, 0, 255)  
sleep(0.5)
rgb.fill( (0, 0, 0) )
rgb.write()

rgb[0] = (0, 100, 0)
sleep(0.05)
rgb[1] = (0, 0, 255)  
sleep(0.05)
rgb[2] = (100, 0, 0)   
sleep(0.5)
rgb.fill( (0, 0, 0) )
rgb.write()

rgb[0] = (0, 0, 255)  
sleep(0.05)
rgb[1] = (100, 0, 0)  
sleep(0.05)
rgb[2] = (0, 100, 0)
sleep(0.5)
rgb.fill( (0, 0, 0) )
rgb.write()

Mind+ graphic programming

insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/weixin_41659040/article/details/131998332