Cannot combine TCMux code with either BME280 or DHT22

Hi to all of you, a bit nervous about posting for the first time, please excuse dumb questions.

My expert level: None. Absolute arduino beginner. Sorry : (

Project aim: Measuring & logging leaf temperature, ambient light, air temperature, humidity, and possibly atmospheric pressure on 5 alpine plots from June to September.

My hardware:
Arduino Mega 2560
Adafruit Data Logger shield
Thermocouple Multiplexer Shield (K - MAX31855K) with 8 selfmade K-type TCs for leaf temp
Ambient Light Sensor (TEMT6000)
and either an Adafruit BME280 temp, humidity and pressure sensor
or a DHT22 (Grove – Temperature & Humidity Sensor Pro (AM2302)

Project step1: Sensor & code testing with the serial monitor - DONE : )

Project step2: Combine code parts into one code and test with seral monitor - STUCK HERE : (

TEMT6000 is no problem, but either the TCMux shield works or the BME280/DHT22.
This is my code at the moment:
TCMux_BME280_TEMT6000_Test.ino (10,6 KB)
TCMux_Tempt6000_DHT22_Test.ino (10,3 KB)

My wiring:
BME280 SCL – Arduino D21
BME280 SDA – Arduino D20
BME280 VIN – Arduino 3.3V
BME280 GND – Arduino GND
DHT22 Sig – Arduino D2
DHT22 VCC – Arduino 5V
DHT22 GND – Arduino GND

Is there a solution or do I need a second Arduino board?
Thanks so much!

I miss the complete wiring diagram (the BME280 is a 3.3V sensor so you must not connect it directly). As you have other components (MAX31855) that also must have lower signals I assume you use some other circuitry (not the sensor chips directly) but you haven't provided any link to it's components.

Post the code that is using the TCM and the DHT and the BME instead of posting separate codes that are irreverent to your posted issue.

I just tried the DHT instead of der BME thinking it would work better. Neither does. Each of them would be fine for me, so I postet both codes (TCM & BME / TCM & DHT) hoping one of them is easier to fix. Sorry if I do not understand your answer correctly.

This are the wiring diagrams:


Perhaps to make it clearer this is how I thought to progress
Once I get the data from the combined sensors on my serial monitor I will try to log them to the SD.
Then I will need to think about battery saving and will try how get a measurment per min but log the data only every 15 min.
Then I will need to make all the electronics save for hight alpine environment and test it in rough weather to see that everything is air/water tight.

Just wondering what does the DHT give that a BME280 does not?

Nothing. First bought the DHT and than was recommended the BME. Just need air temp and humidity, pressure would be a plus. Also wanted to see if they measure more or less the same T and RH and which is better suited for outdoor conditions.

I've had a BME 680 outside now for 2.5 years.

Thens a lot for the info!
How do you keep it dry in rainy/windy/snowy weather?

Idahowalker via Arduino Forum arduino@discoursemail.com 11.02.2022 16:44 >>>

| Idahowalker Tesla
February 11 |

  • | - |

I've had a BME 680 outside now for 2.5 years.

By keeping the sensor in the top of a Stevenson's Shield,

If you are wanting to use solar and batteries, you might want to consider that using 2 sensors instead of one will increase the current draw of the project.

Good idea! I have some radiation shields lying around I could probably use but I need to talk to others about it. I hoped to make my plots as unobtrusive as possible from tourists and hikers as there are longtime experimental plots nearby.
I thought about making a battery pack with 2 serial Li-SOCCl2 batteries (3.6 V/19Ah) to go in with 7.2V. Bying solar panels are not possible (low budget).
Once everything is running I will measure how much mA is used (also while saving) to see how much power I need.

Idahowalker via Arduino Forum arduino@discoursemail.com 11.02.2022 17:20 >>>

| Idahowalker Tesla
February 11 |

  • | - |

By keeping the sensor in the top of a Stevenson's Shield,

If you are wanting to use solar and batteries, you might want to consider that using 2 sensors instead of one will increase the current draw of the project.

I'm very surprised to hear that the logger shield's RTC chip work on the Mega. The chip connects the I2C signals to A4/A5 so the library should not work on the Mega but you have code for it.

Please post the exact serial output you get for both sketches! I would expect the sketch to stop at the endless loop after the RTC initialization.

Post the schematics of the BME280 breakout board you use! Is it exactly the pictured one?

This is the new datalogger shield:

Should work on Mega.

This is the serial monitor output from the TCMux

#2: 21.00 degC Int: 21.6250 degC
2022/2/14 11:2:40
Light value = 277

#3: 21.25 degC Int: 21.6250 degC
2022/2/14 11:2:41
Light value = 277

#4: 21.00 degC Int: 21.6250 degC
2022/2/14 11:2:42
Light value = 277

#5: 21.00 degC Int: 21.6250 degC
2022/2/14 11:2:43
Light value = 276

#6: 21.00 degC Int: 21.6250 degC
2022/2/14 11:2:44
Light value = 274

#7: 21.00 degC Int: 21.6250 degC
2022/2/14 11:2:46
Light value = 272

#8: 21.00 degC Int: 21.6875 degC
2022/2/14 11:2:47
Light value = 270

#1: 21.25 degC Int: 21.6875 degC
2022/2/14 11:2:48
Light value = 267

This is the serial output from BME280

Communication established!

2022/2/1411:12:59
Temperature = 21.43 °C
Pressure = 988.83 hPa
Humidity = 26.56 %

Light value = 286

2022/2/1411:13:1
Temperature = 21.46 °C
Pressure = 988.84 hPa
Humidity = 26.54 %

Light value = 278

#2: 21.00 degC Int: 21.9375 degC
2022/2/1411:13:2
Temperature = 21.47 °C
Pressure = 988.83 hPa
Humidity = 26.50 %

Light value = 284

#3: 21.00 degC Int: 21.8750 degC
2022/2/1411:13:3
Temperature = 21.48 °C
Pressure = 988.86 hPa
Humidity = 26.46 %

Light value = 274

#4: 21.25 degC Int: 21.8750 degC
2022/2/1411:13:4
Temperature = 21.50 °C
Pressure = 988.87 hPa
Humidity = 26.43 %

Yes, the BME is exactly the pictured one (Gy-bme280 at az-delivery.de – AZ-Delivery)

Success!
I rewrote the code and now TCMux, DHT22 and TEMT6000 are giving me nice data on the serial monitor.

//Lib für RTC
#include "RTClib.h"
//lib for TCMux
#include <string.h> //Use the string Library
#include <ctype.h>
#include <EEPROM.h>
//lib for DHT22
#include <DHT.h>;

//Define var für rtc funktion
RTC_PCF8523 rtc;

// Define var for temt6000
int temt6000Pin = A0;
float light;
int light_value;

// vars and pins for TCMux
/* PINS */
//#define SHOWMEYOURBITS // Display the raw 32bit binary data from the MAX31855
#define PINEN 7 //Mux Enable pin
#define PINA0 4 //Mux Address 0 pin
#define PINA1 5 //Mux Address 1 pin
#define PINA2 6 //Mux Address 2 pin
#define PINSO 12 //TCAmp Slave Out pin (MISO)
#define PINSC 13 //TCAmp Serial Clock (SCK)
#define PINCS 9  //TCAmp Chip Select Change this to match the position of the Chip Select Link

/* COM */
static const uint16_t BAUDRATE = 9600;

int Temp[8], SensorFail[8];
float floatTemp, floatInternalTemp;
char failMode[8];
int internalTemp, intTempFrac;
unsigned int Mask;
//char data[16];
byte j, NumSensors =8, UpdateDelay = 1;
char i;
char Rxchar, Rxenable, Rxptr, Cmdcomplete, R;
char Rxbuf[32];
char adrbuf[3], cmdbuf[3], valbuf[12];

int val = 0, Param;
unsigned long time;

//Define var & const for DHT22
//Constants
#define DHTPIN 2     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
//Variables
int chk;
float hum;  //Stores humidity value
float temp; //Stores temperature value

void setup() {
  // setup for RTC
  Serial.begin(BAUDRATE);

  // überprüft ob er den rtc findet oder nicht
  // wenn nicht Couldnt find rtc und wartet 10ms
  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!");
  }
  
  // When time needs to be re-set on a previously configured device, the
  // following line sets the RTC to the date & time this sketch was compiled
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  rtc.start();
 
  //setup for temt6000
  pinMode(temt6000Pin, INPUT); //data pin for ambientlight sensor
 
  //setup for TCMux  
  pinMode(PINEN, OUTPUT);     
  pinMode(PINA0, OUTPUT);    
  pinMode(PINA1, OUTPUT);    
  pinMode(PINA2, OUTPUT);    
  pinMode(PINSO, INPUT);    
  pinMode(PINCS, OUTPUT);    
  pinMode(PINSC, OUTPUT);    
  
  digitalWrite(PINEN, HIGH);   // enable on
  digitalWrite(PINA0, LOW); // low, low, low = channel 1
  digitalWrite(PINA1, LOW); 
  digitalWrite(PINA2, LOW); 
  digitalWrite(PINSC, LOW); //put clock in low

   //setup for DHT22
   dht.begin();  
}

void selectChannel(char thermo) {
    switch (thermo) //select channel
    {
        case 0:
          digitalWrite(PINA0, LOW); 
          digitalWrite(PINA1, LOW); 
          digitalWrite(PINA2, LOW);
        break;
        case 1:
          digitalWrite(PINA0, HIGH); 
          digitalWrite(PINA1, LOW); 
          digitalWrite(PINA2, LOW);
        break;
        case 2:
          digitalWrite(PINA0, LOW); 
          digitalWrite(PINA1, HIGH); 
          digitalWrite(PINA2, LOW);
        break;
        case 3:
          digitalWrite(PINA0, HIGH); 
          digitalWrite(PINA1, HIGH); 
          digitalWrite(PINA2, LOW);
        break;
        case 4:
          digitalWrite(PINA0, LOW); 
          digitalWrite(PINA1, LOW); 
          digitalWrite(PINA2, HIGH);
        break;
        case 5:
          digitalWrite(PINA0, HIGH); 
          digitalWrite(PINA1, LOW); 
          digitalWrite(PINA2, HIGH);
        break;
        case 6:
          digitalWrite(PINA0, LOW); 
          digitalWrite(PINA1, HIGH); 
          digitalWrite(PINA2, HIGH);
        break;
        case 7:
          digitalWrite(PINA0, HIGH); 
          digitalWrite(PINA1, HIGH); 
          digitalWrite(PINA2, HIGH);
        break;
  }
  delay(5);
  digitalWrite(PINCS, LOW); //stop conversion
  delay(5);
  digitalWrite(PINCS, HIGH); //begin conversion
  delay(100);  //wait 100 ms for conversion to complete
  digitalWrite(PINCS, LOW); //stop conversion, start serial interface
  delay(1);
}

void loop() {
    // loop for rtc
    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.println();
    delay(1000);

    //loop for TCMux
    if (millis() > (time + ((unsigned int)UpdateDelay*1000)))
  {
    time = millis();
    
    //loop for temt6000
    int light_value = analogRead(temt6000Pin);
    light = light_value * 0.0976;// percentage calculation 
    Serial.print("Light value = ");
    Serial.println(light_value);
    Serial.println();

    // loop for DHT22 - either that or TCMux - Why
    //Read data and store it to variables hum and temp
    hum = dht.readHumidity();
    temp= dht.readTemperature();
    //Print temp and humidity values to serial monitor
    Serial.print("Humidity: ");
    Serial.print(hum);
    Serial.print(" %, Temp: ");
    Serial.print(temp);
    Serial.println(" Celsius");
    
    if (j<(NumSensors-1)) j++;
    else j=0;

    selectChannel(j);

    //this logic could be coded much more clear:
    Temp[j] = 0;
    failMode[j] = 0;
    SensorFail[j] = 0;
    internalTemp = 0;
    for (i=31;i>=0;i--)
    {
      digitalWrite(PINSC, HIGH);
      delay(1);   
      if ((i<=31) && (i>=18)) //14bit T data, 31 sign, 30 MSB 2^10, 18LSB 2^-2 (0.25 degC)
        {
          Mask = 1<<(i-18);
          if (digitalRead(PINSO)==1)
          {
            if (i == 31)
              Temp[j] += (0b11<<14);//pad the temp with the bit 31 value so we can read negative values correctly
            Temp[j] += Mask;
          }
        }
      if (i==16)
        SensorFail[j] = digitalRead(PINSO);
        
      if ((i<=15) && (i>=4)) //12 bit internal Temp, 15 sign, 14 MSB 2^6, 4 LSB 2^-4 (0.0625degC)
      {
        Mask = 1<<(i-4);
        if (digitalRead(PINSO)==1)
        {
          if (i == 15)
            internalTemp += (0b1111<<12);//pad the temp with the bit 31 value so we can read negative values correctly
            internalTemp += Mask;//should probably pad the temp with the bit 15 value so we can read negative values correctly
        }
      }
      if (i==2)
        failMode[j] += digitalRead(PINSO)<<2;//bit 2 is set if shorted to VCC
      if (i==1)
        failMode[j] += digitalRead(PINSO)<<1;//bit 1 is set if shorted to GND
      if (i==0)
        failMode[j] += digitalRead(PINSO)<<0;//bit 0 is set if open circuit
        
      digitalWrite(PINSC, LOW);
      delay(1);
      //delay(1);
    }
    //Serial.println();
    
    //Serial.println(Temp,BIN);
    Serial.print("#");
    Serial.print(j+1,DEC);
    Serial.print(": ");
    if (SensorFail[j] == 1)
    {
      Serial.print("FAIL");
      if ((failMode[j] & 0b0100) == 0b0100)
        Serial.print(" SHORT TO VCC");
      if ((failMode[j] & 0b0010) == 0b0010)
        Serial.print(" SHORT TO GND");
      if ((failMode[j] & 0b0001) == 0b0001)
        Serial.print(" OPEN CIRCUIT");
    } else
    {
      floatTemp = (float)Temp[j] * 0.25;
      Serial.print(floatTemp,2);
      Serial.print(" degC");
    }
    Serial.print(" Int: ");
    floatInternalTemp = (float)internalTemp * 0.0625;
    Serial.print(floatInternalTemp, 4);
    Serial.print(" degC");
    Serial.println("");

    
   }//end time
  if (Serial.available() > 0)    // Is a character waiting in the buffer?
  {
    Rxchar = Serial.read();      // Get the waiting character

    if (Rxchar == '@')      // Can start recording after @ symbol
    {
      if (Cmdcomplete != 1)
      {
        Rxenable = 1;
        Rxptr = 1;
      }//end cmdcomplete
    }//end rxchar
    if (Rxenable == 1)           // its enabled so record the characters
    {
      if ((Rxchar != 32) && (Rxchar != '@')) //dont save the spaces or @ symbol
      {
        Rxbuf[Rxptr] = Rxchar;
        //Serial.println(Rxchar);
        Rxptr++;
        if (Rxptr > 13) 
        {
          Rxenable = 0;
        }//end rxptr
      }//end rxchar
      if (Rxchar == 13) 
      {
        Rxenable = 0;
        Cmdcomplete = 1;
      }//end rxchar
    }//end rxenable

  }// end serial available


   
  if (Cmdcomplete == 1)
  {
    Cmdcomplete = 0;
     cmdbuf[0] = toupper(Rxbuf[1]); //copy and convert to upper case
     cmdbuf[1] = toupper(Rxbuf[2]); //copy and convert to upper case
     cmdbuf[2] = 0; //null terminate        Command = Chr(rxbuf(3)) + Chr(rxbuf(4))
     //   Command = Ucase(command)
  //Serial.println(cmdbuf);
     valbuf[0] = Rxbuf[3]; //        Mystr = Chr(rxbuf(5))
        R = Rxptr - 1;
            for (i = 4 ; i <= R ; i++)//For I = 6 To R
            {
                valbuf[i-3] = Rxbuf[i]; //Mystr = Mystr + Chr(rxbuf(i))
            }
     valbuf[R+1] = 0; //null terminate
     Param = atoi(valbuf);//   Param = Val(mystr)

     //Serial.println(Param); //   'Print "Parameter: " ; Param

       
              if (strcmp(cmdbuf,"NS")==0)       //NumSensors
              {
                   //'Print "command was ON"
                   if ((Param <= 8) && (Param > 0)) 
                   {
                      NumSensors = Param;                   
                   }
                   
              }
              if (strcmp(cmdbuf,"UD")==0)       //UpdateDelay
              {
                   //'Print "command was ON"
                   if ((Param <= 60) && (Param >= 0)) 
                   {
                      UpdateDelay = Param;                   
                   }
                   
              }
              if (strcmp(cmdbuf,"SV")==0)       //Save
              {
                   EEPROM.write(0,NumSensors);
                   EEPROM.write(1,UpdateDelay);
                   EEPROM.write(511,1);
              }
         }  
}

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