Arduino IDE: RBD_Light Example Blink Without DelayNeed help writing code? You can hire Robots + Big Data to write Arduino software for your project! Click here to get started...
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++
#include <RBD_LightSensor.h>

RBD::LightSensor light_sensor(A0);

void setup() {
  Serial.begin(115200);
}

void loop() {
  Serial.println(light_sensor.getPercentValue());
}
Public Methods

RBD::LightSensor

RBD::LightSensor constructor(pin)

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

EXAMPLE SKETCH
RBD::LightSensor light_sensor(A0);

void setup() {
  ...
}
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
void loop() {
  light_sensor.getValue();
}
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
void loop() {
  light_sensor.getRawValue();
}
light_sensor
.getPercentValue()

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

EXAMPLE SKETCH
void loop() {
  light_sensor.getPercentValue();
}
light_sensor
.getInverseValue()

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

EXAMPLE SKETCH
void loop() {
  light_sensor.getInverseValue();
}
light_sensor
.getInversePercentValue()

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

EXAMPLE SKETCH
void loop() {
  light_sensor.getInversePercentValue();
}
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
void setup() {
  light_sensor.setFloor(10);
}
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
void setup() {
  light_sensor.setCeiling(999);
}
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
void setup() {
  light_sensor.resetFloor();
}
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
void setup() {
  light_sensor.resetCeiling();
}