Example Code for Arduino-I2C Port
Last revision 2025/12/27
This article provides example code for using the Arduino I2C port with FireBeetle 2 ESP32-E and SHT40 sensor, covering hardware setup, software installation, wiring, and code execution.
Hardware Preparation
- FireBeetle 2 ESP32-E IoT Microcontroller (SKU: DFR0654) ×1
- Terminal Block Board for FireBeetle 2 ESP32-E(SKU: DFR0923)
- SHT40 Temperature & Humidity Sensor(SKU: SEN0428) x 1
- USB Cable ×1
- M-M/F-M/F-F Jumper wires
Software Preparation
- Download Arduino IDE: Click to download Arduino IDE.
- Install SDK: click to enter FireBeetle ESP32-E IoT Microcontroller Wiki for SDK installation tutorial.
- Download and install the DFRobot_SHT
- For Arduino IDE V1.8.19 (or earlier), install the library manually: How to Add a Library?
- For Arduino IDE V2.0.0 (or later), directly search for the "DFRobot_SHT" in the Library Manager and install it.
Wiring Diagram

Description:
FireBeete 2 Terminal Shield: VCC to SHT40 VCC
FireBeete 2 Terminal Shield: GND to SHT40 GND
FireBeete 2 Terminal Shield: SCL to SHT40 SCL
FireBeete 2 Terminal Shield: SDA to SHT40 SDA
Sample Code
Get the ambient temperature and humidity using SHT40 to domenstrate I2C function.
#include"DFRobot_SHT40.h"
DFRobot_SHT40 SHT40(SHT40_AD1B_IIC_ADDR); // Instantiate SHT40 sensor object with specified I2C address
uint32_t id = 0; // Variable to store sensor device ID
float temperature, humidity; // Variables to store measured temperature and humidity
void setup() {
// Initialize serial communication at 9600 baud rate
Serial.begin(9600);
// Initialize SHT40 sensor
SHT40.begin();
// Loop until valid device ID is retrieved (check sensor connection)
while((id = SHT40.getDeviceID()) == 0){
Serial.println("ID retrieval error, please check whether the device is connected correctly!!!");
delay(1000);
}
delay(1000);
// Print the retrieved device ID in hexadecimal format
Serial.print("id :0x"); Serial.println(id, HEX);
}
void loop() {
// Read temperature with high precision mode
temperature = SHT40.getTemperature(/*mode = */PRECISION_HIGH);
// Read humidity with high precision mode
humidity = SHT40.getHumidity(/*mode = */PRECISION_HIGH);
// Check if temperature reading is successful
if(temperature == MODE_ERR){
Serial.println("Incorrect mode configuration to get temperature");
} else{
Serial.print("Temperature :"); Serial.print(temperature); Serial.println(" C");
}
// Check if humidity reading is successful
if(humidity == MODE_ERR){
Serial.println("The mode for getting humidity was misconfigured");
} else{
Serial.print("Humidity :"); Serial.print(humidity); Serial.println(" %RH");
}
// Turn on heater when humidity exceeds 80% RH (prevent sensor condensation)
if(humidity > 80){
SHT40.enHeater(/*mode = */POWER_CONSUMPTION_H_HEATER_1S);
}
// Delay 1 second before next measurement cycle
delay(1000);
Serial.println("----------------------------------------");
}
Result
Print the sensor data on the serial monitor.

Was this article helpful?
