Example Code for Arduino-Weather Station

Last revision 2026/01/31

This article offers example code and detailed instructions for creating a weather station using Arduino, with sensors for temperature, humidity, UV, and pressure, and an interactive display interface to visualize data dynamically.

Hardware Preparation

Software Preparation

Wiring Diagram

DFR0997-Wiring daigram

Sample Code

The display screen can be combined with Arduino UNO and various weather environmental sensors (including temperature and humidity sensors, UV sensors, and pressure sensors) to create a visually appealing weather station display interface. The display screen can show various icons, animations, text, time, and other effects.

#include "DFRobot_LcdDisplay.h"
#include <MsTimer2.h>
DFRobot_Lcd_IIC lcd(&Wire, /*I2CAddr*/ 0x2c);
//LTR390 UV Sensor
#include "DFRobot_LTR390UV.h"
#include <SoftwareSerial.h>
#define UARTMODE //UART mode
SoftwareSerial mySerial(/*rx =*/4, /*tx =*/5);
DFRobot_LTR390UV ltr390(/*addr =*/LTR390UV_DEVICE_ADDR, /*s =*/&mySerial);
//BMP388 Pressure Sensor
#include <DFRobot_BMP3XX.h>
DFRobot_BMP388_I2C  sensor(&Wire, sensor.eSDOVDD);
#define CALIBRATE_ABSOLUTE_DIFFERENCE
//DHT11 Temperature and Humidity Sensor
#include <dht11.h>
dht11 DHT;
#define DHT11_PIN 10

uint8_t Tem, Hum, Pre, Ult, lcdTimeId,labelId1,labelId2,labelId3,labelId4;
uint8_t hour = 9, Minute = 8, second = 56;
uint8_t lastSecond = 0;
void flash()   //Interrupt processing program for time change
{
  second++;
  if (second > 59) {
    second = 0;
    Minute++;
  }
  if (Minute > 59) {
    Minute = 0;
    hour++;
  }
}

void setup(void){
  lcd.begin();
  lcd.setBackgroundColor(WHITE);
  lcd.cleanScreen();
  delay(500);
  MsTimer2::set(1000, flash);        // Set the interrupt function, enter the interrupt every 1000ms
  MsTimer2::start();                 // Start counting
  
  lcdTimeId = lcd.drawLcdTime(10, 90, hour, Minute, second, 1, LIGHTGREY); //Display the current time
  lcd.drawLcdDate(10, 120, 4, 7, 7, 0, LIGHTGREY); //Display the current date
  lcd.drawGif(20, 5, "Cloudy.gif", 512);  //Create an icon
  
  //Create icons for corresponding sensors
  lcd.drawIcon(124, -8, "/sensor icon/thermometer.png", 120);
  lcd.drawIcon(119, 35, "/sensor icon/raindrops.png", 120);
  lcd.drawIcon(110, 78, "/sensor icon/pressure.png", 120);
  lcd.drawIcon(109, 118, "/season icon/sunny.png", 120);

  //Create multiple icons
  lcd.drawIcon(0, 176, "/botany icon/Potted plant flower.png", 256);
  lcd.drawIcon(53, 176, "/botany icon/cactus3.png", 256);
  lcd.drawIcon(106, 176, "/botany icon/grass.png", 256);
  lcd.drawIcon(159, 176, "/botany icon/grass1.png", 256);
  lcd.drawIcon(212, 176, "/botany icon/cactus1.png", 256);
  lcd.drawIcon(265, 176, "/botany icon/cactus2.png", 256);

  //Create progress bars to display temperature
  Tem = lcd.creatBar(164, 22, 80, 15, ORANGE);
  labelId1 = lcd.drawString( 255, 22, "0°C", 0, ORANGE);
  //Create progress bars to display humidity
  Hum = lcd.creatBar(164, 62, 80, 15, RED);
  labelId2 = lcd.drawString( 255, 62, "0%", 0, RED);
  //Create progress bars to display pressure
  Pre = lcd.creatBar(164, 102, 80, 15, BLUE);
  labelId3 = lcd.drawString( 255, 102, "0Pa", 0, BLUE);
  //Create progress bars to display UV intensity
  Ult = lcd.creatBar(164, 142, 80, 15, GREEN);
  labelId4 = lcd.drawString( 255, 142, "0lux", 0, GREEN);

  //LTR390 UV Sensor initialization settings
  #define UARTMODE
  mySerial.begin(9600);
  Serial.begin(115200);
  while(ltr390.begin() != 0){
    Serial.println(" Sensor initialize failed!!");
    delay(1000);
  }
  Serial.println(" Sensor  initialize success!!");
  ltr390.setALSOrUVSMeasRate(ltr390.e18bit,ltr390.e100ms);//18-bit data, sampling time 100ms 
  ltr390.setALSOrUVSGain(ltr390.eGain3);//3x gain
  ltr390.setMode(ltr390.eALSMode);//Set UV mode

  //BMP388 Pressure Sensor initialization settings
    int rslt;
    while( ERR_OK != (rslt = sensor.begin()) ){
      if(ERR_DATA_BUS == rslt){
        Serial.println("Data bus error!!!");
      }else if(ERR_IC_VERSION == rslt){
        Serial.println("Chip versions do not match!!!");
      }
      delay(3000);
    }
    Serial.println("Begin ok!");
    while( !sensor.setSamplingMode(sensor.eUltraPrecision) ){
      Serial.println("Set samping mode fail, retrying....");
      delay(3000);
    }
    delay(100);
    #ifdef CALIBRATE_ABSOLUTE_DIFFERENCE
    if( sensor.calibratedAbsoluteDifference(540.0) ){
      Serial.println("Absolute difference base value set successfully!");
    }
    #endif
    float sampingPeriodus = sensor.getSamplingPeriodUS();
    Serial.print("samping period : ");
    Serial.print(sampingPeriodus);
    Serial.println(" us");
  
    float sampingFrequencyHz = 1000000 / sampingPeriodus;
    Serial.print("samping frequency : ");
    Serial.print(sampingFrequencyHz);
    Serial.println(" Hz");
    delay(1000);
}

void loop(void){
  if(lastSecond != second){
    lastSecond = second;
    lcd.updateLcdTime(lcdTimeId, 10, 90, hour, Minute, lastSecond, 1, LIGHTGREY); //Display the current time
  }
  delay(100);
  uint32_t date4 = 0;
  date4 = ltr390.readALSTransformData(); //Get the transformed data of ambient light
  float date3 = sensor.readPressPa(); //Get the pressure data of the pressure sensor
  DHT.read(DHT11_PIN);  //Get the temperature and humidity data of the temperature and humidity sensor
  Serial.println("-------------");
  Serial.println(DHT.temperature);
  Serial.println(DHT.humidity);
  Serial.println(date3);
  Serial.println(date4);
  
  //Set the value of the progress bars
  lcd.setBarValue(Tem, DHT.temperature);
  lcd.updateString(labelId1, 255, 22, String(DHT.temperature)+"°C", 0, ORANGE);
  delay(100);
  lcd.setBarValue(Hum, DHT.humidity);
  lcd.updateString(labelId2, 255, 62, String(DHT.humidity)+"%", 0, RED);
  delay(100);
  lcd.setBarValue(Pre, date3);
  lcd.updateString(labelId3, 255, 102, String(date3)+"Pa", 0, BLUE);
  delay(100);
  lcd.setBarValue(Ult, date4);
  lcd.updateString(labelId4, 255, 142, String(date4)+"lux", 0, GREEN);
  delay(100);
}

Result

DFR0997-Weather Station

Was this article helpful?

TOP