DEV Community

Cover image for Arduino MicroPython LDR Module
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at newbiely.com

Arduino MicroPython LDR Module

An LDR light sensor module lets your Arduino know whether it is light or dark, either as a simple on/off signal or as a graded analog reading. This is a nice first sensor to wire up if you are coming from software: you get both a digital GPIO read and an analog-to-digital read from the same cheap breakout, and you can test it live from the MicroPython REPL in Thonny.

The full tutorial, with every detail and the line-by-line code explanation, is on our site: Arduino MicroPython LDR Module

What you need

Wiring

Arduino MicroPython LDR Module wiring diagram

The module has four pins. VCC goes to a 3.3V to 5V supply, and GND goes to ground. DO is the digital output pin, and AO is the analog output pin; you can use either one alone or wire both if you want both readings. In the example code, DO connects to pin D2 on the Arduino Giga R1 WiFi. The module also has two onboard LEDs: a PWR-LED that just confirms power is connected, and a DO-LED that mirrors the state of the DO pin.

How it works

A potentiometer on the module sets a light threshold. When the surroundings are brighter than that threshold, DO goes LOW and the DO-LED turns off; when it is darker than the threshold, DO goes HIGH and the DO-LED lights up. You can turn the potentiometer to make the sensor more or less sensitive to light and dark. The AO pin instead gives a continuous value: the reading goes down as light increases and up as it gets darker, so you can use it to measure light level rather than just a yes/no state.

Code

The DO example sets up D2 as a Pin.IN and loops forever, calling .value() on it every half second. A value of 1 means dark (prints "The light is NOT present"), and 0 means light is detected. The small utime.sleep() delay just keeps the Shell output readable instead of flooding it. The separate AO example follows the same loop structure but reads the analog pin instead, so you can watch the numeric value change as you block and unblock light on the sensor.

arduino-micropython-ldr-module.py

"""
This Arduino MicroPython script was developed by newbiely.com
This Arduino MicroPython script is made available for public use without any restriction
For comprehensive instructions and wiring diagrams, please visit:
https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-ldr-module
"""

from machine import Pin
import utime  # For timing functions

DO_PIN = Pin('D2', Pin.IN) # The Arduino Giga R1 WiFi pin connected to the DO pin of the LDR module

while True:
    light_state = DO_PIN.value()  # Read the digital value from the pin

    if light_state == 1:
        print("The light is NOT present")
    else:
        print("The light is present")

    utime.sleep(0.5)  # Add a small delay to avoid spamming the output
Enter fullscreen mode Exit fullscreen mode

Full tutorial

The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-ldr-module

Top comments (0)