• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here: Home » Arduino LED Candle : Some Codes to Help You

By Abhishek Ghosh December 2, 2021 7:17 pm Updated on December 2, 2021

Arduino LED Candle : Some Codes to Help You

Advertisement

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 :

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.

Advertisement

---

The second one is the below one, with 4 LEDs :

/**
  ******************************************************************************
  * @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

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.

This Article Has Been Shared 544 Times!

Facebook Twitter Pinterest
Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to Arduino LED Candle : Some Codes to Help You

  • Arduino With DHT 11 Sensor and Arduino Online IDE : Basic IoT

    Arduino With DHT 11 Sensor and Arduino Online IDE is Example of Basic IoT Which Needs No Special Hardware But Arduino, DHT11, Internet Connection & Web Browser.

  • Extra Battery, Inverter For Car For DIY Electronics Car Automation

    We Need More Current to Run Raspberry Pi, Monitors, Sensors. Here is Guide to Setup Extra Battery, Inverter For Car For DIY Electronics Car Automation.

  • What is SPI? – Serial Peripheral Interface

    What is SPI? SPI stands for Serial Peripheral Interface, it is a synchronous serial data bus named by Motorola in 1980s.

  • 74HC595 Shift Register Theory For Arduino Tutorials

    With Shift Register, You Can Use One Arduino Pin To Blink Multiple LED. Here is 74HC595 Shift Register Theory For Arduino Tutorials.

  • How to Build ESP32 Arduino Glass Touch Switch With LED

    These days, glass touch panels are becoming common even for the household switch boards. Here is How to Build ESP32 Arduino Glass Touch Switch With LED.

Additionally, performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (22.1K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • How to Make the Most of Your S Pen (S22 Ultra) June 29, 2022
  • Safe Chargers for Samsung Galaxy S22 Ultra June 27, 2022
  • How Telecoms Can Use The Cloud To Power Their 5G Network June 24, 2022
  • A Beginner Guide to Cloud Computing for Development June 22, 2022
  • 5 Benefits of Using a Virtual Data Room Today June 19, 2022

About This Article

Cite this article as: Abhishek Ghosh, "Arduino LED Candle : Some Codes to Help You," in The Customize Windows, December 2, 2021, June 30, 2022, https://thecustomizewindows.com/2021/12/arduino-led-candle-some-codes-to-help-you/.

Source:The Customize Windows, JiMA.in

This website uses cookies. If you do not want to allow us to use cookies and/or non-personalized Ads, kindly clear browser cookies after closing this webpage.

Read Privacy Policy.

PC users can consult Corrine Chorney for Security.

Want to know more about us? Read Notability and Mentions & Our Setup.

Copyright © 2022 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy