Code for Gy-906 and IR proximity sensor with LCD+i2C

Hello,
I would like to make an infrared thermometer like in this tutorial: DIY Non Contact Thermometer - Open Green Energy

however I do not have an OLED screen at my disposal I would like if possible to use an LCD screen (16x2) with i2C.

But I can't find the code; please help me.
Thanks

#include <Adafruit_SSD1306.h>   //library oled
#include <Adafruit_MLX90614.h>  //library sensor temperature
#include <Wire.h>               //library I2C
#include <millisDelay.h>        //library looping
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
const int GREEN_LED = 3;    // pin D3 for green LED 
const int RED_LED = 5;      // pin D5 for red LED
const int buzzer = 7;       // pin D7 for buzzer      
const int statePin = 9;     // pin D9 for IR proximity sensor
bool measurement = false;    //check temperature measurement running
const unsigned long interval_sensor = 50; // interval refresh sensor in mS 
millisDelay sensorDelay;                  // the delay object
const unsigned long interval_display = 500; // interval refresh OLED display in mS 
millisDelay displayDelay;                   // the delay object
const unsigned long delay_hold_red = 5000; // delay hold red LED in mS
const unsigned long delay_hold_green = 1000; // delay hold green LED in mS
millisDelay holdDelay;                       // the delay object
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Declaration for an MLX90614 sensor connected to I2C (SDA, SCL pins)
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
void setup() {
  Serial.begin(9600);
  
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("OLED Display allocation failed"));
    for(;;);
  }
  pinMode(statePin, INPUT);
  pinMode(GREEN_LED, OUTPUT);
  pinMode(RED_LED, OUTPUT);
  pinMode(buzzer, OUTPUT);
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(20, 20);
  display.println("Initializing");
  display.display();
  delay(250);
  display.clearDisplay();
  mlx.begin();                          //sensor initialization
  
  displayDelay.start(interval_display); //refresh display
}
void loop() 
{
  static float temperature = -1;  //initial condition
  
  int state = digitalRead(statePin);  //IR sensor condition
  
  // check if the IR sensor detect people in front of sensor. If it is, the state is LOW:
  if (state == LOW && measurement == false)
  {
    sensorDelay.start(interval_sensor); //read sensor
    displayDelay.finish();              //finish interval refresh for display
    measurement = true;                  //change state of measurement
  }
  
  if (measurement == true)
  {
    //if sensor reading
    temperature = GetTemp(); //get temperature in celcius
    
  }else
  {
    temperature = -1;  //marker if sensor not reading temperature
  }
  
  ShowTemp(temperature );   //display temperature to OLED
   holdReading();  //call  holdReading function
}
float GetTemp()
{
  static int index = 0;
  static float temptot = 0;
  float hasil = 0;
  if (sensorDelay.justFinished()) 
  {
    // read sensor and repeat
    sensorDelay.repeat(); // repeat
   // temptot += mlx.readObjectTempC(); //add the reading to the total
      temptot +=  mlx.readObjectTempF();
      index++; //increment index
    if(index==19)
    {
      //if already 20 measurement
      hasil = temptot/20;     //calculate average
      temptot = 0;            //zero total
      index = 0;          //zero index
      
      sensorDelay.stop();     //stop reading
      displayDelay.finish();  //complete the refresh interval so that it shows immediately
      
      return hasil;           //give results
    }
  }
  return hasil; //As long as there are no results, give a value of result = 0
}
void ShowTemp(float temperature)
{
  if (displayDelay.justFinished()) 
  {
    displayDelay.repeat(); // repeat
    //show temperature
    if(temperature == -1)
    {
      //if there are no object in front of sensor
      display.clearDisplay();
      display.setTextSize(2);
      display.setCursor(35, 5);
      display.print("-----");
      display.setCursor(105, 20);
      display.print("");
      display.setTextSize(2);
      display.setCursor(35, 40);
      display.print("-----");
      display.setCursor(105, 46);
      display.print("");
      display.display();
    }else if(temperature == 0)
    {
      //if still reading temperature
      display.clearDisplay();
      display.setTextSize(1);
      display.setTextColor(WHITE);
      display.setCursor(20, 25);
      display.println("WAIT ....");
      display.display();
    }else
    {
      //if there is a new result
      temperature += 8.5; // sensor temperature calibration
      
      display.clearDisplay();     
      
      if( temperature > 100)
      {
        display.setTextSize(3);
        display.setCursor(5, 20);
        display.print(temperature,1);      
        display.print("F"); 
        // display.print("C");       
      }
      else
      {
      display.setTextSize(3);
      display.setCursor(10, 20);
      display.print(temperature,1);      
      display.print(" F");
     // display.print("C ");
      }
      display.display();
      if (temperature > 100.4) 
      {
        //if the temperature is too high
        digitalWrite(RED_LED, HIGH);
        holdDelay.start(delay_hold_red);  //run delay for red LED or temp > 38
      }else
      {
        //if the temperature is normal
        digitalWrite(GREEN_LED, HIGH);
        holdDelay.start(delay_hold_green);  //run delay for green LED or temp < 38
      }
      digitalWrite(buzzer, HIGH);
      
      displayDelay.stop();          //stop refresh display
    }
  }
}
void  holdReading()
{
  if (holdDelay.justFinished()) {
    // if holddelay finish
    digitalWrite(RED_LED, LOW);
    digitalWrite(GREEN_LED, LOW);
    digitalWrite(buzzer, LOW);
    measurement = false; //allow new measurements
    
    displayDelay.start(interval_display); //restart the OLED display 
  }
}

Thinks But in this video he did'nt use a IR proximity sensonr

Pull the part that uses the proximity sensor to trigger a reading from the original code and put it into the code with the right LCD.

Here is an example of how I would do it. I use the hd44780 library for the LCD. It is the best library for I2C LCDs (my opinion). Install the hd44780 library using the IDE library manager. I also use the state change detection method from my state change detection for active low inputs tutorial. The program will take a temperature measurement and display the readings each time that the proximity sensor transitions state from HIGH to LOW. The reading is held until the next reading is triggered.


//Prateek
//www.prateeks.in
// modified by c goulding aka groundfungus
// to activate measurement with digital state change
// (button switch, proximity sensor, ... )

#include <Wire.h>
#include <Adafruit_MLX90614.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header

byte deg[] = // degree symbol
{
   B01111,
   B01001,
   B01001,
   B01111,
   B00000,
   B00000,
   B00000,
   B00000
};

const byte sensorPin = 9; // proximity sensor

Adafruit_MLX90614 mlx = Adafruit_MLX90614();
hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip

double temp_amb;
double temp_obj;

void setup()
{
   Serial.begin(9600);
   Serial.println("Temperature Sensor MLX90614");
   
   pinMode(sensorPin, INPUT_PULLUP); // or just INPUT, to suit

   //Initialize LCD I2C
   lcd.begin(16, 2);
   lcd.backlight();
   lcd.createChar(1, deg);
   lcd.print("put near target");
   lcd.setCursor(0,1);
   lcd.print("to measure temp");
   //Initialize MLX90614
   mlx.begin();
}

void loop()
{
   static unsigned long lastSensorState = HIGH;
   static unsigned long timer = 0;
   unsigned long interval = 100;
   if (millis() - timer >= interval)
   {
      timer = millis();
      bool sensorState = digitalRead(sensorPin);
      if (sensorState != lastSensorState)
      {
         if (sensorState == LOW)
         {
            lcd.clear();
            temp_amb = mlx.readAmbientTempC();
            temp_obj = mlx.readObjectTempC();
            if (temp_obj > 37)
            {
               Serial.print("Warning...HIGH TEMP...");
               lcd.clear();
               lcd.print("HIGH TEMP...");
            }

            //lcd display
            else
            {
               lcd.setCursor(0, 0);
               lcd.print("Room Temp:");
               lcd.setCursor(10, 0);
               lcd.print(temp_amb);
               lcd.setCursor(15, 0);
               lcd.write(1);
               lcd.setCursor(0, 1);
               lcd.print("Body Temp:");
               lcd.setCursor(10, 1);
               lcd.print(temp_obj);
               lcd.setCursor(15, 1);
               lcd.write(1);

               //Serial Monitor
               Serial.print("Room Temp = ");
               Serial.println(temp_amb);
               Serial.print("Object temp = ");
               Serial.println(temp_obj);
            }
         }
         lastSensorState = sensorState;
      }
   }
}


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