DHT11, RTC and LCD

// include the library code:
#include <LiquidCrystal.h> // include the LCD library

const int rs = PB11, en = PB10, d4 = PA4, d5 = PA3, d6 = PA2, d7 = PA1; //STM32 Pins to which LCD is connected 
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); //Initialize the LCD
              
#include <DHT.h>  //Library for using DHT sensor 

#define DHTPIN PA0 
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);     //initilize object dht for class DHT with DHT pin with STM32 and DHT type as DHT11

#include <STM32RTC.h>
/* Get the rtc object */
STM32RTC& rtc = STM32RTC::getInstance();

/* Change these values to set the current initial time */
const byte seconds = 0;
const byte minutes = 11;
const byte hours = 10;

/* Change these values to set the current initial date */
const byte weekDay = 1;
const byte day = 29;
const byte month = 2;
const byte year = 20;

/*Setup IO Pins*/
#define pinLED PC13

void setup()
{ 
  rtc.begin(); // initialize RTC 24H format
  rtc.setTime(hours, minutes, seconds);
  rtc.setDate(weekDay, day, month, year);

  pinMode(pinLED, OUTPUT);
  
  // initialize the LCD
  lcd.begin(20,4);
  dht.begin();                                  
  lcd.print("DHT11 with STM32");
  delay(3000);
  lcd.clear();
}

void loop()
{
  float h = dht.readHumidity();       //Gets Humidity value
  float t = dht.readTemperature();    //Gets Temperature value
  lcd.setCursor(0,0);
  lcd.print("Temp: ");
  lcd.print(t);
  lcd.print(" C");
  lcd.setCursor(0,1);
  lcd.print("Humid: ");
  lcd.print(h);
  lcd.print(" %");

  // Print date...
  lcd.setCursor(0,3);
  print2digits(rtc.getDay());
  lcd.print("/");
  print2digits(rtc.getMonth());
  lcd.print("/");
  print2digits(rtc.getYear());
  lcd.print(" ");

  // ...and time
  print2digits(rtc.getHours());
  lcd.print(":");
  print2digits(rtc.getMinutes());
  lcd.print(":");
  print2digits(rtc.getSeconds());

  //Serial.println();

  digitalWrite(pinLED, HIGH);
  delay(100);
  digitalWrite(pinLED, LOW);
  
}

void print2digits(int number) {
  if (number < 10) {
    lcd.print("0"); // print a 0 before if the number is < than 10
  }
  lcd.print(number);
}

Comments powered by CComment