Wednesday, 19 July 2017

Working light sensor


I have managed to get my light sensor working now, and plotting light intensity on the computer screen. Below is a video of the light sensor working. When I hold a card reader over the light sensor to put a shadow over it, the graph of light intensity shows that the drop in light intensity is detected.

video

This is the hardware setup:


It is very simple. It is just an Arduino computer with a Grove Base shield on top. The grove light sensor is plugged into the A0 port of the base shield.


Here is a photo of the code and the monitor:


Below is the code that I used. It came from a DIY Hacking page by Arvind Sanjeev.


int sensorPin = A0; // select the input pin for LDR
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
Serial.begin(9600); //sets serial port for communication
}
void loop() {
sensorValue = analogRead(sensorPin); // read the value from the sensor
Serial.println(sensorValue); //prints the values coming from the sensor on the screen
delay(100);
}


--------------------------------------------

The next thing that I would like to do is to include an ethernet shield on top so that I can store the Arduino on a remote location with the light sensor outside, and so that the light information will be fed back to my PC like a weather station. I'm going to start by trying to write the values as comma-delimited text to the SD card on the ethernet shield. 

I'm taking the code that I have, and trying to incorporate the additional commands from the Arduino page on writing to SD. I am also following the Arduino instructions on concatenating strings. 




This is the code I have so far and it compiles and runs. It writes the numerical data on light intensity to the SD card. 



#include <SPI.h>
#include <SD.h>

const int chipSelect = 4;

int sensorPin = A0; // select the input pin for LDR
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
  Serial.begin(9600); //sets serial port for communication
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
}
void loop() {
  // make a string for assembling the data to log:
  String dataString = "";
  dataString += ", ";
  sensorValue = analogRead(sensorPin); // read the value from the sensor
  dataString += sensorValue;


  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }

  sensorValue = analogRead(sensorPin); // read the value from the sensor
  Serial.println(sensorValue); //prints the values coming from the sensor on the screen
  delay(100);
}


--------------------------------------------

The next thing I have done it to make the Arduino write the light intensity value to the LCD so that the system can be used as a hand-held light meter in the garden.

Here is a video of it working:

video

I got the code for the LCD from the Seeed-Studio section of GitHub


Here is my current total program:




#include <SPI.h>
#include <SD.h>


#include <Wire.h>
#include "rgb_lcd.h"

rgb_lcd lcd;                                           

const int colorR = 0;
const int colorG = 0;
const int colorB = 0;

const int chipSelect = 4;

int sensorPin = A0; // select the input pin for LDR
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
  Serial.begin(9600); //sets serial port for communication
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");

  // set up the LCD's number of columns and rows:
      lcd.begin(16, 2);
     
     lcd.setRGB(colorR, colorG, colorB);
     
      // Print a message to the LCD.
      lcd.print("Light intensity is");
  
     delay(1000);
}
void loop() {
  // make a string for assembling the data to log:
  String dataString = "";
  sensorValue = analogRead(sensorPin); // read the value from the sensor
  dataString += sensorValue;


  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }

  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }

  sensorValue = analogRead(sensorPin); // read the value from the sensor
  Serial.println(sensorValue); //prints the values coming from the sensor on the screen
  delay(100);
  
     // For LCD: set the cursor to column 0, line 1
     // (note: line 1 is the second row, since counting begins with 0):
     lcd.setCursor(0, 1);
     // print the number of seconds since reset:
     lcd.print(dataString);

     delay(100);
}


Here is the whole thing working together. No idea why the camera can't focus when taking video.


video

I now need to test whether the light sensor has enough range to measure everything from darkness to the brightest sunlight. It seems to max out in bright torch light which is a bit odd. I thought it would have something above that.

I did some tests and the range didn't seem to be sufficient so I am going to strip out all unnecessary stuff and see if I can increase the range.

This is continued at Optimizing light sensor.