Example Code for Arduino-display measured temperature and humidity
Getting the temperature and humidity displayed on the OLED screen is a very intuitive and interesting project. Below you will learn the basic OLED display and use the I2C interface to obtain the temperature and humidity sensor data.
Hardware Preparation
- DFR0868 Beetle ESP32-C3 x 1
- DFR0650 0.96”128x64 IIC/SPI OLED monochrome display x1
- SEN0330 FermionSHT30 temperature and humidity sensor x1 (or other SHT temperature and humidity sensor using the same SHT3x code base)
- Jumper Wires
Software Preparation
- Download Arduino IDE: Click to download Arduino IDE
- Add ESP32 board to Arduino IDE
- Download the DFRobot_SHT3X library: DFRobot_SHT3X library
- About how to install the library?
Wiring 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 does not rotate, landscape, drawing direction is from left to right
U8G2_R1 is rotated 90 degrees clockwise, and the drawing direction is from top to bottom
U8G2_R2 is rotated 180 degrees clockwise, and the drawing direction is from right to left
U8G2_R3 is rotated 270 degrees clockwise, and the drawing direction is from bottom to top
U8G2_MIRROR displays mirrored content normally (use version v2.6.x and above) Note: U8G2_MIRROR needs to be used in conjunction with setFlipMode().
U8x8_PIN_NONE means the pin is empty and the reset pin will not be used
---Display hardware SPI interface---
Connect cs by pin (pin can be selected by yourself)
Connect dc by pin (pin can be selected by yourself)
*/
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(/* rotation=*/U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
//When ADR is connected to VDD, 0x45 can be selected; when ADR is connected to GND, 0x44 can be selected
//The default is 0x45, RST (reset pin) does not need to be connected
DFRobot_SHT3x sht3x(&Wire,/*address=*/0x45,/*RST=*/4);
//To use SPI, you need to comment the above code and use the following code
//DFRobot_SHT3x sht3x;
void setup() {
Serial.begin(115200);
u8g2.begin();
u8g2.setFontPosTop();//When using drawStr to display strings, the default standard is the coordinates at the lower left corner of the displayed characters. The function of this function can be understood as changing the coordinate position to the upper left corner of the display string as the coordinate standard.
//Initialize the 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() {
//Clean the screen
u8g2.clearBuffer();
//Use temperature and humidity read assignments for display
float temp = sht3x.getTemperatureC();
float humi = sht3x.getHumidityRH();
//Display temperature
u8g2.setFont(u8g2_font_osb18_tf); // Choose font and size (see official)
u8g2.drawStr(5,10,"Temp");//Write the character at the specified position
u8g2.setFont(u8g2_font_t0_18b_tr);
u8g2.setCursor(75, 15);//Display starts 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);
}
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?
