NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Sensors, Actuators, and Robotics » Slow sample rate down (prescaler?)

May 03, 2011
by amartinez
amartinez's Avatar

I am working with ADC (among other things). I'm trying to make a simple voltmeter just to try out. I am using the code for the tempsensor and utilizing the input pin 23 (PC0) as a voltage probe (0-5V)

Question: I don't like the sample rate, it's way too high. How can I slow it down?

The prescaler is set to 115.2Khz. I need it much lower, much much lower, possibly in the two digit hertz range meaning I need to divide 1/409600. At present it's 1/128. I need to slow the sample rate down for two reasons.

1- It will look better to me as the numbers won't cycle so fast 2- I would like to leave the MCU clock cycles free for other sensors in the future. I feel it's a waste of cycles.

Am I asking too much? If not, how is this done? Do I change this line of code? If so, what needs to be changed?

   CODE ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);

Thank you

Al

May 03, 2011
by Noter
Noter's Avatar

I don't think you can go less than 50khz. Take a look at chapter 23 in the datasheet and see if you come to the same conclusion.

May 03, 2011
by bretm
bretm's Avatar

Sure you can. Just don't start the next sample until you need it.

May 04, 2011
by Rick_S
Rick_S's Avatar

To paraphrase Noter and bretm, while you can't slow the actual speed of the ADC below a certain point (Noter), you can reduce the overhead you see in the tempsensor program by only taking a sample when you need to (bretm).

In other words, leave the ADC speed alone, but set the program to poll the ADC maybe once per second and then display the results. Or, take a 100 sample average once per second and display the result. If you want less frequent updates, span your sample time to every 2 or 3 seconds.

Rick

May 04, 2011
by Ralphxyz
Ralphxyz's Avatar

This is why I started my jumpy ADC thread.

Look at bretm's pseudo code. I have not tried it yet but it looks good.

Once we get this the solution should go into a FAQ don't you think?

Ralph

May 04, 2011
by Rick_S
Rick_S's Avatar

FAQ Plug BigGrin BigGrin

May 04, 2011
by Ralphxyz
Ralphxyz's Avatar

[quote]FAQ Plug ![](http://i166.photobucket.com/albums/u117/rdshear/0581.gif[/quote]

Well maybe there is some interest, I think this would be a good subject for a FAQ, of course having "Quotes" would be nice also.

Gee I was trying to not be so obvious.

Ralph

May 04, 2011
by Noter
Noter's Avatar

If one doesn't search the forum or the internet or read a datasheet, I doubt a FAQ would be referenced either.

May 04, 2011
by Ralphxyz
Ralphxyz's Avatar

Well speaking for myself, I often search the forum and the internet and also read datasheets and I would like a FAQ.

But, yes there would be those that would not reference one.

Ralph

May 04, 2011
by Rick_S
Rick_S's Avatar

I'm just goofing with you Ralph, I see you sneak in the subliminal hint pretty often.

Rick

May 04, 2011
by Noter
Noter's Avatar

I wasn't implying anything, I just don't see a FAQ here having enough value to justify the effort required to implement and maintain it. Everything that might be in it is already in the forums. But Maybe I form my opinion too quickly and I should look at some of the other forum FAQs you and Rick like. Can you give a link or two to your favorite FAQs on other forums?

May 05, 2011
by Ralphxyz
Ralphxyz's Avatar

Actually most FAQs are terrible, they are usually dated and not kept up or maintained by the administration.

I was thinking a community supported WiKi style FAQ would be a asset.

Ralph

May 05, 2011
by Ralphxyz
Ralphxyz's Avatar

So amartinez and others looking to slow the ADC sample rate down I published my working code (bretm's modifications) and Noter published an over sampling code sample that should also do the trick.

Ralph

May 06, 2011
by amartinez
amartinez's Avatar

Ralph, thank you for your help on this. Actually I got it to do exactly what I needed it to do using the delay_ms function right under the sample routine in the tempsensor code.

  for(i=0; i<100; i++) {
  last_sample = adc_read();
  this_temp = sampleToFahrenheit(last_sample);

  // Al note: This delay slows down the sample rate

  delay_ms(2);
  temp_avg = temp_avg + this_temp/100.0;

  }

Am I missing something? It seems to work fine.

Great thread guys. It was fun to read and really informative.

May 06, 2011
by bretm
bretm's Avatar

Nope, not missing anything. That's really all you need to do unless you want to do other work during those 2 milliseconds.

Post a Reply

Please log in to post a reply.

Did you know that multiple microcontrollers can communicate with each other? Learn more...