Step 4Control?
I wanted to make one control algorithm generic enough for handling patterns/sequences and also controlling the brightness and colour of each LED.
To control the LEDs I have to send to the MCP23016 one frame of 4bytes (1 byte = 8 bits). One byte with the address of the IC correspondent to the colour, 1 byte with the command "write" and 2 bytes with the value of the 16bits (LEDs). The IC is connected to the LEDs as "sink", meaning, one logic value 0 at the pin will light the LED.
And now the challenging part, how to make PWM control for 48 LEDs?
Let's study PWM for one LED! PWM explained @ Wikipedia.
If I want the brightness of the LED at 50%, my PWM value is 50%. This means the LED, in one period of time, should be on the same amount of time as off.
Let's take a period of 1 second. PWM of 50% means that in this 1 second, the on time is 0.5 seconds and the off time is 0.5 seconds. PWM of 80%? 0.2 seconds off, 0.8 seconds on!
Easy, right?
In digital world: With period of 10 clock cycles, 50% means that for 5 cycles the LED is on, and for another 5 cycles the LED is off. 20%? 2 cycles on, 8 cycles off. 45%? Well, we can't really get 45%... Since the period is in cycles and we have only 10 cycles, we can only divide the PWM in steps off 10%.
This means the evolution of the pin should be, for 50%: 1,1,1,1,1,0,0,0,0,0; Or even 1,0,1,0,1,0,1,0,1,0;
In programming we can make this sequence of turning on and off an array. For each cycle we output to the pin the value of the index were the cycle is.
Did I make sense so far?
If we want to make LED0 50%, and LED1 20%, we can add both arrays.
For driving the LED0 pin: 1,1,1,1,1,0,0,0,0,0;
For driving the LED1 pin: 2,2,0,0,0,0,0,0,0,0;
Resulting in LED0+LED0: 3,3,1,1,1,0,0,0,0,0;
Outputing this sequence of numbers in the port expander IC, we would get the LED0 with 50% brightness and LED1 with 20%!!
Simple for 2 LEDs, right? Now we have to make this for 16 LEDs, for each colour!
For each one of these arrays, we have a combination of brightness for each colour (16 LEDs)
Every time we want another combination of colours, we have to change this array.
| « Previous Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|













































