Using digital pin to power on and off the MLX90614 sensor

Hello everyone, for my current project i’m using an Arduino uno. I’m using the gy-906 mlx90614 temperature sensor to transmit the object temperature and the current emissivity value used by the sensor to the arduino, i also use vl53l0x sensor to measure distance. All of these values are displayed on the ssd1306, and OLED display, all of these devices use i2c. The project is working fine, however, I’m also trying to use two buttons to increase or decrease the emissivity stored in the mlx90614 and it’s causing some problems. i should also add that i’m using the adafruit library for the mlx90614.

So, using the code below, when i start altering the emissivity i works for a few moments but then the temperature starts getting weird until i turn the arduino off and back on again. After reading the datasheet, i discovered that when a change to the emissivity register is made, you need to turn the sensor off and back on again. However, the sensor is in the 3.3V pin so it’s always on, i think this is why i need to turn off the Arduino to make it work again.

#include <VL53L0X.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
#include <Adafruit_MLX90614.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET    -1 // Reset pin # (or -1 if sharing Arduino reset pin)

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //Declaring the display name (display)
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
VL53L0X sensor;
int aumentar = 4;
int diminuir = 6;
double emissividade = 1.0;

void setup() {  
  mlx.begin(); 
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start the OLED display
  display.clearDisplay();
  display.display();
  if (!sensor.init())
  {
    while (1) {}
  }
  sensor.startContinuous();         //Inicializar medição de distancia
  pinMode(aumentar, INPUT);
  pinMode(diminuir, INPUT);
  Serial.begin(9600);
}

void loop() {
  display.clearDisplay();
  Serial.println(mlx.readEmissivity());
  
  if(digitalRead(aumentar) == HIGH && mlx.readEmissivity()<1.0 ){       //Programar os botões
        emissividade = emissividade + 0.05;
        mlx.writeEmissivity(emissividade);
    }
  if(digitalRead(diminuir) == HIGH && mlx.readEmissivity() > 0.1){  
      emissividade = emissividade - 0.05;
      mlx.writeEmissivity(emissividade);
    }
  
  display.setTextSize(1);                    
  display.setTextColor(WHITE);             
  display.setCursor(0,8);                
  display.println("Emissivity:"); 
  
  display.setTextSize(2);
  display.setCursor(65,4);
  display.println(mlx.readEmissivity(),2);  //display emissivity
  
  display.setTextSize(1);                    
  display.setTextColor(WHITE);             
  display.setCursor(0,28);                
  display.println("Objeto: "); 
  
  display.setTextSize(2);
  display.setCursor(52,25);
  display.println(mlx.readObjectTempC(),1); //display current object temperature
  
  display.setCursor(100,25);
  display.println("C");
  // Display distance

  display.setTextSize(1);
  display.setCursor(0,50);
  display.println("Distancia: ");
  display.setTextSize(2);
  display.setCursor(65,50);
  if(sensor.readRangeContinuousMillimeters()>=8190){
    display.setTextSize(1);
    display.print("ALCANCE");
  }
  else if(!sensor.timeoutOccurred()){
  display.print(sensor.readRangeContinuousMillimeters());
  display.print("mm");
  }
  else if(sensor.timeoutOccurred()){
    display.print("ERRO");
  }

  display.display();
  
  delay(500);

}

My idea is, connect the VIN pin of the mlx90614 to a digital pin instead, so when i make a change to the emissivity the digital this pin sets to LOW, turning off the sensor, and after a delay it goes back to HIGH, turning it back on. But, i read that this is not as simple with i2c devices, i’m kind of a beginner with electronics and i’m worried if this might damage the sensor. So my question is, will this work? and will it damage the sensor?

You are correct, it is a bad idea to power down a device when the I/O lines are connected to powered equipment.

Why not just turn off the Arduino+MLX after making the change, then turn it all on again?

FYI the emissivity corrections can be applied after you make the measurement – there is no need to update the sensor. To do that you need to acquire both the ambient (chip) temperature Ta, and the object temperature Tom in Kelvin as measured by the sensor.

The corrected object temperature Toc is given by the following expression in C/C++, where eps is the surface emissivity:

Toc = pow( ( ( (pow(Tom,4)-pow(Ta,4))/eps) + pow(Ta,4) ), 0.25);

For very shiny objects (emissivity < 0.1), correction is usually not possible and contactless temperature measurements become impractical or impossible.

About the first suggestion, i guess this would kind of work, i would just have to change this command

double emissividade = 1.0;

to:

double emissividade = mlx.readEmissivity();

So that when i change the emissivity and turn the arduino off and back on again it would read the changed emissivity and not go back to '.0. But this would require me to keep manually restarting the Arduino every time i want to make a change right? this project will be battery powered so i would have to keep flipping the on/off switch, no very practical.
I think that your suggestion to correct the temperature after the measurement is the ideal! i will try to change my code to do this. Just a question, can i make the Arduino “remember” the last emissivity value used before it was turned off? so that when I power it back up it keeps the same value? thanks!

You can store values in the Arduino EEPROM at any time, and they will be there the next time you turn it on. See the reference page for the Arduino EEPROM library.

It is much easier to do the correction in the Arduino code.

I will take a look. about this equation:

Toc = pow( ( ( (pow(Tom,4)-pow(Ta,4))/eps) + pow(Ta,4) ), 0.25);

The corrected temperature is in kelvin right? i don't know how to receive the Ta and Tom directly in kelvin so i could just receive in Celsius and convert the values to kelvin, and then after the calculation convert the Toc to Celsius

both the ambient (chip) temperature Ta, and the object temperature Tom in Kelvin

Add 273.15 to degrees C. Make sure you have changed the sensor back to the default emissivity of 1.0 before applying the formula.

You should find this application note from Melexis of interest.

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