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:
TASK:
To produce a sawtooth wave from PWM signal using Arduino Nano.
PROCEDURE:
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)
{
while(1)
{
brightness+=5;
delay(100);
analogWrite(pwm_pin, brightness);
if(brightness>250)
{
digitalWrite(pwm_pin,LOW); // Statement:1 for sudden drop in output HIGH ---> LOW
brightness=0;
break;
}
}
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() {
brightness=upramp(brightness);
}
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:
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 |
circuit |
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
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 http://sim.okawa-denshi.jp/en/OPseikiLowkeisan.htm , just by specifying the cut-off frequency.
Below is the comparison of the output of sallen key and RC filter:
Below is the comparison of the output of sallen key and RC filter:
Yellow:- Output of Sallen Key LPF, Green :- Output of RC LPF |
Perfect!!!!
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???
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???
Comments
Post a Comment