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

Software Preparation

Wiring Diagram

DFR0923-I2C 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.

DFR0923-I2C result

Was this article helpful?

TOP