Wednesday 4 September 2013

Flicking lights in hobbit cottage

Hello,

In the last weeks,  I have been working in a miniproject that consist on create a blinking effect in a led to simulate a flame. The result is a small board that can manage 8 leds outputs but can be easily scalable to have more outputs.

To get the flame effect in a led, you need to have a variable brightness in the led, and to get it you have to modify every a random time the intensity of the light with a random value. To get you have to modify the PWM of a led. You can get a more realistic effect using a dual led red-yellow.

Lets start by the hardware. I have used ATMEGA8515 because I had some samples at home but it is not the best choice. The hardware is really simple and only has the microcontroller, a voltage regulator, some high side drivers to manage the leds and some connectors.
Schematic

If you have an stable 5V external power supply you can remove the voltage regulator and mount R18 and Z1 instead of it. I'm using the voltage regulator, that is low drop, feed by 4 AA batteries, so I haven't R18 and Z1 mounted. Another point is the decision to use high drivers to manage the leds and the reason to use it instead of low side is to be able to manage dual leds. If you use a dual led (red-yellow) you can get a more realistic effect. I have added an oscillator in the system just in case I'd need to implement a UART communication, but I'm using the internal oscillator, so I haven't mount X1, C1 and C2.

Regarding the software I have modified the PWM values  according the next constants.
const T_u8 VAR_DUTY[8] = {20, 20, 20, 20, 20, 20, 20, 20};
const T_u8 CONST_DUTY[8] = {40, 40, 10, 10, 40, 40, 40, 40};
const T_u8 VAR_WAIT[8] = {50, 50, 50, 50, 50, 50, 50, 50};
const T_u8 CONST_WAIT[8] = {50, 50, 50, 50, 50, 50, 50, 50};

VAR_DUTY is the variable duty cycle of each output and CONST_DUTY the constant duty cycle. In this case the software takes PWM value between 40 plus a random value between 0 and 20. The duty cycle is mesured in %. 

VAR_WAIT is the variable time between 2 duty cycles changes and CONST_WAIT is the minimum time. We have a minimum time of 50 ms plus a random time between 0 and 50 ms. The times and duty cycles of the outputs are completely independent.

As the microcontroller doesn't have enough PWM outputs, I have managed them by software. Basically I have a list of duty cycle for each output and an interrupt. At the beginning of the cycle, starts all the outputs that are on, and calculates then next time that the driver has to change the state of a pin. Then depending on it, reconfigures the timer value to generate the next interrupt. The PWM is implemented in the file pwm.c and is easily scalable

All the software and schematic is available in a SVN repository.
 https://svn.code.sf.net/p/svbot/code/05_HOBBIT

 Here there are some pictures from the board an a small video were you will see the flicking effect.
Bottom layer

Top layer