Skip to content

Commit

Permalink
Usage with multiple Sensors
Browse files Browse the repository at this point in the history
* add sample code with description to README
* sample code shows how to connect two SHT31 sensors
  • Loading branch information
JHahn96 authored and abrauchli committed Aug 14, 2018
1 parent e8095e7 commit 386efbc
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions README.md
Expand Up @@ -67,3 +67,62 @@ void loop() {
delay(1000);
}
```

### Usage with two SHT31 sensors

```c++
#include <Wire.h>
#include "SHTSensor.h"

// Sensor with normal i2c address
// Sensor 1 with address pin pulled to GND
SHTSensor sht1(SHTSensor::SHT3X);

// Sensor with alternative i2c address
// Sensor 2 with address pin pulled to Vdd
SHTSensor sht2(SHTSensor::SHT3X_ALT);

void setup() {
// put your setup code here, to run once:
Wire.begin();
Serial.begin(9600);
delay(1000); // let serial console settle

// init on a specific sensor type (i.e. without auto detecting),
// does not check if the sensor is responding and will thus always succeed.

// initialize sensor with normal i2c-address
sht1.init();

// initialize sensor with alternative i2c-address
sht2.init();
}
void loop() {
// put your main code here, to run repeatedly:
// read from first sensor
if (sht1.readSample()) {
Serial.print("SHT1 :\n");
Serial.print(" RH: ");
Serial.print(sht1.getHumidity(), 2);
Serial.print("\n");
Serial.print(" T: ");
Serial.print(sht1.getTemperature(), 2);
Serial.print("\n");
} else {
Serial.print("Sensor 1: Error in readSample()\n");
}
// read from second sensor
if (sht2.readSample()) {
Serial.print("SHT2:\n");
Serial.print(" RH: ");
Serial.print(sht2.getHumidity(), 2);
Serial.print("\n");
Serial.print(" T: ");
Serial.print(sht2.getTemperature(), 2);
Serial.print("\n");
} else {
Serial.print("Sensor 2: Error in readSample()\n");
}
delay(1000);
}
```

2 comments on commit 386efbc

@winkj
Copy link
Member

@winkj winkj commented on 386efbc Sep 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add this to the examples/ directory, too?

@abrauchli
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, in that case I'd just move it there (and only link it in the README). @JHahn96 could you create a PR?

Please sign in to comment.