Polly
Member
Hello,
I am writing a code that monitors a pin (which is attached to a signal generator) using the ADC i am using the ADC library written by pedvide and am using a Teensy 4.1. The code then writes the signal to a .csv file on an SD card.
I need this to happen as fast as the Teensy can manage as I am trying to recording ultrasound upto about 350kHz. What I am seeing (and will include below) is that it doesn't seem to be using the 10bit range and is cutting the signal up into steps which are effect y-axis resolution. Each of these steps is sampled either 16 or 17 times and then the next point is sampled. On the lower frequency signals (10kHz I know this isn't ultrasound frequency I just needed to sanity check a few things) the sine wave is coming out fairly smooth but as I move faster (50kHz and 100kHz) it becomes more and more prone to the stepping effect (which makes sense).
My question is why is this stepping occuring and what could I try to make the signals recorded smoother. Code is below, along with some pictures of the graphs I can create afterwards, I can't upload my result files (these are .csv, but I have changed the extension to .txt and .xls and neither will upload so I think that might be due to file size for some reason?
Many thanks
Polly
My code:
I am writing a code that monitors a pin (which is attached to a signal generator) using the ADC i am using the ADC library written by pedvide and am using a Teensy 4.1. The code then writes the signal to a .csv file on an SD card.
I need this to happen as fast as the Teensy can manage as I am trying to recording ultrasound upto about 350kHz. What I am seeing (and will include below) is that it doesn't seem to be using the 10bit range and is cutting the signal up into steps which are effect y-axis resolution. Each of these steps is sampled either 16 or 17 times and then the next point is sampled. On the lower frequency signals (10kHz I know this isn't ultrasound frequency I just needed to sanity check a few things) the sine wave is coming out fairly smooth but as I move faster (50kHz and 100kHz) it becomes more and more prone to the stepping effect (which makes sense).
My question is why is this stepping occuring and what could I try to make the signals recorded smoother. Code is below, along with some pictures of the graphs I can create afterwards, I can't upload my result files (these are .csv, but I have changed the extension to .txt and .xls and neither will upload so I think that might be due to file size for some reason?
Many thanks
Polly
My code:
Code:
#include <Wire.h>
#include <TimeLib.h>
#include <SPI.h>
#include <SD.h>
#include <ADC.h>
#include <ADC_util.h>
#include <ADC_Module.h>
const int BuffSize = 110000;
int value[BuffSize];
ADC *adc = new ADC();
void setup()
{
pinMode(16, INPUT_DISABLE);
pinMode (A2, INPUT);
Serial.begin(9600);
adc->adc1->setReference(ADC_REFERENCE::REF_3V3);
adc->adc1->enableInterrupts(adc1_isr);
adc->adc1->setAveraging(1); // set number of averages
adc->adc1->setResolution(10); // set bits of resolution
adc->adc1->setConversionSpeed(ADC_CONVERSION_SPEED::HIGH_SPEED); // change the conversion speed
adc->adc1->setSamplingSpeed(ADC_SAMPLING_SPEED::VERY_HIGH_SPEED); // change the sampling speed
adc->adc1->startContinuous(A2);
!SD.begin(BUILTIN_SDCARD);
}
void loop()
{
for(int i=0; i<BuffSize;i++)
{
value[i] = (uint16_t)adc->adc1->analogReadContinuous();
}
//Open a file and write to it.
File dataFile = SD.open("File001.csv", FILE_WRITE); // *** name is in 8.3 format *** add date to this some how, perhaps make it automatic??
if (dataFile)
{
for(int i=0; i<BuffSize;i++)
{
dataFile.print(micros()); // i added this after to check it was reading at different time points and not just writing the same time point over and over again
dataFile.print(",");
dataFile.println(value[i]);
}
dataFile.close(); //Data isn't actually written until we close the connection!
}
else{}
delay(0.1);
}
void adc1_isr(void) {
adc->adc1->analogReadContinuous();
}