[Part 2] Arduino powered Weather Station

This is the continuation of the the weather station I am slowly building along with other hobbies I have on the go at the same time. Not a lot has changed besides I have added a DHT11 humidity / temperature, SDCard for logging data, I/O Expansion shield, and an ambient light sensor, where hopefully I can find a way to control the backlight to the LCD. Still yet to add a RTC and barometric sensor.

And here is the code that I have put together from sample code that is available with the parts I have used.

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <SD.h>

#define LOG_INTERVAL 1000
#define ECHO_TO_SERIAL 1 // turn serial output on/off
#define LOOP_DELAY 5000

#define tempPin 0 // ADC0 - lm35
#define DHT11_PIN 1 // ADC1 - dht11
#define lightPin 2 // ADC2 - light sensor

LiquidCrystal_I2C lcd(0x27,16,2);

const int chipSelect = 4; // sdcard pin

const float vcc = 4.961;

File logfile;

// error
void error(char *str)
{
  Serial.print("error: ");
  Serial.println(str);

  while(1);
}
// end error

// dht11
byte read_dht11_dat()
{
  byte i = 0;
  byte result=0;
  for(i=0; i< 8; i++)
  {
    while(!(PINC & _BV(DHT11_PIN)));  // wait for 50us
    delayMicroseconds(30);
    if(PINC & _BV(DHT11_PIN)) 
      result |=(1<<(7-i));
    while((PINC & _BV(DHT11_PIN)));  // wait '1' finish
  }
  return result;
}
// end dht11

void setup(){
  DDRC |= _BV(DHT11_PIN); //dht11
  PORTC |= _BV(DHT11_PIN); //dht11
  sdcard();
  lcd.init(); // initialize the lcd 
  lcd.backlight();
  Serial.begin(9600);
}
// end setup

void sdcard(){
  Serial.print("Initializing SD card...");
  pinMode(10, OUTPUT);

  if (!SD.begin(chipSelect)) {
    Serial.print("Card failed, or not present");
    return;
  }
  Serial.println("card initialized.");

  char filename[] = "data00.txt";
  for (uint8_t i = 0; i < 1000; i++) {
    filename[4] = i/10 + '0';
    filename[5] = i%10 + '0';
    if (! SD.exists(filename)) {
      logfile = SD.open(filename, FILE_WRITE); 
      break;
    }
  }

  if (! logfile) {
    error("couldnt create file");
  }

  Serial.print("Logging to: ");
  Serial.println(filename);
}
// end sdcard

void lm35(){
  int sensorTemp = analogRead(tempPin);
  sensorTemp = analogRead(tempPin);
  float volt = sensorTemp * vcc / 1024.0;  
  float tempC = volt * 100;
//  float tempF = (tempC * 9 / 5) + 32;
  
#if ECHO_TO_SERIAL
  Serial.print("LM35 Temperature = ");
  Serial.print(tempC);
  Serial.println("°C");
//  Serial.print(tempF);
//  Serial.println("°F");
#endif

  lcd.setCursor(0,1);
  lcd.print("T ");
  lcd.print((long)tempC % 100);
  lcd.print(".");
  lcd.print((long)tempC / 100);
  lcd.print((char)223);
  lcd.print("C ");

  logfile.print("LM35 Temperature = ");
  logfile.print(tempC);
  logfile.println("°C ");
//  logfile.print(tempF);
//  logfile.println("°F");
}
// end lm35

void light(){
  int lightSensor = analogRead(lightPin);
//  lightSensor = analogRead(lightPin);
#if ECHO_TO_SERIAL
  Serial.print("Lux = ");
  Serial.println(lightSensor);//print the value to serial
#endif

  lcd.setCursor(10,1);
  lcd.print("L ");
  lcd.print(lightSensor);
  
  logfile.print("Lux = ");
  logfile.println(lightSensor);
}
// end light sensor

void dht11(){
  byte dht11_dat[5];
  byte dht11_in;
  byte i;// start condition
  // 1. pull-down i/o pin from 18ms
  PORTC &= ~_BV(DHT11_PIN);
  delay(18);
  PORTC |= _BV(DHT11_PIN);
  delayMicroseconds(40);
  DDRC &= ~_BV(DHT11_PIN);
  delayMicroseconds(40);

  dht11_in = PINC & _BV(DHT11_PIN);
  if(dht11_in)
  {
    Serial.println("dht11 start condition 1 not met");
    return;
  }
  delayMicroseconds(80);
  dht11_in = PINC & _BV(DHT11_PIN);
  if(!dht11_in)
  {
    Serial.println("dht11 start condition 2 not met");
    return;
  }

  delayMicroseconds(80);// now ready for data reception
  for (i=0; i<5; i++)
    dht11_dat[i] = read_dht11_dat();
  DDRC |= _BV(DHT11_PIN);
  PORTC |= _BV(DHT11_PIN);
  byte dht11_check_sum = dht11_dat[0]+dht11_dat[1]+dht11_dat[2]+dht11_dat[3];// check check_sum
  if(dht11_dat[4]!= dht11_check_sum)
  {
    Serial.println("DHT11 checksum error");
  }
#if ECHO_TO_SERIAL
  Serial.print("Current Humdity = ");
  Serial.print(dht11_dat[0], DEC);
  Serial.print(".");
  Serial.print(dht11_dat[1], DEC);
  Serial.print("%  ");
  Serial.print("Temperature = ");
  Serial.print(dht11_dat[2], DEC);
  Serial.print(".");
  Serial.print(dht11_dat[3], DEC);
  Serial.println("C  ");
#endif

  lcd.setCursor(0,0);
  lcd.print("H ");
  lcd.print(dht11_dat[0], DEC);
  lcd.print(".");
  lcd.print(dht11_dat[1], DEC);
  lcd.print("% ");
  lcd.print("T ");
  lcd.print(dht11_dat[2], DEC);
  lcd.print(".");
  lcd.print(dht11_dat[3], DEC);
  lcd.print((char)223);
  lcd.print("C");
  
  logfile.print("Current Humdity = ");
  logfile.print(dht11_dat[0], DEC);
  logfile.print(".");
  logfile.print(dht11_dat[1], DEC);
  logfile.print("% ");
  logfile.print("Temperature = ");
  logfile.print(dht11_dat[2], DEC);
  logfile.print(".");
  logfile.print(dht11_dat[3], DEC);
  logfile.println("°C");
}
// end dht11

void loop() {
  lcd.clear();
  dht11();
  lm35();
  light();
  delay(LOOP_DELAY);
}

Leave a Reply