Skip to main content

Simple Light Sensor - Detecting and measuring light with regular light sensor (example)

This page contains a simple example with function documentation on how to detect and measure intensity of present light with the sensor.


initialization

To use the sensor, first include the required library, create the sensor object and SimpleFireSensor object, and initialize it in the setup() function. You can use the return value of begin() to check if everything is connected correctly.

#include "Simple-light-sensor-easyC-SOLDERED.h"

#define ANALOG_PIN
#define DIGITAL_PIN

SimpleLightSensor sensor(ANALOG_PIN);

void setup(){
sensor.begin();
}
//...

SimpleLightSensor sensor()

Creates SimpleLightSensor object

Returns value: none

Function parameters:

TypeNameDescription
uint16_tpinAnalog pin number for data communication

sensor.begin()

Initializes the sensor.

Returns value: Returns true if initialization is successful, false otherwise.


Measuring with both digital and analog output

In this library, there are two options for displaying the analog value: for a percentage value, call the getResistance() function, and for the raw value, call the getLux() function.

#include "Simple-light-sensor-easyC-SOLDERED.h"

#define ANALOG_PIN 14
#define DIGITAL_PIN 15

SimpleLightSensor sensor(ANALOG_PIN);

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

void loop(){
Serial.print("Resistance of a LDR: "); // Print information message
Serial.print(sensor.getResistance()); // Prints percent value of light sensor
Serial.println(" Ohms."); // Print information message

Serial.print("Light intensity: "); // Print information message
Serial.print(sensor.getLux()); // Prints raw value of light sensor
Serial.println(" lux."); // Print information message

// You can adjust treshold light intesity using potentiometer on breakout board
Serial.println(digitalRead(DIGITAL_PIN) ? "Treshold light intesity is past." : "Treshold intensity is not past.");

// Wait a bit before next reading
delay(500);
}

sensor.getResistance()

Returns the measurement in percentage.

Returns value: Returns float representation of fire chance percentage.

sensor.getLux()

Returns the raw ADC value.

Returns value: Returns integer representation of fire value

Sensor when light is not present
Sensor when light is not present
Serial Monitor output
Serial Monitor output
Sensor when light is present
Sensor when light is present
Serial Monitor output
Serial Monitor output

Full example

Try all of the functions mentioned above in the full example below:

Read_values_native.ino

Example for using the digital and analog read functions for Simple light sensor.