Arduino and I2C Boards

Hi all!

I am extremely new to Arduinos and electronics/circuitry in general so please bear with!
For a project I have, I'm using 20 analog pressure sensors (SEN0297 from DFRobot) to measure the pressure across different areas under the foot when running/walking.
I have an Arduino Nano 33 IoT which only has 8 analog inputs, hence I've expanded it using ADC I2C boards (these cheap ones from amazon Amazon.co.uk) which give 4 analog inputs per board. I've tested this setup with 3 I2C boards, but it seems that every one that I add slows down the output to the serial monitor massively to the point where a step or stride isn't discernable anymore :(
I'll put my code below but any help to speed this section up is really appreciated! Thank you!

// Trying to write code for my arduino. I have no idea how any of this works so bear with....

int usePressure = 1;  // Add variable for if you want to use the pressure sensors
int useI2C = 1;       // Add variable if you want to use the I2C stuff

// Define values for the sensor reading bits
const int sensorPins[] = { A0, A1, A2, A3, A6, A7 };

const int numSensors = 6;
const int numADCs = 16;

const int delay_ms = 10;

int thispin = A0;

// Define array for all the sensors
int SensorsOutput[9];

#include <Arduino_LSM6DS3.h> // Include the IMU sensor toolbox
#include <Wire.h> // Include I2C reading toolbox

#include <ADS1X15.h> // Include ADS toolbox

ADS1015 ADS_1(0x48);    // This is the first ADS board, with nothing connected to ADDR
ADS1015 ADS_2(0x49);    // ADS board 2, with ADDR connected to VCC
ADS1015 ADS_3(0x4a);    // ADS board 3, with ADDR connected to SDA
ADS1015 ADS_4(0x4b);    // ADS board 4, with ADDR connected to SCL

const int ADCMin = 0;
const int ADCMax = 1100;
const int MaxVal = 1500;

void setup() {

  // put your setup code here, to run once:
  Serial.begin(9600);

  if (useI2C) {
    Wire.begin();
    ADS_1.begin();
    ADS_2.begin();
    ADS_3.begin();
    ADS_4.begin();
  }
};

void loop() {

  if (usePressure) {
    // Loop through all sensors
    for (int i = 0; i < numSensors; i++) {

      thispin = sensorPins[i];

      uint16_t sensorValue = analogRead(thispin);

      Serial.print(map(sensorValue,0,950,0,1023));

      if (i < numSensors - 1) {
        Serial.print(",");
      }
        else if (useIMU) {
        Serial.print(",");
      }
    }
  }

  if (useI2C) {

    Serial.print(",");

    ADS_1.setGain(0);ADS_2.setGain(0);ADS_3.setGain(0);ADS_4.setGain(0);
    
    uint16_t ADC1_1 = ADS_1.readADC(0);uint16_t ADC1_2 = ADS_1.readADC(1);uint16_t ADC1_3 = ADS_1.readADC(2);uint16_t ADC1_4 = ADS_1.readADC(3);
    if (ADC1_1 > MaxVal) {ADC1_1 = 0;} if (ADC1_2 > MaxVal) {ADC1_2 = 0;} if (ADC1_3 > MaxVal) {ADC1_3 = 0;} if (ADC1_4 > MaxVal) {ADC1_4 = 0;}
    
    uint16_t ADC2_1 = ADS_2.readADC(0);uint16_t ADC2_2 = ADS_2.readADC(1);uint16_t ADC2_3 = ADS_2.readADC(2);uint16_t ADC2_4 = ADS_2.readADC(3);
    if (ADC2_1 > MaxVal) {ADC2_1 = 0;} if (ADC2_2 > MaxVal) {ADC2_2 = 0;} if (ADC2_3 > MaxVal) {ADC2_3 = 0;} if (ADC2_4 > MaxVal) {ADC2_4 = 0;}
    
    uint16_t ADC3_1 = ADS_3.readADC(0);uint16_t ADC3_2 = ADS_3.readADC(1);uint16_t ADC3_3 = ADS_3.readADC(2);uint16_t ADC3_4 = ADS_3.readADC(3);
    if (ADC3_1 > MaxVal) {ADC3_1 = 0;} if (ADC3_2 > MaxVal) {ADC3_2 = 0;} if (ADC3_3 > MaxVal) {ADC3_3 = 0;} if (ADC3_4 > MaxVal) {ADC3_4 = 0;}
    
    //uint16_t ADC4_1 = ADS_4.readADC(0);uint16_t ADC4_2 = ADS_4.readADC(1);uint16_t ADC4_3 = ADS_4.readADC(2);uint16_t ADC4_4 = ADS_4.readADC(3);
    //if (ADC4_1 > MaxVal) {ADC4_1 = 0;} if (ADC4_2 > MaxVal) {ADC4_2 = 0;} if (ADC4_3 > MaxVal) {ADC4_3 = 0;} if (ADC4_4 > MaxVal) {ADC4_4 = 0;}

    Serial.print(ADC1_1);Serial.print(",");Serial.print(ADC1_2);Serial.print(",");Serial.print(ADC1_3);Serial.print(",");Serial.print(ADC1_4);Serial.print(",");
    
    Serial.print(ADC2_1);Serial.print(",");Serial.print(ADC2_2);Serial.print(",");Serial.print(ADC2_3);Serial.print(",");Serial.print(ADC2_4);Serial.print(",");
    
    Serial.print(ADC3_1);Serial.print(",");Serial.print(ADC3_2);Serial.print(",");Serial.print(ADC3_3);Serial.print(",");Serial.print(ADC3_4);Serial.print(",");
    
    Serial.print(ADC4_1);Serial.print(",");Serial.print(ADC4_2);Serial.print(",");Serial.print(ADC4_3);Serial.print(",");Serial.print(ADC4_4);Serial.print(",");
    
    Serial.println();
  }

  Serial.println();

  delay(delay_ms);
}

You could speed up the serial prints by using 115200 rather than 9600.
I suspect using a couple of these would also speed things up, if the bottleneck is the I2C AtoDs

Welcome to the forum

As your topic does not relate directly to the installation or operation of the IDE it has been moved to the Programming category of the forum

For boards with native USB (like the Nano 33 IoT used by OP) that has no influence.

1 Like

You can use two HC4067 multiplexer to multiplex the ADC of your board.
Might be faster than I2C.

library available - GitHub - RobTillaart/HC4067: Arduino library for CD74HC4067 1 x 16 channel multiplexer and compatibles. · GitHub


An other option is to use SPI ADC's e.g. MCP3208 - 12 bit 8 channels
These are faster than I2C but use more pins to control (the HC4067 is simpler).

Library available - GitHub - RobTillaart/MCP_ADC: Arduino library for MCP3001 MCP3002 MCP3004 MCP3008 MCP3201 MCP3202 MCP3204 MCP3208 · GitHub

All good suggestions, but I’d like to see the Serial Monitor content with timestamps enabled, to show us what’s really going on.

I will take a SWAG as I do not have that processor. It appears to me you are probably losing time in your reading of the a/d, here are a few things that may help:
A. Increase data rate to 860 SPS
B. Use continuous conversion mode
C. Avoid channel switching if possible
D. Use 400 kHz I2C