stratigrafia

ADDING LIGHT TO THE TEMPERATURE LOGGER

08/20/2017

During tomorrow’s eclipse, air temperature is supposed to drop, so what’s a scientist supposed to do? Get some data, of course! I’ll modify my Arduino temperature logger so that I can log the light level and the temperature simultaneously.

Parts and wiring

Adding the circuitry for measuring light is cheap and easy. For new parts, you’ll need:

The photoresistor is cheap, only $0.95 from Adafruit.

Wiring it is easy. Connect one end (either end) of the photoresistor to 5V coming from the Arduino. Connect the other end to the 1 K pull-down resistor, and connect the other end of the 1 K resistor to ground on the Arduino. Last connect the ground leg of the photoresistor to the Analog 0 pin of the Arduino. The complete circuit (most of which is the temperature logger) looks like this:


New code

The additional code is also minimal. One line needs to be added to the top to declare the pin number for the photocell:

int photocellPin = 0

Inside the loop() function, one line needs to be added to read the photocell:

int photocellReading = analogRead(photocellPin);

Two lines need to be modified inside the loop() function so that the photocell reading can be saved and shown for logging

writeDataToCard(timeNow, temperature, photocellReading);
writeDataToSerial(timeNow, temperature, photocellReading);

The functions writeDataToCard() and writeDataToSerial() need to be slightly modified so that they record light as well as temperature:

void writeDataToCard(uint32_t time, float temperature, int light) {
  myFile = SD.open("temper.txt", FILE_WRITE);
  myFile.print(time);
  myFile.print(", ");
  myFile.print(temperature);
  myFile.print(", ");
  myFile.print(light);
  myFile.print("\n");
  myFile.close();
}
  
void writeDataToSerial(uint32_t time, float temperature, int light) {
  Serial.print(time);
  Serial.print(", ");
  Serial.print(temperature);
  Serial.print(", ");
  Serial.println(light);
}

Make these changes, install the code onto the Arduino, and logging begins, with a reading every 5 seconds. The values for light range from 0 (no light) to 1024 (maximum light). By changing the 1 K pull-down resistor, the light sensitivity can be adjusted to favor bright light or low light conditions. This tutorial at Adafruit has more instructions, plus an explanation of how the photoresistor works.

Complete Code

#include <SD.h>
#include <OneWire.h>
#include <SPI.h>
 
File myFile;
int sdCardPin = 10;
int temperatureProbePin = 8;
int greenLEDPin = 3;
int redLEDPin = 2;
int pauseBetweenReadings = 5000; // in milliseconds
int timeForLitLED = 1000;
uint32_t startTime;
int probeErrorCode = -1000;
int photocellPin = 0;   // Analog Pin 0
 
OneWire ds(temperatureProbePin);
 
void setup() {
  pinMode(sdCardPin, OUTPUT);
  pinMode(greenLEDPin, OUTPUT);
  pinMode(redLEDPin, OUTPUT);
  
  startTime = millis();
  Serial.begin(9600);
  
  // SET UP SD CARD FOR WRITING
  if (!SD.begin(sdCardPin)) {
    Serial.println("initialization failed\n");
    return;
  } else {
    Serial.println("SD card initialized\n");
    myFile = SD.open("temper.txt", FILE_WRITE);    // open file for writing
    if (myFile) {  // if file can be opened, write to it
      myFile.print("time, temperature, light\n");
      myFile.close();
      Serial.print("temper.txt file opened for writing\n");
      Serial.print("time, temperature, light\n");
    } else {   // if file can’t be opened, show an error
      Serial.println("ERROR: not able to open temper.txt\n");
    }
  }
}
 
void loop() {
  uint32_t timeNow = (millis() - startTime)/1000;
  float temperature = getTemp();
  int photocellReading = analogRead(photocellPin);
  
  if (temperature != probeErrorCode) {
    writeDataToCard(timeNow, temperature, photocellReading);
    writeDataToSerial(timeNow, temperature, photocellReading);
    digitalWrite(greenLEDPin, HIGH);
  } else {
    digitalWrite(redLEDPin, HIGH);
  }
  
  delay(timeForLitLED);
  digitalWrite(redLEDPin, LOW);  
  digitalWrite(greenLEDPin, LOW);
 
  delay(pauseBetweenReadings-timeForLitLED);
}
 
float getTemp() {
  byte data[12];
  byte addr[8];
 
  if (!ds.search(addr)) {
    //no more sensors on chain, reset search
    ds.reset_search();
    return -1000;
  }
 
  if (OneWire::crc8( addr, 7) != addr[7]) {
    Serial.print("CRC is not valid!\n");
    return -1000;
  }
 
  if (addr[0] != 0x10 && addr[0] != 0x28) {
    Serial.print("Device is not recognized\n");
    return -1000;
  }
 
  ds.reset();
  ds.select(addr);
  ds.write(0x44,1);
 
  byte present = ds.reset();
  ds.select(addr);
  ds.write(0xBE);
 
  for (int i = 0; i < 9; i++) {
    data[i] = ds.read();
  }
 
  ds.reset_search();
  int LowByte = data[0];
  int HighByte = data[1];
  float TRead = (HighByte << 8) + LowByte;
  float Temperature = TRead / 16;
  return Temperature;
}
 
void writeDataToCard(uint32_t time, float temperature, int light) {
  myFile = SD.open("temper.txt", FILE_WRITE);
  myFile.print(time);
  myFile.print(", ");
  myFile.print(temperature);
  myFile.print(", ");
  myFile.print(light);
  myFile.print("\n");
  myFile.close();
}
 
void writeDataToSerial(uint32_t time, float temperature, int light) {
  Serial.print(time);
  Serial.print(", ");
  Serial.print(temperature);
  Serial.print(", ");
  Serial.println(light);
}

 

Home