Getting a better shape for 500kHz square pulses

Hello. I tested a sketch taken from section 18.4 of the book ‘Arduino Cookbook’ by Michael Margolis.
See Page 612->

The sketch generates pulses within the frequency range of 1 MHz to 1 Hz using Timer1 PWM on pin 9. I tested it with Arduino UNO for 500kHz.

A picture of the pulses:

I used the TimerOne library (.cpp,.h) from:

The sketch:

#include <TimerOne.h>
#define pwmRegister OCR1A // the logical pin, can be set to OCR1B
const int outPin = 9; // the physical pin
long period = 2; // the period in microseconds
float pulseWidth = 1; // width of a pulse in microseconds
int prescale[] = {0,1,8,64,256,1024}; // the range of prescale values
void setup()
pinMode(outPin, OUTPUT);
Timer1.initialize(period); // initialize timer1, 1000 microseconds
void loop()
bool setPulseWidth(long microseconds)
bool ret = false;
int prescaleValue = prescale[Timer1.clockSelectBits];
// calculate time per counter tick in nanoseconds
long precision = (F_CPU / 128000) * prescaleValue ;
period = precision * ICR1 / 1000; // period in microseconds
if( microseconds < period)
int duty = map(microseconds, 0,period, 0,1024);
if( duty < 1)
duty = 1;
if(microseconds > 0 && duty < RESOLUTION)
Timer1.pwm(outPin, duty);
ret = true;
return ret;

I’m not sure but the shape of the square pulses could be product of internal RC circuit and do raises the question of whether is possible or not to shape up of the pulses using external electronic devices like 7414 Schmitt trigger or other to obtain cleaner pulses. Regards!

Besides the oscilloscope, what does the rest of your circuit look like? What are you driving with this pin?

I am driving a LED (red) in series with a 10K resistor. I haven't detailed the 10x probe of the scope.

I suppose the next question is:

Why are you driving an LED at 500kHz?

Unhook the LED and see what happens to the square wave. I have a suspicion that the LEDs turn off time (don't know the technical term that would be in the data sheet) is the culprit here.

Thank you jiggy-Ninja for your comments. I had the LED and resistor from a prior project. I'll be far from my office (and scope) this weekend but hoping to confirm your note on Monday. Regards!

Before you remove the LED put your finger on top of the LED (so it reflects onto itself) to see what happens.

The only way I can explain that trace is if the scope is connected to the junction of
the anode of the LED and the 10k resistor, the anode goes to ground and the other
end of the resistor to the pin.

When the pin goes to 5V the LED is pulled into conduction (Vf across it), when the
pin drops to 0V the storage charge and capacitance of the LED causes a slow
decay in voltage.

You are right regarding the LED connection.
My schematic:

I'll do the finger test on Monday and let you know.


Fit a short circuit across the LED and see what happens to to the signal. If the LED is offering capacitance to the circuit that will explain the tail-off AFTER the LED has stopped conducting (at around 2 volts or so). Because you have a 10k load resistor, even a minute amount of capacitance will produce the effect shown.

Alternatively replace the 10k by 220ohms and I'm certain you'll get back to a square wave.

Why are you worried about the wave shape BTW?

Try a 220 ohm resistor and you’ll get a much faster response (and more light).

Another thing to try is a 1N4148 across the 10k resistor, anode to ground.

Why the wave shape? I'd say for aesthetic reasons, or may be to reconcile theory and practice? Over 30 years ago, trying to obtain a 500kHz pulse train from a VCO (4046), I was only able to get an almost sinusoidal signal. :fearful:.
In his article 'Pulsed LED Characterization', Ed Nisley in Circuit Cellar #280 (Nov.2013) states,

...the exponential drop in forward voltage during the pulses shows the effect of junction temperature.

Is he right? I will discover that on Monday.


the exponential drop in forward voltage

If you are going to be measuring the dynamic change in forward voltage of the LED, you had first better place the LED where you will get proper readings.

Connect the output of the arduino to the resistor, the other end of the resistor to the anode of the LED, the cathode of the LED to gnd.
Now when you connect the CRO to the anode of the LED and the CRO gnd arduino grn, you will be measuring directly the forward voltage drop.

Your existing circuit is measuring the volt drop across the resistor which is proportional to LED current. You can with some maths work out the LED drop but it will, unless you measure the direct output from the arduino as well, not be accurate.

Tom...... 8) (42DegC, 18:30, 2/2/14)

As promises are made to keep, here some tests you requested in my UNO-LED-R pulses in order to get shaped square pulses.

With LED disconnected, that is, scope probe direct to P9: I got a shaped square signal! See picture #2.

With finger on the LED before remove it: the original shape doesn't change.

Shorting the LED: Same as picture #2.
Using 220 ohm. Same as picture #2.

With 1N4004 across the 10K resistor, anode to ground: I got a shaped square signal with LOW period >0. See picture #3.

I am not measuring dynamic changes in Vf. Just mentioned Nisley comment.

-The tail was created by the LED.

I should like to thank everyone who has contributed to this brief review. Palliser.