Advertisement
Arduino lights circuit

Arduino Camping LED Light

Here is a simple Arduino Camping Light project. In this design, only one push button switch is used to control the light output (from a bunch of white LEDs) between off, dim, medium, and full brightness. Further, if you hold down the push button the light output will cycle between brightnesses. The whole system can be powered from any 12VDC power source, for instance from an automobile battery.

Schematic of the Arduino Camping Lights Circuit

Arduino lights circuit

Light Dimming Levels

arduino dimming levels

Arduino Sketch Code

/*
* Arduino Camping Light
* Camping Light with a single push button switch to control 
the light output between off, dim, medium, and high brightness
* Test Configuration:
MCU: Arduino UNO R3
IDE: Arduino 0022
SIMULATOR: Virtronix-Simulator For Arduino
PROTOTYPE: Build & Tested @ TechNode Engineering Electronics
DATE: 12 February 2014
*/
const int switchPin = 8; 
const int lightPin = 10; 
int lightMode = 1; 
void setup() 
{
    pinMode(lightPin, OUTPUT);
    pinMode(switchPin, INPUT);
    digitalWrite(switchPin, HIGH); // internal Pull-up enabled
}
void loop()
{ 
    if (digitalRead(switchPin) ==LOW) 
    { 
        lightMode = lightMode + 1;
        if (lightMode == 5)
        {
            lightMode = 1;
        }
    }
    if (lightMode == 1)
    {
        digitalWrite(lightPin, LOW);
    }
    else if (lightMode == 2)
    {
        analogWrite(lightPin, 64);
    }
    else if (lightMode == 3)
    {
        analogWrite(lightPin, 128);
    }
    else
    {
        digitalWrite(lightPin, HIGH);
    }
    delay(250); // see text
}

Arduino simulator window

arduino simulator

The Arduino Sketch (code) is simple and flexible. The Delay () at the end of the Loop procedure is added deliberately to avoid possible switching errors. The delay of 250ms woks fine in my prototype. However, try to modify this as per your requirement, in case of any switching malfunctions in front of you. Similarly, one BS170 mosfet was used in my
prototype to drive the 3 x White LEDs strip. This part number (and ofcourse the LED type) is not very critical. If you want to use a bi-polar transistor, don’t forget to add a resistor (~1KΩ ) between Arduino D10 and the base terminal of the transistor.

7 Comments

Join the conversation!

Error! Please fill all fields.
  • engineerfaizanhamayungmail-com

    I am doing a LED dimmer program with npn transistor tip122 with no resistor at base and LED +ve connected to 12volt switching adapter and LED -ve connected to collector and emitter connected to ground.
    The issue i am facing is their is a flickering going on coming from attiny85 controller the LED flickering little bit if seen closely.IS their any possibility to get smooth output.
    With arduino noisy sound coming from arduino board what to do in order to get rid of it.

  • engineerfaizanhamayungmail-com

    Any updates regarding the code update?

  • engineerfaizanhamayungmail-com

    I will wait for the code to be update.

  • engineerfaizanhamayungmail-com

    Hi i have seen this code it is really good work.
    I want to modify this code using two button dimming can it is possible to have such thing i have tried but it works only on one button .

    • T.K.Hareendran

      It’s possible by changing the code. I will try to post an updated code for two buttons (+/-) within a week or so. Thanks!

  • Tonnny Sørland

    I like the article very much.

  • Tonnny Sørland

    Hello, I’m a greenhorn on Arduino.
    I see this application can solve my dream of doing some home automation.

    I tried to multiply the code to do to separate channels, by copy the code and duplicate it with an another address, but no luck. When I test it, button 1 increase both channels but turns off only channel 1. Button 2 has no influence at all. I would like to duplicate from one channel(lights) to 4 and 6. How can I do that? And if someone see what I’m doing wrong I’ll be appreciated if you would explane to me where I’m lost.

    The code:

    /*
    Increase LED Brightness with Reset

    Each press of the button increases the LED brightness by 10%.
    Press and hold the button for 3 seconds to reset the LED to 0%.

    created 2012
    by Aaron Van Bokhoven
    */

    const int buttonPin1 = 2; // number of the button pin
    const int buttonPin2 = 4; // number of the button pin
    const int ledPin1 = 9; // number of the LED pin
    const int ledPin2 = 10; // number of the LED pin

    int buttonState1 = 0; // button1 state starts at 0
    int brightness1 = 0; // brightness1 of LED starts at 0
    int fadeAmount1 = 25.50; // fadeAmount1 is equal to 10% of 255(max LED brightness)
    int buttonPressedCounter1 = 0; // timer1 set to 0 whe depressing button
    int buttonCounter1 = 0; // button1 counter set to 0
    int buttonLastState1 = 0; // button1 last state set to 0

    int buttonState2 = 0; // button2 state starts at 0
    int brightness2 = 0; // brightness2 of LED starts at 0
    int fadeAmount2 = 25.50; // fadeAmount2 is equal to 10% of 255(max LED brightness)
    int buttonPressedCounter2 = 0; // timer2 set to 0 whe depressing button
    int buttonCounter2 = 0; // button2 counter set to 0
    int buttonLastState2 = 0; // button2 last state set to 0

    void setup() {
    // set button as input
    pinMode(buttonPin1, INPUT);
    // set LED as output
    pinMode(ledPin1, OUTPUT);
    // set button as input
    pinMode(buttonPin2, INPUT);
    // set LED as output
    pinMode(ledPin2, OUTPUT);
    // init serialize communication at 9600 bits per second
    Serial.begin(9600);
    }

    void loop() {
    // get button state
    buttonState1 = digitalRead(buttonPin1);

    // activate when button is being held down
    if (buttonState1 == HIGH) {
    buttonPressedCounter1 ++; // start counter
    Serial.print(“Pressed Timer1: “);
    Serial.println(buttonPressedCounter1);
    // if counter equals 100 turn off LED and reset counter to 0
    if (buttonPressedCounter1 == 50) {
    buttonCounter1 = 0;
    analogWrite(ledPin1, 0); // reset LED
    }
    }
    // get button state2
    buttonState2 = digitalRead(buttonPin1);

    // activate when button is being held down
    if (buttonState2 == HIGH) {
    buttonPressedCounter2 ++; // start counter
    Serial.print(“Pressed Timer2: “);
    Serial.println(buttonPressedCounter2);
    // if counter equals 100 turn off LED and reset counter to 0
    if (buttonPressedCounter2 == 50) {
    buttonCounter2 = 0;
    analogWrite(ledPin2, 0); // reset LED
    }
    }
    else {
    // on button depress, set counter to 0
    buttonPressedCounter1 = 0;
    }

    // activate once to increase button counter, which adjusts the LED brightness
    if (buttonState1 != buttonLastState1) {
    if (buttonState1 == HIGH) {
    // increase counter only if less than 10
    if (buttonCounter1 < 10) {
    buttonCounter1 ++;
    }
    Serial.print("LED Level1: ");
    Serial.println(buttonCounter1);
    analogWrite(ledPin1, buttonCounter1 * fadeAmount1); // adjust LED brightness to buttoncounter * fadeamount
    }
    buttonLastState1 = buttonState1;
    }
    else {
    // on button depress, set counter to 0
    buttonPressedCounter2 = 0;
    }

    // activate once to increase button counter, which adjusts the LED brightness
    if (buttonState2 != buttonLastState2) {
    if (buttonState2 == HIGH) {
    // increase counter only if less than 10
    if (buttonCounter2 < 10) {
    buttonCounter2 ++;
    }
    Serial.print("LED Level2: ");
    Serial.println(buttonCounter2);
    analogWrite(ledPin2, buttonCounter2 * fadeAmount2); // adjust LED brightness to buttoncounter * fadeamount
    }
    buttonLastState2 = buttonState2;
    }
    }

Looking for the latest from TI?