Teensy 3.2. A little guidance on ADC please

Status
Not open for further replies.

muggins

Well-known member
I have a project requiring 3 low resolution measurements at say 5 second intervals. More importantly I need to sample at 12 bit resolution another input at precise 2ms intervals. A couple of questions.
1. Should I use the ADC library by Pedvide
2. Can I/Should I separate the low and high resolution to the 2 ADC channels
3. For the high resolution does this make any sense: (in plain English) Without going into detail there are occasions when the interval will be more than 2ms.
4. How many readings am I likely to get during say the 2ms interval

dim a counter
dim total value
do until next interval
total value += analog read
increment counter
loop
averaged value = total value/counter

thanks
 
Even with simple, not-very-efficient code, the ADC can easily make 100000 measurements/sec. So 2 ms intervals ought to be pretty simple.
 
That equates to 200 readings in a 2ms interval. I have a lot going on in my sketch and am only getting 18 readings per 2ms
 
Well, there's a variety of ways to improve this, with varying levels of complexity.

Pedvide's ADC library may be the best path.

Another approach that might work is analogRead from inside an IntervalTimer interrupt function, as long as you don't also access the ADC from your main program.

The hardware has many ways for timers to automatically trigger ADC measurements, but accessing the low-level registers and managing the hardware directly has a steep learning curve.
 
Thanks Paul
I'll have a look at the Pedvide library. I'm a little confused about using analogRead inside an IntervalTimer. Surely inside the timer actions occur more slowly dependent on the interval. I have analogRead "free" running in the loop and then within the timerInterval I have averaging.
 
I don't fully understand what you're trying to do.

Do you want to take a measurement every 2ms or make a rolling average of as many measurements as possible during 2ms and store/use it?

The first is very easy using IntervalTimer and the ADC library (have a look at the examples folder).

The second one requires a bit more work, but it's fairly easy too. Setup a continuous measurement with the maximum number of repetitions (so the hardware does most of the work). Set a interrupt that updates the rolling average into a (volatile!) variable. Then, either inside of the isr or in the main loop (or even a IntervalTimer), detect when 2ms have passed and do whatever you want.
Now, you're focusing on the number of measurements per 2ms, but that's not really important. The best way to get a high quality and low noise signal is to do fewer measurements with a slower speed. Set both the conversion and sampling speeds to the minimum for that.

For the other measurements every 5 seconds you should use the other ADC. Setup that ADC with whatever resolution/speed you want. Both ADCs are independent.
 
Hi
I do want to take as many measurements as possible in the 2ms. I think? I'm doing what you suggest, that is a continuous measurement within the loop. Then during each interval I add all the measurements and divide by number of counts. This seems the logical way for me as the interval can sometimes be longer.
(there is an attached servo that accelerates up to it's maximum speed which is 2mS delay between incremental movements.) Is the rolling average a better bet? How do I implement that?
thanks.
 
Status
Not open for further replies.
Back
Top