An LDR (light dependent resistor) sensor module outputs an analog voltage that changes with ambient light intensity. Connect the module’s power (+) and ground (-) to your board, and read the light-dependent signal from the S analog pin using an Arduino/ESP32 analog input.
| Pin | Signal | Description |
|---|---|---|
S |
analog | Signal |
+ |
power | VCC |
- |
ground | GND |
| From | To | Wire |
|---|---|---|
board:A0 |
x1:S |
green |
board:5V |
x1:+ |
red |
board:GND |
x1:- |
black |
This example was written and compile-checked for the arduino-uno. Codey Online adapts it to any other supported board for you.
// Canonical beginner example for an LDR Sensor Module
// Reads the analog light level and prints it to Serial.
const int ldrPin = A0;
void setup() {
Serial.begin(9600);
while (!Serial) {
; // Needed for some boards; safe on Uno as well
}
Serial.println("LDR Sensor Module example");
Serial.println("Reading analog light level from A0...");
}
void loop() {
int lightValue = analogRead(ldrPin);
Serial.print("Light level: ");
Serial.print(lightValue);
Serial.println(" / 1023");
delay(500);
}
Describe what you want to build. Codey Online writes the code, draws the wiring diagram, compiles it and flashes your board straight from the browser.
Start for free