RobTillaart / TCA9548

Arduino library for TCA9548 8 channel I2C multiplexer and compatibles.
MIT License
20 stars 7 forks source link

Problem with configuring multiple AM2320 sensors with PCA9546 #17

Closed Idek97 closed 10 months ago

Idek97 commented 10 months ago

Hello, I would like to connect three AM2320 sensors to the ESP8266 through the PCA9546 multiplexer, but I do not know how to configure them to work with it. I can configure a single sensor, but I don't understand the principle of accessing it through the multiplexer channel.

Could you please create a simple code example for configuring the sensors?

RobTillaart commented 10 months ago

Thanks for the issue, I will look into it later today,

RobTillaart commented 10 months ago

Wrote some basic code, it compiles but I have no hardware around to test.

please let me know if it works or other questions.

//
//    FILE: TCA9548_demo_AM2320.ino
//  AUTHOR: Rob Tillaart
// PURPOSE: demo TCA9548 I2C multiplexer
//     URL: https://github.com/RobTillaart/TCA9548
//     URL: https://github.com/RobTillaart/AM232X

#include "AM232X.h"
#include "TCA9548.h"

PCA9546 MP(0x70);
uint8_t channels = 0;

AM232X living;    //  channel 0
AM232X kitchen;   //  channel 1
AM232X outside;   //  channel 2

uint32_t lastTime = 0;

void setup()
{
  Serial.begin(115200);
  Serial.println(__FILE__);
  Serial.print("TCA9548_LIB_VERSION: ");
  Serial.println(TCA9548_LIB_VERSION);
  Serial.print("AM232X_LIB_VERSION: ");
  Serial.println(AM232X_LIB_VERSION);
  Serial.println();

  Wire.begin();
  //  initialize multiplexer
  if (MP.begin() == false)
  {
    Serial.println("Multiplexer error");
  }
  channels = MP.channelCount();

  //  initialize the temperature sensors
  MP.selectChannel(0);
  if (living.begin() == false)
  {
    Serial.println("living error");
  }
  MP.selectChannel(1);
  if (kitchen.begin() == false )
  {
    Serial.println("kitchen error");
  }
  MP.selectChannel(2);
  if (outside.begin() == false )
  {
    Serial.println("outside error");
  }
}

void loop()
{
  if ((millis() - lastTime) > 5000)
  {
    lastTime = millis();

    MP.selectChannel(0);
    living.read();
    Serial.print(living.getTemperature(), 1);
    Serial.print("\t");

    MP.selectChannel(1);
    kitchen.read();
    Serial.print(kitchen.getTemperature(), 1);
    Serial.print("\t");

    MP.selectChannel(2);
    outside.read();
    Serial.print(outside.getTemperature(), 1);
    Serial.print("\n");
  }
}

//  -- END OF FILE --
Idek97 commented 10 months ago

Thank you very much, the program works perfectly. I thought that when creating an AM2320 sensor object, it had to be assigned to a multiplexer channel at the time of creating the object.

RobTillaart commented 10 months ago

Note to myself: It could be an idea to have a AM2320_MX class that holds the channel for the multiplexer.

RobTillaart commented 10 months ago

Thank you very much, the program works perfectly. I thought that when creating an AM2320 sensor object, it had to be assigned to a multiplexer channel at the time of creating the object.

Good to hear it works, I will include it in a release asap.