Skip to content

Commit 815fe79

Browse files
committed
Adding examples
1 parent c01a08c commit 815fe79

File tree

3 files changed

+258
-51
lines changed

3 files changed

+258
-51
lines changed

examples/Example1_BasicControl/Example1_BasicControl.ino

Lines changed: 42 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,101 +2,92 @@
22
Use the Qwiic Mux to access multiple I2C devices on seperate busses.
33
By: Nathan Seidle @ SparkFun Electronics
44
Date: May 17th, 2020
5-
License: This code is public domain but you buy me a beer if you use this
5+
License: This code is public domain but you buy me a beer if you use this
66
and we meet someday (Beerware license).
77
88
Some I2C devices respond to only one I2C address. This can be a problem
9-
when you want to hook multiple of a device to the I2C bus. An I2C Mux
9+
when you want to hook multiple of a device to the I2C bus. An I2C Mux
1010
solves this issue by allowing you to change the 'channel' or port that
11-
a device is connected to on the mux.
12-
13-
This example shows how to hook up two MMA8452Q accelerometers with the same address.
14-
You can read the MMA8452Q hookup guide and get the library from https://learn.sparkfun.com/tutorials/mma8452q-accelerometer-breakout-hookup-guide
11+
the master is talking to.
1512
13+
This example shows how to connect to different ports.
1614
The TCA9548A is a mux. This means when you enableMuxPort(2) then the SDA and SCL lines of the master (Arduino)
17-
are connected to port 2. Whatever I2C traffic you do, such as accel.init() will be communicated to whatever
18-
sensor you have on port 2. You don't need accel2.init() or accel5.init().
19-
20-
Outputs two sets of XYZ acceleration
15+
are connected to port 2. Whatever I2C traffic you do, such as distanceSensor.startRanging() will be communicated to whatever
16+
sensor you have on port 2.
2117
2218
Hardware Connections:
2319
Attach the Qwiic Mux Shield to your RedBoard or Uno.
24-
Plug two Qwiic MMA8452Q breakout boards into ports 0 and 1.
25-
Serial.print it out at 9600 baud to serial monitor.
20+
Plug a device into port 0 or 1
21+
Serial.print it out at 115200 baud to serial monitor.
2622
2723
SparkFun labored with love to create this code. Feel like supporting open
2824
source? Buy a board from SparkFun!
2925
https://www.sparkfun.com/products/14685
30-
3126
*/
3227

3328
#include <Wire.h>
3429

35-
TwoWire Wire1(1); //Will use pads 8/9
36-
3730
#include <SparkFun_I2C_Mux_Arduino_Library.h> //Click here to get the library: http://librarymanager/All#SparkFun_I2C_Mux
3831
QWIICMUX myMux;
3932

40-
#include <SparkFun_MMA8452Q.h> //Click here to get the library: http://librarymanager/All#SparkFun_MMA8452
41-
MMA8452Q accel;
42-
43-
#define NUMBER_OF_SENSORS 2
44-
4533
void setup()
4634
{
4735
Serial.begin(115200);
36+
Serial.println();
4837
Serial.println("Qwiic Mux Shield Read Example");
4938

50-
Wire1.begin();
51-
if (myMux.begin(0x70, Wire1) == false)
39+
Wire.begin();
40+
41+
if (myMux.begin() == false)
5242
{
5343
Serial.println("Mux not detected. Freezing...");
5444
while (1)
5545
;
5646
}
5747
Serial.println("Mux detected");
5848

59-
//myMux.setPort(8);
49+
myMux.setPort(1); //Connect master to port labeled '1' on the mux
50+
6051
byte currentPortNumber = myMux.getPort();
6152
Serial.print("CurrentPort: ");
6253
Serial.println(currentPortNumber);
6354

64-
while (1)
65-
;
66-
67-
//Initialize all the sensors
68-
for (byte x = 0; x < NUMBER_OF_SENSORS; x++)
69-
{
70-
myMux.setPort(x); //Tell mux to connect to port X
71-
accel.init(); //Init the sensor connected to this port
72-
}
73-
74-
Serial.println("Mux Shield online");
55+
Serial.println("Begin scanning for I2C devices");
7556
}
7657

7758
void loop()
7859
{
79-
for (byte x = 0; x < NUMBER_OF_SENSORS; x++)
60+
Serial.println();
61+
62+
byte nDevices = 0;
63+
for (byte address = 1; address < 127; address++)
8064
{
81-
myMux.setPort(x); //Tell mux to connect to this port, and this port only
65+
Wire.beginTransmission(address);
66+
byte error = Wire.endTransmission();
8267

83-
if (accel.available())
68+
if (error == 0)
8469
{
85-
accel.read();
86-
87-
Serial.print("Accel ");
88-
Serial.print(x);
89-
Serial.print(": ");
90-
Serial.print(accel.cx, 2);
91-
Serial.print(" ");
92-
Serial.print(accel.cy, 2);
93-
Serial.print(" ");
94-
Serial.print(accel.cz, 2);
95-
Serial.print(" ");
96-
97-
Serial.println(); // Print new line every time.
70+
Serial.print("I2C device found at address 0x");
71+
if (address < 0x10)
72+
Serial.print("0");
73+
Serial.print(address, HEX);
74+
Serial.println();
75+
76+
nDevices++;
77+
}
78+
else if (error == 4)
79+
{
80+
Serial.print("Unknown error at address 0x");
81+
if (address < 0x10)
82+
Serial.print("0");
83+
Serial.println(address, HEX);
9884
}
9985
}
10086

101-
delay(1); //Wait for next reading
87+
if (nDevices == 0)
88+
Serial.println("No I2C devices found");
89+
else
90+
Serial.println("Done");
91+
92+
delay(1000);
10293
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
Use the Qwiic Mux to access multiple I2C devices on seperate busses.
3+
By: Nathan Seidle @ SparkFun Electronics
4+
Date: May 17th, 2020
5+
License: This code is public domain but you buy me a beer if you use this
6+
and we meet someday (Beerware license).
7+
8+
Some I2C devices respond to only one I2C address. This can be a problem
9+
when you want to hook multiple of a device to the I2C bus. An I2C Mux
10+
solves this issue by allowing you to change the 'channel' or port that
11+
the master is talking to.
12+
13+
This example shows how to hook up two VL53L1X laser distance sensors with the same address.
14+
You can read the VL53L1X hookup guide and get the library from https://learn.sparkfun.com/tutorials/qwiic-distance-sensor-vl53l1x-hookup-guide
15+
16+
The TCA9548A is a mux. This means when you enableMuxPort(2) then the SDA and SCL lines of the master (Arduino)
17+
are connected to port 2. Whatever I2C traffic you do, such as distanceSensor.startRanging() will be communicated to whatever
18+
sensor you have on port 2. This example creates an array of objects. This increases RAM but allows for
19+
independent configuration of each sensor (one sensor may be configured for long range, the others for short, etc).
20+
21+
Outputs two sets of distances in mm and ft.
22+
23+
Hardware Connections:
24+
Attach the Qwiic Mux Shield to your RedBoard or Uno.
25+
Plug two Qwiic VL53L1X breakout boards into ports 0 and 1.
26+
Serial.print it out at 115200 baud to serial monitor.
27+
28+
SparkFun labored with love to create this code. Feel like supporting open
29+
source? Buy a board from SparkFun!
30+
https://www.sparkfun.com/products/14685
31+
*/
32+
33+
#include <Wire.h>
34+
35+
#include <SparkFun_I2C_Mux_Arduino_Library.h> //Click here to get the library: http://librarymanager/All#SparkFun_I2C_Mux
36+
QWIICMUX myMux;
37+
38+
#define NUMBER_OF_SENSORS 2
39+
40+
#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X
41+
42+
SFEVL53L1X **distanceSensor; //Create pointer to a set of pointers to the sensor class
43+
44+
void setup()
45+
{
46+
Serial.begin(115200);
47+
Serial.println("Qwiic Mux Shield Read Example");
48+
49+
Wire.begin();
50+
51+
//Create set of pointers to the class
52+
distanceSensor = new SFEVL53L1X *[NUMBER_OF_SENSORS];
53+
54+
//Assign pointers to instances of the class
55+
for (int x = 0; x < NUMBER_OF_SENSORS; x++)
56+
distanceSensor[x] = new SFEVL53L1X(Wire);
57+
58+
if (myMux.begin() == false)
59+
{
60+
Serial.println("Mux not detected. Freezing...");
61+
while (1)
62+
;
63+
}
64+
Serial.println("Mux detected");
65+
66+
byte currentPortNumber = myMux.getPort();
67+
Serial.print("CurrentPort: ");
68+
Serial.println(currentPortNumber);
69+
70+
//Initialize all the sensors
71+
bool initSuccess = true;
72+
73+
for (byte x = 0; x < NUMBER_OF_SENSORS; x++)
74+
{
75+
myMux.setPort(x);
76+
if (distanceSensor[x]->begin() != 0) //Begin returns 0 on a good init
77+
{
78+
Serial.print("Sensor ");
79+
Serial.print(x);
80+
Serial.println(" did not begin! Check wiring");
81+
initSuccess = false;
82+
}
83+
else
84+
{
85+
//Configure each sensor
86+
distanceSensor[x]->setIntermeasurementPeriod(180);
87+
distanceSensor[x]->setDistanceModeLong();
88+
distanceSensor[x]->startRanging(); //Write configuration bytes to initiate measurement
89+
Serial.print("Sensor ");
90+
Serial.print(x);
91+
Serial.println(" configured");
92+
}
93+
}
94+
95+
if (initSuccess == false)
96+
{
97+
Serial.print("Freezing...");
98+
while (1)
99+
;
100+
}
101+
102+
Serial.println("Mux Shield online");
103+
}
104+
105+
void loop()
106+
{
107+
int distance[NUMBER_OF_SENSORS];
108+
float distanceFeet;
109+
110+
for (byte x = 0; x < NUMBER_OF_SENSORS; x++)
111+
{
112+
myMux.setPort(x); //Tell mux to connect to this port, and this port only
113+
distance[x] = distanceSensor[x]->getDistance(); //Get the result of the measurement from the sensor
114+
115+
Serial.print("\tDistance");
116+
Serial.print(x);
117+
Serial.print("(mm): ");
118+
Serial.print(distance[x]);
119+
120+
distanceFeet = (distance[x] * 0.0393701) / 12.0;
121+
122+
Serial.print("\tDistance");
123+
Serial.print(x);
124+
Serial.print("(ft): ");
125+
Serial.print(distanceFeet, 2);
126+
}
127+
128+
Serial.println();
129+
130+
delay(180); //Wait for next reading
131+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
Use the Qwiic Mux to access multiple I2C devices on seperate busses.
3+
By: Nathan Seidle @ SparkFun Electronics
4+
Date: May 17th, 2020
5+
License: This code is public domain but you buy me a beer if you use this
6+
and we meet someday (Beerware license).
7+
8+
This library supports different Wire ports and different I2C addresses
9+
of the mux itself. By sending the mux address to the library you can
10+
ostensibly have up to 8 mux boards with 64 devices behind it!
11+
12+
This example shows how to start the lib with a different Wire port and
13+
a different I2C address on the mux. It will fail on the Uno because the Uno
14+
has only one Wire port.
15+
16+
Hardware Connections:
17+
Close the ADR0 jumper on the Mux board to change the address to 0x71.
18+
Attach the Qwiic Mux to your RedBoard or Uno.
19+
Serial.print it out at 115200 baud to serial monitor.
20+
21+
SparkFun labored with love to create this code. Feel like supporting open
22+
source? Buy a board from SparkFun!
23+
https://www.sparkfun.com/products/14685
24+
*/
25+
26+
#include <Wire.h>
27+
28+
#include <SparkFun_I2C_Mux_Arduino_Library.h> //Click here to get the library: http://librarymanager/All#SparkFun_I2C_Mux
29+
QWIICMUX myMux;
30+
31+
void setup()
32+
{
33+
Serial.begin(115200);
34+
Serial.println("Qwiic Mux Shield Read Example");
35+
36+
Wire1.begin(); //This line will fail to compile on an Uno. Use a dev platform with multiple Wire ports
37+
38+
//Setup mux to use Wire1. If you have multiple muxes, pass address as first argument
39+
//ADR0 must have a the jumper closed with solder to use address 0x71. Use 0x70 for all jumpers open.
40+
if (myMux.begin(0x71, Wire1) == false)
41+
{
42+
Serial.println("Mux not detected. Freezing...");
43+
while (1)
44+
;
45+
}
46+
Serial.println("Mux detected");
47+
48+
myMux.setPort(1); //Connect master to port labeled '1' on the mux
49+
50+
byte currentPortNumber = myMux.getPort();
51+
Serial.print("CurrentPort: ");
52+
Serial.println(currentPortNumber);
53+
54+
Serial.println("Begin scanning for I2C devices");
55+
}
56+
57+
void loop()
58+
{
59+
Serial.println();
60+
61+
byte nDevices = 0;
62+
for (byte address = 1; address < 127; address++)
63+
{
64+
Wire1.beginTransmission(address);
65+
byte error = Wire1.endTransmission();
66+
67+
if (error == 0)
68+
{
69+
Serial.print("I2C device found at address 0x");
70+
if (address < 0x10)
71+
Serial.print("0");
72+
Serial.print(address, HEX);
73+
Serial.println();
74+
75+
nDevices++;
76+
}
77+
}
78+
79+
if (nDevices == 0)
80+
Serial.println("No I2C devices found");
81+
else
82+
Serial.println("Done");
83+
84+
delay(1000);
85+
}

0 commit comments

Comments
 (0)