Hello dear Khabrovites, well, here I am now helping you.
In the process of developing an interesting device, I encountered a problem, it was necessary to quickly connect several htu 21 sensors to the arduinka and record the readings. The sensor was pleased with the presence of the i2c bus, but then there was a bummer, because their addresses are static. An internal arduino genius came up with a clever way with alternate switching and polling of sensors, rubbing my palms, I watched the controller sometimes freeze, because everything is not so simple. Googling, I found only this , the same genius, but he apparently did not test the bundle, because it is not operational for more than a couple of minutes due to the through current through the protective diode of the port.
In the comments, smart people suggested different ways, and oh yes, the simplest, cheapest way turned out to be using the cd4051be analog multiplexer. Quoting the comment:
"Yes, it's easier. And there are fewer legs, and I2C hardware (we are not talking about controllers in which SCL can be brought out on any foot?) - an analog multiplexer such as even a prehistoric CD4051, SDA is installed directly on all chips, SCL through a multiplexer. Three additional legs, 8 additional channels. Do not forget to put the SCL pull-up to the power supply after the multiplexer, and not before. "
I connected it according to the scheme from here , only the output of the multiplexer is not to A0, but to A5. 3 SCL sensors are connected to leg 15, 13 and 1, SDA common.
The simplest Indian code, based on the state table
address buses are enabled via digitalWrite () (please do not hit, I do not rummage)
int temp1;
int hum1;
int temp2;
int hum2;
int temp3;
int hum3;
#include <HTU21D.h>
HTU21D myHTU21D(HTU21D_RES_RH12_TEMP14);
void setup() {
pinMode(8, OUTPUT); // CD4051 pin 11 (A)
pinMode(9, OUTPUT); // CD4051 pin 10 (B)
pinMode(10, OUTPUT); // CD4051 pin 9 (C)
Serial.begin(9600);
while (myHTU21D.begin() != true)
{
Serial.println(F("Sensor is faild or not connected")); //(F()) saves string to flash & keeps dynamic memory free
delay (200);
// 9600 .
}
}
void loop() {
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
myHTU21D.begin();
delay (150);
temp1 = myHTU21D.readTemperature();
hum1 = myHTU21D.readCompensatedHumidity();
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
myHTU21D.begin();
delay (150);
temp2 = myHTU21D.readTemperature();
hum2 = myHTU21D.readCompensatedHumidity();
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
myHTU21D.begin();
delay (150);
temp3 = myHTU21D.readTemperature();
hum3 = myHTU21D.readCompensatedHumidity();
Serial.print(" T1: "); Serial.print(temp1 );
Serial.print(" H1: "); Serial.print(hum1 );
Serial.print(" T2: "); Serial.print(temp2 );
Serial.print(" 2: "); Serial.print(hum2 );
Serial.print(" 3: "); Serial.print(temp3 );
Serial.print(" 3: "); Serial.print(hum3 );