Problems with sparkintervaltimer and Adafruit_ADS1X15 library

Hi @peekay123 … i’m having some problems when using the sparkintervaltimer with theAdafruit ADS1X15 library. the code i’m trying to use is the following, so each 1.25ms the function ReadADC() is called in order to take a converted sample from the external ADC. However, after flasing the code to my photon device, the status led flashes red … but when i call the function ReadMic() everything is OK. In addition, i used the software timer from photon firmware with ReadADC() function, and everything is OK. could you please tell me where the problem can be?. i would like to use sparkintervaltimer lib, because on the final sketch, the sampling period will be 125uSec … is there any alternative lib to sparkintervaltimer i could test?.
thanks a lot!

// This #include statement was automatically added by the Particle IDE.
#include <SparkIntervalTimer.h>

#include "Adafruit_ADS1X15.h"

Adafruit_ADS1115 ads;  /* Use this for the 16-bit version */
//Adafruit_ADS1015 ads;     /* Use thi for the 12-bit version */


// CONNECT PHOTON TO ADS1115 BOARD AS FOLLOWS:
//
//  PHOTON PIN  ->  ADS11x5 PIN
//  ----------      -----------
//  3.3V        ->  VDD
//  GND         ->  GND
//  D0          ->  SDA (I2C DATA)
//  D1          ->  SCL (I2C CLOCK)
//  D2          ->  ALERT

#define AUDIO_TIMING_VAL 1250
#define BVSM_AUDIO_INPUT  A1
IntervalTimer readMicTimer;




void setup(void) 
{
  Serial.begin(9600);
  
  ads.begin();
  ads.setGain(GAIN_ONE);        // 1x gain   ± 4.096V  1 bit =     2mV       0.125mV

  readMicTimer.begin(readADC, AUDIO_TIMING_VAL, uSec); //<---------------------------------------

}

void loop(void) 
{

}

void readMic(void) {
    uint16_t value12 = analogRead(BVSM_AUDIO_INPUT);
    Serial.println(value12);
}

void readADC(void) {
    short adc0 = ads.readADC_SingleEnded(0);
    Serial.println(adc0);
   
}

@fbt, you shouldn’t be calling Serial.print() from an ISR! Declare value12 and adc0 as global volatile variables and print their value in loop(). Also, you need to look at the ads.readADC_SingleEnded() code to see if it is blocking in any way. ISRs need to be small, fast and not cause a lot of stack activity (local vars, calling functions that call functions, etc).

UPDATE: I just looked at the ADS1X15 library and I can see that it calls delay() which will NOT work within an ISR so this is most likely your problem. Also, I’m not sure if I2C will cause any issues.

1 Like

I am not a fan of the adafruit library for the ADS1115. Its not well written , and relies on delays.

I have had much better results using this library : https://github.com/jrowberg/i2cdevlib/tree/master/Arduino/ADS1115

It takes a bit of modification to work with the photon, but well worth it.

3 Likes

Alex, I’m a newbie but have used a couple libraries. I would like to use the ads1115 library you mention (on my photon project) but have no clue what changes I would need to make…will an attempted compile walk me through a bunch of errors? Any chance that the”modified” library is somewhere already?

I really don’t like delays…

EDIT: I think I am figuring out what I need…thanks anyway