Can't get function generator stable

Hi All,

I’m trying to build a function generator with the arduino (based on

It uses an 8-bit resistor ladder to convert the digital signals from PORTD to an analog value.
In general it works fine, only issue is that the signal is not stable.
On a scope it looks like every so often it skips a beat causing the next output value to be delayed for a loop if the script.
By using a timer interrupt on timer1 I was expecting a smooth signal, but it does not seems to work that way.

I’m hoping someone can point me in the right direction to smooth out the signal.


portb-test.ino (87.6 KB)

Running interrupts at that rate, I'd want to make sure you're not taking too long in the ISR. I'd suggest setting a single output bit when you enter the ISR, and clearing it when you exit. You should see a VERY consistent pulse rate on that pin, but my guess is you won't. That would indicate that you're sometimes late getting to the ISR, or spending too much time IN the ISR, and missing the next interrupt. You could also try just reducing the interrupt rate, and see if the problem goes away. I'm guessing it will.

Also, you have several variables that you use in the ISR, that are set in loop(), that are not declared volatile. That will lead to problems when you eventually start doing something more with them than simply re-setting them to their current value.

Regards, Ray L.

There are more functions operating using interrupts like millis(). Once an interrupt service is active (like for example millis()) yours has to wait until it is finished.... That could cause some glitches..

Yes, its the timer0 interrupts used for delay/millis that are causing the glitches. Once an ISR is active other interrupts have to wait for it to complete.

If you can live without timer0 interrupts then its easy to turn them off:

  TIMSK0 = 0 ;

Tanks for the replies guys.

It appeared that the source of the instability was the pot on A0. After i set a minimum change in the frequency of 25 the signal became very stable.