Cannot output data from multiple SHTC3 devices using tca9548a multiplexer

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
rhythm1
 
Posts: 1
Joined: Wed Feb 08, 2023 2:32 pm

Cannot output data from multiple SHTC3 devices using tca9548a multiplexer

Post by rhythm1 »

I am trying to read temperature and humidity data from 2 different SHTC3 sensors. I have the UNO board attached to an adafruit datalogger which is connected to a tca9548a where the sensors are connected. The code is sensing the two sensors attached. But the serial monitor out is as follows
Serial monitor output from code
Serial monitor output from code
Sensor output.PNG (6.83 KiB) Viewed 80 times
. Not sure where what my mistake is. Any help would be appreciated.

Code: Select all

// including all of the necesary libraries
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include "RTClib.h"
#include "Adafruit_SHTC3.h"
#include <LiquidCrystal.h>


// int LCD screen
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

// int temp and humidity sensor
Adafruit_SHTC3 shtc3 = Adafruit_SHTC3();

// int time sensor
RTC_PCF8523 rtc;

// int SD card 
const int chipSelect = 10;

// int File name
File pucks18;


// Select I2C BUS
void TCA9548A(uint8_t bus){
  Wire.beginTransmission(0x70);  // TCA9548A address
  Wire.write(1 << bus);          // send byte to select bus
  Wire.endTransmission();
  Serial.print(bus);
}
 

void setup() {
  Serial.begin(115200);
  Wire.begin();
  lcd.begin(16, 2);
  pinMode(chipSelect, OUTPUT);
  
#ifndef ESP8266
  while (!Serial); // wait for serial port to connect. Needed for native USB
#endif


// int sensors in the setup  
  // Init sensor 1
  TCA9548A(0);
  Serial.println("SHTC3 test");
  if (! shtc3.begin()) {
    Serial.println("Couldn't find SHTC3");
    while (1) delay(1);
  }
  Serial.println("Found SHTC3 1 sensor");
  

  // Init sensor 2 
  TCA9548A(1);
  Serial.println("SHTC3 test");
  if (! shtc3.begin()) {
    Serial.println("Couldn't find SHTC3");
    while (1) delay(1);
  }
  Serial.println("Found SHTC3 2 sensor");

//    // Init sensor 3
//  TCA9548A(2);
//  Serial.println("SHTC3 test");
//  if (! shtc3.begin()) {
//    Serial.println("Couldn't find SHTC3");
//    while (1) delay(1);
//  }
//  Serial.println("Found SHTC3 sensor");
//  
//
//  // Init sensor 4 
//  TCA9548A(3);
//  Serial.println("SHTC3 test");
//  if (! shtc3.begin()) {
//    Serial.println("Couldn't find SHTC3");
//    while (1) delay(1);
//  }
//  Serial.println("Found SHTC3 sensor");
//
//    // Init sensor 5
//  TCA9548A(4);
//  Serial.println("SHTC3 test");
//  if (! shtc3.begin()) {
//    Serial.println("Couldn't find SHTC3");
//    while (1) delay(1);
//  }
//  Serial.println("Found SHTC3 sensor");
//  
//
//  // Init sensor 6 
//  TCA9548A(5);
//  Serial.println("SHTC3 test");
//  if (! shtc3.begin()) {
//    Serial.println("Couldn't find SHTC3");
//    while (1) delay(1);
//  }
//  Serial.println("Found SHTC3 sensor");
//
//    // Init sensor 7
//  TCA9548A(6);
//  Serial.println("SHTC3 test");
//  if (! shtc3.begin()) {
//    Serial.println("Couldn't find SHTC3");
//    while (1) delay(1);
//  }
//  Serial.println("Found SHTC3 sensor");
//  
//
//  // Init sensor 8
//  TCA9548A(7);
//  Serial.println("SHTC3 test");
//  if (! shtc3.begin()) {
//    Serial.println("Couldn't find SHTC3");
//    while (1) delay(1);
//  }
//  Serial.println("Found SHTC3 sensor");


// int SD card in the setup  
  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (SD.begin(chipSelect))  {
    Serial.println("SD card is initialized. Ready to go");
   } 
  else  {
    Serial.println("Failed");
    return;
  }

// Create/Open file 
 pucks18 = SD.open("data18.txt", FILE_WRITE);
  
  // if the file opened okay, write to it:
  if (pucks18) {
    pucks18.print("Date");pucks18.print(",");pucks18.print("Time");pucks18.print(",");pucks18.print("Temperature");pucks18.print(",");pucks18.print("Humidity");

    }
  // if the file didn't open, print an error:
  else {
    Serial.println("error opening test.txt");
  }

// int time in the setup
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    while (1) delay(10);
  }
  
  if (! rtc.initialized() || rtc.lostPower()) {
    Serial.println("RTC is NOT initialized, let's set the time!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

  float drift = 43; // seconds plus or minus over oservation period - set to 0 to cancel previous calibration.
  float period_sec = (7 * 86400);  // total obsevation period in seconds (86400 = seconds in 1 day:  7 days = (7 * 86400) seconds )
  float deviation_ppm = (drift / period_sec * 1000000); //  deviation in parts per million (μs)
  float drift_unit = 4.34; // use with offset mode PCF8523_TwoHours
  // float drift_unit = 4.069; //For corrections every min the drift_unit is 4.069 ppm (use with offset mode PCF8523_OneMinute)
  int offset = round(deviation_ppm / drift_unit);
  // rtc.calibrate(PCF8523_TwoHours, offset); // Un-comment to perform calibration once drift (seconds) and observation period (seconds) are correct
  // rtc.calibrate(PCF8523_TwoHours, 0); // Un-comment to cancel previous calibration

  //Serial.print("Offset is "); Serial.println(offset); // Print to control offset
}
  
}


void loop() {
//  sensors_event_t humidity, temp;
//  shtc3.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
sensors_event_t humidity, temp;
shtc3.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data

  // get time reading and print 
    DateTime now = rtc.now();

    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(",");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.print(",");

//    Serial.print(temp.temperature); Serial.print(","); Serial.print(humidity.relative_humidity);Serial.print(",");
  
   TCA9548A(0);
   Serial.print(temp.temperature); Serial.print(","); Serial.print(humidity.relative_humidity);Serial.print(",");

   TCA9548A(1);
   Serial.print(temp.temperature); Serial.print(","); Serial.print(humidity.relative_humidity);Serial.print(",");
   Serial.println(" ");
   
  lcd.setCursor(0, 0);
  lcd.print("Humidity: "); lcd.print(humidity.relative_humidity); lcd.print("%");

  lcd.setCursor(0,1);
  lcd.print("Temp: "); lcd.print(temp.temperature); lcd.print("C");

   pucks18 = SD.open("data18.txt", FILE_WRITE);
  
  // if the file opened okay, write to it:
  if (pucks18) {
    pucks18.print(now.year(), DEC);
    pucks18.print('/');
    pucks18.print(now.month(), DEC);
    pucks18.print('/');
    pucks18.print(now.day(), DEC);
    pucks18.print(",");
    pucks18.print(now.hour(), DEC);
    pucks18.print(':');
    pucks18.print(now.minute(), DEC);
    pucks18.print(':');
    pucks18.print(now.second(), DEC);
    pucks18.print(","); 
    pucks18.close(); // close the file
    }
  // if the file didn't open, print an error:
  else {
    Serial.println("error opening test.txt");
  }

  
  Serial.println();
  delay(10000);
}

User avatar
adafruit_support_bill
 
Posts: 88096
Joined: Sat Feb 07, 2009 10:11 am

Re: Cannot output data from multiple SHTC3 devices using tca9548a multiplexer

Post by adafruit_support_bill »

You need to select the channel before you read the data from the sensor. It doesn't matter which channel is selected when you go to write it.

Locked
Please be positive and constructive with your questions and comments.

Return to “Microcontrollers”