Arduino Weather Station

FullSizeRender

An Arduino with a DHT22 Temperature/Humidity sensor and a BMP180 pressure sensor.

Fairly basic code, just scrolls through 3 screens showing temperature, humidity and pressure. The min/max is also displayed.

// include the library code:
#include <LiquidCrystal.h>
#include <stdio.h>
#include “DHT.h”
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP085_U.h>
//—-DHT setup
#define DHTTYPE DHT22   // DHT 22  (AM2302)
#define DHTPIN 2 //Temp humidity sensor
DHT dht(DHTPIN, DHTTYPE);

//—-BMP180 setup
//Connect SCL to analog 5
//Connect SDA to analog 4
//Connect VDD to 3.3V DC
//Connect GROUND to common ground
Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085);

//—-LCD setup
LiquidCrystal lcd(8,9,4,5,6,7);
int lcd_key     = 0;
int adc_key_in  = 0;
#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5

float temp = 5.5f;
float max_temp = 0;
float min_temp = 9999;

float pressure = 0.0f;
float max_pressure = 0;
float min_pressure = 9999;

float humidity = 1.0f;
float max_humidity = 0;
float min_humidity = 9999;
int displayCount = 0;

void setup() {
//setup dht
Serial.begin(9600);
dht.begin();
bmp.begin();
// set up the LCD’s number of columns and rows:
lcd.begin(16, 2);
lcd.noAutoscroll();
reset();
}

void loop() {

readTempAndHumidity();
readPressure();
updateScreen();
//logToSDCard();
logToSerial();
checkForReset();
delay(4000);
}

void readTempAndHumidity()
{
humidity = dht.readHumidity();
if(humidity>max_humidity) max_humidity = humidity;
if(humidity<min_humidity) min_humidity = humidity;

// Read temperature as Celsius
temp = dht.readTemperature();
if(temp>max_temp) max_temp = temp;
if((temp<min_temp)&&(temp!=0.0)) min_temp = temp;
}

void readPressure()
{
sensors_event_t event;
bmp.getEvent(&event);
if (event.pressure)
{
pressure = event.pressure;
if(pressure>max_pressure) max_pressure = pressure;
if(pressure<min_pressure) min_pressure = pressure;
}
else
{
pressure = 0.0f;
}

}

void updateScreen()
{
float dp;
float dp1;
float dp2;
char buff1 [17];
char buff2 [17];
lcd.clear();

switch(displayCount)
{
case 0:
sprintf(buff1,”%s”,”Weather Station”);
sprintf(buff2,”%s”,”—————“);
break;
case 1:
dp = (temp-(int)temp)*10;
dp1 = (max_temp-(int)max_temp)*10;
dp2 = (min_temp-(int)min_temp)*10;
sprintf(buff1,”Temp:%2d.%1dC”,(int)temp,(int)dp);
sprintf(buff2,”+:%2d.%1dC -:%2d.%1dC       “,(int)max_temp,(int)dp1,(int)min_temp,(int)dp2);
break;
case 2:
dp = (humidity-(int)humidity)*10;
dp1 = (max_humidity-(int)max_humidity)*10;
dp2 = (min_humidity-(int)min_humidity)*10;
sprintf(buff1,”Humidity:%2d.%1d%”,(int)humidity,(int)dp);
sprintf(buff2,”+:%2d.%1d -:%2d.%1d       “,(int)max_humidity,(int)dp1,(int)min_humidity,(int)dp2);
break;
case 3:
dp = (pressure-(int)pressure)*10;
dp1 = (max_pressure-(int)max_pressure)*10;
dp2 = (min_pressure-(int)min_pressure)*10;
sprintf(buff1,”Press.:%2d.%1dmb”,(int)pressure,(int)dp);
sprintf(buff2,”+:%2d.%1d -:%2d.%1d       “,(int)max_pressure,(int)dp1,(int)min_pressure,(int)dp2);
break;
}
lcd.setCursor(0, 0);
lcd.print(buff1);
lcd.setCursor(0, 1);
lcd.print(buff2);
displayCount++;
if(displayCount>3)
{
displayCount = 1;
}

}
void logToSDCard()
{
}

void logToSerial()
{
Serial.print(temp);
Serial.print(“,”);
Serial.print(humidity);
Serial.print(“,”);
Serial.println(pressure);
}
void checkForReset()
{
if(read_LCD_buttons()==btnSELECT)
{
reset();
}
}

void reset()
{
max_temp = 0.0f;
min_temp = 9999.0f;
max_pressure = 0.0f;
min_pressure = 9999.0f;
max_humidity = 0.0f;
min_humidity = 9999.0f;
}
// read the buttons
int read_LCD_buttons()
{
adc_key_in = analogRead(0);      // read the value from the sensor
//Serial.println(adc_key_in);
// For V1.0 comment the other threshold and use the one below:
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
if (adc_key_in < 50)   return btnRIGHT;
if (adc_key_in < 195)  return btnUP;
if (adc_key_in < 380)  return btnDOWN;
if (adc_key_in < 555)  return btnLEFT;
if (adc_key_in < 790)  return btnSELECT;

return btnNONE;  // when all others fail, return this…
}

Add a Comment

Your email address will not be published. Required fields are marked *