Click here to Skip to main content
15,868,049 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:


C++
<pre>#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:


C++
<pre>#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 join them, they did not work:


What I have tried:

C++
<pre>#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.
Posted
Comments
Garth J Lancaster 11-Jul-20 1:31am    
"they did not work:" - that is ambiguous isnt it ? what do you mean by that - did you get no result whatsoever on the serial monitor, or a reading from one of the sensors, or ? - you should use Improve question to update the information and state exactly what that means.

Ok, so, I guess the sensors are wired correctly (in parallel) in terms of the SDA, SCL lines with a pull-up resistor between them and A4 (SDA) and A5 (SCL) on the UNO. They both have different I2C addresses (MLX90614 = 0x5A, VL53LOX = 0x29) by default, so unless you've changed the addresses, it's not that

I see in your combined setup code
void setup()
{
  Serial.begin(115200);
  mlx.begin();  
  
  while (! Serial)
  {
    delay(1);
  }
  
  if (!lox.begin())
  {
    Serial.println(F("Failed to boot VL53L0X"));
    while(1);
  }
}
could you please try moving the mlx.begin(); 'down', so
void setup()
{
  Serial.begin(115200);  
  
  while (! Serial)
  {
    delay(1);
  }
  mlx.begin();

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


I'd also ask if you really need the
if (Serial.available())
{
}
around the combined sensor reading code since you wait for serial to be available in the setup code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900