Atmega328 Thermo-/Hygrometer mit LCD

Nachdem die Bauteile herumgelegen sind, habe ich mich dazu entschlossen ein Thermo-/Hygrometer mit einem Atmega328 und Nokia 5110 LCD zu bauen, das mir auch Durchschnitts-, Minimal- und Maximalwerte anzeigen soll.

Der Jumper links oben ist zum ein-/ausschalten; der rechts unter der oberen Steckleiste ist für die Hintergrundbeleuchtung. Um die Beleuchtung nur kurzzeitig zu betätigen, habe ich nachträglich noch einen Taster angebracht, der nur dann etwas bewirkt, wenn der Jumper offen ist.

Der Sensor ist ein AM2302 (auch als DHT22 zu finden). Am Datenpin hängt ein 1k Pull-Up Widerstand.

Auf der Rückseite ist ein AM1117-3.3 angebracht, um das ganze mit einer 9V Batterie speisen zu können.

Der Code kann leicht angepasst werden, um statt der Durchschnittswerte z.B. die Minimalwerte anzeigen zu lassen. Für die Durchschnittswerte wird ein Zähler mit Datentyp ‘unsigned long’ verwendet, wodurch das Programm über knappe 680 Jahre den Durchschnitt berechnen kann, wenn alle 5 Sekunden neue Werte kommen ;)

Verwendet wurden folgende Libraries:

https://github.com/adafruit/DHT-sensor-library

https://github.com/adafruit/Adafruit-PCD8544-Nokia-5110-LCD-library

/*********************************************************************
This is an example sketch for our Monochrome Nokia 5110 LCD Displays
 
  Pick one up today in the adafruit shop!
  ------> http://www.adafruit.com/products/338
 
These displays use SPI to communicate, 4 or 5 pins are required to
interface
 
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
 
Written by Limor Fried/Ladyada  for Adafruit Industries.
BSD license, check license.txt for more information
All text above, and the splash screen must be included in any redistribution
*********************************************************************/
 
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
#include "DHT.h"
 
// Software SPI (slower updates, more flexible pin options):
// pin 7 - Serial clock out (SCLK)
// pin 6 - Serial data out (DIN)
// pin 5 - Data/Command select (D/C)
// pin 4 - LCD chip select (CS)
// pin 3 - LCD reset (RST)
//Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);
 
// Hardware SPI (faster, but must use certain hardware pins):
// SCK is LCD serial clock (SCLK) - this is pin 13 on Arduino Uno
// MOSI is LCD DIN - this is pin 11 on an Arduino Uno
// pin 5 - Data/Command select (D/C)
// pin 4 - LCD chip select (CS)
// pin 3 - LCD reset (RST)
Adafruit_PCD8544 display = Adafruit_PCD8544(5, 4, 3);
// Note with hardware SPI MISO and SS pins aren't used but will still be read
// and written to during SPI transfer.  Be careful sharing these pins!
 
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
 
#define DHTPIN 2
#define DHTTYPE DHT22
 
DHT dht(DHTPIN, DHTTYPE);
 
float hsum=0,havg=0,tsum=0,tavg=0,tmax=0,tmin=0,hmax=0,hmin=0;
unsigned long n=1;
 
void setup()   {
  Serial.begin(9600);
  dht.begin();
  display.begin();
  display.setContrast(55);
}
 
 
void loop() {
  display.clearDisplay();
 
  char lcdDegC = char(247);
 
  if(n==1){
    float hmax = dht.readHumidity();
    float tmax = dht.readTemperature();
    float hmin = dht.readHumidity();
    float tmin = dht.readTemperature();
  }
 
  float h = dht.readHumidity();
  float t = dht.readTemperature();
 
  if(h > hmax){
    hmax = h;
  }
 
  if(t > tmax){
    tmax = t;
  }
 
  if(h < hmin){
    hmin = h;
  }
 
  if(t < tmin){
    tmin = t;
  }
 
 
  hsum += h;
  havg = hsum / n;
 
  tsum += t;
  tavg = tsum / n;
 
  display.setTextSize(1);
  display.setTextColor(BLACK);
  display.setCursor(0,0);
  display.print("Temp: ");
  display.print(t);
  display.print(lcdDegC);
  display.println("C");
 
  display.print("Humid: ");
  display.print(h);
  display.println("%");
 
  display.print("Tmax: ");
  display.print(tmax);
  display.print(lcdDegC);
  display.println("C");
 
  display.print("Hmax:  ");
  display.print(hmax);
  display.println("%");
 
  display.print("Tavg: ");  
  display.print(tavg);
  display.print(lcdDegC);
  display.println("C");
 
  display.print("Havg:  ");
  display.print(havg);
  display.println("%");
 
  display.display();
  n++;
  delay(5000);
}