Teensy 3.2 acquisition of data in a time interval

Status
Not open for further replies.

Lexoli

Member
Hi, I bought a teensy 3.2 and I have to work on a project. I am fairly new in the world of programming, and I have some questions.
I have to acquire the signal from a photodiode placed in front of a LED, I have to turn on and off the LED every 220 us, and do about 10 misureas in each time interval. then the sampling frequencies of about 50 kHz, such a system is feasible with this microcontroller?
Where can I find information on the various registers that control the ADC and timers counter?
and information such as the default clock frequency or the prescaler of the ADC or the bits of the various counter Timers?
I can not find a manual where to access this information, someone could direct me to the right path? Thanks for your help.
 
The ADC Library is a good place to go, also the Teensy 3.2 can run the ADC with DMA meaning the CPU does not have to do any of the work or waiting. Or if you set the ADC up right you can do Analog Reads in about 5uS each.

There is a DMA Ring Buffer example with the ADC library that is packaged with the Teensyduino software.

If your looking to go in deep with Register control then the manual is probably the best place to go.
 
Yes, very feasible.

This is much simpler if you don't have to rigidly adhere to a 50 kHz sample rate. That can be done, and the audio library code is probably the place to start looking for the complex details of using the PDB timer and DMA to efficiently acquire data at a fast, fixed sample rate.

But if the timing is just a matter of reading the (probably very noisy) photodiode signal many times so you can get a good average for both ambient room conditions (the LED off) and for your LED path, just a loop running analogRead() many times should be pretty close. It's about 20 microseconds per measurement with the default settings.

In fact, I just did a quick test:

Code:
void setup() {
}

void loop() {
  int sum = 0;
  elapsedMicros usec=0;
  for (int i=0; i < 20; i++) {
    sum += analogRead(A0);
  }
  Serial.print(usec);
  Serial.print(", ");
  Serial.println(sum);
}

Indeed it prints this: (with A0 floating... just random noise)

Code:
199, 3306
200, 2871
199, 2849
198, 3174
199, 3236

Don't forget a brief delay between changing the LED and reading the signal. If your photodiode is connected with the normal current-to-voltage opamp circuit using a large value resistor, the sudden change in LED current will almost certainly disturb the opamp and take a few microseconds to setting back to it's normal (probably noisy) steady state.
 
Last edited:
Thanks for the answers :) I looked at the manual (very complex, but in the extreme case will draw inspiration from it).
I tried to write this code, and working a bit 'better with the timing could fit in my specifications. Is right to work in this way we say "brutal" or agrees to work with interrupts? another question: do you know the default adc's sampling frequency?

void setup() {
pinMode(15, OUTPUT);
Serial.begin(115200);
}

void loop()
{
elapsedMicros usec=0;
int sum=0;
int sum1=0;
digitalWrite(15,HIGH);
for (int i=0; i < 20; i++) {
sum += analogRead(A5);
}

digitalWrite(15,LOW); for (int i=0; i < 20; i++) {
sum1 += analogRead(A5);
}

int val= (sum-sum1)/20;

delayMicroseconds(1240);

Serial.print("The signal is: ");
Serial.print(val);
Serial.print(", in ");
Serial.println(usec);
}



The signal is: 50, in 1645
The signal is: 24, in 1652
The signal is: 74, in 1645
The signal is: -27, in 1648
The signal is: 0, in 1650
The signal is: 16, in 1649
 
do you know the default adc's sampling frequency?

The default way, where you call analogRead() in a loop, does not have a precisely defined sampling frequency. It's approximately 50 kHz, but varies depending on the speed of the loop. If other libraries are active and using interrupts, the speed can change as well.
 
Good evening, I have a question probably stupid.
why if I make a test with a simple program like this I get this strange problem?
I have a delay in each cycle of a few microseconds (9-10), then suddenly every 4-5 cycles have a delay of 950 microseconds, can you tell me why?

Code:
void setup() {
}

elapsedMicros usec;

void loop() {
Serial.println(usec);
}


Code:
281038687
281038698
281038709
281039648
281039659
281039670
281039681
281039693
 
Status
Not open for further replies.
Back
Top