Skip to content

Commit a3defd5

Browse files
committed
adding demo code for PCA9546
Adding demo code since it will be in two guides (breakout and stemma breakout)
1 parent e05484a commit a3defd5

4 files changed

Lines changed: 170 additions & 0 deletions

File tree

PCA9546_Demos/PCA9546_I2C_Scanner/.metro_m0.test.only

Whitespace-only changes.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
/**
5+
* PCA9546 I2CScanner.ino -- I2C bus scanner for Arduino
6+
*
7+
* Based on https://playground.arduino.cc/Main/I2cScanner/
8+
*
9+
*/
10+
11+
#include "Wire.h"
12+
13+
#define PCAADDR 0x70
14+
15+
void pcaselect(uint8_t i) {
16+
if (i > 3) return;
17+
18+
Wire.beginTransmission(PCAADDR);
19+
Wire.write(1 << i);
20+
Wire.endTransmission();
21+
}
22+
23+
24+
// standard Arduino setup()
25+
void setup()
26+
{
27+
while (!Serial);
28+
delay(1000);
29+
30+
Wire.begin();
31+
32+
Serial.begin(115200);
33+
Serial.println("\nPCAScanner ready!");
34+
35+
for (uint8_t t=0; t<4; t++) {
36+
pcaselect(t);
37+
Serial.print("PCA Port #"); Serial.println(t);
38+
39+
for (uint8_t addr = 0; addr<=127; addr++) {
40+
if (addr == PCAADDR) continue;
41+
42+
Wire.beginTransmission(addr);
43+
if (!Wire.endTransmission()) {
44+
Serial.print("Found I2C 0x"); Serial.println(addr,HEX);
45+
}
46+
}
47+
}
48+
Serial.println("\ndone");
49+
}
50+
51+
void loop()
52+
{
53+
}

PCA9546_Demos/PCA9546_MultiSensors/.metro_m0.test.only

Whitespace-only changes.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// SPDX-FileCopyrightText: 2023 Liz Clark for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
/**
5+
* PCA9546 I2C Multi Sensor Example
6+
*
7+
* Using two VL53L4CD sensors on ports 0 and 1
8+
*
9+
*/
10+
11+
#include <Arduino.h>
12+
#include <Wire.h>
13+
#include <vl53l4cd_class.h>
14+
#include <string.h>
15+
#include <stdlib.h>
16+
#include <stdio.h>
17+
#include <stdint.h>
18+
#include <assert.h>
19+
20+
#define PCAADDR 0x70
21+
#define DEV_I2C Wire
22+
23+
#define SerialPort Serial
24+
25+
// create two instances of the sensor
26+
VL53L4CD tof1(&DEV_I2C, A1);
27+
VL53L4CD tof2(&DEV_I2C, A1);
28+
29+
void pcaselect(uint8_t i) {
30+
if (i > 3) return;
31+
32+
Wire.beginTransmission(PCAADDR);
33+
Wire.write(1 << i);
34+
Wire.endTransmission();
35+
}
36+
37+
// standard Arduino setup()
38+
void setup()
39+
{
40+
while (!Serial);
41+
delay(1000);
42+
43+
Wire.begin();
44+
45+
Serial.begin(115200);
46+
Serial.println("\nMultiSensor PCA9546");
47+
48+
// define the port on the PCA9546 for the first sensor
49+
pcaselect(0);
50+
// setup the first sensor
51+
tof1.begin();
52+
tof1.VL53L4CD_Off();
53+
tof1.InitSensor();
54+
tof1.VL53L4CD_SetRangeTiming(200, 0);
55+
tof1.VL53L4CD_StartRanging();
56+
57+
// define the port on the PCA9546 for the 2nd sensor
58+
pcaselect(1);
59+
60+
// setup the 2nd sensor
61+
tof2.begin();
62+
tof2.VL53L4CD_Off();
63+
tof2.InitSensor();
64+
tof2.VL53L4CD_SetRangeTiming(200, 0);
65+
tof2.VL53L4CD_StartRanging();
66+
67+
}
68+
69+
void loop()
70+
{
71+
uint8_t NewDataReady = 0;
72+
VL53L4CD_Result_t results;
73+
uint8_t status;
74+
char report[64];
75+
76+
// define port on the PCA9546
77+
pcaselect(0);
78+
79+
// loop for time of flight sensor 1
80+
do {
81+
status = tof1.VL53L4CD_CheckForDataReady(&NewDataReady);
82+
} while (!NewDataReady);
83+
84+
if ((!status) && (NewDataReady != 0)) {
85+
// (Mandatory) Clear HW interrupt to restart measurements
86+
tof1.VL53L4CD_ClearInterrupt();
87+
88+
// Read measured distance. RangeStatus = 0 means valid data
89+
tof1.VL53L4CD_GetResult(&results);
90+
snprintf(report, sizeof(report), "ToF 1 - Status = %3u, Distance = %5u mm, Signal = %6u kcps/spad\r\n",
91+
results.range_status,
92+
results.distance_mm,
93+
results.signal_per_spad_kcps);
94+
SerialPort.println(report);
95+
}
96+
97+
// define port on PCA9546
98+
pcaselect(1);
99+
100+
// loop for time of flight sensor 2
101+
do {
102+
status = tof2.VL53L4CD_CheckForDataReady(&NewDataReady);
103+
} while (!NewDataReady);
104+
105+
if ((!status) && (NewDataReady != 0)) {
106+
// (Mandatory) Clear HW interrupt to restart measurements
107+
tof2.VL53L4CD_ClearInterrupt();
108+
109+
// Read measured distance. RangeStatus = 0 means valid data
110+
tof2.VL53L4CD_GetResult(&results);
111+
snprintf(report, sizeof(report), "ToF 2 - Status = %3u, Distance = %5u mm, Signal = %6u kcps/spad\r\n",
112+
results.range_status,
113+
results.distance_mm,
114+
results.signal_per_spad_kcps);
115+
SerialPort.println(report);
116+
}
117+
}

0 commit comments

Comments
 (0)