Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Net and raspberry pie control color light band WS28XX how to realize

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of ".NET and raspberry pie control color light belt WS28XX". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Color light belt, I believe that needless to say, everyone knows that there may be in the light wall of your home. Lao Zhou's hut was built in the early days, so there is no reserved light slot, the bright lights are not good-looking, so Lao Zhou's family did not use light belts. However, in places like the back of the cabinet, the back of the monitor and the edge of the desk, colored light strips can be pasted. Some are also posted on the bookshelf, because the bookshelf is in the shape of a Christmas tree, so there is no way to finish it with a light belt, so we can only cut it into N sections, measure the distance of each section, and then weld it with soldering iron and wire (if you find it troublesome, you can buy a connector directly. No welding).

When buying the light belt, Lao Zhou did not buy the driver. After all, from the beginning, Lao Zhou planned to write a program to control the color of the light belt. The 5V voltage of the decorative lamp belt is enough. If more lights can be considered independent power supply, 5V power supply is more convenient and easy to operate, mobile phone charging head, lithium battery + 5V voltage stabilizer can be easily solved. In fact, there is no problem with using the development board to power directly within two meters in length. For a roll of five meters, there is no problem with the 5V pin of the raspberry pie, but with ESP32, the voltage seems to be a little insufficient and the brightness decreases.

The decorative lights currently used in Lao Zhou's bedroom are made of ESP 32 and powered by two 18650 batteries (the voltage regulator module needs to be adjusted to 5V). Mainly fancy ESP32 has WiFi function, above write a UDP server, through the command received to switch lights with color.

The control command is relatively simple, all in text format, and the client only needs to send commands that meet the format requirements to ESP32. For example, send

SET+255 125 127

First, "SET+" is identified, and the next three values are separated by spaces, which in turn represent the three values of RGB. For example, make the light blue

SET+0 0 255

Color light strips use WS28XX as IC, commonly like WS2812B. Each lamp bead can be controlled separately. After the lamp belt is cut, it only needs to be connected in series. The end of the lamp belt does not need to be connected to the circuit. WS28XX has its own circuit.

The timing of WS28XX is actually very simple. + 5V and GND are power supply interfaces, leaving a data line to transmit signals, so the goods are controlled by a single data line. When N lamp beads are connected in series, the Din input signal of the first lamp bead, the Dout output signal, the Dout of the first lamp bead, the Din; of the second lamp bead, the Dout of the second lamp bead, and the Din of the third lamp bead.

As we all know, the three values of RGB, each one byte, 8 bits, add up to 24 bits, so the data of each lamp bead is 24 bits, 3 byte. For each binary bit, WS28XX determines 0 or 1 by the duration of the high level in a single cycle.

Please take a look at the high-definition no-code beautiful picture below.

The above is the timing diagram of WS28XX. Within a pulse period, if:

The duration of the high level is 0.4 microseconds and the duration of the low level is 0.85 microseconds, so it is 0.

The duration of the high level is 0.85 microseconds and the duration of the low level is 0.4 microseconds, which is 1.

Therefore, the total duration of a cycle is 1.25 microseconds, that is, the frequency is 1000000 / 1.25 = 800 KHz, or 0.8 MHz.

Please remember this frequency, which is useful later.

The WS28XX signal is continuous and does not require a pause; if there is a low level of more than 50 microseconds, then WS28XX will think that your signal has been sent. If there is data on the data line, it will be processed from the first lamp bead (equal to updating the data of the entire lamp belt).

For example, if there are four lamp beads in series, and the RGB of each lamp has 24 bits, then you need to send 24 * 4 = 96 bits of data to WS28XX continuously. Do not pause in the process of data transmission. All data are sent in succession-96 bits continuously without pause in the middle. After all, output a low level of about 50 microseconds to indicate the end of sending.

For example, red, RGB is 255ji0pl 0, then these 24 digits are:

1111 1111 0000 0000 0000

If there are three lamp beads, the first lamp bead is set to green, the second lamp bead is white, and the third lamp bead is blue, then the three groups of RGB are:

[0,255,0] [255255255] [0,0255]

Therefore, the 72-bit binary is:

0000 0000 1111 1111 0000 0000 1111 1111 1111 0000 0000 0000 1111 1111

When sending, these 72 bits are sent continuously, and there is no need for delay. After sending, turn the level down, and the WS28XX will think that you have finished sending the data after 50 us.

As you can see, WS28XX's communication protocol is relatively simple. However, the problem is that its time is very short. If you write code by hand to change the level, the program is required to have high performance. Low-equipped single-chip computers may not be fast enough, and development boards such as raspberry pie must be faster than single-chip microprocessors, but the code is passed to the system driver, which is then transferred to the underlying hardware. And each switching level needs to communicate back and forth twice, which takes too long and may exceed the cycle of 1.25 us.

Therefore, it is generally not to write the GPIO level directly, but with the help of the protocols supported by the hardware. There are two hardware protocols that can do this:

1 、 PWM . I guess you can understand this. Lao Zhou said earlier that we should remember that communication frequency, which is now in use. Set the PWM frequency to 800KHz, and then the length of a cycle is 1.25us. Finally, you must have guessed that when sending 1, the high level lasts 0.85 us, with a duty cycle of 68%; when sending 0, the high level lasts 0.4 us, with a duty cycle of 32%. So, by constantly changing the duty cycle, you can signal WS28XX. Many of the solutions are used in conjunction with DMA to improve speed. The PWM encapsulated by .NET Iot does not support the DMA approach, so this scenario is skipped.

2. SPI, this scheme is the best one at present. The libraries encapsulated by .NET Iot also use the SPI protocol. This is an ingenious use to connect the MOSI port of the SPI with the Din of the lamp belt, and let the SPI timing control the lamp beads.

Why not use IIC (I2C)? Because IIC needs a slave address, we don't really use IIC devices, so it's not suitable here. According to the official example, the following parameters should be set when using SPI:

A, the speed is 2400 KHz, which is exactly three times that of 800K. Therefore, the three binary bits in the SPI correspond to one binary bit in the WS28XX. To send 1, SPI writes to binary 110; to send 0, SPI writes to binary 100. The data of a lamp bead is 3 bytes and 24 bits, so the SPI has to write 72 bits (9 bytes).

B. Select Mode0 in SPI mode.

C, the data length is 8 bits.

Because raspberry pie supports hardware SPI, there is no problem with the SPI scheme. If the development board does not have hardware SPI (simulated by software), the SPI scheme cannot be used and the speed can not keep up.

After knowing the principle, we can do it.

Step 1, reference the Iot.Device.Bindings package (Nuget), and the dependency package System.Device.Gpio is automatically introduced, so don't worry about it.

Step 2, using the following namespaces:

Using System.Device.Spi;using System.Drawing;using Iot.Device.Graphics;using Iot.Device.Ws28xx

Step 3, initialize the SpiDevice object.

SpiConnectionSettings setting = new (0) {Mode = SpiMode.Mode0, ClockFrequency = 24000000000, / / Note rate DataBitLength = 8}; using SpiDevice spidev = SpiDevice.Create (setting)

Set ClockFrequency to 2400000 hz for reasons explained earlier.

Step 4, the lamp belt that Lao Zhou bought is of WS2812B chip, so instantiate WS28XX with Ws2812b class, most of the lamp tape is this.

/ / 30 means 30 lamp beads are used. If you want to use 50 lamp beads, pass 50const int LED_NUM = 30 × Ws28xx leds = new Ws2812b (spidev, LED_NUM).

The lamp belt used by Lao Zhou to test is five meters long and 150 lights, but in order to save electricity, only 30 lights are selected for the experiment, and you can change the code according to the actual situation. We use the lamp belt here, so only consider the width and ignore the height; if you are using a lattice screen, consider the height, that is, a few rows and columns.

Step 5, prepare seven commonly used colors, and we will do the color rotation effect later.

Color [] colors = {Color.Blue, / / blue Color.Yellow, / / yellow Color.White, / / white Color.Red, / / red Color.Green, / / green Color.Pink, / / pink Color.Orange / / orange}

Step 6, color rotation, seven colors are displayed in turn.

/ / Index int theIndex = 0 in use in the Color array / enter the loop and start while (true) {/ / when the value indicating the index is out of the array range, return to 0 if (theIndex > colors.Length-1) {theIndex = 0;} / get the bitmap object BitmapImage image = leds.Image; / / cycle to modify the color for of each lamp bead (int x = 0) X < LED_NUM; x +) {/ / update pixel (corresponding to a lamp bead) image.SetPixel (x, 0, colors [theIndex]); / / call update to refresh the lamp band display leds.Update (); Thread.Sleep (10);} theIndex++; Thread.Sleep (1000);}

You can think of a lamp bead as a pixel, first get a reference to the BitmapImage object from the Image property, and then use the SetPixel method to set the color of each lamp. Here, because the lamp belt is used, so the y coordinate is all 0, only the value on the x coordinate is changed.

After changing the color, if you want the lamp bead to be updated immediately, please call the Update method.

Finally, emphasize: when wiring, the MOSI of the raspberry pie is connected to the data interface (Din or Di) of the light belt.

If SPI is not available, execute sudo raspi-config, and then enter "Interface Options" to confirm that the SPI bus is enabled.

Look at the end result.

[add] if the program is "stuck" after running for a few seconds, it is due to the frequency reduction of SPI by the system. If you encounter this problem, you can modify the GPU overclocking parameter in the config.txt file.

Sudo nano / boot/config.txt

Add these two lines:

Core_freq=250core_freq_min=250

Or

Core_freq=500core_freq_min=250

When configured in this way, at least the idle frequency of the GPU kernel is not less than 250 MHz.

Net and raspberry pie control color light belt WS28XX how to implement, this is the introduction here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report