Tuesday, January 11, 2022

Micro second pulse train generation using Arduino

A very fast pulse train generator can be made using Arduino if we let go of the default instructions of Arduino IDE. The digitalWrite() command has inbuilt steps which makes the execution altogether slower. using this instruction we cannot achieve a pulse train faster than 150 kHz.

If we make changes at register level, pulses as fast as 4 MHz can be generated using Arduino UNO which houses a 16 MHz oscillator.

void setup(){

pinMode (13, OUTPUT);

}
void loop(){
while (1){ // this skips the loop() processing in the background
PORTB = 0b00100000; // write D13 (on Uno) to a 1
delayMicroseconds (5);// 5us delay, I believe it needs to be multiples of 4 on an Uno
PORTB = 0b11011111; // D13 to a 0
delayMicroseconds (20); // 20us delay
} // end while()
} // end loop()


The resulting pulse train from above code is shown. we can see there is jitter in the pulses. This is due to the instruction pinMode() which add problem with the code.

Let us make slight change in the code as below.

void setup(){
//pinMode (13, OUTPUT);

DDRB |= 0B00100000;
cli();
TIMSK0 &= ~(1 << TOIE0);
sei();

}
void loop(){
while (1){ // this skips the loop() processing in the background
PORTB = 0b00100000; // write D13 (on Uno) to a 1
delayMicroseconds (5);// 5us delay, I believe it needs to be multiples of 4 on an Uno
PORTB = 0b11011111; // D13 to a 0
delayMicroseconds (20); // 20us delay
} // end while()
} // end loop()

 

The resulting pulse train is stable even with lower pulse width and higher frequency. We can go for lower value of delay but the output pulse width will not be accurate as can be seen below. For the same code above where Ton is 5us, I am getting pulse width of 4us.

I could reach a pulse width of 60 ns for width set at 1us in code. This is interesting to get such a narrow pulse using a UNO though not accurate.The cause of this inaccuracy at lower pulse width is not known to me.




 

No comments:

Post a Comment

Relation between dBm and dBV with 50 Ohm Impedance

Relationship Between dBm and dBV with 50 Ohm Impedance Relationship Between dBm and dBV with 50 Ohm Impedance ...