Skip to content

Commit 1f605ea

Browse files
committed
Add test sketch for auto-detection methods. Used to detect SCD30 issue #4
1 parent e73e9a9 commit 1f605ea

File tree

2 files changed

+306
-9
lines changed

2 files changed

+306
-9
lines changed

Firmware/Test Sketches/Sensor Autodetect/I2CScanner/I2CScanner.ino

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
#include <Wire.h>
2-
TwoWire myWire(1); //Will use pads 8/9
2+
TwoWire qwiic(1); //Will use pads 8/9
33

44
void setup()
55
{
66
Serial.begin(115200);
77
Serial.println("Scanning...");
88

9-
//Wire.begin();
10-
myWire.begin();
11-
myWire.setClock(100000);
9+
const byte PIN_QWIIC_PWR = 18;
10+
pinMode(PIN_QWIIC_PWR, OUTPUT);
11+
digitalWrite(PIN_QWIIC_PWR, LOW); //qwiicPowerOn();
12+
13+
qwiic.begin();
14+
qwiic.setClock(100000);
15+
16+
qwiic.setPullups(1); //Set pullups to 1k. If we don't have pullups, detectQwiicDevices() takes ~900ms to complete. We'll disable pullups if something is detected.
1217

1318
byte error, address;
1419
int nDevices = 0;
1520
for (address = 1; address < 127; address++ )
1621
{
17-
//Wire.beginTransmission(address);
18-
//error = Wire.endTransmission();
19-
myWire.beginTransmission(address);
20-
error = myWire.endTransmission();
22+
qwiic.beginTransmission(address);
23+
error = qwiic.endTransmission();
2124

2225
if (error == 0)
2326
{
@@ -41,7 +44,6 @@ void setup()
4144
Serial.println("No I2C devices found\n");
4245
else
4346
Serial.println("done\n");
44-
4547
}
4648

4749
void loop()
Lines changed: 295 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,295 @@
1+
#include <Wire.h>
2+
TwoWire qwiic(1); //Will use pads 8/9
3+
4+
//Header files for all possible Qwiic sensors
5+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
6+
7+
#include "SparkFun_Qwiic_Scale_NAU7802_Arduino_Library.h" //Click here to get the library: http://librarymanager/All#SparkFun_NAU7802
8+
NAU7802 loadcellSensor_NAU7802;
9+
10+
#include "SparkFun_VL53L1X.h" //Click here to get the library: http://librarymanager/All#SparkFun_VL53L1X
11+
SFEVL53L1X distanceSensor_VL53L1X(qwiic);
12+
13+
#include "SparkFun_Ublox_Arduino_Library.h" //http://librarymanager/All#SparkFun_Ublox_GPS
14+
SFE_UBLOX_GPS gpsSensor_ublox;
15+
16+
#include "SparkFun_VCNL4040_Arduino_Library.h" //Click here to get the library: http://librarymanager/All#SparkFun_VCNL4040
17+
VCNL4040 proximitySensor_VCNL4040;
18+
19+
#include "SparkFun_MCP9600.h" //Click here to get the library: http://librarymanager/All#SparkFun_MCP9600
20+
MCP9600 thermoSensor_MCP9600;
21+
22+
#include "SparkFun_TMP117.h" //Click here to get the library: http://librarymanager/All#SparkFun_TMP117
23+
TMP117 tempSensor_TMP117;
24+
25+
#include "SparkFun_MS5637_Arduino_Library.h" //Click here to get the library: http://librarymanager/All#SparkFun_MS5637
26+
MS5637 pressureSensor_MS5637;
27+
28+
#include "SparkFun_LPS25HB_Arduino_Library.h" //Click here to get the library: http://librarymanager/All#SparkFun_LPS25HB
29+
LPS25HB pressureSensor_LPS25HB;
30+
31+
#include "SparkFunBME280.h" //Click here to get the library: http://librarymanager/All#SparkFun_BME280
32+
BME280 phtSensor_BME280;
33+
34+
#include "SparkFun_VEML6075_Arduino_Library.h" //Click here to get the library: http://librarymanager/All#SparkFun_VEML6075
35+
VEML6075 uvSensor_VEML6075;
36+
37+
#include "SparkFunCCS811.h" //Click here to get the library: http://librarymanager/All#SparkFun_CCS811
38+
#define CCS811_ADDR 0x5B //Default I2C Address
39+
//#define CCS811_ADDR 0x5A //Alternate I2C Address
40+
CCS811 vocSensor_CCS811(CCS811_ADDR);
41+
42+
#include "SparkFun_SGP30_Arduino_Library.h" //Click here to get the library: http://librarymanager/All#SparkFun_SGP30
43+
SGP30 vocSensor_SGP30;
44+
45+
#include "SparkFun_SCD30_Arduino_Library.h" //Click here to get the library: http://librarymanager/All#SparkFun_SCD30
46+
SCD30 co2Sensor_SCD30;
47+
48+
#include "MS8607_Library.h" //Click here to get the library: http://librarymanager/All#Qwiic_MS8607
49+
MS8607 pressureSensor_MS8607;
50+
51+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
52+
53+
struct struct_QwiicSensors {
54+
bool LPS25HB;
55+
bool MCP9600;
56+
bool BH1749NUC;
57+
bool NAU7802;
58+
bool uBlox;
59+
bool VL53L1X;
60+
bool VCNL4040;
61+
bool TMP117;
62+
bool CCS811;
63+
bool BME280;
64+
bool SGP30;
65+
bool VEML6075;
66+
bool MS5637;
67+
bool SCD30;
68+
bool MS8607;
69+
};
70+
71+
struct_QwiicSensors qwiicAvailable = {
72+
.LPS25HB = false,
73+
.MCP9600 = false,
74+
.BH1749NUC = false,
75+
.NAU7802 = false,
76+
.uBlox = false,
77+
.VL53L1X = false,
78+
.VCNL4040 = false,
79+
.TMP117 = false,
80+
.CCS811 = false,
81+
.BME280 = false,
82+
.SGP30 = false,
83+
.VEML6075 = false,
84+
.MS5637 = false,
85+
.SCD30 = false,
86+
.MS8607 = false,
87+
};
88+
89+
const byte PIN_QWIIC_POWER = 18;
90+
91+
void setup()
92+
{
93+
Serial.begin(115200);
94+
Serial.println("Scanning...");
95+
96+
qwiicPowerOn();
97+
98+
qwiic.begin();
99+
qwiic.setClock(100000);
100+
101+
qwiic.setPullups(1); //Set pullups to 1k. If we don't have pullups, detectQwiicDevices() takes ~900ms to complete. We'll disable pullups if something is detected.
102+
103+
// byte error, address;
104+
// int nDevices = 0;
105+
// for (address = 1; address < 127; address++ )
106+
// {
107+
// qwiic.beginTransmission(address);
108+
// error = qwiic.endTransmission();
109+
//
110+
// if (error == 0)
111+
// {
112+
// Serial.print("I2C device found at address 0x");
113+
// if (address < 16)
114+
// Serial.print("0");
115+
// Serial.print(address, HEX);
116+
// Serial.println();
117+
//
118+
// nDevices++;
119+
// }
120+
// // else if (error == 4)
121+
// // {
122+
// // Serial.print("Unknown error at address 0x");
123+
// // if (address < 16)
124+
// // Serial.print("0");
125+
// // Serial.println(address, HEX);
126+
// // }
127+
// }
128+
// if (nDevices == 0)
129+
// Serial.println("No I2C devices found\n");
130+
// else
131+
// Serial.println("done\n");
132+
}
133+
134+
void loop()
135+
{
136+
qwiicPowerOff();
137+
delay(1000);
138+
qwiicPowerOn();
139+
//delay(1000); //SCD30 acks and responds
140+
//delay(100); //SCD30 acks but fails to start
141+
delay(900); //
142+
Serial.println("On!");
143+
Serial.flush();
144+
145+
bool somethingDetected = false;
146+
147+
for (uint8_t address = 1 ; address < 127 ; address++)
148+
{
149+
qwiic.beginTransmission(address);
150+
if (qwiic.endTransmission() == 0)
151+
{
152+
Serial.printf("Device found at address 0x%02X\n", address);
153+
if (testDevice(address) == false)
154+
Serial.printf("Unknown I2C device found at address 0x%02X\n", address);
155+
else
156+
somethingDetected = true;
157+
}
158+
}
159+
160+
if (somethingDetected == false)
161+
Serial.println("Nothing detected");
162+
}
163+
164+
// Available Qwiic devices
165+
#define ADR_VEML6075 0x10
166+
#define ADR_NAU7802 0x2A
167+
#define ADR_VL53L1X 0x29
168+
#define ADR_MS8607 0x40 //Humidity portion of the MS8607 sensor
169+
#define ADR_UBLOX 0x42
170+
#define ADR_TMP117 0x48 //Alternates: 0x49, 0x4A, and 0x4B
171+
#define ADR_SGP30 0x58
172+
#define ADR_CCS811_2 0x5A
173+
#define ADR_CCS811_1 0x5B
174+
#define ADR_LPS25HB_2 0x5C
175+
#define ADR_LPS25HB_1 0x5D
176+
#define ADR_VCNL4040_OR_MCP9600 0x60
177+
#define ADR_SCD30 0x61
178+
#define ADR_MCP9600_1 0x66
179+
#define ADR_BME280_2 0x76
180+
#define ADR_MS5637 0x76
181+
//#define ADR_MS8607 0x76 //Pressure portion of the MS8607 sensor. We'll catch the 0x40 first
182+
#define ADR_BME280_1 0x77
183+
184+
//Given an address, see if it repsonds as we would expect
185+
//Returns false if I2C address is not known
186+
bool testDevice(uint8_t i2cAddress)
187+
{
188+
switch (i2cAddress)
189+
{
190+
case ADR_LPS25HB_1:
191+
if (pressureSensor_LPS25HB.begin(qwiic, ADR_LPS25HB_1) == true) //Wire port, Address.
192+
if (pressureSensor_LPS25HB.isConnected() == true)
193+
qwiicAvailable.LPS25HB = true;
194+
break;
195+
case ADR_LPS25HB_2:
196+
if (pressureSensor_LPS25HB.begin(qwiic, ADR_LPS25HB_2) == true) //Wire port, Address.
197+
if (pressureSensor_LPS25HB.isConnected() == true)
198+
qwiicAvailable.LPS25HB = true;
199+
break;
200+
case ADR_MCP9600_1:
201+
if (thermoSensor_MCP9600.begin(ADR_MCP9600_1, qwiic) == true) //Address, Wire port
202+
if (thermoSensor_MCP9600.isConnected() == true)
203+
qwiicAvailable.MCP9600 = true;
204+
break;
205+
case ADR_VCNL4040_OR_MCP9600:
206+
// if (thermoSensor_MCP9600.begin(ADR_MCP9600_1, qwiic) == true) //Address, Wire port
207+
// if (thermoSensor_MCP9600.isConnected() == true)
208+
// qwiicAvailable.MCP9600 = true;
209+
if (proximitySensor_VCNL4040.begin(qwiic) == true) //Wire port. Checks ID so should avoid collision with MCP9600
210+
qwiicAvailable.VCNL4040 = true;
211+
break;
212+
case ADR_NAU7802:
213+
if (loadcellSensor_NAU7802.begin(qwiic) == true) //Wire port
214+
qwiicAvailable.NAU7802 = true;
215+
break;
216+
case ADR_UBLOX:
217+
if (gpsSensor_ublox.begin(qwiic, ADR_UBLOX) == true) //Wire port, address
218+
qwiicAvailable.uBlox = true;
219+
break;
220+
case ADR_VL53L1X:
221+
if (distanceSensor_VL53L1X.begin() == 0) //Returns 0 if init was successful. Wire port passed in constructor.
222+
qwiicAvailable.VL53L1X = true;
223+
break;
224+
case ADR_TMP117:
225+
if (tempSensor_TMP117.begin(ADR_TMP117, qwiic) == true) //Adr, Wire port
226+
qwiicAvailable.TMP117 = true;
227+
break;
228+
case ADR_CCS811_1:
229+
if (vocSensor_CCS811.begin(qwiic) == true) //Wire port
230+
qwiicAvailable.CCS811 = true;
231+
break;
232+
case ADR_BME280_1:
233+
if (phtSensor_BME280.beginI2C(qwiic) == true) //Wire port
234+
qwiicAvailable.BME280 = true;
235+
break;
236+
case ADR_SGP30:
237+
if (vocSensor_SGP30.begin(qwiic) == true) //Wire port
238+
qwiicAvailable.SGP30 = true;
239+
break;
240+
case ADR_VEML6075:
241+
if (uvSensor_VEML6075.begin(qwiic) == true) //Wire port
242+
qwiicAvailable.VEML6075 = true;
243+
break;
244+
case ADR_MS5637:
245+
{
246+
//By the time we hit this address, MS8607 should have already been started by its first address
247+
if (qwiicAvailable.MS8607 == false)
248+
{
249+
if (pressureSensor_MS5637.begin(qwiic) == true) //Wire port
250+
qwiicAvailable.MS5637 = true;
251+
}
252+
break;
253+
}
254+
case ADR_SCD30:
255+
if (co2Sensor_SCD30.begin(qwiic) == true) //Wire port
256+
{
257+
qwiicAvailable.SCD30 = true;
258+
Serial.println("SCD30 found!");
259+
}
260+
else
261+
{
262+
Serial.println("SCD30 failed to respond");
263+
264+
//Give it 2s to boot and then try again
265+
delay(2000);
266+
267+
if (co2Sensor_SCD30.begin(qwiic) == true) //Wire port
268+
{
269+
qwiicAvailable.SCD30 = true;
270+
Serial.println("SCD30 found on second attempt!");
271+
}
272+
}
273+
break;
274+
case ADR_MS8607:
275+
if (pressureSensor_MS8607.begin(qwiic) == true) //Wire port. Tests for both 0x40 and 0x76 I2C addresses.
276+
qwiicAvailable.MS8607 = true;
277+
break;
278+
default:
279+
Serial.printf("Unknown device at address 0x%02X\n", i2cAddress);
280+
return false;
281+
break;
282+
}
283+
return true;
284+
}
285+
286+
void qwiicPowerOn()
287+
{
288+
pinMode(PIN_QWIIC_POWER, OUTPUT);
289+
digitalWrite(PIN_QWIIC_POWER, LOW);
290+
}
291+
void qwiicPowerOff()
292+
{
293+
pinMode(PIN_QWIIC_POWER, OUTPUT);
294+
digitalWrite(PIN_QWIIC_POWER, HIGH);
295+
}

0 commit comments

Comments
 (0)