Sensing Light Levels with a Photocell

Table of Contents

  1. Introduction
  2. Assemble the Parts
  3. Configure the Radio
  4. Wire up the Circuit
  5. Use it!

1) Introduction

Photocells are a popular component in electronics projects; they let you sense and take action based on the amount of light hitting the cell. Not only are they great for sensing the ambient light to tell if it's day or night, but they can also be used as an interface input. That is to say, if someone waves their hand over the sensor, the shadow of the user's hand can actuate some response. Best of all, these sensors are cheap—at about a dollar a piece, they're a great component to keep stocked in your bins.

The resistance across the two leads of the cell varies according to the amount of light hitting the cell. The more light that hits the cell, the less the resistance is. And, of course, the less light means greater resistance. We're going to use the XBee 802.15.4 radio's analog-to-digital converter (ADC) to send these various light levels out into the airwaves as digital data.

2) Assemble the Parts

To hook up a photocell to an XBee, you'll need:

  • XBee 802.15.4 Radio
  • Photocell - available from Adafruit, Sparkfun, and many others.
  • Resistor - The resistance will depend on your photocell (it should be on the same order of magnitude as the resistance of your photocell). We're using a 1K resistor because our photocell has a resistance between 1K and 10K ohms.
  • Breadboard
  • Jumper cables
  • XBee Breakout Board - for your circuit.
  • XBee Explorer USB - for programming your radio.
  • A DC power source, 2.8 - 3.3V - We'll be using two C batteries in a battery holder from Radio Shack.

3) Configure the Radio

If you're not familiar with configuring radios using AT commands, review the steps in the Basic XBee 802.15.4 Chat tutorial, which walks you through configuring CoolTerm to program the radios.

  • Insert the XBee into the Explorer USB and connect it to your computer.
  • Launch CoolTerm and connect to the XBee.
  • Here are the commands we're going to use to configure the radio:
Function Command Parameter
Reset ATRE N/A (resets the radio to its factory settings)
PAN ID ATID 3001 (any address from 0 to FFFE will do)
MY Address ATMY 1
Destination address high ATDH 0 (indicates a 16-bit address)
Destination address low ATDL 2 (the address of the receiving radio)
Pin 0 I/O Configuration ATD0 2 (analog input)
Sample Rate ATIR 14 (20 ms, 14 in hexadecimal = 20 in decimal)
Write to memory ATWR N/A (save the settings to flash memory)
  • Here's what your terminal session might look like. The user input is in bold:
ATID 3001
ATD0 2

Note: Remember to issue the ATWR command when you're done so that the settings are saved in the radio's flash memory. If you don't issue this command, the radio will revert to its old settings when it loses power.

4) Wire up the Circuit

  • Place the XBee adapter into the breadboard and wire up the power buses to each other.

  • Connect the VCC terminal of the XBee adapter to the positive rail and the ground terminal to the negative rail.

  • Connect the VREF terminal of the XBee adapter to the positive rail. This will give the XBee a reference for what voltage an analog sensor is working with.

  • Connect one lead of the photocell to the positive voltage rail, and the other lead to pin 20 of the XBee. It doesn't matter which sides of the photocell you choose, it's not polarized. Place the resistor between ground and the photocell lead that connects to the XBee. This creates a simple voltage divider circuit.

  • This is what the voltage divider looks like as a schematic diagram.

  • Here's how your circuit might look as laid out on a breadboard.

  • Insert the XBee into the breakout board and connect the battery pack.

5) Use it!

Now that you know how to connect a photocell to the analog input of an XBee radio, take a look at our output tutorials for how you can use the light level information. Just be sure that the the transmitter's address is set as the receiver's I/O input address setting (ATIA). The video above shows a wireless photocell connected to a wireless panel meter.

Tags: 802.15.4