Skip to content

Commit aa233da

Browse files
committed
Move example from README to its own file
Nove the multiple/two sensors example from the readme to its own file. Add an additional note on why autodetection doesn't work and what sensor combination is expected to work.
1 parent 386efbc commit aa233da

File tree

2 files changed

+68
-87
lines changed

2 files changed

+68
-87
lines changed

README.md

Lines changed: 6 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -36,93 +36,12 @@ necessary:
3636
from the sensor, but return the values read last. To read a new sample, make
3737
sure to call `readSample()`
3838

39-
### Sample code
40-
```c++
41-
#include <Wire.h>
39+
## Example projects
4240

43-
#include <SHTSensor.h>
41+
See example project
42+
[sht-autodetect](examples/sht-autodetect/sht-autodetect.ino)
4443

45-
SHTSensor sht;
44+
### Usage with multiple SHT31 sensors
4645

47-
void setup() {
48-
// put your setup code here, to run once:
49-
50-
Wire.begin();
51-
Serial.begin(9600);
52-
sht.init();
53-
}
54-
55-
void loop() {
56-
// put your main code here, to run repeatedly:
57-
58-
sht.readSample();
59-
Serial.print("SHT:\n");
60-
Serial.print(" RH: ");
61-
Serial.print(sht.getHumidity(), 2);
62-
Serial.print("\n");
63-
Serial.print(" T: ");
64-
Serial.print(sht.getTemperature(), 2);
65-
Serial.print("\n");
66-
67-
delay(1000);
68-
}
69-
```
70-
71-
### Usage with two SHT31 sensors
72-
73-
```c++
74-
#include <Wire.h>
75-
#include "SHTSensor.h"
76-
77-
// Sensor with normal i2c address
78-
// Sensor 1 with address pin pulled to GND
79-
SHTSensor sht1(SHTSensor::SHT3X);
80-
81-
// Sensor with alternative i2c address
82-
// Sensor 2 with address pin pulled to Vdd
83-
SHTSensor sht2(SHTSensor::SHT3X_ALT);
84-
85-
void setup() {
86-
// put your setup code here, to run once:
87-
Wire.begin();
88-
Serial.begin(9600);
89-
delay(1000); // let serial console settle
90-
91-
// init on a specific sensor type (i.e. without auto detecting),
92-
// does not check if the sensor is responding and will thus always succeed.
93-
94-
// initialize sensor with normal i2c-address
95-
sht1.init();
96-
97-
// initialize sensor with alternative i2c-address
98-
sht2.init();
99-
}
100-
void loop() {
101-
// put your main code here, to run repeatedly:
102-
// read from first sensor
103-
if (sht1.readSample()) {
104-
Serial.print("SHT1 :\n");
105-
Serial.print(" RH: ");
106-
Serial.print(sht1.getHumidity(), 2);
107-
Serial.print("\n");
108-
Serial.print(" T: ");
109-
Serial.print(sht1.getTemperature(), 2);
110-
Serial.print("\n");
111-
} else {
112-
Serial.print("Sensor 1: Error in readSample()\n");
113-
}
114-
// read from second sensor
115-
if (sht2.readSample()) {
116-
Serial.print("SHT2:\n");
117-
Serial.print(" RH: ");
118-
Serial.print(sht2.getHumidity(), 2);
119-
Serial.print("\n");
120-
Serial.print(" T: ");
121-
Serial.print(sht2.getTemperature(), 2);
122-
Serial.print("\n");
123-
} else {
124-
Serial.print("Sensor 2: Error in readSample()\n");
125-
}
126-
delay(1000);
127-
}
128-
```
46+
See example project
47+
[multiple-sht-sensors](examples/multiple-sht-sensors/multiple-sht-sensors.ino)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <Wire.h>
2+
#include "SHTSensor.h"
3+
4+
// Note that all i2c devices sharing one bus must have distinct addresses. Thus
5+
// this example only works with sensors that have distinct i2c addresses (e.g.
6+
// SHT3x and SHT3x_alt, or SHT3x and SHTC3).
7+
// Make sure not to use auto-detection as it will only pick up one sensor.
8+
9+
// Sensor with normal i2c address
10+
// Sensor 1 with address pin pulled to GND
11+
SHTSensor sht1(SHTSensor::SHT3X);
12+
13+
// Sensor with alternative i2c address
14+
// Sensor 2 with address pin pulled to Vdd
15+
SHTSensor sht2(SHTSensor::SHT3X_ALT);
16+
17+
void setup() {
18+
// put your setup code here, to run once:
19+
Wire.begin();
20+
Serial.begin(9600);
21+
delay(1000); // let serial console settle
22+
23+
// init on a specific sensor type (i.e. without auto detecting),
24+
// does not check if the sensor is responding and will thus always succeed.
25+
26+
// initialize sensor with normal i2c-address
27+
sht1.init();
28+
29+
// initialize sensor with alternative i2c-address
30+
sht2.init();
31+
}
32+
33+
void loop() {
34+
// put your main code here, to run repeatedly:
35+
// read from first sensor
36+
if (sht1.readSample()) {
37+
Serial.print("SHT1 :\n");
38+
Serial.print(" RH: ");
39+
Serial.print(sht1.getHumidity(), 2);
40+
Serial.print("\n");
41+
Serial.print(" T: ");
42+
Serial.print(sht1.getTemperature(), 2);
43+
Serial.print("\n");
44+
} else {
45+
Serial.print("Sensor 1: Error in readSample()\n");
46+
}
47+
48+
// read from second sensor
49+
if (sht2.readSample()) {
50+
Serial.print("SHT2:\n");
51+
Serial.print(" RH: ");
52+
Serial.print(sht2.getHumidity(), 2);
53+
Serial.print("\n");
54+
Serial.print(" T: ");
55+
Serial.print(sht2.getTemperature(), 2);
56+
Serial.print("\n");
57+
} else {
58+
Serial.print("Sensor 2: Error in readSample()\n");
59+
}
60+
61+
delay(1000);
62+
}

0 commit comments

Comments
 (0)