-1

I am trying to read several temperature and humidity sensors, for this I have a LILYGO T-Display-S3 board, a TCA9548A multiplexer and a pair of Adafruit AHT20 sensors....

enter image description here

I am trying to initialize the sensors, but I know that the program hangs at that point.... I have tried several things and I can't get it to go beyond that

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_AHTX0.h>

#define TCAADDR 0x70

uint8_t dataPin = 43;
uint8_t clockPin = 44;

Adafruit_AHTX0 dev0;
Adafruit_AHTX0 dev1;

Adafruit_Sensor *sens0Humd, *sens0Temp;
Adafruit_Sensor *sens1Humd, *sens1Temp;

void tcaselect(uint8_t i) {
  Serial.println("Trying to select channel from TCA9548A.");
  if (i > 7) return;
  Wire.setPins(dataPin, clockPin);
  Wire.beginTransmission(TCAADDR);
  Wire.write(1 << i);
  Wire.endTransmission();
  Serial.println("Channel TCA9548A selected.");  
}


void setup(void) {
  Serial.begin(9600);
  Serial.println("--> Setup");
  Serial.println("Initializing sensors...");

  /* Initialise the 1st sensor */
  tcaselect(0);
  Serial.println("Starting Device 0.");
  if(!dev0.begin()){
    Serial.println("Ooops, no sensor detected ... Check your wiring!");
  } else {
    Serial.println("Sensor successfully initialized!");
  }

  /* Initialise the 2nd sensor */
  tcaselect(1);
  Serial.println("Starting Device 1.");
  if(!dev1.begin()){
    Serial.println("Ooops, no sensor detected ... Check your wiring!");
  } else {
    Serial.println("Sensor successfully initialized!");
  }

  /* Display some basic information on this sensor */
  tcaselect(0);
  sens0Humd = dev0.getHumiditySensor();
  sens0Humd->printSensorDetails();
  sens0Temp = dev0.getTemperatureSensor();
  sens0Temp->printSensorDetails();

  tcaselect(1);
  sens1Humd = dev1.getHumiditySensor();
  sens1Humd->printSensorDetails();
  sens1Temp = dev1.getTemperatureSensor();
  sens1Temp->printSensorDetails();
}

void loop(void) {

}

Here is what I get back via messages through the serial port...

11:13:45.144 -> ESP-ROM:esp32s3-20210327
11:13:45.144 -> --> Setup
11:13:45.144 -> Initializing sensors...
11:13:45.144 -> Trying to select channel from TCA9548A.
11:13:45.144 -> Channel TCA9548A selected.
11:13:45.144 -> Starting Device 0.

This is the connection diagram: 1: https://i.sstatic.net/eChr70vI.png

And program code:

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_AHTX0.h>

#define TCAADDR 0x70

uint8_t dataPin = 18;
uint8_t clockPin = 17;

Adafruit_AHTX0 dev0;
Adafruit_AHTX0 dev1;

Adafruit_Sensor *sens0Humd, *sens0Temp;
Adafruit_Sensor *sens1Humd, *sens1Temp;

void tcaselect(uint8_t i) {
  Serial.println("Trying to select channel from TCA9548A.");
  if (i > 7) return;
  Wire.setPins(dataPin, clockPin);

  Wire.beginTransmission(TCAADDR);
  Wire.write(1 << i);
  Wire.endTransmission();
  Serial.println("Channel TCA9548A selected.");  
}

void setup(void) {
  Serial.begin(9600);
  Serial.println("--> Setup");
  Serial.println("Initializing sensors...");

  /* Initialise the 1st sensor */
  tcaselect(2);
  Serial.println("Starting Device 0.");
  if(!dev0.begin()){
    Serial.println("Ooops, no sensor detected ... Check your wiring!");
    while(1);
  } else {
    Serial.println("Sensor successfully initialized!");
  }

  /* Initialise the 2nd sensor */
  tcaselect(7);
  Serial.println("Starting Device 1.");
  if(!dev1.begin()){
    Serial.println("Ooops, no sensor detected ... Check your wiring!");
    while(1);
  } else {
    Serial.println("Sensor successfully initialized!");
  }

  /* Display some basic information on this sensor */
  tcaselect(2);
  sens0Humd = dev0.getHumiditySensor();
  sens0Humd->printSensorDetails();
  sens0Temp = dev0.getTemperatureSensor();
  sens0Temp->printSensorDetails();

  tcaselect(7);
  sens1Humd = dev1.getHumiditySensor();
  sens1Humd->printSensorDetails();
  sens1Temp = dev1.getTemperatureSensor();
  sens1Temp->printSensorDetails();
}

void loop(void) {

}

Serial output:

08:55:37.078 -> ESP-ROM:esp32s3-20210327
08:55:37.078 -> --> Setup
08:55:37.078 -> Initializing sensors...
08:55:37.078 -> Trying to select channel from TCA9548A.
08:55:37.078 -> Channel TCA9548A selected.
08:55:37.078 -> Starting Device 0.
10
  • 1
    Call Wire.begin() to setup I2C before calling tcaselect(0);. BTW, is there any reason you use pin 43 & 44 as I2C bus instead of the default pins?
    – hcheung
    Commented Jun 7 at 1:57
  • 2
    There is an example I2C scanner on the github site for your LILYGO T-Display-S3 board here which you could try. Change sda and scl to match your choice of pins. Hopefully it finds your TCA9548A device. github.com/Xinyuan-LilyGO/T-Display-S3/blob/main/examples/…
    – 6v6gt
    Commented Jun 7 at 3:29
  • 1
    It seems like the program is hanging during the initialization of the first sensor. You can use an I2C scanner to check if the devices are detected on the I2C bus. github.com/RalphBacon/TCA9548A-I2C_MULTIPLEXER/blob/master/… You can also switch to a temperature sensor that follows other communication protocol such as DS18B20. theengineeringprojects.com/2016/03/…
    – tepalia
    Commented Jun 7 at 14:37
  • 1
    That is the expected result. The scanner can see only the multiplexer. It cannot see the devices which are connected to the multiplexer channels. So you now have to work out what is wrong with your original code which manipulates the multiplexer to connect to your sensors. Update your question to include a link to the example you found on which you based your code.
    – 6v6gt
    Commented Jun 8 at 1:47
  • 1
    Also draw a diagram showing the connections between the pins of the AHT20 sensors, the multiplexer and the LILYGO board. It appears from your picture that there is a direct connection with black wires between a gpio pin of the board (it could be P24 but the image isn't clear) and the sensors. I would have expected only power connections between the sensors and the LILYGO board.
    – 6v6gt
    Commented Jun 8 at 2:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.