ATmega328: PWM as DAC

Go To Last Post
28 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm trying to use Timer/Counter1 to generate an analog output signal. So I configured it as PWM and connected a RC filter. The output is on pin PB1.

 

On the same port (pin PB4) I have a LED (SMD 0805 with 470R in series) that flashes every 500ms. Incredibly I see a 500ms noise overimposed on the analog output voltage derived from PWM.

 

I already tried to move the flash on PD3 (another port), but it seems the problem is there.

 

I think the cause is the high level voltage on PWM output that decreases when the LED is on.

 

So the question is: how to decrease this side effect at the minimum?

Last Edited: Tue. Dec 12, 2017 - 01:04 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Show us a picture of your setup, bread board perhaps???

 

 

Jim

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

pozzugno wrote:

I think the cause is the high level voltage on PWM output that decreases when the LED is on.

 

Is this just a guess or have you proved it?

 

Is you chip on a PCB or a prototype board?

 

Are your power rails properly decoupled?

 

Do you have a solid ground connection?

 

What value R and C do you have in your filter?

 

Any chance of a photo of your setup?

'This forum helps those who help themselves.'

 

pragmatic  adjective dealing with things sensibly and realistically in a way that is based on practical rather than theoretical consideration.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

pozzugno wrote:
a 500ms noise overimposed on the analog output voltage

How, exactly, are you "seeing" that?

 

Post a picture of what you're seeing.

 

Instructions for posting pictures: http://www.avrfreaks.net/comment...

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:
Is you chip on a PCB or a prototype board?

or a "solderless breadboard" ?

 

Do you have a solid ground connection?

Also, good, solid connections for the positive supply?

 

Please also post your schematic.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm using a good two-layers PCB (from a PCB manufacturer) and the MCU is soldered on the PCB, SMD version.

I have one 100nF capacitor near pins 4 and 6 (one capacitor for both), near pin 18 (AVCC) and 1uF near pin 20 (AREF). AREF is not connected (only capacitor).

 

Don't bother with PWM, I just tried without PWM, a simple GPIO configured as output high-level on PB1. The voltage on this pin changes if the LED on PB4 is switched on/off. The voltage change is around 20-30mV (I know, it is small, but the analog output voltage should be very stable).

 

If I change the pin of LED from PB4 to PD3 that isn't connected on the PCB, even if I move PD3 (1->0->1) the output on PB1 doesn't change. I think this means the cause of the problematic behaviour is the current drawn by the LED.

 

It is curious for me to see how the current drawn from one pin affects the voltage on a different pin of a different port.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

GPIO pins have an effective series resistance of about 80 ohms at Vcc=5V and about 100 ohms at Vcc = 3.3V. So, of course, if you add a load, the voltage will drop. That is the way the universe works!

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Still need to see:

 

  • schematic
  • photos
  • how are you measuring these voltages - if scope, post traces

 

pozzugno wrote:
It is curious for me to see how the current drawn from one pin affects the voltage on a different pin of a different port.

Obviously it will.

 

Any practical power supply, and all interconnections, have some "internal impedance" (resistance) - so any load drawn from it will affect the output to some degree.

 

You don't say if your VDD is 3V or 5V - but 20-32mV isn't much on either.

 

the analog output voltage should be very stable

That is exactly why there is a seprate analogue VDD!! It is precisely to decouple the analogue from the noise in the digital supply!

 

You wouldn't be running your LEDs without resistors, would you ... ?

 

(something which would be immediately obvious from a schematic)

 

EDIT

 

typo

 

added interconnect impedance (resistance)

Last Edited: Mon. Dec 11, 2017 - 04:58 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thinking about it a bit as I plan tonights dinner...

 

There is no guarantee that 'noise' will not couple from one port pin to another. It's certainly not anything that appears in the chip specs.

'This forum helps those who help themselves.'

 

pragmatic  adjective dealing with things sensibly and realistically in a way that is based on practical rather than theoretical consideration.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You mentioned the cap on AVcc, I assume AVcc is also tied to Vcc?

Add another 0.1 uF cap right at the AVcc pin to Ground.

 

The micro has thousands of gates, all switching on and off, and your project's LED switching on and off.

The current demands on the power supply bus, both on the PCB, and internally within the micro itself, are constantly changing.

Hence the voltage level has "noise" on it, and bounces up and down a bit.

 

You might, (untested), do a little bit better with the micro driving an NFet, (2N7000, for example), which draws a negligible load from the micro, and having the LED tied directly to the V+ supply, so its current is coming directly from the power supply, and not through the micro's internal power bus.

 

JC

 

 

 

Last Edited: Mon. Dec 11, 2017 - 05:26 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Do you observe the "noise" on VCC when the LED switches ON/OFF?

 

David (aka frog_jr)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What value if current limiting resistor do you have on your LED's!!!

 

Jim

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have a LED (SMD 0805 with 470R in series) that flashes every 500ms. Incredibly I see a 500ms noise overimposed on the analog output voltage derived from PWM.

 

So, I think Yes, and 470.

 

JC 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Oh yes - I see it now.

 

But that would have been immediately obvious from a schematic - trying to describe circuits & wiring in text is just folly.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It isn't a noise that couples from one point to another, externally or internally. If the LED is on for 1s, the voltage decreases and stays decreased for the entire second (not only on the edge).

So it is related to the current drawn from the LED and the internal impedance on VCC lines.

 

 

The SPI/debugWire connector isn't connected when I make the measures. The high output voltage on PB1 decreases when LED DL2 is switched on (i.e., PB4 is set to 1). LED DL1 is off.

 

I will try to use a transistor to draw the current for the LED from the VCC rails.

 

Pardon, the MCU is the ATmega328.

Last Edited: Tue. Dec 12, 2017 - 01:04 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

pozzugno wrote:
 it is related to the current drawn from the LED and the internal impedance on VCC lines.

and the power supply.

 

Which is all entirely to be expected - so where's the problem?

 

The PCB layout will affect this - If you want people to check your PCB layout, you're going to have to post that, too ...

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If your power supply drops its voltage when trying to drive one LED then next question is: "What are you using for a power supply?"

 

A watch battery won't (generally) be sufficient.

 

A decent power supply won't drop its voltage when an LED draws a few mA!

 

JC

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

 So I configured it as PWM and connected a RC filter. The output is on pin PB1.

 

You say you have an RC filter...where is it???!!?  You say you see noise...HOW MUCH?  Where is your power supply circuit? How can you expect help with a lousy explanation?

 

When in the dark remember-the future looks brighter than ever.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

DocJC wrote:
A decent power supply won't drop its voltage when an LED draws a few mA!

I think the power source is good because I see the voltage drop only to the output pin, not on the VCC rail.

 

avrcandies wrote:
You say you have an RC filter...where is it???!!? You say you see noise...HOW MUCH? Where is your power supply circuit? How can you expect help with a lousy explanation?

The RC filter is on a different schematic sheet. It's a typical second-order active low-pass filter (see here).

I already wrote the amount of drop voltage seen on the output pin (around 20-30mV). The VCC is 5V.

 

The power source is an external AC/DC with 5V output. On the PCB there are only some capacitors on VCC rails.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I still maintain that nothing in the chips design will ensure that one output's voltage will not alter as another output sinks or sources current. They are after all digital outputs not analogue.

 

You have two choices...

 

1) Take any high currents off-chip as in post #10 or

2) Buffer your PWM output with a CMOS gate.

'This forum helps those who help themselves.'

 

pragmatic  adjective dealing with things sensibly and realistically in a way that is based on practical rather than theoretical consideration.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can try to put a ferrite bead in series between the 5V supply and AVCC, and a large cap (let's say 100uF) in parallel with the 100nF AVCC-GND decoupling cap. This will probably reduce the ripple, but you can never completely eliminate it.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

More than likely it's the internal micro power bus's drop when driving 20ma on a pin.  Those micron-size "traces" are pretty thin.

 

Why don't you have some +5V supply caps on you processor?--I'm not talking about rf/"noise" caps, but power reserve caps, like 10uF (or even 100uf)...there don't appear to be any serious supply caps on your board.

You could try adding a heavy load  resistor onto your supply as a test...if too lightly loaded wallwarts & such can drift around...too light of a load can cause supply variations.   

You could also feed your logic PWM into a p-channel fet transistor or even n-chan), then filter...so then the output amplitude is then processor-independent. 

 

I see the voltage drop only to the output pin, not on the VCC rail

A scope in DC mode won't show 20mv out of 5V clearly.  A good meter will show it, but it may be an RMS value, which can be different than the avg value your PWM filter shows (so the rms could change little, but the avg change more).  A big cap will even things out.

 

If you know the drop well & repeatable, you could just turn up your PWM a few counts whenever the led is on (unless you need to generate 100% on)---see, software fixes everything.

 

 

When in the dark remember-the future looks brighter than ever.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So far we haven't seen any code!

Just to make sure that it's not related to something else. (like wrong init of the RS485 or something like that).

And are you sure that there aren't any shorts between pin etc!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

3) Convert PWM to analog by DAC

Linear Technology

Accurate, Fast Settling Analog Voltages from Digital PWM Signals

Design Note 538

by Mark Thoren and Chad Steward

http://cds.linear.com/docs/en/design-note/dn538f.pdf

Linear Technology

LTC2644 - Dual 12-/10-/8-Bit PWM to VOUT DACs with 10ppm/°C Reference

http://www.linear.com/product/LTC2644

 

"Dare to be naïve." - Buckminster Fuller

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@gchapman

Those look like interesting (unusual) parts.  Of course since the source it right there not (1000 feet on the end of a cable), you could just add an IIC DAC & skip the pwm altogether.

Bet these parts would cost a lot more than a single transistor & some RC's

 

You can get a faster response by cascading two "quick" RC's.  The cascade is like washing your hands twice--very effective in removing dirty stuff.  

When in the dark remember-the future looks brighter than ever.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:
Those look like interesting (unusual) parts.
That's about what was stated in the current edition of AofE.

avrcandies wrote:
... you could just add an IIC DAC & skip the pwm altogether.
or the 8b DAC in a tiny214, or a delta-sigma DAC (bit-stream in, analog out)

avrcandies wrote:
Bet these parts would cost a lot more than a single transistor & some RC's
Indeed ... 8b LTC2644 are minimum 3USD each for 100.

avrcandies wrote:
You can get a faster response by cascading two "quick" RC's.
LTC2644 has zero latency and settles in 8micro-second typical.

pozzugno mentions analog filtering; assumption is a typical RC will work well.

 


http://www.cambridge.org/us/academic/subjects/physics/electronics-physicists/art-electronics-3rd-edition

http://www.microchip.com/wwwproducts/en/ATTINY214

 

"Dare to be naïve." - Buckminster Fuller

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

I have been playing with the "synchronous filter" DAC described in  https://www.edn.com/design/analog/4323340/Fast-settling-synchronous-PWM-DAC-filter-has-almost-no-ripple with the basic circuit shown in https://m.eet.com/media/1131161/14342-figure_1.pdf with additional parts for stability and to reduce the affects of control coupling to switch input/output.

 

An ATmega328, clocked at  8 MHz and 8 bit Counter/Timer0, clocked at full CPU speed provide both PWM output and Sample/Hold clock with a 32usec PWM period.  Counter/Timer's Compare Register A is used to vary the PMW duty cycle (PT0) and Compare Register B is set at 1/2 the PWM Period to provide the Sample/Hold command (PT1).

 

My PWMDAC  implementation was built on a plug-in prototyping board with parts that I had in house (Sparkfun voltage translator, CD4066BC switches, CD4049 inverters, TL084 op amp) so I did not expect very good static results but was surprised when measured DC out (w. a Fluke 77) for 9 steps of duty cycle was linear within 12 bits (0.024% of maximum output).  I am about to check the circuit's dynamic performance with a 32 sample sine wave duty cycle but expect its performance to be somewhat filtered since I sized the sample and hold capacitors to charge within 96% of the steady state value during the sample half of the PWM period (i.e. time constant of the typical switch resistance and hold capacitance values to be 1/3 of half the PWM period).

 

When dynamic testing is complete, I plan to implement the same circuit with two MAX4266 switches and TL082 dual op amp so its price and parts count should be low.

 

BTW, the Linear LTC2644 is only available in a MSOP package which is needlessly small for my applications. In addition, it would be a challenge for me to solder even using techniques presented in  AVRfreaks.

 

Alan

 

BTW there is a nice thread on filtering PWM at http://www.avrfreaks.net/forum/d...

Last Edited: Sun. Dec 24, 2017 - 12:26 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Interesting circuit yes