Qwiic MUX Hookup Guide
Arduino Example
Note: This tutorial assumes you are familiar with Arduino products and you are using the latest stable version of the Arduino IDE on your desktop. If this is your first time using the Arduino IDE, please review our tutorial on installing the Arduino IDE.
SparkFun has written some example code to enable and disable ports on the Qwiic Mux. Go ahead and download this example code here.
Additionally, you will need to install the MMA8452Q Arduino library if you are using two MMA8452Q accelerometers. First, you’ll need the Sparkfun MMA8452Q Arduino library. You can obtain these libraries through the Arduino Library Manager. Search for Sparkfun MMA8452Q Accelerometer by SparkFun Electronics to install the latest version. If you prefer downloading the libraries from the GitHub repository and manually installing it, you can grab them here:
Arduino Example Example1-BasicReadings.ino
Opening Example1-BasicReadings
will open two tabs in the Arduino IDE, the first example, and also Mux_Control
. Let's take a look under the hood of Mux_Control
to get an idea of what's going on. There are two functions here, boolean enableMuxPort(byte portNumber)
and boolean disableMuxPort(byte portNumber)
which is pretty much all we need to specify which channels we'd like to talk to on the Mux. If we have a sensor on channel 0, we simply call enableMuxPort(0)
to open that channel on the multiplexer. Then we'll take whatever reads and perform whatever actions we'd like to the sensor on that channel. Once finished, we have to call disableMuxPort(0)
to close communication on that channel so we don't accidentally perform actions on the sensor on that channel. The below example code shows how to read from two MMA8452Q accelerometers.
language:c
#include <Wire.h>
#include <SparkFun_MMA8452Q.h> //From: https://github.com/sparkfun/SparkFun_MMA8452Q_Arduino_Library
#define NUMBER_OF_SENSORS 2
MMA8452Q accel[NUMBER_OF_SENSORS]; //Create an array of MMA8452Q sensors
void setup()
{
Serial.begin(115200);
Serial.println("Qwiic Mux Shield Read Example");
Wire.begin();
//Disable all eight mux ports initially, then we can enable them one at a time
for (byte x = 0 ; x <= 7 ; x++)
{
disableMuxPort(x);
}
//Initialize all the sensors
for (byte x = 0 ; x < NUMBER_OF_SENSORS ; x++)
{
enableMuxPort(x); //Tell mux to connect to port X
accel[x].init(); //Init the sensor connected to this port
disableMuxPort(x);
}
Serial.println("Mux Shield online");
}
void loop()
{
for (byte x = 0 ; x < NUMBER_OF_SENSORS ; x++)
{
enableMuxPort(x); //Tell mux to connect to this port, and this port only
if (accel[x].available())
{
accel[x].read();
Serial.print("Accel ");
Serial.print(x);
Serial.print(": ");
Serial.print(accel[x].cx, 2);
Serial.print(" ");
Serial.print(accel[x].cy, 2);
Serial.print(" ");
Serial.print(accel[x].cz, 2);
Serial.print(" ");
Serial.println(); // Print new line every time.
}
disableMuxPort(x); //Tell mux to disconnect from this port
}
delay(1); //Wait for next reading
}
With the example provided, you should be able to read two I2C sensors with the same address on the same bus! Try opening up the Arduino Serial Monitor set to 115200 baud in order to read the sensor values.