Enide! my web project

Candle Simulator

Description

candle foto

The aim of this project is to create a credible simulation of the light of a candle. Candle light is usually warm and waves slowly with the random oscilations of the air in the surrounding environment.
Here's a sample image showing the effect of a candle. The oscillations are somewhat random, depending on the flow of the air surrounding the candle.
It should be possible to simulate the effect of a candle with a small electronic circuit that generates random patterns or numbers.



Design and Implementation

To create the candle effect two light emitters can be used: Light Bulbs or LEDs. Light bulbs are not very convenient for this project because of the amount of current they consume versus the light they emit and also because of their lifetime which is around 1000 hours, for a regular bulb.
A LED can last at least 10 years and provides more intense light with less current than a light bulb!

Having previously developed an LED brightness control using PWM modulation, I decided to re-use the same concept to implement this simulator.

PWM wave from 100% to 0% duty-cycle.

PWM or Pulse-width modulation of a signal or power source involves the modulation of its duty cycle, to control the amount of power sent to a load. This is because the average power delivered is proportional to the modulation duty cycle. With a sufficiently high modulation rate, passive electronic filters can be used to smooth the pulse train and recover an average analog waveform [1].

Due to the simplicity of this project, there will be no filters after the modulator, just the LED.



Old prototyping board

This is how the prototype board looked like.

To speed up the development I used my old PIC12 prototyping board. I assembled this board when I was trying to mimic the effect of an iBook sleeping LED, so it's more than appropriated for the task. This board features a small PIC 12F675 micro-controller from Microchip, a 78L05 voltage regulator, a 100nF decoupling capacitor, a 470Ω resistor and a white 3mm LED.



Modifications to the prototyping board


Since the original prototyping board only provided 5mA of current to the 3mm LED, the amount of light emitted was not enough to properly simulate the candle effect.

The PIC 12f675 datasheet tells that it can provide enough current for one LED. But it would increase the power dissipation in the uC. So I added a FET transistor to drive two LEDs instead of one. This way I have all the intense light I need.

Each LED has a 68Ω resistor to limit its current to 25mA.

Vcc = 5V, Vled = 3.3V

I=(Vcc-Vled)/68 = 25mA

The transistor BS170 Enhancement-FET with a maximum drain current of 500mA is more than adequate for the job of driving the LEDs.

The PIC micro-controller consumes about 1mA which leads to a total amount of current consumed by the circuit of 51mA.

The 78L05 power regulator can handle currents up to 100mA, so it's safe!


Schematic

The schematic can be seen in the picture on the left. The circuit is very simple to analyse:Schematic

  1. the 78l05 regulates the voltage to 5V;
  2. the 100nF capacitor decouples the voltage;
  3. the PIC generates the pulses that drive the FET;
  4. the FET works like a ON/OFF switch for the LEDs;
  5. the 68 Ω resistors limit the current of the LEDs to 25mA each;
  6. and the LEDs emit the light.

These LEDs provide almost 10 times more light than the previous 3mm LED.

The implementation assumes the use of the micro-controller internal 4MHz oscillator instead of an external crystal.

The power supply should be able to provide 7Vdc/60mA or more in order to make the circuit work properly.

Replacements

  • It is possible to replace the 78L05 with any other 5V/100mA voltage regulator.
  • It is also possible to replace the FET with another one capable of driving at least 50mA or 60mA.
  • The PIC may also be replaced by another one in the same family as long as the replacement is very similar: PIC 12F629 can be used instead!


PCB

No PCB was developed since this is a very small project.
Instead everything was assembled in a pre-drilled and easy to cut, prototyping board.


Software

The software is basically a PWM generator and a random bit generator to drive the PWM!

  • The PWM is implemented with two timers, just like any classic PWM generator.
  • The random bit generator is implemented with a 16 bit shift register and the polynom 1+X10+X11 as the linear feedback function.
  • PWM works by interrupt and its implementation is inside the interrupt service routine;
    1. Timer 0 is used to define the 10ms (100Hz) period of the wave;
    2. Timer 1 counts the amount of time elapsed since the begining of the current period.
  • The PWM is generated like this: timer 0 turns the output ON and starts timer 1 periodically; timer 1, when running, will count the amount of time after which the output should be turned OFF. When the time if over it turns the output and itself off.
  • Random bits: each iteraction of the linear feedback function will return a random bit and also update the value inside the shift register.[2]

 

Main loop.

The main function runs a loop, reading a random bit, updating the current brightness level based on the bit received and delaying a few milliseconds before repeating itself again.

The project HEX file, required to program the PIC micro-controller, is available for download.
As an alternative, and if you like to see how it is done, the source code written in C is available for download. It may be compiled with the demo version of the PICC compiler from HI-TECH.


Evaluation

The first test was very successful! Most of the software was already tested and the only thing needed to be tuned was the delay between brightness changes. After two or three adjustments in the delay loop, it was perfect.

There are several videos below that show the software in action:


Since the software implements a 16 bit shift-register to generate bits these are pseudo-random. Pseudo-random sequences repeat themselves after a while. So far I was unable to see any similarities between the random oscilations of the light during the days that it has been working! This means the sequence is long enough to seem random to people.

Downloads

Here are all the files from this project available for download:



References
  1. Wikipedia definition of Pulse-width modulation
  2. Wikipedia definition of Linear feedback shift register