Thanks to China for making various designs of LED candles available all over the world. Indeed, most of these LED candles are not closest to the real candle, but they do give us the idea to think around playing around creating our microcontroller controlled LED candle. I was looking for a realistic feel and soon discovered that two people in project sites already worked a lot behind the creation of realistic looking candles. In this guide, we will share the codes and show you the emulation on Tinkercad.
The first LED candle is with one LED :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | int ledPin = 0; // Pin the LED is connected to int pulseMin = 1; // Minimum number of pulses in each set int pulseMax = 5; // Maximum number of pulses in each set int brightMin = 92; // Baseline LED brightness. Cannot exceed 128. int minDelay = 1000; // Minimum delay between pulse sets (ms) int maxDelay = 5000; // Maximum delay between pulse sets (ms) void setup() { randomSeed (analogRead (3)); // Randomise pinMode(ledPin, OUTPUT); // Sets LED Pin to be an output } void loop() { // For loop sets the number of pulses and repeats these pulses for (int x = random(pulseMin, pulseMax); x > 0; x-- ) { int bright = 224 - random(96); // Sets a random maximum brightness level for this pulse // For loop raises the brightness of the LED from minimum to maximum value for (int y = brightMin; y < bright ; y++) { analogWrite(ledPin, y); delay(3); } // For loop lowers the brightness of the LED from maximum to minimum value for (int y = bright; y > brightMin; y--) { analogWrite(ledPin, y); delay(3); } delay(10); // Adds a delay between pulses to make them more visible } analogWrite(ledPin, brightMin); delay(random(minDelay,maxDelay)); // Adds a delay between pulse sets } /* LED Candle Makes an LED pulse randomly to simulate the flame of a candle. Takes a series of values and randomly generates sets of pulses with varied brightness and frequency within these values. "by A Green for x2Jiggy.com" */ |
You can look at this emulation on TinkerCAD. Click the “Simulate” button to start the animation. To create the circuit with Arduino, you need to connect digital pin 0 with one LED and a 220 Ohm resistor.
---
The second one is the below one, with 4 LEDs :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | /** ****************************************************************************** * @file pumpkin.ino * @author Ryan DeWitt * @version V2 * @date 21-Oct-2018 * @brief Realistically simulates a candle flame using four LEDs ****************************************************************************** Copyright (c) 2018 Ryan DeWitt (Pie Thrower) All rights reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************** */ #include // First red LED int ledR1 = 0; // First yellow LED int ledY1 = 1; // Second red LED int ledR2 = 2; // Second yellow LED int ledY2 = 3; // Current LED Strength int curStrength = 120; // Maximum amplitude of flickering int maxAmp = 100; // Milliseconds per frame int frameLength = 10; void setup() { // Pin configuration pinMode(ledR1, OUTPUT); pinMode(ledY1, OUTPUT); pinMode(ledR2, OUTPUT); pinMode(ledY2, OUTPUT); } void loop() { // Keep the lights flickering flicker(); } void flicker() { // Amplitude of flickering int amp = random(maxAmp)+1; // Length of flickering in milliseconds int length = random(10000/amp)+1000; // Strength to go toward int endStrength = random(255-(amp/4))+(amp/4); // Flicker function flickerSection(length, amp, endStrength); } void flickerSection(int length, int amp, int endStrength) { // Initilize variables int i, max, min; double oldStrengthRatio, endStrengthRatio; // Number of frames to loop through int frameNum = length/frameLength; // Use variable to hold the old LED strength int oldStrength = curStrength; // Loop times for(i = 0; i <= frameNum; i += 1){ // The ratio of the old/end strengths should be proprtional to the ratio of total frames/current frames // Use type casting to allow decimals oldStrengthRatio = (1-(double)i/(double)frameNum); endStrengthRatio = ((double)i/(double)frameNum); // Fade current LED strength from the old value to the end value curStrength = (oldStrength*oldStrengthRatio) + (endStrength*endStrengthRatio); // LED brightnesses must be in between max and min values // Both values are half an amplitude's distance from the average max = curStrength+(amp/2); min = curStrength-(amp/2); // Light LEDs to random brightnesses setRandom(ledR1, max, min); setRandom(ledY1, max, min); setRandom(ledR2, max, min); setRandom(ledY2, max, min); // Wait until next frame delay(frameLength); } } void setRandom(int led, int max, int min) { // Set chosen LED to a random brightness between the maximum and minimum values analogWrite(led, random(max - min) + min); } |
![Arduino LED Candle](https://thecustomizewindows.cachefly.net/wp-content/uploads/2021/12/Arduino-LED-Candle-1.png)
You can watch the emulation here on TinkerCAD. Click the “Simulate” button to start the animation. To create the circuit with Arduino, you need to connect digital pin 0, pin 1, pin 2 and pin 3 with four LEDs and a 50 Ohm resistor.
Final Words on LED Candle
Both of the person behind the above codes (Flickering LED candle and Realistic LED Candle) worked too hard and there is a little space to modify their ideas.
Tagged With arduino flickering led code , https://thecustomizewindows com/2021/12/arduino-led-candle-some-codes-to-help-you/ , candle flicker microcontroller