Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

PWM Pin Breakdown


  • Please log in to reply
10 replies to this topic

#1 Bendage

Bendage

    Advanced Member

  • Members
  • PipPipPip
  • 153 posts
  • LocationIrvine, CA

Posted 11 March 2012 - 03:49 AM

Can anyone fully explain the PWM pin in retrospect to the Netduino board and Micro Framework. 1. How do they work? Whats going on behind the scenes. 2. How to access them to their full (hack) potential and manipulate them with the Micro Framework? 3. What are the definitions of the SetPulse parameters and how does it equate to Mhz conversion? Max/min values and how they equate? 4. What is SetDutyCycle and how does is relate and how can I use it? Maybe give a thorough example of how it controls a servo and what is really happening behind the scenes. Thank you.
  • Arron Chapman likes this

#2 Bendage

Bendage

    Advanced Member

  • Members
  • PipPipPip
  • 153 posts
  • LocationIrvine, CA

Posted 15 March 2012 - 02:40 PM

Bump... Anyone wanna take a crack at this?

#3 Paul Newton

Paul Newton

    Advanced Member

  • Members
  • PipPipPip
  • 724 posts
  • LocationBerkshire, UK

Posted 15 March 2012 - 06:36 PM

HI Ben,

I have done very little with PWM, what I did do was not too hard once I had a few key bits of knowledge.

Have you seen this Wiki page? I think it will help you get started.

Regards - Paul

#4 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 15 March 2012 - 06:43 PM

There is also very nice article by Pete Brown.

#5 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 15 March 2012 - 06:53 PM

Can anyone fully explain the PWM pin in retrospect to the Netduino board and Micro Framework.

1. How do they work? Whats going on behind the scenes.
2. How to access them to their full (hack) potential and manipulate them with the Micro Framework?
3. What are the definitions of the SetPulse parameters and how does it equate to Mhz conversion? Max/min values and how they equate?
4. What is SetDutyCycle and how does is relate and how can I use it?


Maybe give a thorough example of how it controls a servo and what is really happening behind the scenes.

Thank you.


Bendage!...How are you going?

Here are some details...
The PWM is NOT a port, but rather a particular drive applied on an OutputPort.
The ultimate goal is having an analog output, even not having the hardware dedicated. In other words, the goal is to symmetrize the AnalogPort, which converts a "real" value (in terms of voltage) applied in it, to a numeric value, ranging from 0 to 1023 (for the Netduino).
The hardware components acting the analog-to-digital conversion is called ADC, and it's embedded in the Netduino microcontroller. For making the opposite function, so generating a "real" voltage from a number, the component is called DAC.
Both ADCs and DACs are typically expensive, and the manufacturers tend to avoid to surcharge a MCU without any valid reason. Furthermore, while an ADC can't be replaced easily with equivalent solutions, the DAC can be "simulated" with a PWM output drive.

The ability of simulate a DAC rely on several limitations:
  • the precision is much lower;
  • the resulting voltage is *FAR* from being stable ("stable" is not the right term, but let's see later...)
  • the settling time is much slower.
So, well...if you are going to dim a led, a lamp or so, a PWM is surely enough. Even for a small bot the PWM is okay, but if you would like to generate accurate waveforms, voltages, etc. the DAC is mandatory. Think to a music player or a motorized arm.

The principle is somewhat simple.
The PWM stage drives the output port as a square wave, which is a period waveform having only two states (high and low). The ratio of the high-level time to the full period time is called "Duty-Cycle", thus it spans from 0% to 100%.
From the secondary school, you can calculate the area of the rectangle having height=+V and as base the duration of the high level. This area could be thought as the energy given by the battery (via the port). By the way, this area should be seen over the whole period, so the *AVERAGE* is proportional to the duty-cycle.

We still haven't mentioned how should be the "whole" period. Mathematically the average is always the same, but...if the period were 10 seconds, your light dimmer would be a garbage.
Indeed, the frequency (which is the inverse of the period) should be chosen carefully, according to the kind of device that you want to drive.
In the Netduino (4.1) the frequency has been fixed at 10kHz, which is a pretty good choice. If you connect a led or a bulb, of sure you won't notice any blink...I guess that maybe many animals cannot perceive a so high frequency. In particular for a bulb, the warm inertia is huge respect to the oscillation of the PWM, thus the brightness is actually "stable". For a led is very different, and 10kHz is still a frequency able to be reproduced "by live".

Most of the users love to use the PWM with small motors. I don't want to talk about how to drive a motor, because is a very complex task. If you connect a scope on a motor leads, you'll be scared.
Instead, I'd like to point out what I meant for "stable". The correct term should be "continuous", but I was afraid to create confusion with the ability to span the voltage from 0 to +3.3V, as the brother ADC does.
Well, yeah...the problem is right the square wave, because it's so: it's a square wave, not a continuous level. Thus you must to "filter" it. To filter it you should add capacitors and inductors, then resistors...all that is cumbersome, and moreover increase the slowness of the response.

As "response" I mean how fast my average voltage takes to settle from a certain value to another one. For instance, my PWM is set to 50%, then I set to 100%. Of sure the square wave changes immediately, but -once filtered- how long will take the filtered voltage to get to the maximum?
That's another fault of the PWM respect to the DAC.

How is the PWM made internally?
Yet another "stupid" machine. It's just a free counter, let's say counting at 1MHz rate (I don't know by real). This counter is pretty limited, and it counts up to 1000 (just as an example), then rolls to zero again.
Then there's a "comparator", that matches the counter value with a prefixed set, basically the "duty-cycle". When you set the duty at 35%, then the low-level firmware sets 350 as value to be matched. When the comparator matches the preset, then lowers the output, till the counter rolls to zero. At that point the output is pulled high for a new comparison.

I don't know very much the features of the Netduino PWM, nor the changes made on the firmware 4.2.
In the 4.1 version the frequency is fixed at 10kHz, but should be variable in the new version.
The SetPulse function, however, should allow to set different frequencies, even on the 4.1.

Hope it helps.
Cheers
  • Arron Chapman likes this
Biggest fault of Netduino? It runs by electricity.

#6 Paul Newton

Paul Newton

    Advanced Member

  • Members
  • PipPipPip
  • 724 posts
  • LocationBerkshire, UK

Posted 15 March 2012 - 07:15 PM

There is also very nice article by Pete Brown.


Yes, Pete's article was very helpful to me, but I think there is a mistake in the example code.
I did ask a question on the page, but he never replied.

He says the values are in micro-seconds, but then in his code examples he multiplies by a million to get milli-seconds:

const uint period = 3 * 1000 * 1000;    // 3 ms


I'll leave it to you to make your own mind up.
Someone tell me if I am being stupid.

Paul

#7 Bendage

Bendage

    Advanced Member

  • Members
  • PipPipPip
  • 153 posts
  • LocationIrvine, CA

Posted 16 March 2012 - 03:47 AM

Nicely done Mario! Nicely done! And thanks for the links CW2 and Paul! Hope you guys aren't getting irritated. I am the type of person that when I get into something, I like to master it. There are 100 guys on here that are just like me. I'm just the only one who does not care to show how stupid I am by asking B) How else you gonna learn? A lot of this knowledge is held in the heads of those who own an oscilloscope and a logic analyzer. Some day soon!
  • Arron Chapman likes this

#8 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 16 March 2012 - 06:36 AM

Bendage, here's my proposal... I'm setting up some idea for helping the users like you on understanding better how certain circuits work. As you can see, English is not my mother-language, and I'm having a little effort in writing correctly...I have to review, and correct often what I write. Well, would you create some Wiki pages, as Arron Chapman suggested, by cleaning up the posts, and then I'll check the result? It is not lazyness, but I'm plenty of things to sort out, and I'd disliked to be depicted as the "eternal promise"... Yes, I'm just like a stack going overflow: many pushes, few pops... Cheers
Biggest fault of Netduino? It runs by electricity.

#9 Bendage

Bendage

    Advanced Member

  • Members
  • PipPipPip
  • 153 posts
  • LocationIrvine, CA

Posted 28 August 2012 - 06:46 AM

Yes, Pete's article was very helpful to me, but I think there is a mistake in the example code.
I did ask a question on the page, but he never replied.

He says the values are in micro-seconds, but then in his code examples he multiplies by a million to get milli-seconds:

const uint period = 3 * 1000 * 1000;    // 3 ms


I'll leave it to you to make your own mind up.
Someone tell me if I am being stupid.

Paul


He still never did get back to you Paul did he. Well, I ordered a scope today and can't wait to get to the bottom of this.

Stand by :)

#10 Bendage

Bendage

    Advanced Member

  • Members
  • PipPipPip
  • 153 posts
  • LocationIrvine, CA

Posted 12 September 2012 - 02:25 AM

Paul.... Milliseconds it is

#11 Paul Newton

Paul Newton

    Advanced Member

  • Members
  • PipPipPip
  • 724 posts
  • LocationBerkshire, UK

Posted 12 September 2012 - 05:53 AM

Paul.... Milliseconds it is


So I'm not going mad after all. :blink:




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.