CxD Archive
Internet of Things Project
Internet of Things Project
  • IoT Project Background
  • IoT Project Challenge
  • IoT Project Outline
    • 1-1 Smartphone Research
    • 1-2 Smart Thermostat Research
    • 1-3 Smart Device Research
    • 1-4 Introduction to Photon Kit
    • 1-5 Build Smart Lightbulb
    • 1-6 Generate Project Ideas
    • 1-7 Build Smart Thermostat
    • 2-1 Evaluate Project Ideas
    • 2-2 Plan User Research
    • 2-3 Compile Research Data
    • 2-4 Construct Personas
    • 2-5 Construct Journey Map
    • 2-6 Define Value Proposition
    • 2-7 Define Design Requirements
    • 3-1 Generate Solution Ideas
    • 3-2 Evaluate and Refine Solution Ideas
    • 3-3 Create System Model of Solution
    • 3-4 Create Wireflow for Web App
    • 3-5 Create Storyboard for Solution
    • 3-6 Evaluate Solution Design
    • 3-7 Present Project Proposal
    • 4-1 Track Progress and Issues
    • 4-2 Build Smart Device
    • 4-3 Create Photon App
    • 4-4 Create Web App
    • 4-5 Conduct Integration Testing
    • 4-6 Create Product Demo Video
    • 4-7 Create Product Marketing Website
    • 5-1 Evaluate Smart Device and Web App
    • 5-2 Evaluate Product Marketing Website
    • 5-3 Improve Solution Based on Evaluations
    • 6-1 Create Project Poster
    • 6-2 Present Project to Public
    • 6-3 Write Personal Reflection
  • References for Wiring & Coding
    • Creating Circuits
    • Wiring Programming Language
    • Particle Cloud
    • Hello World App for Photon
    • LED Light
    • Push Button
    • Micro OLED Display
    • Humidity and Temp Sensor
    • Speaker
    • Servo Motor
    • Motion Sensor
    • Magnetic Switch
    • Potentiometer
    • Photocell
    • Moisture Sensor
    • Accelerometer
    • RFID Reader
    • Fingerprint Scanner
    • GPS Receiver
    • Camera
    • Microphone
    • Force Sensitive Resistor
    • Keypad
    • Continuous Rotation Servo Motor
  • Glossary
  • Updated Project Guidebook
  • Updated Code Guidebok
Powered by GitBook
On this page
  • Code for Photocell
  • Library
  • Global Variables
  • setup( ) function
  • Measuring Amount of Light
Export as PDF
  1. References for Wiring & Coding

Photocell

PreviousPotentiometerNextMoisture Sensor

Last updated 6 years ago

A 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.

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.

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.

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):

Experiment 6
photocell
Photocell