Read ADXL1005 and show g values

frohr

Well-known member
Hi all,
I have ADXL1005 eval board and Teensy 4. I want to measure g RMS value on bearing housing. Reading will be for frequency 500-10000Hz.
But values which I got from my code are nonsense. I am checking it with professional measuring tool. Any ideas how to do it? Thanks a lot.

Code:
#include <SoftwareSerial.h>

#define SAMPLES 4095
#define SAMPLING_FREQUENCY 20000 // freq  20000Hz

unsigned long sampling_period_us;// freq  10000hz
unsigned long useconds_sampling;
unsigned long refresh_period_us;

double vRealg[SAMPLES];


//raw
float  sensorValue;
//g
float  sensorValueG;
int scale = 100;

double sum_plus_g;
double sum_g;
double rms_g;
double rms_g7;

//filter
float EMA_a_low = 0;    
float EMA_a_high = 0;
float EMA_S_low = 0;        
float EMA_S_high = 0;
float highpass = 0;
float bandpass = 0;

void setup()
 {
   Serial.begin(1500000);
   sampling_period_us = round(1000000*(1.0/SAMPLING_FREQUENCY)); // 50 micro
   // filter
   sensorValue= analogRead(A1);
   EMA_S_low = mapf(sensorValue, 0, 4095, -scale, scale);     
   EMA_S_high = mapf(sensorValue, 0, 4095, -scale, scale);
 }

void RMS_G () // vypocet RMS v G jen plus hodnoty
{  
    sum_plus_g=0;
    sum_g=0;
    rms_g=0;
    for(int i=1; i<(SAMPLES);i++ )  
    { 
     double n=vRealg[i];
     if ( n >= 0 )
     {
     sum_plus_g += vRealg[i]*vRealg[i];
     }
     sum_g= (sum_plus_g);
     rms_g7= sqrt(sum_g/SAMPLES); 
     rms_g= rms_g7+(rms_g7*0.707); 
}  
}  

void SAMPLE_10 ()
{
  EMA_a_low =  0.0002;  //500 hz
  EMA_a_high = 1;    //20 000 Mhz
  useconds_sampling = micros(); 
   for(int i=0; i<SAMPLES; i++)      
  {
   sensorValue= analogRead(A1);
   sensorValueG= mapf(sensorValue, 0, 4095, -scale, scale);  

   EMA_S_low = (EMA_a_low*sensorValueG) + ((1-EMA_a_low)*EMA_S_low);  
   EMA_S_high= (EMA_a_high*sensorValueG) + ((1-EMA_a_high)*EMA_S_high);
   highpass = sensorValueG - EMA_S_low;     
   bandpass = EMA_S_high - EMA_S_low;  

   vRealg[i] = bandpass;//sensorValueG; 
   while(micros() < (useconds_sampling + sampling_period_us))
   {
   }
   }
}

 
void loop()
{
      for(int ip=0; ip<10;ip++ )
      {                     
        SAMPLE_10(); 
        RMS_G(); 
        Serial.print("g RMS: ");
        Serial.println(rms_g);         
      }   
}


float mapf(float val, float in_min, float in_max, float out_min, float out_max)
{
   return (val - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
 
Back
Top