Witty Apa
Witty Apa
Witty Apa
com
www.didel.com/WittyApa.pdf
Strip length
@define Npix 8
Brt levels
#define Brt 2
0-255 intensity
ApaRGB(r,g,b);
APA102C survey
RGB chips have an interesting internal architecture. Circuits are cascaded, the closer to the
controller keeping the first pixel data sent.
There are three families of RGB chips.
1) 2 SPI-like signals (Ck,Data) that is 6 pins package, 6 lines between LEDs
Circuits are APA102, Sk9822. Libs used here are from Pololu and Didel
2) 1 encoded signal, that is a 4 pins package, 4 lines between LEDs
Circuits are APA104, WS2812B, SK6812
3) 1 encoded signal plus one "secure" input, that is a 6 pins package, 5 lines between LEDs
Circuit is WS2813B. For these two cases, libs are NeoPixel and WS28. See www.didel.com/WS28.pdf )
An APA102 strip is a long shift register with a data signal sampled by the positive edge of the
clock.
A synchronization word (32 zero bits) and additional final clocks avoid a delay between update
sequences, as it must be done with other strips to say the transfer is finished.
SPI can be used. It allows a very fast transfer, not requires for less than 1000 LEDs strips, and
brings the drawback of using 2 specific lines of the microcontroller.
Software transfer is quite efficient if Arduino digitaWrite is not used. Definitions and 8-bit serial
transfer function is given below. Execution time is less than 5us on an AVR 328 16MHz
Next picture shows the transfer format. Each LED receives a 4-byte word.
First byte has 3 bits at logic "1" and 5 bit of "brightness", value 0 to 31.
Follows the PWM values for the 3 colors. The shift order depends on the manufacturer, Order of colors
in documentation and for listing parameters will always be RGB.
Several clocks must be added the end of the transfer of all the pixels.
Eye sensitivity
The eye responds to an enormous range of light intensity, exceeding 10 units on logarithmic scale.
Contrast sensitivity is related when one think to applications.
PWM is linear, the difference of perceived intensity is great between PWM =1 and PWM =6, but not
noticable between 250 and 255. A correction must occur to give the impression of a regular increase
of intensity.
Neopixel and other libs e.g. strip.setPixelColor() use the linear PWM value, we name LinRGB or RGB().
Our lib allow to use LogRGB(), that access a table to set the PWM values.
The table has 32 entries, 3 bits are lost when accessing the table. Values 0 to 7, 8 to 15, .. 246 to
255 have the same effect.
We duplicate the RGB and Hue functions to offer both approaches. Run the test programs to see the
difference.
Understand we need the flexibility to work on the same strip with different length of LEDs and
we do not have a buffer (can be easily added, but the idea is to work with a small
microcontroller (AVR Tiny or PIC). Considering the pixel in sequence save a parameter. The
other parameter savec is the global brightness BR. It is hence more easy to
As explained in the detailed doc, N/2 clocks must terminate the sequence
void ApaStop (byte nn) {
DaOff;
for (byte i=0; i<nn/2; i++) {CkOn; CkOff;}
}
First experiment
Module is connected on pins 14 (Da) 15 (Ck) 16 (+5V)
and 17 (Gnd). pinMode notation is also proposed if
you are not keen with logic operations (and willing to
learn by comparing)
#define bCk 1 //pin 15 A1
#define bDa 0 //pin 14 A0
#define b5V 2
#define bGnd 3 pinMode (14,OUT);
#define CkOn bitSet (PORTC,bCk) pinMode (15,OUT);
#define CkOff bitClear (PORTC,bCk) pinMode (16,OUT);
#define CkPulse bitSet(PORTC,bDa); \ pinMode (17,OUT);
bitClear(PORTC,bDa) #define CkOn digitalWrite(15,HIGH)
#define DaOn bitSet (PORTC,bDa) #define CkOff digitalWrite(15,LOW)
#define DaOff bitClear (PORTC,bDa) #define DaOn digitalWrite(14,HIGH)
void SetupApa () { #define DaOn digitalWrite(14,LOW)
DDRC void SetupApa () {
|= (1<<bCk)+(1<<bDa)+(1<<b5V)+(1<<bGnd); pinMode(14,OUTPUT);
PORTC |= (1<<b5V); pinMode(15,OUTPUT);
PORTC &= ~(1<<bGnd); pinMode(16,OUTPUT);
} pinMode(17,OUTPUT);
digitalWrite(16,HIGH); // +5V
If you power the LED from Gnd and +5V, the definitions for digitalWrite(17,LOW); // Gnd
bGnd and b5V are not required. If you need more than 10 }
LEDs and dazzling light, you have to power directly (pins
are limited to 20mA).
Sample program
Exemple 1
//Apa102First 784b 9v color first led
#define Npix 4 // Number of pixels in the string (for Clear();
#define BR 2 // max 31 brigtness
#include "APA102.h" // define pin here
void setup () {
SetupApa102(); // clear the first Npix
}
void loop () {
Start(); RGB (100,0,0); Stop(1);
delMs (1000);
Start() RGB (0,100,0); Stop(1);
delMs (1000);
}
Exemple 3 Rainbow
Appendix
Library functions
#define Npix 6 // strip length
#define BR 2 // brightness
void Start () {
DaOff;
for (byte i=0;i<32;i++) { CkPulse; }
}
void RGB (byte rr,byte gg,byte bb) {
S8 (0xE+BR); S8 (bb); S8 (gg); S8 (rr);
}
void Stop (byte nn) {
DaOff;
for (byte i=0; i<nn/2; i++) {CkOn; CkOff; }
}
Other tests
We just need to document the main program. You have a strip of 10 correctly connected.
Let us test the 3 colors on the strip.and answer the question
Have all LED the same intensity with the same parameters?
Have the RGB LEDs the same physiological intensity with the same PWM values?
Is the perceived light intensity proportional to PWM?
What is the effect of brightness BR (0 to 31)?
//ApaTest1.ino Compare LED intensity at different pwm
#define Npix 6 //number of LEDS
#define BR 2 //brightness 0..31
#include "APA102.h"
void setup () { SetupApa(); }
Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
from?? Fichier apa102producdescription détruit le 190325
Protocol
These LED strips are controlled through an SPI protocol on the data and clock input lines. The protocol is
documented in the APA102C datasheet (1MB pdf), but we describe it below with some modifications that
we have found to work better.
The default, idle state of the clock signal line is low, and the data signal is read on each rising edge of the
clock. To update the LED colors, you need to toggle the clock line while driving the data line with the
value of each bit to send; this can be done through software (bit-banging), or it can be handled by a
hardware SPI peripheral in a microcontroller. There is no minimum clock frequency, although using a
lower frequency means that it will take longer to update the entire sequence of LEDs (especially when
controlling a long strip or many strips are chained together), so you will probably want to use the fastest
practical clock speed to get the best update rate.
The diagram above shows how entire LED frames are forwarded from one device to the next one. Once a
device detects a start frame (more than 31 zero bits), it will interpret the next “1” bit as start of its own
LED frame. 32 bits are clocked into the PWM registers, while zeroes are pushed to the output. After the
entire LED frame as been read, any subsequent data is simply forwarded until another start frame is
detected.
The End Frame
As we have learned above, the only function of the “End frame” is to supply more clock pulses to the
string until the data has permeated to the last LED. The number of clock pulses required is exactly half
the total number of LEDs in the string. The recommended end frame length of 32 is only sufficient for
strings up to 64 LEDs. This was first pointed out by Bernd in a comment. It should not matter, whether
the end frame consists of ones or zeroes. Just don’t mix them.
Furthermore, omitting the end frame will not mean that data from the update is discarded. Instead it will
be loaded in to the PWM registers at the start of the next update.
Summary
In summary, each update of an APA102 based LED string should consist of the following:
1. A start frame of 32 zero bits (<0x00> <0x00> <0x00> <0x00>)
2. A 32 bit LED frame for each LED in the string (<0xE0+brightness> <blue> <green> <red>)
3. An end frame consisting of at least (n/2) bits of 1, where n is the number of LEDs in the string.
Unlike the WS2812 protocol, no waiting period is required before the next update. As discussed before, I
strongly suggest to only use the full brightness setting (31) to reduce flicker.
I have no recommendation for a maximum or minimum SPI clock speed. There is no specification for this
in the datasheet. So far, it seems that the LED is able to handle any clock setting that is thrown at it. I had
no issues with 4 MHz and others have successfully tested 10 MHz and above.