User Tools

Site Tools


oak:tutorials:photocell

This is an old revision of the document!


In this lesson, we will connect a photoresistor to our Oak. There some great background information and an Arduino tutorial on Adafruit, so please do take a look there for further learning. In essence a photoresistor is simply a potentiometer where light is acting to turn the knob. When there is a lot of light, the resistance drops; with low light, the resistance is higher.

Requirements

This tutorial will use the following (in addition to an Oak):

Connecting the circuit

Here is the wiring connection we'll use:

If you prefer a real life example, here is the setup:

photocell-wiring.jpg

We connect one leg of the photocell directly to Vcc, and the other leg to two different “outlets:” one to analog pin, A0, and the other to ground through the 10k resistor. This creates what is known as a voltage divider, which is required for measuring small changes in voltage. There's a helpful writeup of why this is required on the Electronics StackExchange Q&A site here. The Adafruit tutorial also features a discussion on adjusting the resistor value used to target different ranges of measurement in the section on using a photoresistor.

The code

For an introduction to these sensors, we'll read the light value to get a handle on the expected ranges that will be encountered, and then pick a cutoff value at which point to turn on the built-in LED.

Reading the photocell is straightforward:

// variable to store our reading
int light;

// the setup() loop sets pinModes, starts things like Serial,
// and is run once when the Oak powers on
void setup()
{                

  // use the on-board LED to illuminate when the button is pressed
  pinMode(1, OUTPUT);
  pinMode(1, LOW);
  
  // initialize the analog pin as an input
  pinMode(A0, INPUT);

  Serial.begin(9600);

}


// the loop() routine runs over and over again
void loop()
{

  // take a reading
  light = analogRead(A0);
  
  // print it out
  Serial.println(light);

  // a short delay as we don't need readings every millisecond
  delay(250);

}

The code above assumes you will use a USB UART or Arduino to read the data via serial, however you are free to access it through the Particle.variable() if you prefer. Using serial, the output will look like this as you cover and uncover the sensor:

764
737
620
421
313
246
200
153
157
165
408
630
491
235
153
163
763
765

Based on these readings, pick a value you want to consider “dark enough;” I'll use 250. If we add some code to our sketch, we can automatically turn on the on-board LED when the sensor is in low light. This is just the modified loop() section; include everything else from above in the full sketch.

// the loop() routine runs over and over again
void loop()
{

  // take a reading
  light = analogRead(A0);
  
  // print it out
  Serial.println(light);

  // turn on/off the LED based on light reading
  if(light < 250) { digitalWrite(1, HIGH); }
  if(light >= 250) {digitalWrite(1, LOW); }
  
  // a short delay as we don't need readings every millisecond
  delay(250);

}

Adjust the cutoff values as desired; with this code added, we get a nice effect like this (you may need to click and open in a separate tab to see the animation):

So, what could you use this for? That's up to you, but you now have a new sensor in your toolbox for future projects. Here's some [perhaps silly] ideas to get you going:

  • an automated night light
  • reminder to turn off your outside flood lights in the morning
  • track the sunset/sunrise time each day
  • auto close your curtains/blinds when it's bright
oak/tutorials/photocell.1458525875.txt.gz · Last modified: 2016/03/20 19:04 by jwhendy