Example Code for Arduino-Read Sensor Data (Version 2.0)

Upload the program to the FireBeetle Board ESP32-E and open the serial monitor to observe various environmental parameters. Function: Retrieves and prints all sensor data.

Hardware Preparation

  • FireBeetle Board ESP32-E ×1
  • SEN0500 Multi-functional Environmental Sensor ×1
  • Dupont wires ×4

Software Preparation

Wiring Diagram

Other Preparation Work

Regarding UART/I2C Mode:

  1. The default code operates in UART mode. Connect the wires according to the diagram above to the corresponding UART pins.
  2. To switch to I2C mode, connect the wires as shown in the diagram to the designated I2C pins and modify the code by replacing 1 with 0 at the beginning, as illustrated.

Procedure

  1. Connect the module to the FireBeetle Board ESP32-E using either of the wiring configurations shown above.
  2. Open the Arduino IDE and upload the provided code to the FireBeetle Board ESP32-E.
  3. Launch the Serial Monitor in the Arduino IDE, set the baud rate to 115200, and observe the printed output.

Sample Code

#include "DFRobot_EnvironmentalSensor.h"

#if defined(ARDUINO_AVR_UNO) || defined(ESP8266)
#include <SoftwareSerial.h>
#endif

#define MODESWITCH        /*UART:*/1 /*I2C: 0*/

#if MODESWITCH
  #if defined(ARDUINO_AVR_UNO) || defined(ESP8266)
    SoftwareSerial mySerial(/*rx =*/4, /*tx =*/5);
    DFRobot_EnvironmentalSensor environment(/*addr =*/SEN050X_DEFAULT_DEVICE_ADDRESS, /*s =*/&mySerial);
  #else
    DFRobot_EnvironmentalSensor environment(/*addr =*/SEN050X_DEFAULT_DEVICE_ADDRESS, /*s =*/&Serial1);
  #endif
#else
  DFRobot_EnvironmentalSensor environment(/*addr =*/SEN050X_DEFAULT_DEVICE_ADDRESS, /*pWire =*/&Wire);
#endif

void setup() {
  #if MODESWITCH
    // Initialize MCU communication serial port
    #if defined(ARDUINO_AVR_UNO) || defined(ESP8266)
      mySerial.begin(9600);
    #elif defined(ESP32)
      Serial1.begin(9600, SERIAL_8N1, /*rx =*/D3, /*tx =*/D2);
    #else
      Serial1.begin(9600);
    #endif
  #endif

  Serial.begin(115200);
  while (environment.begin() != 0) {
    Serial.println("Sensor initialization failed!");
    delay(1000);
  }
  Serial.println("Sensor initialized successfully!");
}

void loop() {
  // Print sensor data
  Serial.println("-------------------------------");
  Serial.print("Temperature: ");
  Serial.print(environment.getTemperature(TEMP_C));
  Serial.println(" °C");
  Serial.print("Temperature: ");
  Serial.print(environment.getTemperature(TEMP_F));
  Serial.println(" °F");
  Serial.print("Humidity: ");
  Serial.print(environment.getHumidity());
  Serial.println(" %");
  Serial.print("UV Intensity: ");
  Serial.print(environment.getUltravioletIntensity());
  Serial.println(" mW/cm²");
  Serial.print("Lux: ");
  Serial.print(environment.getLuminousIntensity());
  Serial.println(" lx");
  Serial.print("Pressure: ");
  Serial.print(environment.getAtmospherePressure(HPA));
  Serial.println(" hPa");
  Serial.print("Altitude: ");
  Serial.print(environment.getElevation());
  Serial.println(" m");
  Serial.println("-------------------------------");
  delay(500);
}

Result

The Serial Monitor will display the following formatted data, containing all readings from the sensor module:

Library Function Definitions

/**
 * @fn begin
 * @brief Initializes the SEN0500/SEN0501 environmental sensor
 * @return Initialization status
 * @retval 0  Success
 * @retval -1 Failure
 */
int8_t begin(void);

/**
 * @fn getTemperature
 * @brief Retrieves temperature data from SEN0500/SEN0501
 * @param units Temperature unit selection
 * @n     TEMP_C  Celsius (°C)
 * @n     TEMP_F  Fahrenheit (°F)
 * @return Measured temperature value
 */
float getTemperature(uint8_t units);

/**
 * @fn getHumidity
 * @brief Retrieves relative humidity data from SEN0500/SEN0501
 * @return Measured humidity percentage (%)
 */
float getHumidity(void);

/**
 * @fn getUltravioletIntensity
 * @brief Retrieves UV intensity data from SEN0500/SEN0501
 * @return Measured ultraviolet intensity (mW/cm²)
 */
float getUltravioletIntensity(void);

/**
 * @fn getLuminousIntensity
 * @brief Retrieves ambient light intensity data from SEN0500/SEN0501
 * @return Measured illuminance (lux)
 */
float getLuminousIntensity(void);

/**
 * @fn getAtmospherePressure
 * @brief Retrieves atmospheric pressure data from SEN0500/SEN0501
 * @param units Pressure unit selection
 * @n     HPA  Hectopascal (hPa)
 * @n     KPA  Kilopascal (kPa)
 * @return Measured atmospheric pressure
 */
uint16_t getAtmospherePressure(uint8_t units);

/**
 * @fn getElevation
 * @brief Retrieves altitude/elevation data from SEN0500/SEN0501
 * @return Measured elevation (meters above sea level)
 */
float getElevation(void);

Was this article helpful?

TOP