# Measuring Light with a BeagleBone Black

## Overview

In this tutorial, you will learn how to connect a photoresistor to a BeagleBone Black.

![](https://cdn-learn.adafruit.com/assets/assets/000/009/314/medium800/beaglebone_overview_web.jpg?1396887408)

Because the BBB runs Linux, there are many ways in which it can be programmed. In this tutorial we show how to read analog values with a photoresistor light sensor using Python.

# Measuring Light with a BeagleBone Black

## You Will Need

To try out this tutorial, you will need:

The photoresistor used has a dark resistance in excess of 200 kΩ and under bright light, the resistance falls to 1 or 2 kΩ. You can use other photoresistors, but you may need to change the value of the 10 kΩ resistor in order to get good resolution for your light measurements.

# Measuring Light with a BeagleBone Black

## Installing the Python Library

This tutorial uses Ångström Linux, the operating system that comes pre-installed on the BBB.  
  
Follow the instructions here, to install the Python IO BBIO library. [http://learn.adafruit.com/setting-up-io-python-library-on-beaglebone-black](http://learn.adafruit.com/setting-up-io-python-library-on-beaglebone-black "Link: http://learn.adafruit.com/setting-up-io-python-library-on-beaglebone-black")

# Measuring Light with a BeagleBone Black

## Wiring

Wire up the solderless breadboard using the header leads as shown below.

![](https://cdn-learn.adafruit.com/assets/assets/000/009/320/medium800/beaglebone_fritzing.png?1396887528)

Both the photoresistor and the resistor can be placed either way around. They are arranged in what is called a "voltage divider" arrangement.  
  
The orange lead to pin **P9.40** , which is also analog input 1 ( **AIN1** ). Note that only certain pins can be used as analog inputs (see [http://learn.adafruit.com/setting-up-io-python-library-on-beaglebone-black/adc](http://learn.adafruit.com/setting-up-io-python-library-on-beaglebone-black/adc "Link: http://learn.adafruit.com/setting-up-io-python-library-on-beaglebone-black/adc"))  
  
Connect the blue lead from pin **34** of **P9** to the bottom of the resistor and the red lead from pin 32 of P9 to the top connection of the photoresistor.  
  
The pins are numbered left to right, 1, 2 then on the next row down 3,4 etc. You can find out about all the pins available on the P8 and P9 connecters down each side of the BBB here: [http://stuffwemade.net/hwio/beaglebone-pin-reference/](http://stuffwemade.net/hwio/beaglebone-pin-reference/ "Link: http://stuffwemade.net/hwio/beaglebone-pin-reference/")

# Measuring Light with a BeagleBone Black

## Photoresistors

The photocell used is of a type called a photoresistor, sometimes called an LDR - **Light Dependent Resistor**. As the name suggests, these components act just like a resistor, except that the resistance changes in response to how much light is falling on them.  
  
The photoresistor used has a dark resistance in excess of 200 kΩ and under bright light, the resistance falls to 1 or 2 kΩ.  
  
To convert this varying value of resistance into something we can measure on BBB's analog input, it need to be converted into a voltage between 0 and 1.8V.  
  
The simplest way to do that is to combine it with a fixed resistor, in this case of 10 kΩ in an arrangement called a voltage divider.

![](https://cdn-learn.adafruit.com/assets/assets/000/009/321/medium800/beaglebone_voltage_divider.png?1396887564)

The voltage at the analog input will be pulled up towards 1.8V by the photoresistor and down towards 0V by the fixed resistor. Exactly who is willing this tug-or-war will depend on the resistance of the photoresistor.  
  
If the photoresistor is brightly illuminated, then its resistance will fall so it will pull the voltage up closer to 1.8V. If, on the other hand, the photoresistor is in the dark, the resistance will increase, so the fixed resistor will dominate and the analog input will be pulled close to 0V.  
  
The **VDD\_ADC1** pin ( **P9** , pin **32** ) is a steady 1.8V designed to be used to provide a reference voltage for situations like this.

# Measuring Light with a BeagleBone Black

## The Python Console

Before writing a Python program to measure and report the light level, we can try some experiments in the Python Console.  
  
To launch the Python Console type:

```auto
# python
Python 2.7.3 (default, Apr  3 2013, 21:37:23) 
[GCC 4.7.3 20130205 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
```

First, we need to import the library, so enter the command:

```auto
>>> import Adafruit_BBIO.ADC as ADC
```

Now enter the command below into the Python Console to setup the ADC (Analog to Digital Convertor).

```auto
>>> ADC.setup()
```

You can now read the value at the analog input (between 0 and 1) using the command below. Try it a few times while moving your hand closer to and further away from the photoresistor. The readings should change in response to your hand movements. Cover the photoresistor completely and the reading should drop almost to 0.

```auto
>>> ADC.read("P9_40")
0.54777777194976807
>>> ADC.read("P9_40")
0.55000001192092896
>>> ADC.read("P9_40")
0.55166667699813843
>>> ADC.read("P9_40")
0.39388889074325562
>>> 
```

Info: 

# Measuring Light with a BeagleBone Black

## Writing a Program

To automate this, we can write a short Python program to repeatedly display the analog reading (between 0 and 1) and the voltage that this corresponds to, printing values once per second.  
  
Exit the Python Console by typing:

```auto
>>> exit()
```

This should take you back to the Linux prompt.  
  
Enter the following command to create a new files called light.py

```auto
nano light.py
```

Now paste the code below into the editor window.

```auto
import Adafruit_BBIO.ADC as ADC
import time

sensor_pin = 'P9_40'

ADC.setup()

print('Reading\t\tVolts')

while True:
    reading = ADC.read(sensor_pin)
    volts = reading * 1.800
    print('%f\t%f' % (reading, volts))
    time.sleep(1)
```

![](https://cdn-learn.adafruit.com/assets/assets/000/009/322/medium800/beaglebone_nano.png?1396887600)

Save and exit the editor using CTRL-x and the Y to confirm.  
  
To start the program, enter the command:

```auto
python light.py
```

You will then see a series of readings.

```auto
Reading		Volts
0.000556	0.001000
0.000000	0.000000
0.026111	0.047000
0.448889	0.808000
0.449444	0.809000
0.452222	0.814000
0.730000	1.314000
0.738889	1.330000
0.722778	1.301000
0.726111	1.307000
0.445556	0.802000
0.401667	0.723000
0.717778	1.292000
```

When you want to stop the readings, use CTRL-c.  
  
The readings taken are not in any useful units of light intensity. Photoresistors are not carefully calibrated sensors. If you wanted to make a light meter, with absolute measurement of light intensity in meaningful units, you would need to create a lookup table that related readings with readings taken from a properly calibrated light meter.

Info: 

# Measuring Light with a BeagleBone Black

## Next Steps

Now that you can measure the light, you could do other things with it like using an 'if' command in the program to display a message when it gets dark.   
  
You could also use the project as a light logger, using Python to write the readings to a file, each accompanied by a time stamp. If the readings are written one per line, with a comma between the time and the reading, then you will be able to import it directly into a spreadsheet and produce charts from the data.  
  
You could also use other types of resistive sensor in place of the photoesistor, such as:

- Force sensitive resistor: [http://www.adafruit.com/products/166](http://www.adafruit.com/products/166)  
- Ribbon sensor: [http://www.adafruit.com/products/178](http://www.adafruit.com/products/178)  

  
**About the Author.**  
As well as contributing lots of tutorials about Raspberry Pi, Arduino and now BeagleBone Black, Simon Monk writes books about open source hardware. You will find his books for sale [here](http://www.adafruit.com/index.php?main_page=adasearch&q=simon+monk) at Adafruit.
## Featured Products

### BeagleBone Black - Rev B

[BeagleBone Black - Rev B](https://www.adafruit.com/product/1278)
**[Adafruit is no longer shipping the BeagleBone Black Rev B, it has been replaced with the Rev C as of 5/12/14](https://www.adafruit.com/products/1876) - the Rev C now has 4G flash and also comes with Debian, it also costs slightly more. There are no exchanges or...**

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/1278)
[Related Guides to the Product](https://learn.adafruit.com/products/1278/guides)
### Half Sized Premium Breadboard - 400 Tie Points

[Half Sized Premium Breadboard - 400 Tie Points](https://www.adafruit.com/product/64)
This is a cute, half-size breadboard with&nbsp;400 tie points, good for small projects. It's 3.25" x 2.2" / 8.3cm&nbsp;x 5.5cm&nbsp;with a standard double-strip in the middle and two power rails on both sides.&nbsp;You can pull the power rails off easily to make the breadboard as...

In Stock
[Buy Now](https://www.adafruit.com/product/64)
[Related Guides to the Product](https://learn.adafruit.com/products/64/guides)
### Adafruit Proto Plate for Beagle Bone & Beagle Bone Black

[Adafruit Proto Plate for Beagle Bone & Beagle Bone Black](https://www.adafruit.com/product/702)
Ready to prototype your next [Beagle Bone](http://www.adafruit.com/products/1278) project? You'll certainly want this handy Proto-Plate! Not only is it really good looking, its also very useful, holding your [Beagle Bone or Beagle...](http://www.adafruit.com/products/1278)

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/702)
[Related Guides to the Product](https://learn.adafruit.com/products/702/guides)
### Premium Male/Male Jumper Wires - 40 x 3" (75mm)

[Premium Male/Male Jumper Wires - 40 x 3" (75mm)](https://www.adafruit.com/product/759)
Handy for making wire harnesses or jumpering between headers on PCB's. These premium jumper wires are 3" (75mm) long and come in a 'strip' of 40 (4 pieces of each of ten rainbow colors). They have 0.1" male header contacts on either end and fit cleanly next to each other...

In Stock
[Buy Now](https://www.adafruit.com/product/759)
[Related Guides to the Product](https://learn.adafruit.com/products/759/guides)
### Photo cell (CdS photoresistor)

[Photo cell (CdS photoresistor)](https://www.adafruit.com/product/161)
CdS cells are little light sensors. As the squiggly face is exposed to more light, the resistance goes down. When it's light, the resistance is about ~1KΩ, when dark it goes up to ~10KΩ.

To use, connect one side of the photocell (either one, it's symmetric) to power...

In Stock
[Buy Now](https://www.adafruit.com/product/161)
[Related Guides to the Product](https://learn.adafruit.com/products/161/guides)
### Round Force-Sensitive Resistor (FSR) -  0.3 ~ 10 Newton Force

[Round Force-Sensitive Resistor (FSR) -  0.3 ~ 10 Newton Force](https://www.adafruit.com/product/166)
FSRs are sensors that allow you to detect physical pressure, squeezing and weight. They are simple to use and low cost. This sensor is an Alpha MF01A-N-221-A01&nbsp;with 1/2 diameter sensing region.

We used to stock the Interlink model 402 FSR - the Alpha version is almost half the price...

In Stock
[Buy Now](https://www.adafruit.com/product/166)
[Related Guides to the Product](https://learn.adafruit.com/products/166/guides)
### Extra-long force-sensitive resistor (FSR)

[Extra-long force-sensitive resistor (FSR)](https://www.adafruit.com/product/1071)
FSRs are sensors that allow you to detect physical pressure, squeezing and weight. They are simple to use and low cost. This sensor is a Interlink model 408 FSR with a massive 1/4-inch x 24-inch sensing region. You can press anywhere along the strip and the pressure will be recognized. Note...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/1071)
[Related Guides to the Product](https://learn.adafruit.com/products/1071/guides)
### Square Force-Sensitive Resistor (FSR)

[Square Force-Sensitive Resistor (FSR)](https://www.adafruit.com/product/1075)
FSRs are sensors that allow you to detect physical pressure, squeezing and weight. They are simple to use and low cost. This sensor is an Alpha MF02A-N-221-A01 FSR with a 38mm square sensing region. Note that this sensor can't detect _where_ on the square you pressed (for that, <a...></a...>

Out of Stock
[Buy Now](https://www.adafruit.com/product/1075)
[Related Guides to the Product](https://learn.adafruit.com/products/1075/guides)

## Related Guides

- [Setting up IO Python Library on BeagleBone Black](https://learn.adafruit.com/setting-up-io-python-library-on-beaglebone-black.md)
- [SSH to BeagleBone Black over USB](https://learn.adafruit.com/ssh-to-beaglebone-black-over-usb.md)
- [Setting up WiFi with BeagleBone Black](https://learn.adafruit.com/setting-up-wifi-with-beaglebone-black.md)
- [MPR121 Capacitive Touch Sensor on Raspberry Pi & BeagleBone Black](https://learn.adafruit.com/mpr121-capacitive-touch-sensor-on-raspberry-pi-and-beaglebone-black.md)
- [Using the BMP085/180 with Raspberry Pi or Beaglebone Black](https://learn.adafruit.com/using-the-bmp085-with-raspberry-pi.md)
- [Blinking an LED with BeagleBone Black](https://learn.adafruit.com/blinking-an-led-with-beaglebone-black.md)
- [BeagleBone](https://learn.adafruit.com/beaglebone.md)
- [MCP9808 Temperature Sensor Python Library](https://learn.adafruit.com/mcp9808-temperature-sensor-python-library.md)
- [Bonjour (Zeroconf) Networking for Windows and Linux](https://learn.adafruit.com/bonjour-zeroconf-networking-for-windows-and-linux.md)
- [Adafruit WebIDE](https://learn.adafruit.com/webide.md)
- [Embedded Linux Board Comparison](https://learn.adafruit.com/embedded-linux-board-comparison.md)
- [RePaper eInk Development Board for ARM + GNU/Linux](https://learn.adafruit.com/repaper-eink-development-board-arm-linux-raspberry-pi-beagle-bone-black.md)
- [DHT Humidity Sensing on Raspberry Pi or Beaglebone Black with GDocs Logging](https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging.md)
- [User-space SPI TFT Python Library - ILI9341](https://learn.adafruit.com/user-space-spi-tft-python-library-ili9341-2-8.md)
- [BNO055 Absolute Orientation Sensor with Raspberry Pi & BeagleBone Black](https://learn.adafruit.com/bno055-absolute-orientation-sensor-with-raspberry-pi-and-beaglebone-black.md)
