# Blinking LED with Pulse train

Hi

I was wondering if it would be possible to blink 2 LEDs (connected on 2 PWM pins) alternately at say 1.5Hz?

For example LED A is connected to pin 5 and B to 6.
Can they be programmed to blink alternately at 1.5Hz?

abuhafss:
Hi

I was wondering if it would be possible to blink 2 LEDs (connected on 2 PWM pins) alternately at say 1.5Hz?

For example LED A is connected to pin 5 and B to 6.
Can they be programmed to blink alternately at 1.5Hz?

Yes.

abuhafss:
Hi

I was wondering if it would be possible to blink 2 LEDs (connected on 2 PWM pins) alternately at say 1.5Hz?

For example LED A is connected to pin 5 and B to 6.
Can they be programmed to blink alternately at 1.5Hz?

Have you seen some of the awesome things that people can do with an Arduino? And you're really wondering if it is possible to blink two leds? Of course it is.

Only need one pin for that, even.

``````byte ledPin = 5;
void setup(){
pinMode (ledPin, OUTPUT);
}
void loop(){
digitalWrite (ledPin, HIGH);
delay (333);
digitalWrite (ledPin, LOW);
delay (333);
}
``````

ON time is 333 ms and the OFF time is 333 ms. The cycle period is 666 ms. That means -- the LED is blinking at 1.5015 Hz. But, the OP has asked for 1.5 Hz (exactly)?

So try it, and tweak the numbers a little.
The loop() { } timing uses a little time, so that will slow it a bit closer to 1.5 Hz.
Or use delayMicroseconds() with 333333 and 333334.

You really need to use a scope and tweak it to account for the loop() time.
Or go the PWM route, which I've never done.
Will tone() work at 1.5 Hz?

GolamMostafa:
ON time is 333 ms and the OFF time is 333 ms. The cycle period is 666 ms. That means -- the LED is blinking at 1.5015 Hz. But, the OP has asked for 1.5 Hz (exactly)?

What makes you think that the two digitalWrites and the looping take precisely zero time and so only the delays affect the timing of the loop (precisely)?

Steve

slipstick:
What makes you think that the two digitalWrites and the looping take precisely zero time and so only the delays affect the timing of the loop (precisely)?

It is the role of Post#3, 5 to adjust the ON/OFF timings so that the LED blinks exactly at 1.5 Hz as OP has desired?

If the meaning of 1 Hz stands for '1 event in one second', 1.5 Hz would mean '1 and 1/2 event' in one second. Having in account the finite time duration of digitalWrite() functions and the inherent loop, can the LED be really blinked at 1.5 Hz?

Hi

Is there any way to generate a tone lower than 31Hz?
I am looking for 2Hz.

No.

But you can just flash it with code.

Do not cross post, it is not a different question from flashing at 1.5Hz

``````byte ledPin = 5;
void setup(){
pinMode (ledPin, OUTPUT);
}
void loop(){
digitalWrite (ledPin, HIGH);
delay (250);
digitalWrite (ledPin, LOW);
delay (250);
}
``````

Look familiar? Did you want the code to do something else at the same time?

((((( Sorry the 2Hz post was mistakenly posted in a new thread ))))))))

Flashing with a delay command is beginners approach.
What I am looking is some standalone signal generation at any pin such that the micro-controller is free to perform other jobs.

The tone() command works perfectly but the lowest frequency is 31Hz.

abuhafss:
Flashing with a delay command is beginners approach.
What I am looking is some standalone signal generation at any pin such that the micro-controller is free to perform other jobs.

Then use Blink Without Delay approach. At 2Hz there is a lot of time left for the processor to do whatever else.

Please try to be a little more specific in your questioning. When you post vague one sentence questions with no context, you generally get vague short answers that don't fit into your context. YOU have to communicate what you actually need before anyone can actually help you. Anything short of that is just you wasting other people's time.

abuhafss:
Flashing with a delay command is beginners approach.

Most people who ask questions like if it is possible to blink two LEDs alternately ARE beginners.

And most of us aren't good at reading minds. If you don't write it, we don't know it.

Steve

Blink without delay style, here set for 20 KHz on D2. Adjust as necessarry for speed & other pins.

``````byte triggerOut = 2;

unsigned long currentMicros;
unsigned long nextMicros;
unsigned long duration = 25UL; // flip every 50uS = 20KHz pulse
unsigned long elapsedMicros;

void setup() {
pinMode (triggerOut, OUTPUT);
}
void loop() {
currentMicros = micros();
elapsedMicros = currentMicros - nextMicros;
if (elapsedMicros >= duration) {
nextMicros = nextMicros + duration;
PIND = PIND | 0b00000100; // toggle D2 output by writing it input port.
}
// do other stuff
}
``````

``````PIND = 0b00000100;