Grove - 8 Channel I2C Multiplexer/I2C Hub (TCA9548A)

Hello. Can I use this multi Grove - 8 Channel I2C Multiplexer/I2C Hub (TCA9548A) - Seeed Wiki wih Argon? there is a library or a code that someone has tried before me?
I need this for five sensors MLX90393

Wouldn’t see why not.
There is a library for Particle available here

okay thanks… Can i break the blu board of the multi?? I need only five ports and it’s soo long for my uses.

With the Argon you could use Wire (D0/D1) and Wire1 (D2/D3) to remove the need for a multiplexer entirely as the MLX90393 allows for four distinct addresses per I2C interface.

Hence you could use two of these

or one combined with a base shield like this

(if you don’t want to wire the sensors yourself)

I was using the grove Hub- 6 port connect to the grove shield for the Argon but I have a problem with more sensors togheter. I don’t know how to edit the code for all the sensors.

How can I modify this code and use four sensors ??

This code work with a single sensor:

// Distributed with a free-will license.
// Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
// MLX90393
// This code is designed to work with the MLX90393_I2CS I2C Mini Module available from ControlEverything.com.
// https://www.controleverything.com/products

#include <math.h>

// MLX90393 I2C Address is 0x0C(12)
#define Addr 0x0C

int xMag = 0, yMag = 0, zMag = 0;

int MagInt = 0;

void setup()
{
  // Set variable
  Particle.variable("i2cdevice", "MLX90393");
  Particle.variable("xMag", xMag);
  Particle.variable("yMag", yMag);
  Particle.variable("zMag", zMag);

  // Initialise I2C communication as MASTER
  Wire.begin();
  // Initialise serial communication, set baud rate = 9600
  Serial.begin(9600);

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select Write register command
  Wire.write(0x60);
  // Set AH = 0x00, BIST disabled
  Wire.write(0x00);
  // Set AL = 0x5C, Hall plate spinning rate = DEFAULT, GAIN_SEL = 5
  Wire.write(0x5C);
  // Select address register, (0x00 << 2)
  Wire.write(0x00);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 1 byte of data
  Wire.requestFrom(Addr, 1);

  // Read status byte
  if (Wire.available() == 1)
  {
    unsigned int c = Wire.read();
  }

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Select Write register command
  Wire.write(0x60);
  // Set AH = 0x02
  Wire.write(0x02);
  // Set AL = 0xB4, RES for magnetic measurement = 0
  Wire.write(0xB4);
  // Select address register, (0x02 << 2)
  Wire.write(0x08);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 1 byte of data
  Wire.requestFrom(Addr, 1);

  // Read status byte
  if (Wire.available() == 1)
  {
    unsigned int c = Wire.read();
  }
  delay(300);
}

void loop()
{
  unsigned int data[7];

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Start single meaurement mode,  ZYX enabled
  Wire.write(0x3E);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 1 byte of data
  Wire.requestFrom(Addr, 1);

  // Read status byte
  if (Wire.available() == 1)
  {
    unsigned int c = Wire.read();
  }
  delay(100);

  // Start I2C Transmission
  Wire.beginTransmission(Addr);
  // Send read measurement command, ZYX enabled
  Wire.write(0x4E);
  // Stop I2C Transmission
  Wire.endTransmission();

  // Request 7 bytes of data
  Wire.requestFrom(Addr, 7);

  // Read 7 bytes of data
  // status, xMag msb, xMag lsb, yMag msb, yMag lsb, zMag msb, zMag lsb
  if (Wire.available() == 7);
  {
    data[0] = Wire.read();
    data[1] = Wire.read();
    data[2] = Wire.read();
    data[3] = Wire.read();
    data[4] = Wire.read();
    data[5] = Wire.read();
    data[6] = Wire.read();
  }

  // Convert the data
  xMag = data[1] * 256 + data[2];
  if (xMag > 32767)
  {
    xMag -= 65536;
  }

  yMag = data[3] * 256 + data[4];
  if (yMag > 32767)
  {
    yMag -= 65536;
  }

  zMag = data[5] * 256 + data[6];
  if (zMag > 32767)
  {
    zMag -= 65536;
     }
     
      MagInt = sqrt(xMag*xMag + yMag*yMag + zMag*zMag); 
      
  Particle.publish("Magnetic Field in X-Axis : ", String(xMag));
  Particle.publish("Magnetic Field in Y-Axis : ", String(yMag));
  Particle.publish("Magnetic Field in Z-Axis : ", String(zMag));
   delay(1000);
  Particle.publish("Magnetic field Intensity: ", String(MagInt));
  delay(1000);
}

First you need to set the address of your sensors in hardware via the dedicated Address 0/1 jumpers on your board.
Then you can daisy chain up to four sensors per I2C interface.
Then according to the set addresses (0x0C, 0x0D, 0x0E & 0x0F) you can talk to the individual devices on the bus.

The MLX90393 even comes in multiple variants with a separate base address each with an individual sets of four “sub addresses”


which would allow to have up to 16 sensors on the same bus.

With the code above you’d just have to set the correct value where you currently use your constant Addr 0x0C.

I havent’t tested this library (due to lack of such sensors :wink: ) but it seems to make things a lot easier.
You’d create one instance of the library object per sensor and call the respective ::begin() function to setup the I2C interface and address.

something like this

#include <MLX90393.h> 

SerialLogHandler logger(LOG_LEVEL_ERROR, { { "app", LOG_LEVEL_INFO } });

const int SENSORS = 5;
MLX90393 mlx[SENSORS];              // sensor objects
MLX90393::txyz data[SENSORS];       // strcts to hold sensor data (t, x, y, z)

void setup() {
  Wire.begin();
  Wire1.begin();
  mlx[0].begin(0, 0, -1, Wire);     // A1, A0, no ready pin, I2C interface D0/D1 
  mlx[1].begin(0, 1, -1, Wire); 
  mlx[2].begin(1, 0, -1, Wire); 
  mlx[3].begin(0, 0, -1, Wire1);    //                       I2C interface D2/D3 
  mlx[4].begin(0, 1, -1, Wire1); 

  for (int s = 0; s < SENSORS; s++) {
    mlx[s].setOverSampling(0);
    mlx[s].setDigitalFiltering(0);
  }
}

void loop() {
  for (int s = 0; s < SENSORS; s++) {
    mlx[s].readData(data[s]);       // read all sensors
    Log.info("x: %6.2f, y: %6.2f, z: %6.2f, t: %4.1f °C"
            , data[s].x
            , data[s].y
            , data[s].z
            , data[s].t
            );
  }
  delay(100);
}

thanks a lot <3 I hope that i will solve my problebs, I will let you know :slight_smile:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.