Mutiple I2C Devices with SAME Address - SHT21/HTU21D
Posted: Wed Jan 04, 2017 1:23 pm
This is an example of solving the problem of I2C devices with no user adjustable Device Address.
There are probably other solutions but this is very simple.......
There are no Library changes..........
There are probably other solutions but this is very simple.......
There are no Library changes..........
Code: Select all
/*
This is an Arduino library for SHT21 & HTU21D Digital Humidity & Temperature Sensor
Adapted for Multiple Sensors with no way to change device address.........
Pins, any you have spare on your ESP8266, But NOT A0 or D16(D0 Node)
The devices can share a Clock pin but not the data pin if they share an address.....
In this example I have used...(I only have 3 spare SHT21's)
pin 5 for SCL
Pin 0 for SDA Bus 0
Pin 2 for SDA Bus 1
Pin 4 for SDA Bus 2
Written by enjoyneering79
Adapted Jan 2017
These sensors use I2C to communicate, 2 pins are required to interface
Connect HTU21D to pins : SDA SCL
Uno, Redboard, Pro: A4 A5
Mega2560, Due: 20 21
Leonardo: 2 3
BSD license, all text above must be included in any redistribution
*/
/**************************************************************************/
#include <Wire.h>
#include "HTU21D.h"
HTU21D myHTU21D;
int flip=0;
void setup()
{
Serial.begin(115200);
//Serial.set_tx (2);
Serial.println(F(""));
delay(10);
Wire.begin(4,5);
delay(250);
while (myHTU21D.begin() != true)
{
Serial.println(F("HTU21D sensor is not present"));
delay(5000);
}
Serial.println(F("HTU21D sensor is present"));
}
void loop()
{
if(flip==0){
flip=1;
delay(250);
Wire.begin(0, 5);
delay(100);
myHTU21D.begin();
delay(250);
Serial.println(F("Sensor Bus 0"));
}else if (flip==1){
flip=2;
delay(250);
Wire.begin(2, 5);
delay(100);
myHTU21D.begin();
delay(250);
Serial.println(F("Sensor Bus 1"));
}else{
flip=0;
delay(250);
Wire.begin(4, 5);
delay(100);
myHTU21D.begin();
delay(250);
Serial.println(F("Sensor Bus 2"));
}
Serial.println(F(""));
Serial.println(F("<<DEMO: Default settings, %RH: 12Bit, Temperature - 14Bit>>"));
Serial.println(F(""));
Serial.print(F("Compensated Humidity: "));
Serial.print(myHTU21D.readCompensatedHumidity());
Serial.println(F(" +-2%RH in range 0%RH - 100%RH at tmp. range 0deg.C - 80deg.C"));
Serial.println(F(""));
Serial.print(F("Temperature: "));
Serial.print(myHTU21D.readTemperature());
Serial.println(F(" +-0.5 deg.C"));
Serial.println(F(""));
Serial.println(F("\r\n\r\n"));
delay(5000);
}