Arduino Tutorial: Flicker a LED

24,075

32

16

Introduction: Arduino Tutorial: Flicker a LED

Tired of all those other instructables about how to blink a LED? well how about flickering a LED? eh? there is a ible all about flickering a LED.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Wiring

Before we program the arduino we need to get everthing setup.

A) Plug in your arduino to the computer Via USB cable.
B) Add led positive lead to pin 10 on digitial, then with negitive lead to the GND (ground) pin also on digitial
C) Run Arduino IDE software.
D) Go to next step for code.

using the picture below wire your arduino (incase you dont understand what i am getting at here)

Step 2: Ze Code

to make the led flicker you are first going to need the software to program the chip. go to arduino.cc for a full program and other examples. the code you see here should be copied and pasted into the IDE.

int ledPin = 10;
byte flicker[] = {180, 30, 89, 23, 255, 200, 90, 150, 60, 230, 180, 45, 90};

void setup()
{
pinMode(ledPin, OUTPUT);
}

void loop()
{
for(int i=0; i,7; i++)
{
analogWrite(ledPin, flicker[i]);
delay(2000);
}
}

Now just click the "upload" button and in a few seconds your arduino will be running the code. Ok in the next step I will explain the code.

Step 3: The Finial Product.

see how it flickers? thats basically what the code does!

Got questions? comments? concerns? list below!

Video
can not parse options from: type="application/x-shockwave-flash" width="400" height="300" data="http://www.flickr.com/apps/video/stewart.swf?v=71377" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"> <param name="flashvars" value="intl_lang=en-us&photo_secret=d3eedd10d0&photo_id=3521110370"></param> <param name="movie" value="http://www.flickr.com/apps/video/stewart.swf?v=71377"></param> <param name="bgcolor" value="#000000"></param> <param name="allowFullScreen" value="true"></param><embed type="application/x-shockwave-flash" src="http://www.flickr.com/apps/video/stewart.swf?v=71377" bgcolor="#000000" allowfullscreen="true" flashvars="intl_lang=en-us&photo_secret=d3eedd10d0&photo_id=3521110370" height="300" width="400"></embed></object>, {width:425, height:350}

Be the First to Share

    Recommendations

    • Raspberry Pi Contest 2020

      Raspberry Pi Contest 2020
    • Wearables Contest

      Wearables Contest
    • Fix It Contest

      Fix It Contest

    16 Discussions

    0
    Kodein
    Kodein

    2 years ago

    Hi! I Hope someone is still replying to this thread. I'm trying to make a(ny) light source flicker as if it is about to die (like a fluorescent tube), which means it has a lot more no light than the code above (and the code from Jazon Diaz below in the comments) allow for. I am tweaking the values, but don't seem to get anywhere, as the LED is just showing constant light with a slight flicker. I am interested in that the light turns all the way off at a 'regular' interval.

    Can anybody help me figure this out?

    Thanks in advance!

    0
    Jason Diaz
    Jason Diaz

    6 years ago on Introduction

    I know this is really old, but I found a nice trick using digital pin 13 and no PWM, you will need to download the TrueRandom library:

    #include <TrueRandom.h>

    const int ledPin = 13; // the number of the LED pin

    boolean toggle = true;

    void setup() {

    // set the digital pin as output:

    pinMode(ledPin, OUTPUT);

    }

    void loop()

    {

    //rand represents a random delay time between toggling on/off

    //Larger numbers means more "flicker" here

    int rand = TrueRandom.random(100,10000);

    //This code turns the LED on or off depending on our toggle value

    if(toggle){

    digitalWrite(ledPin, HIGH);

    }

    else{

    digitalWrite(ledPin, LOW);

    }

    //toggle the LED on or off

    toggle = toggle?false:true;

    delayMicroseconds(rand);

    }

    0
    D5quar3
    D5quar3

    9 years ago on Step 2

    This code is a lot easier it might not look as nice but i uploaded the code and it didn't work here is mine int ledPin = 13; // LED connected to digital pin 13 // The setup() method runs once, when the sketch starts void setup() { // initialize the digital pin as an output: pinMode(ledPin, OUTPUT); } // the loop() method runs over and over again, // as long as the Arduino has power void loop() { digitalWrite(ledPin, HIGH); // set the LED on delay(35); // wait a thirty-fifth of a second digitalWrite(ledPin, LOW); // set the LED off delay(35); // wait a thirty-fifth of a second }

    0
    Microbe
    Microbe

    Reply 8 years ago on Step 2

    @D5quar3, but your code is only flashing. The original does work and although the randomness could be improved it does "flicker"

    0
    oweng4000
    oweng4000

    9 years ago on Introduction

    #include void setup(){ } void loop(){ analogWrite(9, TrueRandom.random(0,255)); delay(10); } this is the code I used it requires the TrueRandom library(http://code.google.com/p/tinkerit/wiki/TrueRandom), but the result is rather convincing.

    0
    gotcha99
    gotcha99

    10 years ago on Introduction

    Hey there from Germany.
    I've got a problem.
    After about 3 seconds the LED stops lighting.
    Then after about ten seconds it lights again for about 2 minutes.
    After that, the same thing starts again!

    I'm using an 220 Ohm resistor to not to destroy the LED.
    I used the code from above without editing it (only the delay = 75ms).

    Can anybody help me???
    Thanks, gotcha99

    0
    d-lite
    d-lite

    10 years ago on Introduction

    Are you putting the LED directly across the 5-volt output? Hopefully you are lucky enough for the LED to blow before the Arduino gets wrecked!

    0
    TXTCLA55
    TXTCLA55

    Reply 10 years ago on Introduction

    dont worry. just make sure u use a 5 volt led or a resistor.

    0
    ReCreate
    ReCreate

    Reply 10 years ago on Introduction

    there is no such 5 volt led...any 5v leds just have a tiny resistor in them...

    0
    Derin
    Derin

    Reply 10 years ago on Introduction

    It would be better to use 13 since there is a LED on the board for Duemilanove and Diecimila.I use that when I need to led there be light.

    0
    TXTCLA55
    TXTCLA55

    Reply 10 years ago on Introduction

    No. Pin 13 may be a better choice, and you can edit the code if you like so the led can flash through there, but if you use a 1k resisor before the LED on pin 11 you will be fine.

    0
    ajitam
    ajitam

    10 years ago on Introduction

    yeah I think 13 pin is more wise choise

    0
    qs
    qs

    Reply 10 years ago on Introduction

    The sketch will not run properly on pin 13 - it needs a PWM port for the 'flicker'. Make sure you put a 180-330 ohm resistor before the LED to protect it and the Arduino, which is only rated for 40mA before it's zapped!

    0
    ajitam
    ajitam

    Reply 10 years ago on Introduction

    if I understand correctly this is because we putting analog output ?

    0
    TXTCLA55
    TXTCLA55

    Reply 10 years ago on Introduction

    correct. but the analog is actually a list of different frequencies rather than physical input.

    0
    TXTCLA55
    TXTCLA55

    Reply 10 years ago on Introduction

    thanks for clearing this up. I first tried the sketch with a 1.5 volt LED....sparks flew! The one in the pictures is actualy rated for 5 volts so it needs no resistor.