Photocell

A photocell is a sensor that measures the amount of light in the environment.

Some outdoor lights have photocells that are used to automatically turn the light on or off depending on whether it is dark or light outside.

Most smartphones and tablets have photocells that automatically change the brightness of their screens depending on the amount of ambient light detected.

The photocell has 2 metal legs. Line up the legs different numbered rows on the breadboard, and gently insert the photocell into the breadboard. (If you push too hard, you'll bend the legs. If that happens, just remove the photocell, carefully straighten the legs, and try again.) Then use 2 jumper wires and a resistor to connect the rows to the Photon.

NOTE: One of the photocell legs will have two connections: it should connect to an analog I/O pin through a jumper wire, and it should also connect to ground (GND) through a resistor. In the breadboard row, the jumper wire for the analog I/O pin should be placed between the photocell leg and the resistor leg. Look at the example wiring diagram below as a visual reference.

IMPORTANT: The photocell must connect to an analog I/O pin, such as: A0, A1, A2, A3, A4, A5.

Experiment 6 of the online SparkFun Photon Experiment Guide shows how to connect the photocell. Here is the connection diagram for Experiment 6 (ignore the wiring for the RHT03 humidity and temperature sensor):

Code for Photocell

Library

The photocell does not require any special code library.

Global Variables

In the global variables, you should declare which pin is being used as the photocell analog input pin. The example below declares a variable called "lightPin" (but you could use a different variable name).

// if necessary, change data pin number to match your wiring
int lightPin = A0;

You will probably also want to declare an integer variable to store the reading from the photocell. This will make it easier to do something based on the measurement. The example below declares a variable called "lightReading" (but you could use a different variable name).

// global variable
int lightReading;

setup( ) function

There isn't any code that you need to include in the setup() function for the photocell.

IMPORTANT: Do not set a pin mode for the photocell.

Measuring Amount of Light

You can measure the amount of light using an analogRead() statement. This code would be most likely placed within the loop() function or within a custom function.

lightReading = analogRead(lightPin);

// add code to do something based on value of lightReading

The measurement will be a value ranging from 0-4095.

When there is less light detected, the reading will be lower. When there is more light detected, the reading will be higher.

You will need to add code to do something with the light reading (such as: display it on the OLED screen, send the data to your web app, turn on a LED light if the reading is less than a certain value, etc.).

RECOMMENDATION: Depending on the specific purpose of the photocell in your device, you may need to gather some data under different conditions to see how dark or how bright the environment will actually be where your device will be used. This will help you decide which values to use in your code to make decisions about what the device should do.

Last updated