frankzappa
Well-known member
Look at the ADC library included in TeensyDuino Installer: pedvide.github.io/ADC/docs/Teensy_4_html/namespace_a_d_c__settings.html
> there may be a better example with more tricks
> maybe 12 bits isn't needed - faster
> maybe averaging is required - slower
Is desired 200Ksps across all 8 pins - or each pin? Code below manages 704,208 combined single sample reads per second for the pins in the array list - 88K reads of 8 pins.
A quick hack to this sample "...\hardware\teensy\avr\libraries\ADC\examples\readAllPins\readAllPins.ino" is doing a 12 bit read with single sample ( no avg ) ::
Code:A0: 0.34. A1: 0.00. A2: 0.62. A3: 0.94. A4: 0.99. A5: 1.08. A6: 1.12. A7: 1.06. 8 pins read 88026 times per second
Hacked code follows - adjusted for running on T_4.1 <#elif defined(ADC_TEENSY_4_1) // Teensy 4.1> - it reads all it can of 8 pins, then once per second reports value read and how many times all were similarly read in prior second::
Code:/* Example for analogContinuousRead It measures continuously the voltage on pin A9, Write v and press enter on the serial console to get the value Write c and press enter on the serial console to check that the conversion is taking place, Write t to check if the voltage agrees with the comparison in the setup() Write s to stop the conversion, you can restart it writing r. */ #include <ADC.h> #include <ADC_util.h> ADC *adc = new ADC(); // adc object #if defined(ADC_TEENSY_LC) // teensy LC #define PINS 13 #define PINS_DIFF 2 uint8_t adc_pins[] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12}; uint8_t adc_pins_diff[] = {A10, A11}; #elif defined(ADC_TEENSY_3_0) // teensy 3.0 #define PINS 14 #define PINS_DIFF 4 uint8_t adc_pins[] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13}; uint8_t adc_pins_diff[] = {A10, A11, A12, A13}; #elif defined(ADC_TEENSY_3_1) || defined(ADC_TEENSY_3_2) // teensy 3.1/3.2 #define PINS 21 #define PINS_DIFF 4 uint8_t adc_pins[] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, A20 }; uint8_t adc_pins_diff[] = {A10, A11, A12, A13}; #elif defined(ADC_TEENSY_3_5) // Teensy 3.5 #define PINS 27 #define PINS_DIFF 2 uint8_t adc_pins[] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, A20, A21, A22, A23, A24, A25, A26 }; uint8_t adc_pins_diff[] = {A10, A11}; #elif defined(ADC_TEENSY_3_6) // Teensy 3.6 #define PINS 25 #define PINS_DIFF 2 uint8_t adc_pins[] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, A20, A21, A22, A23, A24 }; uint8_t adc_pins_diff[] = {A10, A11}; #elif defined(ADC_TEENSY_4_0) // Teensy 4.0 #define PINS 14 #define DIG_PINS 10 #define PINS_DIFF 0 uint8_t adc_pins[] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13}; uint8_t adc_pins_dig[] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9}; uint8_t adc_pins_diff[] = {}; #elif defined(ADC_TEENSY_4_1) // Teensy 4.1 #define PINS 8 #define DIG_PINS 10 #define PINS_DIFF 0 uint8_t adc_pins[] = {A0, A1, A2, A3, A4, A5, A6, A7}; uint8_t adc_pins_dig[] = {A0, A1, A2, A3, A4, A5, A6, A7, A8, A9}; uint8_t adc_pins_diff[] = {}; #endif // defined void setup() { pinMode(LED_BUILTIN, OUTPUT); for (int i = 0; i < PINS; i++) { pinMode(adc_pins[i], INPUT); } Serial.begin(9600); ///// ADC0 //// adc->adc0->setAveraging(1); // set number of averages adc->adc0->setResolution(12); // set bits of resolution adc->adc0->setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED); // change the conversion speed adc->adc0->setSamplingSpeed(ADC_SAMPLING_SPEED::HIGH_SPEED); // change the sampling speed ////// ADC1 ///// #ifdef ADC_DUAL_ADCS adc->adc1->setAveraging(1); // set number of averages adc->adc1->setResolution(12); // set bits of resolution adc->adc1->setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED); // change the conversion speed adc->adc1->setSamplingSpeed(ADC_SAMPLING_SPEED::HIGH_SPEED); // change the sampling speed #endif delay(500); } int value = 0; int pin = 0; uint32_t lC = 0; uint32_t lShow = 0; elapsedMillis lT = 0; void loop() { lC++; if ( lT >= 1000 ) { lShow = lC; lC = 0; } int i; for (i = 0; i < PINS; i++) { value = adc->analogRead(adc_pins[i]); // read a new value, will return ADC_ERROR_VALUE if the comparison is false. if ( lShow ) { Serial.print("A"); Serial.print(i); Serial.print(": "); Serial.print(value * 3.3 / adc->adc0->getMaxValue(), 2); Serial.print(". "); if (i == 9) { Serial.println(); } else if (i == 11) { Serial.print("\t"); } else if (i == 13) { Serial.print("\t"); } else if (i == 22) { Serial.println(); } } } if ( lShow ) { Serial.printf("\n\t%u pins read %u times per second \n", i, lShow ); lShow = 0; lT = 0; } // the actual parameters for the temperature sensor depend on the board type and // on the actual batch. The printed value is only an approximation //Serial.print("Temperature sensor (approx.): "); //value = adc->analogRead(ADC_INTERNAL_SOURCE::TEMP_SENSOR); // read a new value, will return ADC_ERROR_VALUE if the comparison is false. //Serial.print(": "); //float volts = value*3.3/adc->adc0->getMaxValue(); //Serial.print(25-(volts-0.72)/1.7*1000, 2); // slope is 1.6 for T3.0 //Serial.println(" C."); // Print errors, if any. if (adc->adc0->fail_flag != ADC_ERROR::CLEAR) { Serial.print("ADC0: "); Serial.println(getStringADCError(adc->adc0->fail_flag)); } #ifdef ADC_DUAL_ADCS if (adc->adc1->fail_flag != ADC_ERROR::CLEAR) { Serial.print("ADC1: "); Serial.println(getStringADCError(adc->adc1->fail_flag)); } #endif adc->resetError(); }
Wow thanks, I will look into that. I’ve seen the pedvide library but I haven’t tried to implement it yet. The examples in the library are not making much sense right now. I need to learn more. I’m just starting out with coding and I’ve learned the C syntax so far. Need to study more to understand the stuff in that library.
I’ve looked at the examples and I understand parts of it but I don’t know what parts of the code I need.
I need 200 ADC readings per ms in total.
So every sensor needs to do 20 readings per ms. Although more is always better as long as they are reasonably accurate. The reason more is better because I also read the time difference of arrival of peaks from the different sensors so faster reads give less error because they read in sequence.
I think 0-1023 is preferable but 0-512 would also work. (Is that 10 bit?).
I could also read all sensors periodically and store them in a multidimensional array. Then every row of the array would be a fraction of a ms so every reading would have a time signature by the position in the array. After the 2ms has passed I could evaluate all the readings.