LedSensor Library Completed

While working on putting together some educational information on the basics of Arduino/SumoBots for the SPaRC MiniSumo Robot Competition, I remembered that a few years back I had read something about using LEDs as sensors. After looking up exactly how it worked, I began development of the LedSensor library for Arduino – a library for measuring light intensity using a regular LED. This library is licensed under a GPLv3 License and the source can be found at: https://github.com/tac0010/LedSensor

— Description —

LedSensor allows you to use a regular LED as a light sensor, by
taking advantage of some pretty cool LED phenomena. Basically,
LEDs have a small amount of capacitance that can be charge up,
and the discharge rate is proportional to the amount of light
reaching the LED. This rate can be measured in order to determine
the intensity of light shining on the LED. Different LEDs have
different responses to different wavelengths of light, where
usually the best response is achieved by measuring light of the
same wavelength as the LED produces.

Using an infrared (IR) LED, one can detect whether an object is
white or black, making it quite useful for line following and

— Usage —

The hardware circuit to use with LedSensor is relatively simple.
First, connect the anode(+) side of the LED to one of the
Arduino’s digital or analog pins. Then place a 100ohm (or similar)
resistor between the LED’s cathode(-) and another digital or
analog pin.

To initialize the sensor, one of the constructors must be called
once at the beginning of your code:

LedSensor(int pinP, int pinN);
LedSensor(int pinP, int pinN, unsigned long timeout);
LedSensor(int pinP, int pinN, unsigned long timeout, boolean resolution);

pinP: The Arduino pin to which the LED’s anode(+) is connected.
pinN: The Arduino pin to which the LED’s cathode(-) is connected.
timeout: Maximum time to wait for the sensor to register a measurement.
resolution: Choose between measuring milliseconds and microseconds. (Options: MILLI or MICRO)

After the sensor has been initialized, you can start reading the sensors
and make adjustments using the following functions:

void LedSensor::calibrate()
Reads the sensor ten times and finds the minimum and maximum values over the ten
readings. The max and min are used in order to use readCalibrated(), and
calibrate() should be run multiple times in order to get the full range of

unsigned int LedSensor::read()
Reads the raw sensor data in terms of milliseconds/microseconds (depending on the
set resolution).

unsigned int LedSensor::readCalibrated()
Reads the calibrated sensor data as a value between 0 and 1000 (regardless of
resolution). Requires that calibrate() has been run, or max and min values
have been manually set.

void LedSensor::setMin(unsigned int minValue)
Manually sets the minimum sensor value in terms of milliseconds/microseconds
(depending on the set resolution).

void LedSensor::setMax(unsigned int maxValue)
Manually sets the maximum sensor value in terms of milliseconds/microseconds
(depending on the set resolution).

void LedSensor::setResolution(boolean resolution)
Set the resolution of the sensor to MILLI (milliseconds) or MICRO (microsec).
If doing white/black line detection, it is a good idea to use MICRO, but
if searching for objects, it is usually better to use MILLI.

unsigned int LedSensor::getMin()
Returns the minimum sensor value in terms of milliseconds/microseconds
(depending on the set resolution).

unsigned int LedSensor::getMax()
Returns the minimum sensor value in terms of milliseconds/microseconds
(depending on the set resolution).

boolean LedSensor::getResolution()
Returns the currently set resolution of the sensor (MILLI or MICRO).

— Limitations —

Using LEDs as sensors can be a little slow compared to using phototransistors
or other forms of light detection. This is mainly a proof of concept, but it
can be useful when slow measurements are acceptable.

— Contribution —

I hope you can find this work useful, and if you would like to contribute to the developer of this utility, you may send any amount of Bitcoins to this address:


LedSensor Library Bitcoin Donation Address
LedSensor Library Bitcoin Donation Address

Any amount is appreciated! (more info on Bitcoins at http://bitcoin.org/)


One thought on “LedSensor Library Completed

  1. Hello very cool website!! Guy .. Excellent .. Superb .. I will bookmark your website and take the feeds alsoI’m satisfied to seek out so many helpful information right here in the post, we want develop extra strategies on this regard, thanks for sharing. . . . . .

Leave a Reply to REGPPNCGOGPPJVPSVMM Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>