Showing posts with label LED Controller. Show all posts
Showing posts with label LED Controller. Show all posts

Thursday, February 4, 2021

How to control RGB leds | RGB fader library for AVR ATmega328P

RGB leds are fun and because they can be used in many projects I have decided to make a library to easily crossfade the colors of one or multiple RGB leds.

To see this library used in a real project, check out this video Digital Clock With RGB Night Lamp & Spherical Shelf.

How to control RGB leds | Crossfading RGB leds | Library for AVR ATmega328P

Crossfading an RGB led in the RGB colorspace

With 8 bits we have 256 values from 0 to 255 that represents the duty cycle - how long a led will be on then off in a period. For example setting the RED led to 255 and GREEN and BLUE to 0 will result in RED color. Or RED 255, GREEN 0 and BLUE 255 will show a purple color. All leds on (255 value) will result in a white lite. So this is how a certain color can be produced but how to cycle through all the possible combinations?

Crossfading an RGB led in the RGB colorspace 1

First the red color is set at 255 and green and blue to 0. Then the red will be decremented and the green will be incremented. When the red will be 0 and green 255 we change the fading up and fading down colors.

Wednesday, February 3, 2021

Multi-channel software PWM library for AVR microcontrollers | ATmega328P

What you do when you run out of PWM pins on hardware? You make software PWM of course. This library is based on "AVR136: Low-Jitter Multi-Channel Software PWM" application note. It supports up to 10 PWM channels (more can be added) and it's suitable for led dimming, DC motor control and RGB led controller.

Multi-channel software PWM library for AVR microcontrollers | ATmega328P

Since this method is already explained in the AVR136 app note I won't go in to too many details. So the basic principles behind software PWM are this. A timer interrupt is set to trigger every 256 system clocks. For 8 bit timers the interrupt is on overflow and for 16 bit timers is on compare match. A 16 bit timer has the advantage that the base frequency can be modified. On every interrupt a variable "softcount" is incremented from 0 to 255 and each time is compared against each PWM channel. At the beginning of the cycle the pins are set high and when "softcount" equals to a channel's set value then the specific pin is set low. On 8MHz CPU the ISR takes between 4 and 10us to execute the code depending on how many channels and on how many ports there are. The size of the "softcount" variable dictates the PWM resolution and it is set to 8 bits. 

Saturday, January 23, 2021

Binary Code Modulation (BCM) aka Bit Angle Modulation (BAM) library for fading leds for AVR microcontrollers

Binary Code Modulation (BCM) was invented by Artistic Licence and is a form of PWM but not really. The main advantage over software PWM is the low CPU usage regardless of how many channels it controls.

This library provides a fast implementation of Binary Code Modulation useful for controlling RGB leds and dimming multiple leds for creating animations like led cubes and includes an array for logarithmic brightness. A complete cycle takes 8 timer interrupts and each interrupt takes only 4us on a 8MHz CPU. The leds can be on different ports.

Bit Code Modulation (BCM) aka Bit Angle Modulation (BAM) library for RGB led dimming - 8-bit

How Binary Code Modulation (BCM) works and how it differs from PWM

To dim a led with PMW is simple. If you want the led to be half as bright you turn the led on for 50% of the cycle and 50% for the other half. Or 20% on and 80% off for an even dimmer led.

 

PWM example

Bit Angle Modulation uses the weight of each bit in a binary number. For example in one byte there are 8 bits with numbers from 0 to 7. Bit 0 is called the Least Significant Bit (LSB) and it's weight is 1. Next bit 1 has a weight of 2, bit 2 has a weight of 4, then 8, 16, 32, 64 and 128. Bit 7 is called the Most Significant Bit (MSB) because it has the highest weight - 128.

8-bit binary weight