Arduino

Light SensorAPI DOCUMENTATION

Read and calibrate photoresistors.

See Example  Public Methods  Release Notes
Overview

Read and calibrate photoresistors; also known as light-dependent resistors (LDR). Calibrate light threshold levels with setFloor() and setCeiling(), then use getValue() or getPercentValue() to read the value.

Example Wiring
  1. Connect one leg of a photoresistor to the Arduino analog pin A0
  2. Connect a 10KΩ resistor between analog pin A0 and ground
  3. Connect the other leg of the photoresistor to Arduino +5v pin
  4. Load the Example Sketch onto the Arduino
  5. Open a serial connection at 115200 baud
  6. Watch a constant stream of the light percentage

A photoresistor wired to an Arduino Micro pin A0 with a 10k pull-down resistor.

Example SketchArduinoC++
Public Methods

RBD::LightSensor constructor(pin)

Create a new sensor and pass in the Arduino pin number.

EXAMPLE SKETCH
light_sensor
.getValue()

Returns an integer from 0 - 1023 for the current light level adjusted for the setFloor() and setCeiling() values. If the floor or ceiling are not set, this method will return getRawValue().

EXAMPLE SKETCH
light_sensor
.getRawValue()

Returns an integer from 0 - 1023 for the current light level reading from the sensor. Use this method to calibrate setFloor() and setCeiling().

EXAMPLE SKETCH
light_sensor
.getPercentValue()

Returns an integer from 0 - 100 for the current light percentage.

EXAMPLE SKETCH
light_sensor
.getInverseValue()

Returns an integer from 1023 - 0 for the opposite of the current light level.

EXAMPLE SKETCH
light_sensor
.getInversePercentValue()

Returns an integer from 100 - 0 for the opposite of the current light percentage.

EXAMPLE SKETCH
light_sensor
.setFloor(value)

Provide an integer from 0 - 1023 to calibrate the sensor with a lower bounds of light detection. This will adjust the scale for all methods that return a value in this library, but will not adjust their documented output range. Calibrate the floor with help from getRawValue().

EXAMPLE SKETCH
light_sensor
.setCeiling(value)

Provide an integer from 0 - 1023 to calibrate the sensor with an upper bounds of light detection. This will adjust the scale for all methods that return a value in this library, but will not adjust their documented output range. Calibrate the ceiling with help from getRawValue().

EXAMPLE SKETCH
light_sensor
.resetFloor()

Change the setFloor() value back to 0, which also resets the lower bounds of the scale for all methods that return a value in this library.

EXAMPLE SKETCH
light_sensor
.resetCeiling()

Change the setCeiling() value back to 1023, which also resets the upper bounds of the scale for all methods that return a value in this library.

EXAMPLE SKETCH