Arduino Forum

Using Arduino => General Electronics => Topic started by: ilovetoflyfpv on Jun 14, 2017, 02:56 pm

Title: RE: Low Pass Filter
Post by: ilovetoflyfpv on Jun 14, 2017, 02:56 pm
Hi Gang

I wish to convert a PWM signal into voltage. I understand that the most straight forward approach is to create a low pass filter resistor capacitor circuit (RC). Probably the most helpful article I've found is Passive Low Pass Filter by Electronics Tutorials (http://www.electronics-tutorials.ws/filter/filter_2.html).

I'm confused however after crunching the numbers (see attached). The typical frequency of a Arduino PWM pin is 490Hz. If a create a RC circuit using a suggested 10µF capacitor and a 10kΩ resistor then according to my spreadsheet my Vout will be 0.16V which is obviously undesirable.

Could someone please tell me where I'm going wrong?

Cheers

Jase  :smiley-confuse:
Title: Re: RE: Low Pass Filter
Post by: septillion on Jun 14, 2017, 03:10 pm
You calculate the passing of the 490Hz signal, not the DC you want to calculate ;) So that's approximately the ripple. For the DC part, use 0Hz for the reactance ;)

But normally we look at the cut off frequency of the filter and the attenuation at the input frequency.

And you can increase the PWM frequency very easily and get away with a smaller RC filter.

Also, you do know this will give you a voltage but you may not pull current?
Title: Re: RE: Low Pass Filter
Post by: ilovetoflyfpv on Jun 14, 2017, 04:27 pm
Hi septillion

Thanks for getting back to me so promptly. I don't quite follow; could you rephrase your first paragraph?

Are you saying a PWM signal comprises of all frequencies up to 490Hz (see attached) and the filter 'highlights' the frequency we're interested in?

Cheers

Jase :)
Title: Re: RE: Low Pass Filter
Post by: aarg on Jun 14, 2017, 04:36 pm
Your rephrasing is a gross oversimplification, but basically true.
Title: Re: RE: Low Pass Filter
Post by: runaway_pancake on Jun 14, 2017, 05:21 pm
The typical frequency of a Arduino PWM pin is 490Hz.
490 Hz on 3, 9, 10, 11.
980 Hz on 5, 6.

https://www.arduino.cc/en/Reference/AnalogWrite (https://www.arduino.cc/en/Reference/AnalogWrite)
Title: Re: RE: Low Pass Filter
Post by: Jiggy-Ninja on Jun 14, 2017, 06:39 pm
Are you saying a PWM signal comprises of all frequencies up to 490Hz (see attached)
Right idea, wrong on the details.

One of the central mathematical principles behind circuit analysis is that any periodic function (read: waveform) can be represented by a sum of sine waves of various frequencies, amplitudes, and phases. If you are able to decompose a complex waveform into its individual frequency components, it's easy to calculate the effect a filter will have on the waveform since you apply the filter to each frequency component individually, then add them back up to get the result waveform.

So it's not "all frequencies up to 490 Hz". Most of the frequencies will actually be harmonics well above 490 Hz. You have the right idea though.

Quote
and the filter 'highlights' the frequency we're interested in?

Cheers

Jase :)
Not in the way you're expecting. The point of the low pass filter is to remove all off the frequency components as much as possible, leaving only the DC component behind.

But there's a tradeoff to this. Lowering the filter's critical frequency will reduce (but never eliminate) the effect of the higher frequency components, but it also means that the DC value will take longer to reach the intended level after it changes.

Depending on what you want to do with this signal, it might be better to get a DAC chip.
Title: Re: RE: Low Pass Filter
Post by: ilovetoflyfpv on Jun 15, 2017, 12:40 am
Hi Gang

Wow! Thanks so much for the replies. So a wave is comprised of frequencies (harmonics) both above and below the fundamental frequency. A low pass filter attenuates frequencies above the cutoff frequency (in my case I want an analog voltage so I'm therefore interested in frequencies below 1Hz). Am I on the right track?

So aside from the cutoff frequency are there any other things to consider when making a RC circuit? I was thinking I need to be mindful of the overall impedance of the circuit so as not to overdrive the PWM pin. Is there a method to selecting the capacitor resistor combination?

Really appreciate the help.

Cheers

Jase :)
Title: Re: RE: Low Pass Filter
Post by: MarkT on Jun 15, 2017, 12:57 am
By definition there is nothing below the fundamental frequency (other than a DC value).  All harmonics
are integer multiples of the fundamental frequency.

Yes, you are on the right track, a LP filter with a corner frequency of a few Hz would be typical for this sort of thing.
Yes the overall impedance is the other variable, together with the corner frequency that defines the
value of both R and C.
Title: Re: RE: Low Pass Filter
Post by: septillion on Jun 15, 2017, 01:10 am
You're indeed on the right track. And as Mark already stated, there is nothing under the fundamental besides DC. And if the wave is a pure sine there are even no fundamentals. A sine is the "most pure" form of a wave and every other wave can be composed out of multiple sines which is what gives you the harmonics.

And for now you're not interested in below 1Hz, you are interested at 0Hz exactly ;)

And besides the corner frequency the PWM frequency is an important factor. As I said, it's easy to increase that if the timer is not used for other things. By default timer0 is used for millis() and other timing so better not to touch that. But timer1 and timer 2 (so pins 3, 9, 10 and 11) are free to increase up to 62500Hz.

And also don't forget this:
Also, you do know this will give you a voltage but you may not pull current?
Title: Re: RE: Low Pass Filter
Post by: tinman13kup on Jun 15, 2017, 01:18 am
What are you trying to accomplish with this? Perhaps there is a better way.
Title: Re: RE: Low Pass Filter
Post by: Southpark on Jun 15, 2017, 01:35 am
Wow! Thanks so much for the replies. So a wave is comprised of frequencies (harmonics) both above and below the fundamental frequency.
Not quite. A periodic waveform can 'hypothetically' (theoretically) be 'considered' as a SUM OF sinusoidal signals (comprising a fundamental sinusoidal signal and harmonics of it), plus maybe a constant value (if there is a DC offset). This is the Fourier Series concept. Note that it does NOT mean that the waveform IS physically a sum of these things. It's a hypothetical (mathematical model) equivalence...... that allows people to analyse a waveform in one way.
Title: Re: RE: Low Pass Filter
Post by: Power_Broker on Jun 15, 2017, 02:18 am
This is the Fourier Series concept. Note that it does NOT mean that the waveform IS physically a sum of these things.
I don't see why it isn't (assuming, of course, that the waveform is truly periodic and exists from time t = -infinity to time t = infinity). Either way, there's no need to confuse OP. For all intents and purposes, a truly periodic signal IS physically a sum of the harmonics found by computing the Fourier Series.


One thing to note is that if the PWM signal changes duty-cycle, the overall signal is no longer periodic from t = -infinity to t = infinity. Therefore, it is better to analyze the signal using the Fourier Transform instead of the Fourier Series. Not like this changes the filter circuit much, but it is something to keep in mind.

OP: google envelope detector for more info.
Title: Re: RE: Low Pass Filter
Post by: Southpark on Jun 15, 2017, 02:37 am
I don't see why it isn't (assuming, of course, that the waveform is truly periodic and exists from time t = -infinity to time t = infinity). Either way, there's no need to confuse OP. For all intents and purposes, a truly periodic signal IS physically a sum of the harmonics found by computing the Fourier Series.
It isn't 'physically' a sum of those things. Because.... physically, the physical signal in the time domain is 'The time domain Signal'.... which ...in this case.... is a periodic 2-level waveform (when the input is held at a constant value that is). Not 'physically' a bunch of sinusoids (plus a DC shift, if there is one).

Although, 'hypothetically'...... if we had the time to do it.... you could physically generate a bunch of sinusoids at the suitable frequencies (together with suitable amplitudes) ...... and add them up..... to come up with an "approximation" for the time waveform.

In the lab, you're not going to be able to generate an infinite number of harmonics. It's like e^(-t)..... it never goes to 'zero' until time reaches infinity.....and infinity means you'll never get there....because it keeps going ....forever.

It wasn't a case of confusing the O.P. It's a case of avoiding confusion right from the start.
Title: Re: RE: Low Pass Filter
Post by: Power_Broker on Jun 15, 2017, 02:52 am
Because.... physically, the physical signal in the time domain is 'The time domain Signal'.... which ...in this case.... is a periodic 2-level waveform (when the input is held at a constant value that is). Not a bunch of sinusoids (plus a DC shift, if there is one).
I'm not saying that the PWM signal is generated from the Arduino is an infinite number of sinusoidal voltage sources in series (quite absurd isn't it lol), but you can't deny that the physical signal can still be analyzed and broken down into an infinite sum of sinusoids. To say that a physical signal doesn't follow this property is to say that Fourier and his findings were wrong.


Also, if you look at a PWM signal specifically, even though it is a "2-level waveform", it still is comprised of sinusoids. Take a look at this (http://mathworld.wolfram.com/FourierSeriesSquareWave.html).

And this, too:
(http://www.dspguide.com/graphics/F_13_10.gif)
Title: Re: RE: Low Pass Filter
Post by: Southpark on Jun 15, 2017, 02:57 am
Also, if you look at a PWM signal specifically, even though it is a "2-level waveform", it still is comprised of sinusoids.
NOPE.... it is NOT physically comprised of sinusoids. The waveform can mathematically be 'expressed' as a bunch of sinusoidal waveforms. It does NOT mean that the physical waveform itself (in this case, the PWM waveform coming out of an arduino) IS a bunch of physical sinusoidal signals (plus a DC value). The sinusoids you're talking about come from the realms of mathematical modelling.
Title: Re: RE: Low Pass Filter
Post by: Power_Broker on Jun 15, 2017, 03:12 am
NOPE.... it is NOT physically comprised of sinusoids. The waveform can mathematically be 'expressed' as a bunch of sinusoidal waveforms. It does NOT mean that the physical waveform itself (in this case, the PWM waveform coming out of an arduino) IS a bunch of physical sinusoidal signals (plus a DC value). The sinusoids you're talking about come from the realms of mathematical modelling.
If you were correct, wouldn't you expect the FFT functions on an oscilliscope to either not work or not be trusted when used on physical signals, assuming the signals couldn't be broken down into a sum of sinusoids? I'm still not sure where the "Fourier Series/Transform is only purely conceptual" rationale is coming from. Do you have any sources to support your claim? I'm truly curious - maybe I'm misunderstanding what you're saying.
Title: Re: RE: Low Pass Filter
Post by: Jiggy-Ninja on Jun 15, 2017, 03:27 am
NOPE.... it is NOT physically comprised of sinusoids. The waveform can mathematically be 'expressed' as a bunch of sinusoidal waveforms. It does NOT mean that the physical waveform itself (in this case, the PWM waveform coming out of an arduino) IS a bunch of physical sinusoidal signals (plus a DC value). The sinusoids you're talking about come from the realms of mathematical modelling.
Is there a point to being that anal about it?
Quote
Although, 'hypothetically'...... if we had the time to do it.... you could physically generate a bunch of sinusoids at the suitable frequencies (together with suitable amplitudes) ...... and add them up..... to come up with an "approximation" for the time waveform.

In the lab, you're not going to be able to generate an infinite number of harmonics. It's like e^(-t)..... it never goes to 'zero' until time reaches infinity.....and infinity means you'll never get there....because it keeps going ....forever.
So? Everything in every engineering discipline involves approximate mathematical models. There is nothing special about Fourier in this regard. You will never be able to generate a perfect square or sine wave with any physical circuit. You won't even be able to generate the exact same signal twice with the same circuit. Noise and other imperfections will always distort it.

Fourier works. There's no point to arguing if it's "really real" when it's a distinction without a difference.
Title: Re: RE: Low Pass Filter
Post by: avr_fred on Jun 15, 2017, 03:34 am
Quote
but you can't deny that the physical signal can still be analyzed and broken down into an infinite sum of sinusoids
I agree 100% with Southpark. Making the statement above only confuses people who do not have a full understanding of why harmonics exist. IMO, saying that harmonics have a "physical" property really distorts the issue to the non-EE - as physical things are those things that you can measure like volts, amps and frequency.

It's like saying a certain device "generates harmonics" - rather than saying "the device uses current in a nonlinear fashion and we can describe that current mathematically using a concept called harmonics". Given the first statement and the second, it is easy to see why the first statement is the norm within engineering circles. Unfortunately, it confuses those who don't speak the same shorthand language.

Quote
I'm not saying that the PWM signal is generated from the Arduino is an infinite number of sinusoidal voltage sources in series (quite absurd isn't it lol)
If I understand your argument, you just invalided it, all in the same sentence. Yes, it's absurd but that is how a square wave is described, the only problem is you need infinity in order to get there...

Quote
Fourier works. There's no point to arguing if it's "really real" when it's a distinction without a difference.
Bingo. Winner winner chicken dinner. As Einstein said, "You truly understand something when you can explain it to your Grandmother".
Title: Re: RE: Low Pass Filter
Post by: Power_Broker on Jun 15, 2017, 05:54 am
Fourier works. There's no point to arguing if it's "really real" when it's a distinction without a difference.
Exactly. Spot on.


IMO, saying that harmonics have a "physical" property really distorts the issue to the non-EE
Valid point, it does take a while to get comfortable thinking in the frequency domain vs the time domain.


If I understand your argument, you just invalided it, all in the same sentence.
Maybe you misunderstood me. I was trying to say that any physical signal can be broken into a set of sinusuids no matter where it originated from. It could be an infinite number of sinusuidal voltage sources in series or it could be a flip-flop - both give the same output.
Title: Re: RE: Low Pass Filter
Post by: Southpark on Jun 15, 2017, 06:24 am
Valid point, it does take a while to get comfortable thinking in the frequency domain vs the time domain.
However, a PWM waveform is a waveform. And waveform implies time-domain. The voltage waveform at the PWM output pin is the PWM signal. Better not tell anybody that the square/rectangular raw waveform is a bunch of individual sinusoids, all individually summing together (at any particular moment) to produce this square/rectangular waveform.

If I put a hot soldering iron tip on a circuit board, and the temperature on that part of the circuit (where the hot iron tip is touching) abruptly rises from 25 degrees C to some relatively high temperature value. Yep...assuming this is happening in the real world, in the time domain. Better not tell anybody that the temperature rise at that part of the circuit is physically (actually) a bunch of sinusoids all individually ganging together at the same time.

I do have a very big appreciation for frequency domain concepts, frequency domain theory etc. The application of that theory is tremendous.

Also, I agree absolutely about what AVR_fred wrote.
Title: Re: RE: Low Pass Filter
Post by: ilovetoflyfpv on Jun 15, 2017, 08:02 am
Hi Guys

I appreciate the input but bear in mind my math/science is mid high school level (work in progress). I'm having great deal of trouble understanding how a signal that is 490Hz has a DC component and this occurs at 0Hz.

When is the signal 0Hz? Are we simply taking a snapshot of the PWM signal when it is high (5V)? If this is the case then the capacitive reactance will be infinite? I feel I'm missing some key concept.  :(

@septillion

I don't understand what a pure sine wave is?

I don't understand what the pros/cons of adjusting the frequency?

@tinman13kup

I'm wanting to generate an analog voltage for an On Screen Display. I know that an RC circuit is commonly used to do this. I want to understand why.

@Southpark & Power_Broker

Fourier Series Concept? YIKES! There's no need to use that kind of language here. ;D This is way over my head.

Any help would be greatly appreciated.

Cheers

Jase :)
Title: Re: RE: Low Pass Filter
Post by: Southpark on Jun 15, 2017, 08:39 am
I appreciate the input but bear in mind my math/science is mid high school level (work in progress). I'm having great deal of trouble understanding how a signal that is 490Hz has a DC component and this occurs at 0Hz.

When is the signal 0Hz? Are we simply taking a snapshot of the PWM signal when it is high (5V)? If this is the case then the capacitive reactance will be infinite? I feel I'm missing some key concept.  :(

@septillion

I don't understand what a pure sine wave is?
I can understand the confusion. At high school, I only knew what a sinewave was. But probably didn't know what frequency was... I think haha.

Anyway..... you know what a sinusoidal signal is, right? In maths....
y(t) = sin(t) defines ONE particular sinusoidal signal from a family of sinusoidal signals. This one happens to be a 'sine' wave. The wavey pattern of y(t) plotted versus time will repeat itself every (2.pi) seconds. This is known, because....in maths.... the sinusoidal expression is generally written with 1 more parameter... ie   y(t) = sin(w.t)    where w = 2.pi.f

'w' is called 'angular frequency', units of radians per second. I can't type greek symbols...but 'w' is actually supposed to be like a running-writing 'w'.

Yep.... pi is 3.14159 etc. And 'f' is the 'cyclic frequency'.

So, for y(t) = sin(t)...which is sin( 1.t ), you can see that the angular frequency 'w' is equal to 1 for this particular case. So w = 1. But, w is defined as 2.pi.f.
So w = 2.pi.f = 1

Rearranging the above gives cyclic frequency, f = 1/(2.pi), in units of Hertz, or 'cycles per second'. It means a cyclic frequency of 1 radian per second corresponds to a cyclic frequency of 1/(2.pi) Hertz. A 'cycle' means one single repetition of a portion of the waveform. If measured in units of 'seconds', 1 cycle will take a certain amount of time. And that certain amount of time (labelled capital "T") is called a 'period', or 'the period'.

The relation between the period T and the cyclic frequency f is simple.....  it is: f = 1/T, or T = 1/f.
So, when angular frequency w (for this particular case) is equal to 1 radian per second, then we can easily figure out that the corresponding cyclic frequency f is equal to 1/(2.pi) Hertz. And the period T is 1/f, which is 2.pi seconds.

So, if you have a 50 Hertz sinusoidal signal that happens to be a 'sine' wave, then you could mathetically write it as y(t) = sin(2.pi.50.t)
And if you want to make this sine wave larger....just put a multiplying factor on the front of it... like...
y(t) = 3sin(2.pi.50.t), or 3sin(100.pi.t) .... or 3sin(314.159t)

One more thing. The 'sine' wave has a value of zero at time t = 0. And, as time increases, the 'sine' wave value gets larger and larger and will eventually reach a value of '1', before dropping back to zero again.... just google 'sine' wave, and you'll see. If you don't want a 'sine' wave, then you can always modify it.... by adding an offset to it.... like

y(t) = sin(w.t - 2.pi.b)    ....'b' is a fractional number....and you could limit 'b' to a value between -1 and +1. For example... if 'b' is one-quarter, ie. b = 0.25, then

y(t) = sin( w.t - pi/2 )


The above is just some starting details for you about sinusoidal signals. Work towards understanding that first.


Title: Re: RE: Low Pass Filter
Post by: ilovetoflyfpv on Jun 15, 2017, 09:08 am
Hi Southpark

I appreciate you getting back to me. At this stage I can manage basic algebra, trig and I'm familiar with what a sine wave looks like. Unfortunately the rest of your response is pitched well and truly above my head possibly even tertiary level.

y(t) = sin(t) (https://www.youtube.com/watch?v=k_6VBh-0OSA)

I'm glad the guy in the above video isn't my teacher I'd be worried if I didn't understand what he was saying he'd hit me with that steel bar.

Cheers

Jase  :(
Title: Re: RE: Low Pass Filter
Post by: Southpark on Jun 15, 2017, 09:12 am
y(t) = sin(t) (https://www.youtube.com/watch?v=k_6VBh-0OSA)
hahaha! Let me check out that video. I'll take a look at that one tonight. Looking forward to seeing it. Thanks Jase.

---
[UPDATE] : Wow! It will be important to pay attention to him in class. Life may depend on it!
Title: Re: RE: Low Pass Filter
Post by: septillion on Jun 15, 2017, 10:59 am
I don't understand what a pure sine wave is?
(http://www.conservapedia.com/images/0/03/Sine-pi.jpg)

I don't understand what the pros/cons of adjusting the frequency?
Simple explanation: higher frequency => need lower RC values (and thus smaller components) to have the same filter effect => faster response on duty cycle change => and/or (if you do take the values higher) better filtering out of the PWM frequency.

And the DC = 0Hz. DC = Direct Current = Not changing = changing with 0Hz :D And the DC part comes from the fact the PWM signal from the Arduino does not cross 0V. It's either 0V or 5V where as each sine crosses the 0V symmetrical. So think of a 50% duty cycle PWM, if you want to make it symmetrical that would be 50% of the time at -2,5V and 50% of the time at 2,5V. This makes it symmetrical and the voltage difference is still 5V. Now shift that whole signal by 2,5V DC up and you get the PWM of an Arduino, 50% of the time at 0V (-2,5V + 2,5V = 0V) and 50% of the time at 5V (2,5V + 2,5V = 5V). So it has a DC component of 2,5V. And that is what you expect because when you filter the PWM of the Arduino you expect a voltage of 2,5V when the duty cycle is 50% ;)

And although it is a big deal of math, it does work in practice although it damn hard to sum an infinite amount of sines :p

I'm wanting to generate an analog voltage for an On Screen Display.
On Screen Display? I can only think of that with my TV or monitor but I can't think of a link with voltage.
Title: Re: RE: Low Pass Filter
Post by: Southpark on Jun 15, 2017, 11:48 am
So? Everything in every engineering discipline involves approximate mathematical models.
Your wording.....  'approximate mathematical models'. Maybe you just mean - a system model that models the behaviour of an actual system. Some system models can model a real system perfectly. It just depends on "what" is being modeled.

Also, the approximation (I was referring to) excludes noise, and 'imperfections'. I'm talking about approximating a square-wave by summing together a few sinusoids.
Title: Re: RE: Low Pass Filter
Post by: ilovetoflyfpv on Jun 15, 2017, 02:03 pm
Hi Gang

So a pure sine wave is a graphed unit circle?

Increasing frequency has the following benefits. It allows us to decrease the the values of our RC circuit thus reducing size, improved responsiveness and less ripple?

The DC component is the average of the duty cycle? I'm still having trouble understanding why then we set our cut-off frequency to 0Hz? Don't we need the voltage to toggle between 0V and 5V in order to achieve PWM?

On Screen Display (OSD) (https://oscarliang.com/best-osd-quadcopter-fpv-data-on-screen-display-video/)

I'm beginning to wonder whether the article that I linked to in my first post is not appropriate for my application. It deals primarily with AC signal while what I'm really interested in is a pulsed DC signal? I found the following article...

Arduino's AnalogWrite - Converting PWM to a Voltage (https://provideyourown.com/2011/analogwrite-convert-pwm-to-voltage/)

The focus seems to be more RC Time Constant and Cut-off Frequency. There is a link to an online calculator (http://sim.okawa-denshi.jp/en/PWMtool.php) but I'm unsure how to use it?

Really appreciate the help.

Cheers

Jase :)
Title: Re: RE: Low Pass Filter
Post by: MarkT on Jun 15, 2017, 03:07 pm
A sine wave is the projection of a regular helix perpendicular to its axis.
Title: Re: RE: Low Pass Filter
Post by: MrMark on Jun 15, 2017, 04:04 pm
It isn't 'physically' a sum of those things. Because.... physically, the physical signal in the time domain is 'The time domain Signal'.... which ...in this case.... is a periodic 2-level waveform (when the input is held at a constant value that is). Not 'physically' a bunch of sinusoids (plus a DC shift, if there is one).
It's not clear why one would make this distinction.  The time and frequency domain representations of a physical signal are mathematically equivalent.  If one is physical the other is by definition.
Title: Re: RE: Low Pass Filter
Post by: septillion on Jun 15, 2017, 05:45 pm
So a pure sine wave is a graphed unit circle?
That's how they are related, yeah.

Increasing frequency has the following benefits. It allows us to decrease the the values of our RC circuit thus reducing size, improved responsiveness and less ripple?
Kind of yeah. If we increase the frequency by a factor 10 we can have the same ripple (but that ripple will still have the frequency of the PWM) if we decrease R x C (so multiplication of R and C) with a factor 10 as well. But we can also let RC stay the same and increase the frequency by 10 and have a ripple that's 10 times less. And you can mix and match. Because lower RC is faster response and smaller but higher ripple etc.

The DC component is the average of the duty cycle? I'm still having trouble understanding why then we set our cut-off frequency to 0Hz?
Ideally you would want the cut off to be at 0Hz but because it's given by Fc = 1 / (2*Pi*R*C) that would mean we need R*C to be infinite which does not fit on most PCB's :p So, the amount of ripple you can tolerate defines where to put it. If a ripple of 1% is fine then the cut off freqency needs to be 1% of the PWM frequency. So for 490Hz that would be at 4,9Hz  which gives a RC of 0,032 which is pretty big (100k and 325nF) and 1% ripple is still "a lot".

Say we have a PWM of 62,5Hz and we allow a 1% ripple so cut off at 625Hz. That just gives a RC of 2,55*10-4 or with the same 100k just a 2,55nF capacitor will work. Or you can increase it to 25nF and just have 0,1% ripple ;)

Don't we need the voltage to toggle between 0V and 5V in order to achieve PWM?
We still do, don't we? That's on the input of the filter and you want just plain DC on the output aka NO switching.

On Screen Display (OSD) (https://oscarliang.com/best-osd-quadcopter-fpv-data-on-screen-display-video/)
Ahhh, Quad copter FPV OSD.

I'm beginning to wonder whether the article that I linked to in my first post is not appropriate for my application. It deals primarily with AC signal while what I'm really interested in is a pulsed DC signal? I found the following article...
Where is the "pulsed" coming from?! You wanted DC didn't you?

Arduino's AnalogWrite - Converting PWM to a Voltage (https://provideyourown.com/2011/analogwrite-convert-pwm-to-voltage/)
Same first order low pass RC-filter. Just another way of explaining it.
Title: Re: RE: Low Pass Filter
Post by: Southpark on Jun 15, 2017, 07:26 pm
It's not clear why one would make this distinction.  The time and frequency domain representations of a physical signal are mathematically equivalent.  If one is physical the other is by definition.
It is clear. Read this following post linked below thanks.

click here (http://forum.arduino.cc/index.php?topic=483686.msg3301932#msg3301932)

And, you just said it, correctly. One is physical..... the real thing, such as .... I present you with the number 3. And this is the number 3..... plain and clear. Sure, you can mathematically decompose it in various ways, like 2.9 + 0.1, or 1.5 * 2, etc. Let's just (by analogy and by example) assume that '3' is the real deal..... physical. The rest are mathematical manipulations. So, if I present to you the number 3, then it is '3'. You normally don't go around teaching people that it IS "physically" some kind of infinite series thing, unless you want to do some kind of analysis or mathematical exercise, or something.

So, mathematically equivalent... yes.... or 'can be hypothetically expressed as', or 'can be modeled as'..... all ok. No problem.

But ..... "IS physically the same as" (ie. occurring in the background IS a bunch of things going on like 0.1 + 2.8 + 0.1)  .... no. Not ok.
Title: Re: RE: Low Pass Filter
Post by: Southpark on Jun 15, 2017, 08:44 pm
So a pure sine wave is a graphed unit circle?
The answer to that is .... no.

However, if an object is moving around a circle repeatedly, then then a plot of the vertical distance (or even the horizontal distance) versus time will have a sine-wave pattern. Another way to convey that is ..... consider a circle. A plot of the y-axis value (of a point) versus the angle of that point will result in a sine wave pattern for the plot. So, it could be considered as a plot of a value versus angle in one case. Or, if we follow an object that travels around a circle, it could be considered as a plot of a value versus time.

For constant speed (constant angular velocity) motion around a circular path...... the angle and the time are related by this formula.....   ANGLE (in radians) = w.t  
'w' is 'omega'.... units of radians per second.

The above formula is under the assumption that the object's initial position on the circle (at time t = 0) is at zero degrees (or zero radians).

See this image obtained from the internet....

(http://forum.arduino.cc/index.php?action=dlattach;topic=483686.0;attach=215551)


It means, you can model (mathematically) a sinewave by using some mathematical formulas that express  the time-changing vertical (or even horizontal) distance as a function of time. The origins of such a model is circular motion. The cyclic nature of the circular movement links nicely with the cyclic nature of the sinewave. It does not necessarily mean that any real (measured) sinewave originates from circular motion. Sometimes....maybe - such as angular measurements for a motor shaft.... but not always.
Title: Re: RE: Low Pass Filter
Post by: Southpark on Jun 15, 2017, 10:05 pm
Jase.... for this PWM generator, what kind of input are you putting into this PWM generator? For example, do you just set an input value every once in a while, so that the pulse width of the signal remains the same for a relatively long time?

Or, are you changing the pulse widths of the PWM waveform at some relatively fast rate?

In any case, it sounds like it will be much better for you to go for a much higher PWM frequency. With higher PWM frequency (relative to 490 Hz), it can be more convenient to design an RC filter that gives you desirable results that you (or we) want. Desirable results mean ..... output DC voltage is relatively clean.... relatively low ripple. And also means - the response time is relatively quick --- like doesn't take forever for the output to reach the new DC value after you change the input.
Title: Re: RE: Low Pass Filter
Post by: Southpark on Jun 15, 2017, 11:32 pm
The DC component is the average of the duty cycle? I'm still having trouble understanding why then we set our cut-off frequency to 0Hz? Don't we need the voltage to toggle between 0V and 5V in order to achieve PWM?
You don't need to set any cutoff frequency to 0 Hz.

The DC component is sometimes called 'average DC value'). If the voltage is finite, and never changes.... eg. 2 Volt..... then the average of this value over time is 2 Volt.

For some kind of periodic signal (where you know its period), a maths formula can be applied .... a 'time average formula'. This formula 'averages' the signal over 1 full cycle of the waveform. The result will be a value. And that value will be the 'DC average'.

For a PWM signal, with 2 levels of voltage..... eg, 0 V and 2 V......  if you have 50 percent duty cycle, the DC average will be half-way between those two levels.... which is 1 Volt., or 0.5 times 2V.

If you have 20 percent duty cycle, then the DC average will be 0.2 times 2V, which is 0.4 Volt.

And, suppose that the PWM waveform has levels of 0 V and 1 V instead. No problem.... the same rule applies.... 50 percent duty cycle would translate to a DC average value of 0.5 times 1V, which is 0.5 Volt.

And..... for 100 percent duty cycle.... we'd get 1 times 1V, which gives 1 Volt. 100 percent duty cycle means that the PWM signal is just a horizontal line, with a value of 1 Volt.
Title: Re: RE: Low Pass Filter
Post by: allanhurst on Jun 16, 2017, 12:03 am
Do we dare speak of Butterworth, Bessel, Chebychev, elliptic  etc?

Allan
Title: Re: RE: Low Pass Filter
Post by: Southpark on Jun 16, 2017, 12:13 am
Do we dare speak of Butterworth, Bessel, Chebychev, elliptic  etc?

Allan
I can't believe you dared to speak of those Allan! But now that you did. I believe.
Title: Re: RE: Low Pass Filter
Post by: allanhurst on Jun 16, 2017, 12:18 am
Sorry. Not appropriate here. Multipole filters are a whole new ballgame.....

Allan
Title: Re: RE: Low Pass Filter
Post by: ilovetoflyfpv on Jun 16, 2017, 02:58 pm
Hi Gang

Well I printed out the thread today and read it thoroughly. I was hoping that it would clarify things however it's raised more questions.

1. If I want to calculate the impedance of the circuit shouldn't I be using the PWM frequency (490Hz) in order to calculate the reactance of the capacitor?

2. The topic of Sine waves is debated numerous times most of which is pitched well above my understanding? I did like the diagram that Power_Broker included in post #13 illustrating the DC component of PWM signal along with the fundamental frequency and harmonics. It actually clarified the concept of fundamental and harmonic frequencies.

3. How do I know whether the device I'm feeding my filtered signal to is drawing current?

4. I'm still unsure about component selection. In post #29 septillion touches on the subject but I don't quite follow. Wouldn't I be interested in the acceptable ripple voltage rather than the ripple frequency? Could you expand on this?

5. The PWM signal constantly changes based on how strong the Received Signal Strength Indicator (RSSI). I imagine I need to balance responsiveness and ripple?

6. If I increase the frequency and reduce the resistor/capacitor size I gather that means the capacitor can charge/discharge quicker hence the better response?

7. If I was to use DAC what would be a good starting point?

As always I really appreciate the help.

Cheers

Jase :)
Title: Re: RE: Low Pass Filter
Post by: Jiggy-Ninja on Jun 16, 2017, 04:07 pm
Hi Gang

Well I printed out the thread today and read it thoroughly. I was hoping that it would clarify things however it's raised more questions.
Most of it is not necessary and will be over your head, so try not to get too bogged down in the details. This firestorm of posts was only triggered by someone deciding to pointlessly argue over a distinction without a difference. If you understand the big picture concept illustrated by Power_Broker's diagram, that's more than good enough. Fourier Transforms are a part of calculus, you don't need to concern yourself with the exact details of what frequency components are in a waveform.

Quote
1. If I want to calculate the impedance of the circuit shouldn't I be using the PWM frequency (490Hz) in order to calculate the reactance of the capacitor?
Impedance is a frequency-dependent value. That's how filters (like a low-pass) work: the different frequency components of a waveform encounter different impedances in the filter, causing the output to be changed and distorted.

For the output of the DC component (the one you care about most), a simple RC low pass filter will have an output impedance equal to R. The capacitor is irrelevant.
Quote
3. How do I know whether the device I'm feeding my filtered signal to is drawing current?
If it's a chip, the datasheet will have large tables of electrical characteristics. One of those is likely to be input bias currents or input impedance for the various inputs. If it's a more complex circuit receiving the signal (like an op amp circuit with feedback networks) then it depends on the circuitry attached to the input.

Quote
4. I'm still unsure about component selection. In post #29 septillion touches on the subject but I don't quite follow. Wouldn't I be interested in the acceptable ripple voltage rather than the ripple frequency? Could you expand on this?
Correct, but the ripple frequency will be the same as the frequency of the PWM signal. You need to know that frequency in order to create an appropriate low pass filter.

Quote
5. The PWM signal constantly changes based on how strong the Received Signal Strength Indicator (RSSI). I imagine I need to balance responsiveness and ripple?
Correct.

Quote
6. If I increase the frequency and reduce the resistor/capacitor size I gather that means the capacitor can charge/discharge quicker hence the better response?
Exactly correct.

Quote
7. If I was to use DAC what would be a good starting point?
Start with the characteristics of the RSSI signal.

1) What is its source? Is it an analog output, or a digital value read from some chip's register?
   1a) If it is analog, what are the minimum and maximum voltages?

2) How quickly does this value change? How quickly do you need to respond to changes?

3) What transformations (if any) are you performing to the signal value? I'm not referring to the analog -> PWM conversion, but changes in the actual signal value itself like multiplying or dividing it, adding or subtracting something, or filtering it in some way (not counting the PWM filter you're intending to apply).

4) What is the receiving device? Since you're trying to LPF the PWM signal, it's obviously an analog input, but to what? For what purpose?

It's entirely possible that some cheap and easy SPI DAC like an MCP4811 will be good enough for your needs. Or if you don't mind discrete components, an R-2R ladder.

Or, if the source and receiver are both analog, take the Arduino out of the signal path entirely and use an op amp circuit.
Title: Re: RE: Low Pass Filter
Post by: ard_newbie on Jun 16, 2017, 04:56 pm

A nice tutorial when one wants to convert a PWM into a voltage:

http://henrysbench.capnfatz.com/henrys-bench/arduino-projects-tips-and-more/arduino-lm358-op-amp-pwm-to-voltage-converter/ (http://henrysbench.capnfatz.com/henrys-bench/arduino-projects-tips-and-more/arduino-lm358-op-amp-pwm-to-voltage-converter/)
Title: Re: RE: Low Pass Filter
Post by: MrMark on Jun 16, 2017, 05:46 pm
If I put a hot soldering iron tip on a circuit board, and the temperature on that part of the circuit (where the hot iron tip is touching) abruptly rises from 25 degrees C to some relatively high temperature value. Yep...assuming this is happening in the real world, in the time domain. Better not tell anybody that the temperature rise at that part of the circuit is physically (actually) a bunch of sinusoids all individually ganging together at the same time.
Maybe you're being subversively ironic, but the Fourier Transform as we know it was introduced in Joseph Fourier's seminal manuscript on heat transfer to analyze essentially the scenario you describe.
Title: Re: RE: Low Pass Filter
Post by: Jiggy-Ninja on Jun 16, 2017, 06:45 pm
Maybe you're being subversively ironic, but the Fourier Transform as we know it was introduced in Joseph Fourier's seminal manuscript on heat transfer to analyze essentially the scenario you describe.
I would have just quipped with a dismissive "Why can't you, if it's useful to the analysis?", but that's just so much better!

Wikipedia (https://en.wikipedia.org/wiki/Joseph_Fourier) (emphasis added):
Quote
Jean-Baptiste Joseph Fourier - (/ˈfʊəriˌeɪ, -iər/; French: [fuʁje]; 21 March 1768 - 16 May 1830) was a French mathematician and physicist born in Auxerre and best known for initiating the investigation of Fourier series and their applications to problems of heat transfer and vibrations.
Title: Re: RE: Low Pass Filter
Post by: Southpark on Jun 16, 2017, 09:12 pm
This firestorm of posts was only triggered by someone deciding to pointlessly argue over a distinction without a difference.
It was actually started by someone(s) (and I'm NOT referring to the OP because the OP is new to this area) that did not understand that a particular kind of analog waveform (referring to the PWM voltage at the PWM pin of an arduino) is not a stage-show performance featuring a bunch of sinusoidal signals - all individually doing their little parts at the same time - and combining right in front of you to yield that waveform.

I disagree with your comment: "distinction without a difference".

The frequency domain theories are fantastic for analysing waveforms, and for designing/building devices to produce waveforms or modify waveforms etc. But the measured voltage waveform at the PWM pin of an arduino is not the result of a bunch of sinusoidal signals putting on a display for you.

There is a distinction. There is a difference.
Title: Re: RE: Low Pass Filter
Post by: Power_Broker on Jun 17, 2017, 01:47 am
1. If I want to calculate the impedance of the circuit shouldn't I be using the PWM frequency (490Hz) in order to calculate the reactance of the capacitor?
Ok, time to set one thing straight that will clarify things for you. The 490Hz that you keep bringing up is only the refresh rate. In other words, the PWM waveform repeats itself 490 times a second and this has very little to do with the frequency components of the signal - at least not as far as you're concerned. On the other hand, if you have any given waveform (periodic or not), your signal is and can be expressed as a sum of sines and cosines at different frequencies. The magnitude of these sinusuids can be plotted for a given signal in two easy to follow ways depending on whether or not the signal is periodic or not.

For instance:

For a periodic signal (such as a PWM signal of constant duty cycle):
(http://mbrreading.org/apfelzer/demos/fourier3/impulsetrain/p2.gif)

OR for a non-periodic signal (such as a single pulse):
(https://mysite.du.edu/~etuttle/electron/circ88.gif)


Notice that you are primarily interested in the first case. Keep that graphic in mind and take my word that the 490Hz you mention doesn't change anything except your fundamental frequency. There will still be an infinite number of frequency components and you are trying to get rid of all of them, leaving the DC component (where n=f=0Hz).


You may also be interested in seeing this graphic, too. It explains more clearly how the Fourier Series works:
(http://people.csail.mit.edu/haitham/Pictures/sFFT.png)


3. How do I know whether the device I'm feeding my filtered signal to is drawing current?
You can add a buffer in between the PWM generator and the filter. You should also add a buffer to the output of the buffer. This ensures that the filter doesn't draw any current except directly from the power supply and that the impedances of the other parts of the circuit doesn't "interfere" with the filter.


5. The PWM signal constantly changes based on how strong the Received Signal Strength Indicator (RSSI). I imagine I need to balance responsiveness and ripple?

6. If I increase the frequency and reduce the resistor/capacitor size I gather that means the capacitor can charge/discharge quicker hence the better response?
Exactly. The lower the RC time constant, the more responsive, BUT that creates a larger ripple. It's a trade off.


I hope these concepts are clearer to you now. You should still google "envelope detector" for more info.
Title: Re: RE: Low Pass Filter
Post by: ilovetoflyfpv on Jun 17, 2017, 05:40 am
Hi Power-Broker

Thanks for getting back to me. I don't understand your post from...

Quote
On the other hand, if you have any given waveform (periodic or not), your signal is and can be expressed as a sum of sines and cosines at different frequencies...
I understand Sine and Cosine relative to triangles.

What is x(t)?

What is X(w)?

What are the red arrows? Are they trying to keep up with the PWM signal?

What are the name of these graphs?

Is the Fourier Series illustration highlighting the different frequencies that comprise a fundamental frequency at a specific period in time?

What is the process for selecting the resistor and capacitor? I'm thinking I start with a resistor that doesn't overdrive the PWM pin (at 490Hz the capacitor will have a very low capacitive reactance). I then start 'playing' with different capacitor values that give good responsiveness and nominal ripple?

Thanks for sticking with me. I'm not the sharpest tool in the shed but I'm willing to work hard.

Cheers

Jase :)



Title: Re: RE: Low Pass Filter
Post by: Power_Broker on Jun 17, 2017, 07:02 am
Thanks for sticking with me. I'm not the sharpest tool in the shed but I'm willing to work hard.
Whoa there, I wouldn't sell yourself short. This is pretty advanced stuff for mid-leveled high school and you seem to be doing a good job so far. I didn't know how electronics or basic signal processing worked myself until somewhere around sophomore year of college (electrical engineering major).


What is x(t)?
What is X(w)?
x(t) is the signal in the time domain. This is what you would see if you were looking at the signal on an oscilloscope. On the x(t) graph, the x-axis represents a change in time.

X(w) is the SAME signal, but in the frequency domain. This is what you would see if you were looking at an FFT of the signal. On the X(w) graph, the x-axis represents a change in frequency. This explicitly shows which frequency components are present in your signal.

Both graphs provide the same exact information about the signal, but in different ways. For instance, it would be like saying that the US President lives at 1600 Pennsylvania Ave and also saying that he lives at 38.8977° N, 77.0365° W (Lat - Long). Both pieces of information are correct, they just have a different form. Much like x(t) and X(w) both give the same information about the signal, just in different form.


What are the red arrows? Are they trying to keep up with the PWM signal?
The red arrows show the presence of harmonics in the signal. They are called Dirac Delta Impulses, but you don't need to worry about that. The important thing is that they show you which frequencies are most prevalent in your signal. At w=0, we can see there is a red arrow, showing that there is a DC component to the signal. Moving along the x-axis we run into the next red arrow. This occurs at the first positive harmonic (we'll pick w = 10rad/sec since no numbers are given on the graph). We can also see another red arrow at w = -10rad/sec. This pair of red arrows indicates a cosine with a magnitude of twice the height of one the arrows. You will find similar pairs of red arrows at the second (w=20rad/sec), third (w=30rad/sec), and fourth (w=40rad/sec) harmonics, up to infinity. The only difference between all of the pairs are their heights - which shows how prevalent the cosines are compared to others. More height means they play a larger role in shaping the signal.

Once you filter the signal, the x(t) graph will become a constant and the X(w) graph will have only one arrow at w=0rad/sec.


What are the name of these graphs?
You can find similar graphs by searching "Fourier Series".


Is the Fourier Series illustration highlighting the different frequencies that comprise a fundamental frequency at a specific period in time?
Yes and no. It does illustrate the different frequencies of the signal, but is NOT for a specific moment in time. It is for the entire periodic signal from t = -infinity to t = infinity. Also, be careful to note that the fundamental frequency isn't comprised of anything; it just is.


What is the process for selecting the resistor and capacitor? I'm thinking I start with a resistor that doesn't overdrive the PWM pin (at 490Hz the capacitor will have a very low capacitive reactance). I then start 'playing' with different capacitor values that give good responsiveness and nominal ripple?
That question is best answered by research and trial and error. I suggest looking here (https://www.allaboutcircuits.com/technical-articles/low-pass-filter-a-pwm-signal-into-an-analog-voltage/) first. Good luck!

As for "playing" with different values, I'd suggest doing that with a circuit simulator first such as LTSpice IV.
Title: Re: RE: Low Pass Filter
Post by: ilovetoflyfpv on Jun 17, 2017, 11:52 am
A nice tutorial when one wants to convert a PWM into a voltage:

http://henrysbench.capnfatz.com/henrys-bench/arduino-projects-tips-and-more/arduino-lm358-op-amp-pwm-to-voltage-converter/ (http://henrysbench.capnfatz.com/henrys-bench/arduino-projects-tips-and-more/arduino-lm358-op-amp-pwm-to-voltage-converter/)
What a great site. Really appreciate the link!  :)
Title: Re: RE: Low Pass Filter
Post by: septillion on Jun 17, 2017, 02:09 pm
Didn't go through it all but just to answer this:
Hi Gang

Well I printed out the thread today and read it thoroughly. I was hoping that it would clarify things however it's raised more questions.
But let me start with, unless you want to learn some pretty advanced Electrical Engineering, don't bother to much with the why. Just take the formulas for granted.

1) Yes, but then you only calculate the impedance for a sine that frequency. And in case of making a voltage from PWM it's easier to not do it the impedance why but the "filtering" way. So for a low pass RC0 filter (first order) is simply the voltage will decrease by 10 fold every time the freqency increases a 10 fold of the cut of frequency.

2) I don't really understand why a sine (at least the basic form) is a hard thing...

3)Datasheets datasheets datasheet. Or if you don't have a spec for it do the second best thing, measure it :)

4) Ripple fundamental frequency is just the PWM you use. And more often then not the circuitry is more worried about less ripple then it is about the ripple frequency. Because the more you reduce the ripple amplitude the more it just looks like noise. So for here, just use a higher frequency then normal, 3kHz and up will already make filtering easier.

5) Yes, what is the fastest change you think you will see?

6) Yep, lower C is less charge to discharge (or charge) => quicker, and less resistance = higher currents => quicker (dis)charge.

Another way of looking at it, your changing of the PWM also is a frequency part (how quick/often you change it) and the bigger the RC, the lower the cut off frequency and the more you also start to dampen your actions.

7) I would say getting pain killers... That is just another can of worms. I expect a voltage measuring input of the FPV OSD to not load the signal much so a RC-filter is just fine and simple.

Title: Re: RE: Low Pass Filter
Post by: ilovetoflyfpv on Jun 17, 2017, 02:45 pm
Hi Gang

Well I took the plunge today and downloaded LTspice. I ran two simulations (see attached). It definitely illustrates responsiveness vs ripple. I tested the 10kΩ/10µF combination in my aircraft and it worked. I'm guessing the On Screen Display (OSD) can cope with a certain degree of ripple. I'm curious to see what this is to maximise the responsiveness. Should I be decreasing the resistor value or the capacitor value or both? Specifically what are the pros and cons?

Cheers

Jase :) 
Title: Re: RE: Low Pass Filter
Post by: septillion on Jun 17, 2017, 04:04 pm
10k is a reasonable resistor value (not extremely low or high) so I would go for the cap. You can then switch to small ceramic caps in the nF range.

And did you also increase the PWM frequency as suggested?
Title: Re: RE: Low Pass Filter
Post by: Power_Broker on Jun 17, 2017, 07:34 pm
Test_1 looks like the better choice. The big problem with test_2 is that you're using a 2,200uF capacitor, which seems ridiculous.

My suggestion is to use a 10uF capacitor and change your resistor value to find the right response time you need.

You should also look into adding input and output buffers to your filter.
Title: Re: RE: Low Pass Filter
Post by: Jiggy-Ninja on Jun 17, 2017, 08:51 pm
The frequency domain theories are fantastic for analysing waveforms, and for designing/building devices to produce waveforms or modify waveforms etc.
Which is exactly what's being done in this thread.
Quote
But the measured voltage waveform at the PWM pin of an arduino is not the result of a bunch of sinusoidal signals putting on a display for you.
The physical method used to generate the signal is irrelevant. This is about the abstract properties of the signal itself.
Quote
There is a distinction. There is a difference.
Even accepting that claim, it is completely irrelevant to the topic at hand.

ilovetoflyfpv, don't get too bogged down in the details. For a simple thing like this, picking a cutoff frequency 10-20 times less than the PWM frequency is good enough. The exact value isn't hugely important, just that it's low enough. Then you can use the formula 1/( 2 * PI * RC) to calculate the RC value you need. Then find a pair of reasonable values for the resistor and capacitor so that when you multiple the values together you get about the RC value you calculated.

If the filter you create responds too slowly to be useful, there are ways to increase the PWM frequency to let you use a lower RC value.

That's it.
Title: Re: RE: Low Pass Filter
Post by: ilovetoflyfpv on Jun 20, 2017, 01:52 pm
Hi Gang

Sorry for taking so long to respond. I'm planning on using an ATtiny25 for this project so I'm not exactly sure how to increase the PWM frequency?

I stumbled across another video in the interim that I found quite useful.

Electricity - Filters (https://www.youtube.com/watch?v=oQFgAz-nO2o)

Cheers

Jase :)
Title: Re: RE: Low Pass Filter
Post by: Jiggy-Ninja on Jun 20, 2017, 04:51 pm
Hi Gang

Sorry for taking so long to respond. I'm planning on using an ATtiny25 for this project so I'm not exactly sure how to increase the PWM frequency?

I stumbled across another video in the interim that I found quite useful.

Electricity - Filters (https://www.youtube.com/watch?v=oQFgAz-nO2o)

Cheers

Jase :)
Timer prescaler. Check the datasheet for the appropriate registers to change. It'll also help you learn why only certain pins can be used as PWM pins. (Hint: look for OC__ in the pin diagrams).
Title: Re: RE: Low Pass Filter
Post by: septillion on Jun 20, 2017, 05:01 pm
I don't now if there is an easy library for it but you can always open op the datasheet to see which registers to set. And just Google a bit about it, it's not that different then an Uno to do. Al least the principle is the same.
Title: Re: RE: Low Pass Filter
Post by: ilovetoflyfpv on Jun 20, 2017, 11:13 pm
Hi Gang

Thanks for the replies. Registers make me break out in a cold sweat. They remind me of crack codes for games. A combination of letters and numbers that make no sense whatsoever but when applied in the correct sequence do something magic.

Cheers

Jase :)
Title: Re: RE: Low Pass Filter
Post by: Jiggy-Ninja on Jun 20, 2017, 11:55 pm
Hi Gang

Thanks for the replies. Registers make me break out in a cold sweat. They remind me of crack codes for games. A combination of letters and numbers that make no sense whatsoever but when applied in the correct sequence do something magic.

Cheers

Jase :)
They shouldn't. Registers are just variables that affect the microcontrollers hardware. There is an extensive amount of documentation in the datasheet about the identities and effects of all of the peripheral registers.
Title: Re: RE: Low Pass Filter
Post by: septillion on Jun 21, 2017, 09:43 am
Unlike with cracks the register values is exactly explained in the datasheet ;) Although it looks "random" you can track it back exactly to what it means.
Title: Re: RE: Low Pass Filter
Post by: aarg on Jun 21, 2017, 11:10 pm
They shouldn't. Registers are just variables that affect the microcontrollers hardware. There is an extensive amount of documentation in the datasheet about the identities and effects of all of the peripheral registers.
But... they interact. So you have to be familiar with all the possible interactions to avoid difficulties.
Title: Re: RE: Low Pass Filter
Post by: Jiggy-Ninja on Jun 22, 2017, 01:53 pm
But... they interact. So you have to be familiar with all the possible interactions to avoid difficulties.
If you set them up to interact they will. If not, most of the modules are pretty independent of each other. Changing the ADMUX register isn't going to screw up any part of Timer2's operation, for example. It's nowhere near as bad as you're worried about.

And what are you afraid is going to happen if you mess up? If your sketch doesn't work properly, change something and load a new one. Registers are in RAM memory, any changes you make are cleared out after a reset so nothing you do will interfere with the bootloader. That level of worry is appropriate for fuses, not registers. Fuses are much harder to change (requiring a programming device) than registers precisely because they are more dangerous.

The bigger concern is if you have two bits of code trying to access the same resource, like if Tone and Servo both try to use Timer2. Then things get screwy. But that's a problem with libraries that hide away their access to the different peripheral resources. If you're setting them up yourself, you know exactly which bit of hardware is devoted to which task, so it won't be a problem.
Title: Re: RE: Low Pass Filter
Post by: ilovetoflyfpv on Jun 27, 2017, 01:19 pm
Hi Gang

Well I've downloaded the ATtiny datasheet (http://www.atmel.com/images/atmel-2586-avr-8-bit-microcontroller-attiny25-attiny45-attiny85_datasheet.pdf). I imagine an Electronics Engineer wrote it for Electronics Engineers. For us mere mortals where on earth do you start? There is no index. The word register appears 658 times. There are no examples. As I stated earlier, registers make me break out in a cold sweat.

Cheers

Jase :)
Title: Re: RE: Low Pass Filter
Post by: septillion on Jun 27, 2017, 01:53 pm
There is no index.
There is, in two places. First is at the back of the datasheet page 227). Second and easiest is the digital version in the PDF. If you open in in Acrobat Reader it's in the bookmark pane. In the reader from Firefox click the icon in the top left (shows page previews) and then the headings icon.

And yes, it's written for engineers and yes it takes some time to fully understand it. But there are lots of write ups on the internet to help you. For example this from Max Embedded (http://maxembedded.com/2011/06/introduction-to-avr-timers/). Although not for an ATtiny, the priciple is the same. You only need to find the right register names for the timer.

The word register appears 658 times.
Of course! Every damn thing in a micro is controlled by a register! Even a ATtiny is already a pretty complex device. That makes them powerful BUT complex.

There are no examples.
There are plenty but most are for assembly, not a higher level language like C/C++. But in AVR writing a register is as simple as
REGISTER_NAME = value

And in a register you can also use the bit names to set or clear them
REGISTER_NAME = 1 << BIT_NAME || 1 << BIT_NAME2
Or in short (aka the same)
REGISTER_NAME = _bv(BIT_NAME) || _bv(BIT_NAME2)

Or if you only want to set specific bits (and leave the rest)
REGISTER_NAME |= _bv(BIT_NAME) || _bv(BIT_NAME2)

Or clear specific bits
REGISTER_NAME &= ~(_bv(BIT_NAME) || _bv(BIT_NAME2))

But that is just bitwise operations and isn't specific for setting registers.

For example, to set PWM freqency of the ATmega328p to +-31kHz on pin 3 and 11
Code: [Select]
//Set PWM of timer2 (pin 3 & 11) to +-31kHz
  TCCR2B = (TCCR2B & 0b11111000) | 0x01;

(TCCR2B & 0b11111000) clears the first 3 bits of the TCCR2B (Timer/Counter Control, timer 2, register B) which are the CS (Set Clock) bits (see page 156 in the ATmega328p datasheet).
and | 0x01 sets the lower bit again. Which makes CS set to 0b001 which is clock without prescaling for timer 2.

Only thing on a ATtiny, I don't know which timer is used for millis() (and general timing) on the ATtiny. The PWM outputs are on timer0 so if it uses timer0 for millis() as well that means that messing with the speed of timer0 also changes the timing of millis() which is annoying...

As I stated earlier, registers make me break out in a cold sweat.
No need to. In contradiction to cracks/cheat codes the value of a register IS completely explained. Every bit is accounted for.