Friday, February 7, 2014

Datalogger

Here I start with a simple project with my Arduino: a data logger. I want to monitor temperature, humidity and light and record all the data on SD card. To do that I need my Arduino Board, a photo or light sensitive resistor (it changes its resistance according to the light), a temperature and humidity sensor and an SD module (and of course breadboard and jumper wires).
The Arduino software (or IDE) is free and comes with many examples. You can also find many useful information on their website and the forum. You can buy Arduino boards and other components from Ebay and many electronics shops. Genuine boards and clones work exactly the same, but only buying genuine boards you support the developers who also provide free software. Genuine boards are marked "made in Italy" (not "design in Italy"). A reasonable price in Australia for a genuine Arduino UNO is about 32 $. I bought a lot of electronic components and sensors from CoreElectronics and I was happy with them, they have genuine boards and also sell on Ebay. If you are not in a hurry, you can buy the other components and sensors from China and Hong Kong on Ebay and save some money.
To learn how to program and wire them up, I found this series of tutorials very useful. If you need to start from the basic then this series of tutorials would be easier.
The light resistor is wired as a voltage divider in series with a fixed resistor. The Arduino reads the voltage across the photo resistor through the Analog input 0. This value will be between 0 and 5 volts (depending on the value of the fixed resistor, I’m using a 1.4 kΩ) but the value read by the Arduino will be between 0 and 1023, because the Analog to Digital Converter uses 10 bits (2^10 = 1024 values).
The temperature and humidity sensor is a cheap DHT11 sensor. It has an internal circuit able to read the analog voltages, calculate the right values and send a string with these values to the Arduino. The sketch or program in the Arduino must include the DHT11 library to be able to read this string.
The SD module is able to read and write data on a file .log or .txt, but only one file at a time. The communication used by the DHT11 is SPI (Serial Peripheral Interface), it needs 4 pins plus ground and VCC (5 volts). These 4 pins in the Arduino UNO are 10 (SDCS), 11 (MOSI), 12 (MISO) and 13 (SCK). The sketch must include the SD library in order to use the commands regarding the SD module.
To do the schematics I used Fritzing, an useful, easy and free software (you can draw PCB as well).


And this is how it looks in real life:

To write the sketch I’ve copied some parts of the Datalogger example sketch and DHT11 sketch:

#include <DHT11.h>
#include <SD.h>

int pin=3;
DHT11 dht11(pin); 

const int chipSelect = 4;

void setup()
{
  pinMode(10, OUTPUT);
  
  if (!SD.begin(chipSelect)) {
    return;
  }
}

void loop()
{
  int err;
  float temp, humi;
  if((err=dht11.read(humi, temp))==0)
  {
  int t,h;
  t = (int) temp;
  h = (int) humi;
  String dataString = "";
    dataString += "temperature:";
    dataString += String(t);
    dataString += ",";
    dataString += "humidity:";
    dataString += String(h);
    dataString += ",";
    dataString += "light:";
    dataString += String(analogRead(0));
        
  File dataFile = SD.open("datalog.log", FILE_WRITE);
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
  }
  }
  delay(30000);
}

I’ve tried many times with different SD cards and looks like some SD cards cannot be read by the SD module, even if they work fine in cameras and other devices. They have to be formatted as FAT (16 or 32), but even then, not all are readable. Anyway, I found one that works and I started recording.
This is how the data was recorded in the file, every 30 seconds (the board doesn't know what time it is, so you should mark the time when you start recording):

temperature:27,humidity:36,light:658
temperature:27,humidity:37,light:671
temperature:27,humidity:36,light:678
temperature:26,humidity:37,light:673
temperature:26,humidity:37,light:670

After few hours I plugged the SD card into my computer, imported the data in Excel and plotted a chart.

No comments:

Post a Comment