Go Down

Topic: how to increase the sampling rate and reduce the adc conversion time (Read 4362 times) previous topic - next topic

aravindaguru

hi,
   I am trying to display the detected value of the sensors and it is displaying but the  problem is ADC conversion is lagging to collect every value detected by the sensor is there any solution to solve this issue and to increase the sampling rate in Arduino zero

artisticforge

hi,
   I am trying to display the detected value of the sensors and it is displaying but the  problem is ADC conversion is lagging to collect every value detected by the sensor is there any solution to solve this issue and to increase the sampling rate in Arduino zero
You need to post the code you are using so that we may read it and look for ways to improve it.
without seeing the code anyone would be providing an answer deprived of important information.


aravindaguru

Thanks for your reply.I have attached the code below



#include <SPI.h>
#include "wiring_private.h" // pinPeripheral() function
//unsigned int m=1;
 
volatile long count1=0;
int runTimer = 1;
int runFor = 10; // time in seconds
int buzzerPin = 13;
int relayPin=10;
//int data = 0;
float Vout=0;
//SPI class(MISO
SPIClass mySPI (&sercom0, 17, 9 , 8, SPI_PAD_2_SCK_3, SERCOM_RX_PAD_0);    // ads1220 17 -->PA4 9-->  8-->
int m=0;
//Uart class
Uart BluetoothSerial( &sercom5, 31, 30, SERCOM_RX_PAD_3, UART_TX_PAD_2) ;  //bluetooth

//Pin definition
#define CHECK_PIN 0
#define ADS1220_CS_PIN SS
#define ADS1220_DRDY_PIN A4
#define DC_DC_ENABLE A5
#define BLUETOOTH_RESET 27
#define PIN_DRDY  A4

#define SPI_MASTER_DUMMY   0xFF
#define RESET 0x06                                                    //Send the RESET command (06h) to make sure the ADS1220 is properly reset after power-up
#define START 0x08                                         //Send the START/SYNC command (08h) to start converting in continuous conversion mode
#define WREG  0x40
#define RREG  0x20
#define STOP   0x02
#define CONFIG_REG0_ADDRESS 0x00
#define CONFIG_REG1_ADDRESS 0x01
#define CONFIG_REG2_ADDRESS 0x02
#define CONFIG_REG3_ADDRESS 0x03


#define REG_CONFIG_DR_20SPS 0x00
#define REG_CONFIG_DR_45SPS 0x20
#define REG_CONFIG_DR_90SPS 0x40
#define REG_CONFIG_DR_175SPS 0x60
#define REG_CONFIG_DR_330SPS 0x80
#define REG_CONFIG_DR_600SPS 0xA0
#define REG_CONFIG_DR_1000SPS 0xC0
#define REG_CONFIG_DR_MASK 0xE0

#define DR_20SPS 20
#define DR_45SPS 45
#define DR_90SPS 90
#define DR_175SPS 175
#define DR_330SPS 330
#define DR_600SPS 600
#define DR_1000SPS 1000

#define REG_CONFIG_PGA_GAIN_1 0x00
#define REG_CONFIG_PGA_GAIN_2 0x02
#define REG_CONFIG_PGA_GAIN_4 0x04
#define REG_CONFIG_PGA_GAIN_8 0x06
#define REG_CONFIG_PGA_GAIN_16 0x08
#define REG_CONFIG_PGA_GAIN_32 0x0A
#define REG_CONFIG_PGA_GAIN_64 0x0C
#define REG_CONFIG_PGA_GAIN_128 0x0E
#define REG_CONFIG_PGA_GAIN_MASK 0x0E

#define PGA_GAIN_1 1
#define PGA_GAIN_2 2
#define PGA_GAIN_4 4
#define PGA_GAIN_8 8
#define PGA_GAIN_16 16
#define PGA_GAIN_32 32
#define PGA_GAIN_64 64
#define PGA_GAIN_128 128

uint8_t Config_Reg0;
uint8_t Config_Reg1;
uint8_t Config_Reg2;
uint8_t Config_Reg3;
int8_t NewDataAvailable;

#define PGA 1
#define VREF 2.048
#define VFSR VREF/PGA
#define FSR (((long int)1<<23)-1)

volatile byte MSB;
volatile byte data;
volatile byte LSB;
volatile long count=0;

volatile byte *SPI_RX_Buff_Ptr;

volatile char DataPacketHeader[16];


void setup() {
  SerialUSB.begin(9600);
 //  Serial.begin(9600);
  BluetoothSerial.begin(115200);
 
  // do this first, for Reasons
  mySPI.begin();
  pinMode(SS, OUTPUT);
  pinMode(ADS1220_DRDY_PIN,INPUT);
  pinMode(DC_DC_ENABLE , OUTPUT);
  pinMode(BLUETOOTH_RESET,OUTPUT);

  pinMode(PIN_DRDY, INPUT);
  pinMode(PIN_DRDY, INPUT_PULLUP);
  attachInterrupt(PIN_DRDY, onA4Change, FALLING);
 
  digitalWrite(DC_DC_ENABLE, HIGH);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(BLUETOOTH_RESET, HIGH);
  //pinMode(CHECK_PIN,OUTPUT);
   
   pinPeripheral(17, PIO_SERCOM_ALT);
   pinPeripheral(9, PIO_SERCOM_ALT);
   pinPeripheral(8, PIO_SERCOM_ALT);
 
   mySPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE1));
   ads1220begin();
}


void onA4Change() {
 //SerialUSB.println("A4 change");
    NewDataAvailable = true;
    count++;
}
uint8_t i = 0;

void loop() {

while(1){
timer();


   }
 
      }
 
void ads1220begin()
{
 
  SPI_Reset();                                           
  delay(100);                                                   

  digitalWrite(ADS1220_CS_PIN,LOW);

  Config_Reg0 = 0x01;  // AN1-AN0, gain=1,PGA enabled
  Config_Reg1 = 0x44;   //0x00-sinlge , 0x04-cont
  Config_Reg2 = 0x10;  //10 //55
  Config_Reg3 = 0x00;
 

  writeRegister( CONFIG_REG0_ADDRESS , Config_Reg0);
  writeRegister( CONFIG_REG1_ADDRESS , Config_Reg1);
  writeRegister( CONFIG_REG2_ADDRESS , Config_Reg2);
  writeRegister( CONFIG_REG3_ADDRESS , Config_Reg3);
 
  delay(100);
 
  Config_Reg0 = readRegister(CONFIG_REG0_ADDRESS);
  Config_Reg1 = readRegister(CONFIG_REG1_ADDRESS);
  Config_Reg2 = readRegister(CONFIG_REG2_ADDRESS);
  Config_Reg3 = readRegister(CONFIG_REG3_ADDRESS);

  SerialUSB.println("Config_Reg : ");
  SerialUSB.println(Config_Reg0,HEX);
  SerialUSB.println(Config_Reg1,HEX);
  SerialUSB.println(Config_Reg2,HEX);
  SerialUSB.println(Config_Reg3,HEX);
  SerialUSB.println(" ");
  digitalWrite(ADS1220_CS_PIN,HIGH); //release chip, signal end transfer

  SPI_Start();
  delay(10);
}
void writeRegister(uint8_t address, uint8_t value)
{
  digitalWrite(ADS1220_CS_PIN,LOW);
  delay(5);
  mySPI.transfer(WREG|(address<<2));       
  mySPI.transfer(value);
  delay(5);
  digitalWrite(ADS1220_CS_PIN,HIGH);



uint8_t readRegister(uint8_t address)
{
  uint8_t data;

  digitalWrite(ADS1220_CS_PIN,LOW);
  delay(5);
  mySPI.transfer(RREG|(address<<2));       
  data = mySPI.transfer(SPI_MASTER_DUMMY);
  delay(5);
  digitalWrite(ADS1220_CS_PIN,HIGH);

  return data;


uint8_t * Read_Data()
{
  static byte SPI_Buff[3];

 // if((digitalRead(ADS1220_DRDY_PIN)) == LOW)             //        Wait for DRDY to transition low
  {
    digitalWrite(ADS1220_CS_PIN,LOW);                         //Take CS low
  //  delayMicroseconds(10);
    for (int i = 0; i < 3; i++)
    {
      SPI_Buff = mySPI.transfer(SPI_MASTER_DUMMY);
    }
  //  delayMicroseconds(10);
    digitalWrite(ADS1220_CS_PIN,HIGH);                  //  Clear CS to high
   
  }
   
  return SPI_Buff;
}

void SPI_Reset()
{
  SPI_Command(RESET);                             
}

void SPI_Start()
{
  SPI_Command(START);
}

void SPI_Stop()
{
  SPI_Command(STOP);
}

void SPI_Command(unsigned char data_in)
{
  digitalWrite(ADS1220_CS_PIN, LOW);
  delay(2);
  digitalWrite(ADS1220_CS_PIN, HIGH);
  delay(2);
  digitalWrite(ADS1220_CS_PIN, LOW);
  delay(2);
  mySPI.transfer(data_in);
  delay(2);
  digitalWrite(ADS1220_CS_PIN, HIGH);
}
void timer() {

   for(int timer = runFor;timer > 0; --timer){
   if(timer <= 10) {
    Checkobject();
           }   
           } 
    SerialUSB.println("Total count:");
    SerialUSB.println(m);
      delay(200); 
      if (m>=2 && m<=4)
      {
         SerialUSB.println("object DETECTED!!!");
      }
      else if (m>=4 && m<=10)
      {
         SerialUSB.println("Object 2 DETECTED!!!");
       }
      m=0;   
}
void Check()
{

  long int bit32;
  long int bit24;
  byte *config_reg;
  if(!(millis()%1000))
  {
     //SerialUSB.println(count); // print to check
    count = 0;
  }

 
  if(NewDataAvailable == true)
  {
   
      SPI_RX_Buff_Ptr = Read_Data();
     
      MSB = SPI_RX_Buff_Ptr[0];   
      data = SPI_RX_Buff_Ptr[1];
      LSB = SPI_RX_Buff_Ptr[2];
   
      bit24 = MSB;
      bit24 = (bit24 << 8) | data;
      bit24 = (bit24 << 8) | LSB;                                 // Converting 3 bytes to a 24 bit int
       
      bit24= ( bit24 << 8 );
      bit32 = ( bit24 >> 8 );                      // Converting 24 bit two's complement to 32 bit two's complement
      float ref=10.0;
      float ref1=-5.0;
      float Vout = (float)((bit32*VFSR*100000)/FSR);     //In  mV
      SerialUSB.println("Sensor:");
      SerialUSB.println(Vout);
       delay(1000);
      if(Vout>ref)
            {       
            m=m+1;
           // SerialUSB.println(m);
            }
             else if(Vout<ref1)
            {       
            m=m+1;
           // SerialUSB.println(m);
            }
// Serial.println("Sensor:");
// Serial.println(Vout);
     
               
//      DataPacketHeader[0] = 0x0A;  // Packet Header
//      DataPacketHeader[1] = 0xFA;  // Packet Header
//      DataPacketHeader[2] = 0x04;  // Data Length LSB in Bytes
//      DataPacketHeader[3] = 0;     // Data Length MSB in Bytes
//      DataPacketHeader[4] = 0x02;  // Packet type 0x01->command , 0x02->data
//   
//   
//      DataPacketHeader[5] = (int)Vout;       // data LSB
//      DataPacketHeader[6] = (int)Vout >> 8;  //
//      DataPacketHeader[7] = (int)Vout >> 16;
//      DataPacketHeader[8] = (int)Vout >> 24; //Data MSB
//   
//      DataPacketHeader[9] = 0x00;
//      DataPacketHeader[10] = 0x0B;      // Packet footer
//   
//      for (i = 0; i < 11; i++)
//      {
//     //   SerialUSB.write(DataPacketHeader); // transmit through uart line
//         BluetoothSerial.write(DataPacketHeader);
//      }
//            NewDataAvailable = false;
     
  }

}

artisticforge

hi,
   I am trying to display the detected value of the sensors and it is displaying but the  problem is ADC conversion is lagging to collect every value detected by the sensor is there any solution to solve this issue and to increase the sampling rate in Arduino zero
Would you be able to post a photograph or the schematic of the circuit?
I would like to attempt to duplicate the circuit and look for way to improve it.

ltheoret

Hi,

In this link Fast Analog Input you will a post by mantoui that give some code that increases the analog sampling rate.

or I also found this on some website someplace.

Code: [Select]
void AdcBooster()
{
    ADC->CTRLA.bit.ENABLE = 0;                       // Disable ADC
    while( ADC->STATUS.bit.SYNCBUSY == 1 );           // Wait for synchronization
    ADC->CTRLB.reg = ADC_CTRLB_PRESCALER_DIV64 |      // Divide Clock by 64.
        ADC_CTRLB_RESSEL_12BIT;                      // Result on 12 bits
    ADC->AVGCTRL.reg = ADC_AVGCTRL_SAMPLENUM_1 |      // 1 sample
        ADC_AVGCTRL_ADJRES(0x00ul);                  // Adjusting result by 0
    ADC->SAMPCTRL.reg = 0x00;                         // Sampling Time Length = 0
    ADC->CTRLA.bit.ENABLE = 1;                        // Enable ADC
    while( ADC->STATUS.bit.SYNCBUSY == 1 );           // Wait for synchronization
} // AdcBooster


int analogPin = 1;

unsigned long start_times[100];
unsigned long stop_times[100];
unsigned long values[100];


void setup() {
   AdcBooster();                                     //uncomment for reading time without 'boost'
 
   Serial.begin(9600);
   pinMode(analogPin, INPUT);
}

void loop() {
    analogReadResolution(12);                      //analog resolution to 12bit
    unsigned int i;

    for(i=0;i<100;i++) {
        start_times[i] = micros();
        values[i] = analogRead(analogPin);
        stop_times[i] = micros();
    }

    // print out the results
    Serial.println("\n\n--- Results ---");
    for(i=0;i<100;i++) {
        Serial.print(values[i]);
        Serial.print(" elapse = ");
        Serial.print(stop_times[i] - start_times[i]);
        Serial.print(" us\n");
    }
}


Luc

Go Up