Reading from two sensors with getEvent()

Hey guys, first time posting.

I wonder why I only get readings from the alternative AHT10 sensor , the one with address x39. If I .getEvent() from the other default AHT10, shouldn't it grab that value? Can I assign sensor_event_t to a sensor ID somehow?

Thanks for your time

#include <Wire.h>
#include <Adafruit_AHTX0.h>
#include <U8x8lib.h>
#include <Arduino.h>

// I2C
// GND = BLUE
// VDD = RED
// SCL = YELLOW
// SDA = BLACK

// Objects
U8X8_SSD1306_128X32_UNIVISION_HW_I2C u8x8(U8X8_PIN_NONE);
Adafruit_AHTX0 shoeSens;
Adafruit_AHTX0 refSens;

// Global variables
int delayTime = 1000;
int lastTimeOff = millis();
int tempRef;
int humiRef;
int tempShoe;
int humiShoe;

void setup(){
  // Outputs
  Wire.begin();
  Serial.begin(9600);
  u8x8.setI2CAddress(0x78);
  u8x8.begin();
  u8x8.setFont(u8x8_font_artossans8_r);
  u8x8.clearDisplay();
  pinMode(2,OUTPUT); // fan
  pinMode(3,OUTPUT); // heat

  // Inputs
  shoeSens.begin(&Wire, 0, AHTX0_I2CADDR_DEFAULT);
  refSens.begin(&Wire, 1, AHTX0_I2CADDR_ALTERNATE);
  pinMode(4, INPUT); // start breaker
}

void loop(){

  sensors_event_t humidity, temp;
  refSens.getEvent(&humidity, &temp);
  tempRef= temp.temperature; 
  Serial.println(tempRef);
  humiRef = humidity.relative_humidity;
  Serial.println(humiRef);
  
  shoeSens.getEvent(&humidity, &temp);
  tempShoe= temp.temperature; 
  Serial.println(tempShoe);
  humiShoe = humidity.relative_humidity;
  Serial.println(humiShoe);
  

  bool breaker = digitalRead(4); // If a shoe is placed

  u8x8.clearDisplay();
  u8x8.setCursor(0, 0);
  u8x8.print(F("Temp: "));
  u8x8.print(tempShoe);
  u8x8.setCursor(0, 2);
  u8x8.print(F("Humi: "));
  u8x8.print(humiShoe);
  delay(delayTime);
  u8x8.clearDisplay();
  u8x8.setCursor(0, 0);
  
  if(breaker == HIGH){ 
    int currentTime = millis();
    int timeDiff = currentTime - lastTimeOff;

    u8x8.print(F("ShoeTemp: "));
    u8x8.print(tempShoe);
    u8x8.setCursor(0, 2);
    u8x8.print(F("ShoeHumi: "));
    u8x8.print(humiShoe);
    delay(delayTime);
    u8x8.clearDisplay();
    u8x8.setCursor(0, 0);

    if (timeDiff < 30000){ // Force fan and heat on for the first 30 seconds
      digitalWrite(2, HIGH);
      digitalWrite(3, HIGH);
      u8x8.print(F("STARTING"));
    }
    else if (tempShoe > 70){ // Shut of heat at temperatures over 70C
      digitalWrite(2, LOW);
      u8x8.print(F("OVERHEAT"));
    }
    else if (humiRef-humiShoe < 10){  // Shoe is dry if humidity difference is less than 10
      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
      u8x8.print(F("DRY"));
    }
    else if (timeDiff < 7200000){ // Restart heat if not over 70C anymore 
      digitalWrite(2, HIGH);
      u8x8.print(F("RUNNING"));
    }
    else { // Safety shutoff after 2 hours
      digitalWrite(2, LOW);
      digitalWrite(3, LOW);
      u8x8.print(F("SAFETY SHUTOFF"));
    }
  }
  else {
    int lastTimeOff = millis();
    u8x8.print(F("STANDBY"));
  }
  delay(delayTime);
}
1 Like

You are using the same variables for both sensors. Could that be a problem ?

Yes, there is something to that. But sensors_event_t only accepts humidity and temp as variable names (I think?) and if I change them to a different names the program crashes. Therefore I wonder if I can assign it to a sensor ID instead.

I am not familiar with the library but if I have any other ideas I will reply here

1 Like

In your code, you have two instances of the Adafruit_AHTX0 class, shoeSens and refSens, each initialized with different addresses:

shoeSens.begin(&Wire, 0, AHTX0_I2CADDR_DEFAULT);
refSens.begin(&Wire, 1, AHTX0_I2CADDR_ALTERNATE);

When you call getEvent() on shoeSens and refSens, you are correctly retrieving readings from each sensor using their respective addresses. However, there seems to be an issue in your loop() function logic.

Your code sets lastTimeOff inside the else block every time, which effectively resets it to the current time every iteration. Instead, you should remove the int declaration inside the else block to update the global lastTimeOff variable.

else {
    lastTimeOff = millis(); // Update the global variable instead of declaring a new one
    u8x8.print(F("STANDBY"));
}

Yes, thanks for pointing that out. However, that doesn't solve the reading values from sensor issue.

I see. Please check the connections too. Check your jumper wires with a multimeter.

Connections are fine. I2C scanner shows consistent connection with all slaves. The problem is with the getEvent() function or something related to that in the code.

So you have modified one AHT10 for the alternate I2C address?

I've just read something about how you can't use two AHT10s on the same I2C bus. There was even the assertion that you can't use other I2C devices at the same time.

A solution was to use software I2C.

a7

I have modified one of them to its alternative address yes. Last thing I read is that you can use up to 10 slaves on the I2C, with correct pull-ups, which we have. Do you have a link to where you read that?

Google

    2 AHT10 on i2c

and you'll run into some concerns expressed, down to assertions that the data sheet says you can't, and ppl saying the address changing mechanism just does not work.

But, hey, that's the internets for ya.

Does your scan show devices at both addresses? Shows all expected devices?

a7

Yes, that's the crazy thing, it does. I have consistent connections at address x38 , x39 and x3C (screen). And it works grabing the values from one of the sensors and sending that to the screen and printing. So two different slaves on I2C defenitly works with grabing and sending values. But still, I think the problem is in the code. That the getEvent() function or sensor_event_t overwrites the second sensor somehow. I would love to bind the sensor_event_t to a specific sensor ID if possible, but don't know how to do it. I have also tried to duplicate the library for the sensor but that didn't seem to work either. Thanks for your help though.

To clarify, I think this because that if I only read from the modified address x39 sensor, it works fine. Even with the screen.

Your calls to getEvent() already select the device

  shoeSens.getEvent(&humidity, &temp);

This example shows reading the sensors separately, perhaps this would be a path:

although it looks like maybe you doing that lready.

Like

theTemp = shoeSens.getTemperatureSensor();

a7

The AHT10 datasheet gives NO indication that the address is selectable. In fact, the description of the serial interface shows ONLY the address only as 0x38. There is a pin named "ADR", which suggests an address select pin, but it describes that pin only as "Power Ground", and make NO mention of any address select capability.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.