ARM, Software

DIY: Generating Ramp/Sawtooth from PWM

DIY: Generating Ramp/Sawtooth from a PWM signal

Lack of any good tutorial to produce a ramp/sawtooth wave from a PWM signal, urged me to make one! This post was in fact a part of one of my recent ongoing project i.e Redesigning the Semiconductor CURVE TRACER, here at CEDT, NSIT.
I was to produce a sawtooth wave from a pwm signal using arduino nano and here are my observations:
To produce a sawtooth wave from PWM signal using Arduino Nano.
Producing a ramp/sawtooth wave out of a PWM signal merely involves passing a PWM signal of varing duty cycle through a low pass filter.
Here’s the code I used :

/*Code for prodcing a Sawtooth wave from PWM*/
int pwm_pin = 9;          
int brightness = 0;  

int upramp(int brightness)
  analogWrite(pwm_pin, brightness);
  digitalWrite(pwm_pin,LOW); // Statement:1 for sudden drop in output HIGH ---> LOW
  return brightness;

// the setup routine runs once when you press reset:
void setup() {
  // declare pin 9 to be an output:
  pinMode(pwm_pin, OUTPUT);

// the loop routine runs over and over again forever:
void loop() {

This code being pretty straightforward, I am not going to explain it.(Do let me know in case of any query in the comment section below).
After our PWM signal is set up we need to look for a decent Low Pass filter, I tested both passive RC filters and active sallen key filter.
First let’s try RC filter:

RC filter:

By default PWM frequency of arduino nano is 490 Hz, so we need to design a RC LPF accordingly.
As per our rule of thumb, RC should be at least 10 times greater then 1/f , where f is our cut off frequency which is 490 hz. So accordingly, i choose resistor to be of 10k and capacitor of 100uf. 
caution: unnecessarily  higher values of resistor could cause larger voltage drop across it and very high value capacitance like 1000uf will completely average out the wave giving us almost constant dc.
Following was the output:
Oscilloscope settings: roll mode, dc coupling on both inputs

Yellow signal corresponds to the PWM wave while Green is of sawtooth like wave


One can also use this wonderful online design tool (with cutoff frequency=490hz) to predict resistance/capacitance values.

As one can easily make out that the output is closer to what we want but still it’s not perfect!! hmm…..

Now let’s try sallen Key active LPF

Sallen-Key LPF

Without going into details of it’s working, let’s dive into the design i used:

one can easily predict values i used using , just by specifying the cut-off frequency.
Below is the comparison of the output  of sallen key and RC filter:

Yellow:- Output of Sallen Key LPF, Green :- Output of RC LPF


Output values of Sallen-key LPF

Output values of RC LPF
Actual circuit
But, before leaving just have a look at fall time in case of RC LPF and Sallen Key LPF. Also, contrast between Vmax and Vmin in both cases.
Apart from the crisp output, Sallen Key can also be configured to provide gain to the output signal, thus providing an added advantage.
Do let me know in case of any query/feedback, would love to hear one!
If you like it don’t forget to share it.
At last, Feedback???

Tagged ,

About Udit kumar agarwal

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.