Go Down

Topic: who can blink LED  like sine wave ??? (Read 12644 times) previous topic - next topic



Jeff K

I am going to assume you don't actually mean you want to blink an LED but make it's intensity (brightness) change following the curve of a sine wave.

I don't have any code examples.  But you should probably do some research on just generating sine waves first.  It seems some people use lookup tables that have been pre generated.  Essentially you would find the equation for your sine wave. Select 255 (or other depending on resolution) evenly divided points across the sine wave.  Get values for these points (should be 0 - 255)... . then store these in the memory of the Arduino.

You would then just loop through these values and apply them to the  output the LED is on.
Jeff K - JKDevices.com - home of the MegaMini


Or just use the sin() function..

Code: [Select]
void loop()
 float something = millis()/1000.0;
 int value = 128.0 + 128 * sin( something * 2.0 * PI  );

Where something is from 0 to 1 (ideally, or a multiple of), and is whatever you base the sin on. Note the 1000[glow].0[/glow], to make it a float calculation (since we need the decimals).

You might have to restrict the int value in the 0 - 255 range to prevent wrap to 0 instead of 256.

And you must use a PWM output pin for analogWrite to work.



raron , the led doesn't blink like sine wave


Jun 20, 2010, 01:55 pm Last Edit: Jun 20, 2010, 02:01 pm by raron Reason: 1
Did you use a PWM output? It works for me.
I don't have a duemilanove, but it should'nt matter.

I forgot to say that the millis() / 1000.0 was for a 1000 ms (as in 1 second) period.


Don't forget to compensate for the eye's logarithmic respose, or you won't perceive a true sine brightnesss curve.
Per Arduino ad Astra


raron, no I haven't connected to pwm,how can I connect it and I'm a beginner


The Duemilanove:

As you can see, not all the pins can provide a PWM output. Only pins 3, 5, 6, 9, 10 and  11. You must use one of these for analogWrite() to work.

That is for a hardware PWM (built-in the arduino). Software PWM is certainly possible too, on any pin, but the program would be more complex and use more processor time. I thought I saw a software PWM library somewhere, but I can't find it at the moment.

Btw, PWM (Pulse Width Modulation) is really a trick, it's not an analogue signal. It's digital, but as the name implies, its pulsing and the width of these pulses are what varies. The frequency on the arduino is about 450-500 Hz, so you wont see any flicker is you connect it to a LED.

Oh and you use a resistor in series with the LED I hope?

Don't forget to compensate for the eye's logarithmic respose, or you won't perceive a true sine brightnesss curve.

True, but I think we can forgo that for now? :P


so  what is the complete code I can upload to the board??? and how can I connect it ??


Well the only thing missing from the code above is basically the setup() routine, like this: http://arduino.cc/en/Reference/Setup
Modify as required (little exercise for you there ;) ). Then just follow it with the loop() function, as in any arduino sketch: http://arduino.cc/en/Tutorial/Sketch

As for how to connect the LED, do it like this: http://www.makingthings.com/documentation/how-to/connect-an-led-to-a-digital-output
Doesn't matterJust remember to use a PWM output.


int Pin = 3;

void setup()
 pinMode(Pin, INPUT);

void loop()
 float something = millis()/5000.0;
 int value = 128.0 + 128 * sin( something * 2.0 * PI  );

I got the code, so amazing , thx raron so much


just to comment..


awesome work !

SO many time do I see beginners talked down to.. or just put off with tech babel or random links with no help explaining what they are being linked to.

KUDOS man!

Delivery of good info..

links to help understand what was just explained to him..

example code

and even a quick 'homework' assignment for him to not be spoon fed as much.

A+ help!!!!!


Go Up