Friday, December 20, 2019

Arduino based Temperature/Humidity/Clock


Above is a Fritzing generated schematic of a project I've recently been working on for the Arduino Nano. The project uses a Real Time Clock component, a temperature/humidity sensor and a liquid crystal display. It is powered by a portable power bank.




The code for the project is shown below.

//-------------------------------------------------------------------------------
//
// Written by Paul Crossley 20/12/2019
//
// www.magmamon.co.uk
//
// Temperature/Humidity sensor on D8
// RTC and Display on A4 and A5
// my own personal temperature adjustment was -2C
// my display I2C address was 0x3F, use i2c-scanner 
//
//-------------------------------------------------------------------------------

#include <SimpleDHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <TimerOne.h>
#include <RTClib.h>

//-------------------------------------------------------------------------------

volatile unsigned int clockMilliSeconds = 0;
volatile byte clockSeconds = 0;
volatile byte clockMinutes = 0;
volatile byte clockHours = 12;
volatile byte clockEnabled = 1;
byte alarmMinutes = 30;
byte alarmHours = 6;
volatile byte alarmEnabled = false;
byte alarmTogglePressed = false;
enum displayModeValues
{
  MODE_CLOCK_TIME,
  MODE_CLOCK_TIME_SET_HOUR,
  MODE_CLOCK_TIME_SET_MINUTE,
  MODE_ALARM_TIME,
  MODE_ALARM_TIME_SET_HOUR,
  MODE_ALARM_TIME_SET_MINUTE
};
byte displayMode = MODE_CLOCK_TIME;
RTC_DS1307 RTC;

//-------------------------------------------------------------------------------

void clockISR ()
{
  if (clockEnabled)
  {
    clockMilliSeconds++;
    if (clockMilliSeconds >= 1000)
    {
      clockMilliSeconds = 0;

      clockSeconds++;
      if (clockSeconds >= 60)
      {
        clockSeconds = 0;

        clockMinutes++;
        if (clockMinutes >= 60)
        {
          clockMinutes = 0;

          clockHours++;
          if (clockHours >= 24)
          {
            clockHours = 0;
          }
        }
      }
    }
  }
}

//-------------------------------------------------------------------------------

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
#define BACKLIGHT_PIN     13

int pinDHT11 = 8;
SimpleDHT11 dht11(pinDHT11);

int tempAdjust = -2;

byte degree[8] = {
  B01100,
  B10010,
  B10010,
  B01100,
  B00000,
  B00000,
  B00000,
};

void setup() {
  Serial.begin (9600);
  lcd.begin(16, 2);
  lcd.clear();
  lcd.createChar(0, degree);
  lcd.setBacklight(BACKLIGHT_ON);
  pinMode(LED_BUILTIN, OUTPUT);
  RTC.begin();
//  RTC.adjust(DateTime(__DATE__, __TIME__));  to set time
}

void loop() {

  byte temperature = 0;
  byte humidity = 0;
  int err = SimpleDHTErrSuccess;

  digitalWrite(LED_BUILTIN, HIGH);

  if ((err = dht11.read(pinDHT11, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) 
  {
    Serial.print("No reading , err="); Serial.println(err); delay(1000);
    return;
  }

  Serial.print("Temp: ");
  Serial.print((int)temperature); Serial.print("C, ");
  Serial.print("Humid: ");
  Serial.print((int)humidity); Serial.println("%");

  lcd.home();

  lcd.print ("T: ");
  if ((temperature + tempAdjust) < 10) { lcd.print (" "); }

  lcd.print ((int)temperature + tempAdjust);
  lcd.write (byte(0));
  lcd.print ("C   ");

  lcd.print ("H: ");
  if ((humidity) < 10) { lcd.print (" "); }
  lcd.print ((int)humidity);
  lcd.print ("%");

  delay (1000);
  digitalWrite(LED_BUILTIN, LOW);

  DateTime now = RTC.now();

  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(' ');
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println(); 

  lcd.setCursor ( 0, 1 );
  if (now.day() < 10) { lcd.print ("0"); }
  lcd.print ((int)(now.day()));
  lcd.print ("/");
  if (now.month() < 10) { lcd.print ("0"); }
  lcd.print ((int)(now.month()));
  lcd.print ("/");
  lcd.print ((int)(now.year()));
  lcd.print (" ");
  if (now.hour() < 10) { lcd.print ("0"); }
  lcd.print ((int)(now.hour()));
  lcd.print (":");
  if (now.minute() < 10) { lcd.print ("0"); }
  lcd.print ((int)(now.minute()));

  delay (1000);
}

No comments: