header
Connect Light Sensor

Along with our temperature sensor, we can hook up other types of sensors to measure different things. Here, we're going to hook up a sensor that measures the amount of light hitting it. With the temperature sensor, we were reading degrees of celsius or fahrenheit. With a light sensor, we are going to read lux, which is a measurement of how much light is hitting an object.

There's one really big difference between our temperature sensors and our light sensors and that has to do with accuracy. If everyone hooks up their temperature sensors and is working in the same room, they should all read just about the same value. This is not the case with our light sensor, where everyone could have very different values. That's ok becausethese light sensors are not designed for everyone to have the same reading. These sensors are instead used to determine if a light is on or off, or is there something blocking the sensor. In these cases, we don't care about the exact reading, but the change in light levels. And for these types of events, our light sensor will work perfectly.

For our solar oven, we're going to use the light sensor to determine when we need to reposition the oven to catch the maximum amount of light. So, we just want to know when the amount of light hitting the sensor changes by some amount.

Note that you can bend the leads on the light sensor, but try not to bend them right by the sensor because you could break the leads right off.

connect light sensor
// include the library code
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12,11,5,4,3,2);

// initialize our variables
int sensorPin = 0;
int photocellPin = 1;
int tempC, tempF, lightReading;

void setup() {
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
}

void loop() {  
  tempC = get_temperature(sensorPin);
  tempF = celsius_to_fahrenheit(tempC);
  lightReading = get_light_reading(photocellPin);
  
  lcd.setCursor(0,0);
  lcd.print(tempF); lcd.print(" "); lcd.print((char)223); lcd.print("F");

  lcd.setCursor(0,1);
  lcd.print("Light: "); lcd.print(lightReading); lcd.print(" lux");
  delay(200);
}

int get_light_reading(int pin) {
  int photocellReading = analogRead(pin);
  return photocellReading;
}

int get_temperature(int pin) {
  // We need to tell the function which pin the sensor is hooked up to. We're using
  // the variable pin for that above
  
  // Read the value on that pin
  int temperature = analogRead(pin);
  
  // Calculate the temperature based on the reading and send that value back
  float voltage = temperature * 5.0;
  voltage = voltage / 1024.0;
  return ((voltage - 0.5) * 100);
}

int celsius_to_fahrenheit(int temp) {
  return (temp * 9 / 5) + 32;
}


When this code is uploaded, your lcd screen should show the temperature on the first line and a light reading on the second. Try covering the light sensor with your hand and see if the values change.

Next: Hook up a speaker