> For the complete documentation index, see [llms.txt](https://docs.launchboard.app/cxd/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.launchboard.app/cxd/internet-of-things-project/references-for-wiring-and-coding/photocell.md).

# Photocell

A [photocell](https://www.sparkfun.com/products/9088) is a sensor that measures the amount of light in the environment.

![Photocell](https://576155698-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LDgX-RdB-DPZBPuXzJ1%2F-LDgX75Qrx69yS4tZ2_Z%2F-LDgXGYyzB9Bb9BC3Csb%2Fphotocell.jpg?generation=1527608056941980\&alt=media)

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.

| Photocell              | Photon Pin                                     |
| ---------------------- | ---------------------------------------------- |
| First leg (either one) | 3.3V                                           |
| Second leg             | (1) any analog I/O pin, (2) GND using resistor |

**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](https://learn.sparkfun.com/tutorials/sparkfun-inventors-kit-for-photon-experiment-guide/experiment-6-environment-monitor) 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):

![](https://576155698-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LDgX-RdB-DPZBPuXzJ1%2F-LDgX75Qrx69yS4tZ2_Z%2F-LDgXGZ4l4dkdz6jrFKM%2Fexperiment-6.jpg?generation=1527608056997310\&alt=media)

## 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).

```java
// 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).

```java
// 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.

```java
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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.launchboard.app/cxd/internet-of-things-project/references-for-wiring-and-coding/photocell.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
