Lavanya rajan
Well-known member
Hi all,
Back again with ADC related question. I have Teensy 4.0 and ADS1220 ADC. I have an ADXL356 based Accelerometer sensor which captures vibration and its output is mapped to ADS1220. I need to continuously read 2k samples from ADC, calculate vibration in terms of Acceleration (either g or m/s2) and plot FFT and find peak frequency.
The input to the sensor is generated from frequency generator with frequency of 159.2 Hz and amplitude of 1V rms
The ADC is purchased from https://cyberblogspot.com/how-to-use-ads1220-adc-module-with-arduino/.
However when I check the output the peak frequency is not detecting properly.
I'm attaching the code I used. Can anyone check and let me know the issue
Back again with ADC related question. I have Teensy 4.0 and ADS1220 ADC. I have an ADXL356 based Accelerometer sensor which captures vibration and its output is mapped to ADS1220. I need to continuously read 2k samples from ADC, calculate vibration in terms of Acceleration (either g or m/s2) and plot FFT and find peak frequency.
The input to the sensor is generated from frequency generator with frequency of 159.2 Hz and amplitude of 1V rms
The ADC is purchased from https://cyberblogspot.com/how-to-use-ads1220-adc-module-with-arduino/.
However when I check the output the peak frequency is not detecting properly.
I'm attaching the code I used. Can anyone check and let me know the issue
Code:
#include "Protocentral_ADS1220.h"
#include <SPI.h>
#include <arduinoFFT.h>
#define SPISPEED 2500000
#define SAMPLES 1024
#define SAMPLING_FREQUENCY 1000
#define PGA 1 // Programmable Gain = 1
#define VREF 5.06 // Internal reference of 2.048V
#define VFSR VREF/PGA
#define FULL_SCALE (((long int)1<<23)-1)
#define ADS1220_CS_PIN 28
#define ADS1220_DRDY_PIN 32
Protocentral_ADS1220 pc_ads1220;
int32_t adc_data;
volatile bool drdyIntrFlag = false;
unsigned long microseconds;
arduinoFFT FFT = arduinoFFT();
unsigned int sampling_period_us;
int i;
double k[SAMPLES];
float adc_volt;
float adc_g;
double vReal[SAMPLES];
double vImag[SAMPLES];
void drdyInterruptHndlr(){
drdyIntrFlag = true;
}
void enableInterruptPin(){
attachInterrupt(digitalPinToInterrupt(ADS1220_DRDY_PIN), drdyInterruptHndlr, FALLING);
}
void setup()
{
Serial.begin(9600);
SPI.beginTransaction(SPISettings(SPISPEED, MSBFIRST, SPI_MODE1)); // start SPI
pinMode(ADS1220_CS_PIN, OUTPUT);
digitalWrite(ADS1220_CS_PIN, LOW); // tied low is also OK.
pinMode(ADS1220_DRDY_PIN, INPUT);
pc_ads1220.begin(ADS1220_CS_PIN,ADS1220_DRDY_PIN);
//pc_ads1220.set_data_rate(DR_1000SPS);
pc_ads1220.set_pga_gain(PGA_GAIN_1);
pc_ads1220.set_OperationMode(MODE_TURBO);
pc_ads1220.set_conv_mode_continuous(); //Set Single shot mode
pc_ads1220.Start_Conv();
sampling_period_us = round(1000000*(1.0/SAMPLING_FREQUENCY));
}
void loop()
{
Serial.println("Samples started");
savesamples();
}
void test()
{
adc_data=pc_ads1220.Read_SingleShot_SingleEnded_WaitForData(MUX_SE_CH0);
adc_volt= convertToMilliV(adc_data)/1000;
adc_g= adc_volt/0.02; //sensitivity of accelerometer
//Serial.println(adc_g);
// delay(100);
}
float convertToMilliV(int32_t i32data)
{
return (float)((i32data*VFSR*1000)/FULL_SCALE);
}
void savesamples()
{
for( i=0; i<SAMPLES; i++)
{
microseconds = micros();
test();
k[i]=adc_g;
//Serial.println(k[i]);
vReal[i] = k[i] ;
vImag[i] = 0.0;
while(micros() < (microseconds + sampling_period_us)){
}
}
FFT.Windowing(vReal, SAMPLES, FFT_WIN_TYP_HAMMING, FFT_FORWARD);
FFT.Compute(vReal, vImag, SAMPLES, FFT_FORWARD);
FFT.ComplexToMagnitude(vReal, vImag, SAMPLES);
double peak = FFT.MajorPeak(vReal, SAMPLES, SAMPLING_FREQUENCY);
int size1= sizeof(vReal);
Serial.println(size1);
Serial.print("Peak frequency is: ");
Serial.println(peak);
}