Simple Candles

I’m always searching for exciting new and fun things to do with microMighty and with the holidays rapidly approaching, I thought I’d put together a quick festive demonstration: Introducing the 12 line flameless candle. 


The simple candle implementation requires nothing more than a microMighty, but I happened to have a photoresistor lying around the lab so I threw in some light sensitivity too.    First, I took my photoresistor (pictured to the left) and connected it to microMighty.  The pin marked "S" is the output of the photoresistor (the signal), and the pin marked "-" is the ground supply.  Since microMighty's connector is wired as (+5V, GND, D0, ...) I cheated a little and reversed the ground a power for this demonstration.  Won't that break the photoresistor? Nope.  I normally don't recommend reversing the power supply on a sensor, but the photoresistor reduces it's resistance as light is applied, so in this case, the signal value will increase with applied light (normally the signal gets smaller with applied light).

Then I wrote some code to read the photorestor and generate the flame effect using the on-board red and yellow LEDs. The 12 lines break down as follows:

  • 2 lines to initialize pins for the LEDs and photoresistor.  
  • 2 lines to generate the candle effect
  • 1 line for delay to control the update rate
  • 2 lines for the loop
  • 5 lines to read the photoresistor and shut off the LEDs when there’s light in the room

Why so much code to shut off the candle effect? Well, it is one if-then-else statement plus the two lines to turn off the effect, so.. 5 lines. So let’s take a look at the actual code and break it down to find out how it works. 

01: {, pin.yellow}, pin.mode.PWM) 
02: 0, pin.mode.ANALOG ) 

These two lines initialize the pin IO modes. The red and yellow LEDs are initialized to use the PWM (pulse-width modulator) is that we can control the brightness. Next pin D0 is initialized to be an analog input. This tells microMighty that we want to read a concerted analog voltage as a value from 0 to 4095 rather than a digital signal ( 0 or 1). This is great news since the photoresistor board that I have had an analog output, so we can use the analog input pin to check how much light is in the room.  

03: repeat

The third line in our project just sets the starting point of the repeat-until block that is the main loop of the project.  

04: if 0 ) < 3000 then

Line four is where some of the magic happens in the application. This conditional statement says that when the value of pin D0 is less-than 3000, the lines of code immediately following the statement will be executed. What is the value of D0? Ah yes, that’s where we connected the photoresistor. Since the resistance of the photoresistor changes it's resistance with applied light, this value tells the application approximately how much light is in the room. The circuit for the photoresistor is a voltage divider with one resistor fixed so as the resistance of the photoresistor changes, the voltage at the measurement point of the divider changes.  The value of 3000 comes from the expected light level of a dimly lit room.  It's pretty easy to tell what the value should be by reading the pin value with and without light.  Using the call while shining a light on the photoresistor, microMighty returns a value of 4095.  Placing a finger over the photoresistor and reading the value returns a value of 1517.  This tells us that as the room gets dimmer the voltage decreases, and the value for a dimly lit room is somewhere between the two experimental readings.

05: pin.write(, cpu.rand(50, 750) )
06: pin.write(pin.yellow, cpu.rand(50, 750) )

Lines 5 and 6 are executed only when the room is dim, and they generate the flickering flame effect.  Since the red and yellow LEDs have been opened as pulse-width modulated outputs, the pin.write() function sets the duty-cycle of the output.  Since the duty-cycle of the output affects the brightness of the LED, setting the value to a random value will randomize the brightness of the yellow and red LEDs.

07: else
08: pin.write(, 0)
09: pin.write(pin.yellow, 0)
10: end

Lines 7 through 10 are executed when the room is brighter than the threshold that was set in the if statement of line 4.  Writing zero to the pins for the LEDs sets the duty cycle to 0, which shuts off the LEDs.

11: cpu.delay( cpu.rand(50, 250) )

Line 11 delays a random length of time between 50 and 250 milliseconds. The call to cpu.delay() uses the internal hardware to delay for a set number of milliseconds. How many milliseconds to delay? Since I wanted to emulate a flame flicker, delaying a random time changes the speed at which the brightness of the LEDs are changing. 

12: until serial.available(0) > 0

Line 12 is the end of the repeat-until loop. I’m using the serial.available() function to wait until there is data available on the serial port. Serial 0 is the USB, and since I was entering everything in the  REPL for This project, I’m waiting for data from the USB toe end the application.  

That’s it. After entering the last line, microMighty gets to work running the code and the LEDs start flickering like a flame.