0

I am using 2 grove components in order to display a temperature. I use an I2C High Accuracy Temperature Sensor - MCP9808 and an OLED Screen using this script:

#include <U8g2lib.h>
#include <SPI.h>
#include <Wire.h>
#include "Seeed_MCP9808.h"

MCP9808 sensor;
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/SCL, /* data=*/SDA, /* reset=*/U8X8_PIN_NONE);  //Software I2C

char strTemp\[16\];
float temp;

void setup() {
  u8g2.begin();
  Serial.begin(115200);
  
  if (sensor.init()) {
    Serial.println("sensor init failed!!");
  }   
  sensor.set_config(SET_CONFIG_ADDR, 0x0008);
  Serial.println("sensor init!!");
}

void loop() {
  sensor.get_temp(&temp);
  Serial.print("temperature value is: ");
  Serial.println(temp);
  delay(1000);

  dtostrf(temp, 7, 2, strTemp);

  u8g2.clearBuffer();                   // clear the internal memory
  u8g2.setFont(u8g2_font_luBIS08_tf);   // choose a suitable font
  u8g2.drawStr(0, 10, "Hello Seeed!");  // write something to the internal memory
  u8g2.drawStr(0, 30, "Temp. value:");  // write something to the internal memory
  u8g2.drawStr(0, 50, strTemp);         // write something to the internal memory
  u8g2.sendBuffer();                    // transfer internal memory to the display
}

The temperature output is not updated on the screen and I think it might be related to I2C addresses. Is it possible?

I run an I2C scanner and I think that the OLED address is 0x3C and the temp. sensor is 0x18. How can I assign them correctly. I never done something like that so any help is welcome.

Thanks a lot.

10
  • Simplify the problem. Try making a copy of your software w/o the OLED code and simply print the temperature out to the serial monitor already part of the IDE. If that works, then it may be the I2C addressing. If it does not then it's likely how you are sampling the temperature sensor.
    – st2000
    Commented Jan 1, 2023 at 17:37
  • Thanks for the reply and recommendation. Without the OLED code it prints the temperature out to the serial monitor, Any idea how to proceed?
    – AsiJapan
    Commented Jan 1, 2023 at 17:44
  • does "Hello Seeed!" show on the display?
    – jsotola
    Commented Jan 1, 2023 at 17:47
  • @jsotola: Yes, it does appear on the display
    – AsiJapan
    Commented Jan 1, 2023 at 17:50
  • ... oh, I see you are already printing out the temperature. I'd still comment out all the u8g2 lines and maybe even remove the OLED hardware and see if you see new temperature data ever second.
    – st2000
    Commented Jan 1, 2023 at 17:51

1 Answer 1

1

You mix software I2C with hardware I2C on the hardware I2C pins.

To use hw I2C with the display use the U8G2_SSD1306_128X64_NONAME_F_HW_I2C constructor.

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

Or use a different pair of pins for the software I2C display control.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.