Example Code for Arduino - Display temperature & humidity on OLED

Get temperature & humidity information and display them on OLED.

Hardware Preparation

  • FireBeetle 2 ESP32-C6 (SKU: DFR1075) * 1

  • USB Type-C Cable (Included with the board) * 1

  • 0.96”128x64 IIC/SPI OLED x1

  • Fermion: SHT30 Temperature & Humidity Sensor x1

  • Jumper wires

Software Preparation

Connection Diagram

Sample Code

#include <Arduino.h>
#include <U8g2lib.h> //Import font library 
//#include <SPI.h>
#include <Wire.h>

#include <DFRobot_SHT3x.h>

/*
---Display Hardware I2C Interface---
U8G2_R0 No rotation, horizontal, draw from left to right
U8G2_R1 Rotate 90 degrees clockwise, draw from top to  bottom
U8G2_R2 Rotate 180 degrees clockwise, draw from right to left
U8G2_R3 Rotate 270 degrees clockwise, draw from bottom to top
U8G2_MIRROR Display image content normally(v2.6.x and above)   Note: U8G2_MIRROR needs to be used with setFlipMode().
U8x8_PIN_NONE for empty pin, reset pin will not be used.
---Display Hardware SPI Interface---
cs connect as pinout(Selected by users)
dc connect as pinout(Selected by users)
*/
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(/* rotation=*/U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

//Select 0x45 when ADR is connected to VDD; select 0x44 when ADR goes to GND
//Default to 0x45, unnecessary to connect RST(reset pin)
DFRobot_SHT3x sht3x(&Wire,/*address=*/0x45,/*RST=*/4);

//Comment out the codes above when using SPI, and run the codes below
//DFRobot_SHT3x   sht3x;

void setup() {
  Serial.begin(115200);
  u8g2.begin();
  u8g2.setFontPosTop();//When you use drawStr to display strings, the default criteria is to display the lower-left coordinates of the characters. The function can be understood as changing the coordinate position to the upper left corner of the display string as the coordinate standard.
  //Init sensor 
  while (sht3x.begin() != 0) {
        Serial.println("Failed to Initialize the chip, please confirm the wire connection");
        delay(1000);
      }
      Serial.print("Chip serial number");
      Serial.println(sht3x.readSerialNumber());
      if(!sht3x.softReset()){
         Serial.println("Failed to Initialize the chip....");
       }
}

void loop() {
  //Clear display 
  u8g2.clearBuffer();
  //Assign reading to temp and humi for displaying 
  float   temp = sht3x.getTemperatureC();
  float   humi = sht3x.getHumidityRH();  
  //Display temperature 
  u8g2.setFont(u8g2_font_osb18_tf);    // Select font type and size(see official)
  u8g2.drawStr(5,10,"Temp");//Write character to the specified position 
  u8g2.setFont(u8g2_font_t0_18b_tr);
  u8g2.setCursor(75, 15);//Display content from this position
  u8g2.print(temp);
  //Display humidity
  u8g2.setFont(u8g2_font_osb18_tf);    
  u8g2.drawStr(5,40,"Humi");
  u8g2.setFont(u8g2_font_t0_18b_tr);
  u8g2.setCursor(75, 45);
  u8g2.print(humi);
  u8g2.sendBuffer(); 
  delay(1000);
}

Other Supplementary Information

Member function

  • u8g2.drawStr(x,y,"Temp")

    • Description: Specify the screen position to display custom content

    • Parameter:

      • X, Y: coordinates to start writing (the display font is displayed from the lower left corner to the upper right corner)

      • "Temp": English and numbers can be filled

  • u8g2.setCursor(x,y) and u8g2.print()

    • Description: Used together, the former is to display the starting position, and the latter has the same function as Arduino's print

    • Parameter:

      • X, Y: coordinates to start writing (the display font is displayed from the lower left corner to the upper right corner)

Was this article helpful?

TOP