Print int value problems u8g2 lib

Hee Guys,

i think i need help, small introductions name is Nigel 30 years old and I am from Holland. I am mechanical engineer so please be kind haha I am not a software pro or anything but I do like Arduino’s.

Goal
current project is for racing motorbikes, the goal (for now) is to display two int value’s (infrared temperature) for my front and rear tyre on a 1.3 inch Oled screen.

Current hardware

  • Two DFRobot MLX90614 infrared sensors
  • Arduino uno (for now later nano for on the race bike)
  • 1,3 inch Oled Cheap copy of heltec and with an SH1106 something chip I think

Wiring diagram

  • Just the o led and two sensor 5V supply en GND
  • Everything runs true the SCL and SDA port

Problems

  • Temp sensors are not giving a separate value but copy the value
  • If I do not adjust the clock speed sensor value s will be all over the place

Code see also below

  • I use the u8g2 lib because SH1106 chip and it is very well documented
  • Everything works well when I used just the sensors and the LCD with I2C
  •   i tried translating it behind the // in english i hope that makes it more clear 
    

thanks in advance

#include <Arduino.h>
#include <SPI.h>
#include <Wire.h>
#include <U8g2lib.h>
#include <DFRobot_MLX90614.h>    


// constructor code for SH1106 chip 128x64 size OLED com protocol 1 and comunnicate with I2C 
// Also no rotations needed use the U8X8 no pins diffined for comunication 

U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); 

#define MLX90614_I2C_ADDR 0x5A                                // mlx9614 Adres Front tyre
#define MLX90614_I2C_ADDR 0x5B                                // mlx9614 Adres Rear tyre 
 DFRobot_MLX90614_I2C sensorvoor(MLX90614_I2C_ADDR, &Wire);   // instantiate an object to drive the sensor (no clue bud it works)
 DFRobot_MLX90614_I2C sensorachter(MLX90614_I2C_ADDR, &Wire); // instantiate an object to drive the sensor (no clue bud it works)
 
  int objectTemp;                                          // Front tyre sensor value 
  int objectTemp2;                                         // Rear tyre sensor value 

void setup() {
  Serial.begin(9600);                                       // Start seriele monitor 
  u8g2.setBusClock(100000);                                 // Set bus speed 
  u8g2.begin();                                             // Start display
  
  sensorvoor.begin();                                       // Start front sensor
  sensorachter.begin();                                     // Start rear sensor
  
  Serial.println("setup klaar");                            // serial print Setup finisht 
}

void loop() {
  delay(2000);                                                
  
  int objectTemp  = sensorvoor.getObjectTempCelsius();       // Read sensor value for Front tyre and save it in Celcius 
  int objectTemp2  = sensorachter.getObjectTempCelsius();    // Read sensor value for Rear tyre and save it in Celcius 
  
  Serial.print("Voorband : ");  Serial.print(objectTemp);  Serial.println(" °C");
  Serial.print("Achterband :"); Serial.print(objectTemp2); Serial.println(" °C"); 
  

 u8g2.firstPage();
   u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_ncenB14_tr);            // set font 
  u8g2.setCursor(0, 15);      
  u8g2.print ("Voor");                           // its the dutch word for Front 
  u8g2.setCursor(100, 15);
  u8g2.print(objectTemp);
    u8g2.setCursor (0, 50);
  u8g2.print ("Achter");                         // its the dutch word for Rear 
  u8g2.setCursor (100, 50);
  u8g2.print(objectTemp2);


  u8g2.sendBuffer();
}

In the first line you define MLX90614_I2C_ADDR to be 0x5A.

In the second line you redefine MLX90614_I2C_ADDR to be 0x5B.

In the third and forth lines, you define two instances of the sensor class, both of which use the address 0x5B, ie, the same physical sensor.

You need to do something like the following:

#define FRONT_SENSOR_ADDR 0x5A                                // mlx9614 Adres Front tyre
#define REAR_SENSOR_ADDR 0x5B                                // mlx9614 Adres Rear tyre 
 DFRobot_MLX90614_I2C sensorvoor(FRONT_SENSOR_ADDR, &Wire);   // instantiate an object to drive the sensor (no clue bud it works)
 DFRobot_MLX90614_I2C sensorachter(REAR_SENSOR_ADDR, &Wire); // instantiate an object to drive the sensor (no clue bud it works)

which declares sensorvoor with the address 0x5A, and sensorachter with the address 0x5B.

Because it appears that by default the U8G2 library sets the I2C clock to 400KHz and leaves it there, and the MLX90614 has a maximum I2C clock of 100KHz. So yes, you've got to set the I2C clock to 100KHz yourself for the sensors to work properly.

@van_der_decken Thanks man for the response! Tomorrow after work i will try the sollution right away😃

Alright just out of curiosity: where did you found the clock speed numbers so quickly? I was trying to find them bud did not succeed.

For the MLX90614 sensor, I just looked it up in its datasheet. For the library, I read through the library source and noticed that the 400KHz value was by far the most common default frequency across the supported displays.

Thanks man for your help really appreciate it ! It works out great this way!

Another thing I discovered by accident yesterday while typing the help question was that the cursor set point is very important for the oled, it will display weird stuff if things are written on top of each other.

You're quite welcome. Don't forget to mark the topic as solved by clicking the :ballot_box_with_check: Solution button at the bottom of the reply that answered your question, as shown in How to get the best out of this forum.

Yes i did and i will read the topic on how to get the best out of this forum

Umm, you're supposed to mark the reply that answered your question.

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