Using 2 sensors I2C at same time

Hello everyone.
I need to implement 2 sensors using I2C in my Uno. The sensors are:

MLX90614 (contactless temperature sensor)
VL53L0X (TOF sensor)

Both are connected in I2C bus in my Arduino Uno.

The code for MLX90614 is:

#include <Wire.h>
#include <Adafruit_MLX90614.h>

Adafruit_MLX90614 mlx = Adafruit_MLX90614();

void setup() 
{
  Serial.begin(115200);
  mlx.begin();  
}

void loop() 
{
  Serial.print(mlx.readObjectTempC());
  Serial.println();
  delay(800);
}

and the code for sensor VL53L0X is:

#include "Adafruit_VL53L0X.h"

Adafruit_VL53L0X lox = Adafruit_VL53L0X();

void setup() 
{
  Serial.begin(115200);
  while (! Serial) 
  {
    delay(1);
  }
  
  if (!lox.begin()) 
  {
    Serial.println(F("Failed to boot VL53L0X"));
    while(1);
  }
}


void loop() 
{

  VL53L0X_RangingMeasurementData_t measure;
  lox.rangingTest(&measure, false);

  if (measure.RangeStatus != 4)
  { 
    Serial.println(measure.RangeMilliMeter);
  } 
  else 
  {
    Serial.println(" out of range ");
  }
    
  delay(800);
}

Both codes work separately, but when I try to run together becames this one and did not work:

#include "Adafruit_VL53L0X.h"
#include <Adafruit_MLX90614.h>
#include <Wire.h>

Adafruit_VL53L0X lox = Adafruit_VL53L0X();
Adafruit_MLX90614 mlx = Adafruit_MLX90614();

void setup() 
{
  Serial.begin(115200);
  mlx.begin();  
  
  while (! Serial) 
  {
    delay(1);
  }
  
  if (!lox.begin()) 
  {
    Serial.println(F("Failed to boot VL53L0X"));
    while(1);
  }
}


void loop() 
{
  if (Serial.available())
  {
    String ch;
    ch = Serial.readString();
    ch.trim();



    if(ch=="read_temperature")
    {
      Serial.print(mlx.readObjectTempC());
      Serial.print("\n");
    }




    if(ch=="read_distance")
    {
      VL53L0X_RangingMeasurementData_t measure;
      lox.rangingTest(&measure, false);
    
      if (measure.RangeStatus != 4)
      { 
        Serial.println(measure.RangeMilliMeter);
      } 
    }
  }
}

Any ideas? Thanks.

Hi Jonas,

as a genreal hint: add serial-output for debugging.

In your case this means

    String ch;
    ch = Serial.readString();
    ch.trim();
    serial.print("ch=#"),
    serial.print(ch),
    serial.println("#"),

to see what the input really is

serial.readstring()

has a timeout https://www.arduino.cc/reference/en/language/functions/communication/serial/

If you are not finished with typing the complete string before timeout the content of ch is just unequal to teh constant you are comparing with.

You seem to have combined two sketches and added the input-function. This means you did two things before testing. In programming you should do one thing after the other. Add one thing test it it. If it works add the next one test it. Especcially if you are not experienced.

This is still a pretty small program. But you already are stumped because of adding two things before testing. And voila: it doesn't work. There is a famous wording:

"The bug has its hands on the keyboard and is staring at the screen" It is really true.

So try a codeversion that just reads both sensors without any userinput.

best regards Stefan