Example Code for Arduino-UART Communication

Last revision 2026/01/18

This guide offers an example code for Arduino-UART communication, detailing hardware and software preparation, wiring setup, sample code execution, and result observation for reading environmental sensor data using DFRobot Beetle RP2040.

Hardware Preparation

  • DFRobot Beetle RP2040, 1, Purchase Link
  • USB Type-C Cable, 1
  • Fermion: Multifunctional Environmental Sensor (SEN0500), 1, Purchase Link

Software Preparation

Wiring Diagram

  • BeetleRP2040: GP29(RX) (connect to) Environmental Sensor: TX(T)
  • BeetleRP2040: GP28(TX) (connect to) Environmental Sensor: RX(R)
  • BeetleRP2040: 3V3 (connect to) Environmental Sensor: 3V3
  • BeetleRP2040: GND (connect to) Environmental Sensor: GND

Other Preparation Work

  1. Open Arduino IDE.
  2. Select "DFRobot Beetle RP2040" as the development board.
  3. Connect the Beetle RP2040 to your computer.
  4. Wire the environmental sensor to UART1 (GP28/TX1, GP29/RX1) as per the diagram.
  5. Install the DFRobot_RTU and DFRobot_EnvironmentalSensor libraries via Arduino Library Manager.

Sample Code

Function: read the value of the environmental sensor and print the readings in the serial port.

#include "DFRobot_EnvironmentalSensor.h"

DFRobot_EnvironmentalSensor environment(/*addr =*/SEN050X_DEFAULT_DEVICE_ADDRESS, /*s =*/&Serial1); //define the UART interface connected to the sensor

void setup()
{
  environment.begin();  //initialize the sensor
  Serial1.begin(9600); //define the baud rate of the UART interface connected to the sensor
  Serial.begin(9600); //define the baud rate of the USB-to-serial interface
}

void loop()
{
  Serial.println("-------------------------------");
  Serial.print("Temp: ");
  Serial.print(environment.getTemperature(TEMP_C));
  Serial.println(" ℃");
  Serial.print("Temp: ");
  Serial.print(environment.getTemperature(TEMP_F));
  Serial.println(" ℉");
  Serial.print("Humidity: ");
  Serial.print(environment.getHumidity());
  Serial.println(" %");
  Serial.print("Ultraviolet intensity: ");
  Serial.print(environment.getUltravioletIntensity());
  Serial.println(" mw/cm2");
  Serial.print("LuminousIntensity: ");
  Serial.print(environment.getLuminousIntensity());
  Serial.println(" lx");
  Serial.print("Atmospheric pressure: ");
  Serial.print(environment.getAtmospherePressure(HPA));
  Serial.println(" hpa");
  Serial.print("Altitude: ");
  Serial.print(environment.getElevation());
  Serial.println(" m");
  Serial.println("-------------------------------");
  delay(500);
}

Result

Open the ArduinoIDE serial monitor, and the monitor window prints the sensor data correctly.

 Result

Additional Information

  • UART1 uses pins GP28 (TX1) and GP29 (RX1) for communication.
  • The environmental sensor communicates at 9600 baud (default setting).

Was this article helpful?

TOP