logo elektroda
logo elektroda
X
logo elektroda

PIC18F45K50 as WS2812 LED strip driver (theory+library)

p.kaczmarek2 6477 5
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • PIC18F45K50 as WS2812 LED strip driver (theory+library) .
    Hello my dears .
    Today's topic will be the WS2812 LED driver. First I will describe its operation from the theoretical side, and then I will present my library to support it designed for the PIC18F45K50.

    What is WS2812? .
    The WS2812 is an intelligent LED controller connected to an RGB LED. It allows you to control a large number of LEDs with essentially one pin, but more on that later. We'll start with what you see at first glance, the housing. The whole thing is housed in a single SMD 5050 housing - meaning it measures 5mm by 5mm!
    PIC18F45K50 as WS2812 LED strip driver (theory+library) .
    PIC18F45K50 as WS2812 LED strip driver (theory+library) .
    PIC18F45K50 as WS2812 LED strip driver (theory+library) .
    As we can see, the whole thing resembles a simple LED. Here the question arises, where is all the intelligence of the driver? Just look at the pins to understand this:
    PIC18F45K50 as WS2812 LED strip driver (theory+library) .
    The WS2812 LED has only four pins, consecutively:
    3
    No. Symbol Description
    1 VDD Power supply
    DVDD 2 DOUT Out control signal
    3 VSS Weight
    4 DIN Control signal input
    [/table:5967c9e953] .
    We will probably be most interested in the pins associated with the control signal. There are only two - input and output. What does this mean?
    It means that the WS2812 is controlled digitally and, what's more, the signal given to one diode can be sent to the next diode by connecting their DIN and DOUT pins - this allows multiple diodes connected in this way to be controlled by a single pin !
    This combination of WS2812 diodes looks as follows:
    PIC18F45K50 as WS2812 LED strip driver (theory+library) .
    Of course, you also need to lead GND and VDD to each, this is not shown in the diagram above. A really large number of LEDs can be connected in this way, however you will need a microprocessor with a suitable protocol developed to control this.
    To summarise, in practice, the whole thing looks like this:
    PIC18F45K50 as WS2812 LED strip driver (theory+library) .

    Control protocol WS2812 .
    All control is done via a single digital signal which we feed to the DIN pin of the first LED. The signal sends the colour bits (appropriately coded) sequentially. There are 24 bits per LED (3 bytes - Red byte, Green byte, Blue byte). This means that we can display a wide range of colours, theoretically as many as 2^24 i.e. 16777216 different shades! The specified bits respectively go on until we freeze the output signal in the low state for at least 50us.
    The bits themselves are encoded with the help of applying a high and low state on the pin through the appropriate time.

    In summary, according to the image (from the WS2812 datasheet note):
    PIC18F45K50 as WS2812 LED strip driver (theory+library) .
    - if we want to transfer a bit 0, we set the communication pin to high state at time T0H and then to low state at time T0L
    - if we want to transmit a bit 1, we set the communication pin high at time T1H and then low at time T1L
    - if we want to communicate that we terminate the data transfer (i.e. after this break we can control the LEDs again from the beginning), we set the pin to low state at least for the time Treset
    The times are given in the table:
    PIC18F45K50 as WS2812 LED strip driver (theory+library) .
    The colour coding is as follows:
    R4 B2
    G7 G6 G6 G5 G4 G3 G4 G2 G1 G0 G0 R7 R6 R5 R4 R3 R2 R1 R0 B7 B6 B5 B4 B3 B2 B1 B0
    [/table:5967c9e953] .
    Yes, this is GRB, not RGB. The 8 bits of green are transmitted first, then red, and finally blue. The transmission starts with the oldest bits.
    Between colour transfers for subsequent LEDs there is no interval - simply continue transmission and the WS2812 will take care of everything. .
    After sending all the bits (for all the LEDs we want to handle) we wait for the Treset time (low state on the pin) and we can send the bits again.

    Used board from PIC .
    For the project I used one of my older boards with the PIC18F45K50 (the PIC18F4550 would also fit here, as the two microcontrollers are almost identical, although the 45K50 version, for example, has a better precision internal oscillator than the 4550). This board has a 20MHz quartz oscillator. Apart from that, it contains only fairly basic components such as a resistor from RESET, capacitors, a USB connector (here only used for power supply), etc.
    PIC18F45K50 as WS2812 LED strip driver (theory+library) PIC18F45K50 as WS2812 LED strip driver (theory+library) .
    I know this board looks the way it does, but it was one of my first designs a few years back. The others were already taken. I've been using so many PICs lately that I think I'll soon make some good PIC18F4550 board for myself, but fully in SMD.

    Schematic of my board (this is a Pinguino schematic, but I programmed the board in MicroC):
    PIC18F45K50 as WS2812 LED strip driver (theory+library) .

    Developed library .
    There are a lot of ready-made LED animations for the WS2812 on the web, and it would be silly not to use them - that's why I developed my library in such a way that I could eventually plug ready-made code from the web into it without any problems. I implemented the communication itself in C code with a little help from inserts __asm nop to be able to wait at least a little more precisely the required times T0H, T0L, T1H, T0L. The precision was not perfect, but the whole thing got off the ground nonetheless.
    I wrote the library in the MikroC PRO for PIC environment, but it is so simple that porting it to another compiler would not likely be a big problem. I programmed the whole thing using my simple PICKIT2 clone.
    Screenshot from MikroC PRO For PIC:
    PIC18F45K50 as WS2812 LED strip driver (theory+library) .
    Screenshot from PICKIT2 during programming:
    PIC18F45K50 as WS2812 LED strip driver (theory+library) .
    Screenshot of the whole during library development:
    PIC18F45K50 as WS2812 LED strip driver (theory+library)
    The library itself is very straightforward, while porting it to other microcontrollers will probably only involve changing the handling of the corresponding wait times. It is worth remembering that in the current version it uses nop to wait, so when changing the microprocessor clocking these waits should also be corrected.
    The configuration of my library basically just involves setting three variables from the WS2812.h file:
    Code: C / C++
    Log in, to see the code
    .
    The first two variables specify which port the LED bar is on, and the third the number of LEDs in the bar.
    The library at the moment is not made to support several separate LED strips at once, but in general this is easily possible. I will probably add support for this in the future (when I buy more strips) and will update this topic then.
    The library could be optimised in a couple of places, e.g. by unwinding the for loop, etc.

    . Effects .
    As intended, I developed my library in such a way that I could then painlessly plug ready-made lighting effects from various libraries into it. In particular, I have taken to:
    https://github.com/adafruit/Adafruit_NeoPixel
    and
    https://www.tweaking4all.com/hardware/arduino/adruino-led-strip-effects/
    The videos show the PIC18F45K50 with my library playing various effects:


    .


    .


    .


    .

    And here the main function code main() used in the above videos:
    Code: C / C++
    Log in, to see the code
    .
    The code itself for the individual animations is copied from the sources above, only the WS2812 control function calls are swapped to mine.


    Summary
    .
    I liked the WS2812 LED driver very much. It is easy to use and cheap. You can get a lot of variety of LED strips on the web and not only based on the WS2812.
    I think that even the information in this topic alone would allow anyone to write the WS2812 support code themselves, but I'll still be tempted to post my library here:
    ws2812_pi..8f.zip Download (1.17 kB)Points: 4 for user .
    And an example of its use (the effects themselves are from Adafruit, ported to C):
    PIC18F45K5...edRing.zip Download (130.57 kB)Points: 1 for user .
    I am also attaching the WS2812 datasheet (source of images from the WS2812 communication description):
    WS2812.pdf Download (271.75 kB) .
    And screenshots of WS2812 libraries from the web which I used as a template and animation source:
    light_ws28...master.zip Download (3.59 MB) .
    Adafruit_N...er (1).zip Download (55.98 kB) .
    WS2812Seri...master.zip Download (129.22 kB) .

    Cool? Ranking DIY
    Helpful post? Buy me a coffee.
    Do you have a problem with Arduino? Ask question. Visit our forum Arduino.
    About Author
    p.kaczmarek2
    Moderator Smart Home
    Offline 
    p.kaczmarek2 wrote 11858 posts with rating 9943, helped 566 times. Been with us since 2014 year.
  • ADVERTISEMENT
  • #2 18015804
    fotomh-s
    Level 24  
    I have recently been controlling these LEDs via ARTNET. I made myself a controller from an ESP-32 module.
    Unfortunately the ARTNET library on the ESP has a bug, sometimes the whole thing crashes if there is a lot of traffic in the WIFI network. If there is no traffic, the whole thing works without problems.
    It is necessary to use the current version of the fastled library, the standard neopixel library and some other (I do not remember exactly) does not work well with ESP-32 and random flashing leds appear (supposedly some problem with timing, interrupts / timer handling).

    For control I recommend Chamsys MagicqPC and GrandMA2 onPC (old version with a bug which allows to release signal without hardware from MA), finally the bug is useful for something one could say ;-) .

    Interestingly, on Allegro, the 74led/m tape cost probably 36zl per metre (i.e. for 74 leds). A decent price for something like that. On ALI it wasn't much cheaper at all (or maybe I was looking wrong?).
  • ADVERTISEMENT
  • #3 18015864
    p.kaczmarek2
    Moderator Smart Home
    fotomh-s wrote:
    .
    Interestingly, on Allegro, the 74led/m tape cost, I think, £36 per metre (i.e. for 74 leds). A decent price for something like that. On ALI it wasn't much cheaper at all (or maybe I was looking wrongly?).

    I have just had a 1 metre 60led/m tape ordered from ALI for just under £15.
    PIC18F45K50 as WS2812 LED strip driver (theory+library) .
    I do not show the name of the seller from whom I bought, who wants to find it.
    Those 'black' in the product parameters are of course the colour of the tape itself, not the LEDs.
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #4 18017469
    radiosimon
    Level 28  
    The problem with software control is, for example, the handling of the encoder or other interrupts. Then the effects are already harder to do without "flashes and clipping". But overall, the price/quality and capabilities are outstanding.
  • ADVERTISEMENT
  • #5 18017680
    fotomh-s
    Level 24  
    The only thing I don't quite like is the minimum brightness of the LEDs (at 0x01 per channel), it could have been slightly lower. Overall though, there is no point in using regular RGB LEDs anymore, it's not worth it considering the convenience of the WS2812 control (one line, daisychain). At Chinese, these WSs are cheaper than the regular LED (not RGB) in stationary shops here.
  • #6 18017928
    hetm4n
    Level 20  
    And what fabulous things can be created with the WS2812b :) .


    .


    .


    .


    .
ADVERTISEMENT