AtMega2560 -> Maximum ADC Frequency

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

I've an Arduino almost entirely reconfigured with the software as optimised as I can get it. Interrupts do little work, set flags and work is done in the loop.

 

There are some features always taking cycles, like PID control, LCD printing, stuff like that. 

 

The ADC measures four channels continuously and looks at one for a zero-cross. When one is detected it goes into "record" mode and accumulates values for the channels. At the next zero-cross record is disabled and computation on the results begins (RMS, real/apparent power etc). Once complete the process starts over. This way I can capture as many samples as possible without the CPU becoming a block. Now I'm onto how many samples is possible.

 

According to the datasheet the maximum frequency for 10 bit resolution is about 125KHz, so that is set.

 

 ADMUX = adc.currentChannel->pin;
  ADCSRB = bit (ADTS1) | bit (ADTS0); // trigger of timer 0 compare
  ADCSRA = bit (ADEN) | bit(ADATE) | bit (ADIE) | bit(ADPS2) | bit(ADPS1) | bit(ADPS0); // enable, auto-trigger interrupt, prescaler = 128 (125KHz)
  adcPrintDisplay1Full();

The datasheet also says the ADC takes 13 ticks which indicates a maximum resolution of 9.84KHz.

 

I configure to fire it off the timer at 5KHz.
 

  
  // timer 0 - trigger adc
  TCCR0A = bit (WGM01); // ctc mode
  OCR0A = 50U; // 5KHz (ADC requires 13 ticks min)
  OCR0B = 0;
  TIMSK0 = 0;
  TCCR0B = bit(CS00) | bit(CS01);  // 16000000/64 = 250000

Assuming the CPU can keep up, that's 1.25KHz per channel.

 

A typical channel frequency (for zero-cross) is 50Hz. 

 

That means I'm only getting 25 reads per period, which is really not enough to be accurate for measuring the RMS of an AC wave. 

 

So you know what's coming :). 

 

1. have I messed up the Hz the ADC can run at, misunderstood something?
2. any optimisations that can be done?
3. ditch the AtMega1280 and go for something much more powerful?
 

 

Last Edited: Sat. Nov 21, 2020 - 11:04 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Not sure what you are talking about...you can do about 10000-15000 samples/sec.  in 1/50th of a sec, that's 200 samples...isn't that plenty?

 

Much more importantly, you don't have to take of the samples in one cycle.  You could take just samples at random & over many cycles, you could get a very exact RMS value (over those cycles).

Now you may need to get an overall increase in total samples used, but they don't need so packed together in time.   

 

It's like throwing darts at the waveform...Even if you only have one dart , you will eventually get a good avg/rms reading (as long as you don't throw at the same spot each time).  Is make tak a lot of dart throws, but you have the time (maybe).

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Sun. Nov 22, 2020 - 03:52 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The OP says that 4  input signals are being sampled and wants more than 25 samples per cycle per channel.

 

Not sure what kind of "exact" is "very exact". Sort of like "nearly exact"? Or "Almost exact"? Random samplling works if you have a long sequence of identical cycles. There were sampling oscilloscopes that used that.

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

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

snoopy33 wrote:

3. ditch the AtMega1280 and go for something much more powerful?

 

For example, the AVR128DA64 is a chip of the same size but has a resolution of 12 bits and a capability of 130ksps.

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

Check out the XMEGA chips. They handle 2-bits per clock so they crank out a 12-bit result in 7 cycles instead of 10-bit in 13 cycles. They're also pipelined allowing two samples to be processed at the same time with only 1 cycle lag between them. The 100-pin A1U chips have dual ADC for even greater throughput.

 

 

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

The signals being measured are voltage and current and the AC in particular is low impedance and ends up being partially sinusoidal. 25 or 50 samples per period is just not going to cut it to form an accurate RMS value.

I also need to calculate the real vs apparent power, i.e detect the zerocross of both AC voltage and current and compute the time delta between them, which needs the resolution.

I''ll check out chips mentioned.

 

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

There specific chips that will do the grunt work way much better that a micro with an adc - and since they are for the power monitoring market, they are quite inexpensive. Analog Devices and Microchip have suitable product. There's also some Chinese chips that are really dirt cheap that have a bit of support in Arduino land. I don't think these ones are quite commercial measurement quality, but they are cheeeeeep!

I blew one of these up recently playing around with a wifi power switch that has power monitoring. Search for HLW8012

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

snoopy33 wrote:
25 or 50 samples per period is just not going to cut it to form an accurate RMS value

Wow - What harmonic content are you having to deal with ?

For pure sine just 2 samples are enough

 

However I believe you have chosen the wrong chip for multi-channel work. At the risk of being banned you should look at MSP430:

 

Texas Instruments

MSP430F67621A

Low-cost Polyphase Metering SoC with 3 Sigma-Delta ADCs, 10-bit SAR ADC, LCD, 64KB Flash, 4KB RAM

 

Relevant App Note: Implementation of a Low-Cost Three-Phase Electronic Watt-Hour Meter Using the MSP430F67641

NB: Microchip may have some competitor chip but it doesn't come to mind.

 

 

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

snoopy33 wrote:
for 10 bit resolution is about 125KHz,
the theoretical max is 200kHz and at 13 ADC clocks for a conversion that means a max 15.3k sample rate. You may find that slowing the CPU will get you closer to 200kHz

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

N.Winterbottom -> the AC source is a 2 phase generator, it's inductive and low impedance so highly affected by the load. I don't think 50 reads per period is that great a resolution and it assumes the CPU can keep up with the accumulation while it's servicing other interrupts.

 

I'm quite invested in the Arduino as a complete platform, for the LCD read-out, the serial monitor, the quick (and Linux compatible) upload interface, so I don't want to go near a chip which doesn't heavily lean towards open-source and ease of making everything work. MPLABX is ****** awful and I won't go near it unless I'm very desperate.

 

HLW8012 looks VERY nice as an add on chip. I wonder how well it deals with harmonics though. 

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

Microchip have MCP39F501which does the same thing in a simplier format than the TI chip. It also has frequency read-out.

First I need to verify the frequency range as I'm requiring 10-200Hz...

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

snoopy33 wrote:
I'm requiring 10-200Hz...

Now that "new-to-us-freaks" specification makes all the difference. It's now a non-trivial problem. I wonder how a 10Hz sub-harmonic affects things ?

 

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

Yes I should have elaborated more, I said a typical frequency is 50Hz but not the full range.

Microchip say it the F501 could work but it's not been tested outside of mains frequency. I imagine it's the same with all of them.

For now I'm back to whinging as much out of the 1284 as possible. I'll start with reading only the two AC channels and the DC channels afterwards rather than intertwining them.

There are 2nd and 3rd harmonics, and noise. It's not a nice easy perfect sine. wink 

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


snoopy33 wrote:

I said a typical frequency is 50Hz but not the full range.

Microchip say it the F501 could work but it's not been tested outside of mains frequency. I imagine it's the same with all of them.

I'm a bit confused.  Admittedly, I did most of my work with ADE7754 and others in this family which arrived up to two decades ago.  But if you are looking at "power meter" chips aren't you going to find something like

https://www.analog.com/media/en/...

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

snoopy33 wrote:
For now I'm back to whinging as much out of the 1284 as possible.
AVR D doubles the maximum ADC clock frequency and adds a ZCD peripheral.

AVR128DB48 - 8-bit Microcontrollers

 


ATmega1284P - 8-bit AVR Microcontrollers

What are the AVR28DA128 AVR32DA128 AVR48DA128 AVR64DA128 ?? | AVR Freaks

 

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