Arduino MicroPython LDR Module

This guide teaches you how to use an Arduino and an LDR light sensor module with MicroPython to monitor and measure light levels. You will learn:

Arduino MicroPython LDR Light Sensor Module

Hardware Preparation

1×Arduino Giga R1 WiFi
1×USB Cable Type-C
1×LDR Light Sensor Module
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino Uno/Mega/Giga
1×(Recommended) Breadboard Shield For Arduino Uno/Mega/Giga
1×(Recommended) Enclosure For Arduino Giga

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of LDR Light Sensor Module

The LDR light sensor module is used to detect light or measure the surrounding light levels. It offers two outputs: a digital output and an analog output.

Pinout

The LDR light sensor module has four pins:

  • VCC pin: Connect this pin to VCC (3.3V to 5V).
  • GND pin: Connect this pin to GND (0V).
  • DO pin: Digital output pin. It outputs HIGH in the dark and LOW in the light. You can adjust the potentiometer to change the light and dark sensitivity.
  • AO pin: Analog output pin. The output value decreases in bright light and increases in darkness.
LDR Light Sensor Module Pinout
image source: diyables.io

The LDR light sensor module also includes two LED indicators:

  • PWR-LED: Lights up to indicate that the module is powered on.
  • DO-LED: Shows the status of the DO pin. It lights up when there is light and turns off in the dark.

How It Works

DO Pin:

  • The potentiometer sets the light threshold level.
  • The DO pin is LOW (DO-LED off) when the light is brighter than the threshold.
  • The DO pin is HIGH (DO-LED on) when the light is dimmer than the threshold.

AO Pin:

  • The AO pin reading changes with light intensity.
  • A lower reading indicates more light.
  • A higher reading indicates darker conditions.
  • The potentiometer does not affect the AO pin value.

This setup allows you to adjust the sensitivity of the DO pin with the potentiometer while obtaining precise light measurements from the AO pin.

Wiring Diagram

The light sensor module offers two outputs, DO and AO, which can be used individually or together.

The wiring diagram between Arduino MicroPython LDR Light Sensor Module

This image is created using Fritzing. Click to enlarge image

Arduino MicroPython Code - Read value from DO pin

""" 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

Detailed Instructions

Here’s instructions on how to run the above MicroPython code on Arduino with Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Make sure MicroPython firmware is installed on your Arduino board.
  • If you are new to Arduino with MicroPython, see the Getting Started with Arduino and MicroPython.
  • Connect the Arduino the LDR module according to the provided diagram.
  • Connect the Arduino board to your computer with a USB cable.
  • Open Thonny IDE and go to Tools Options.
  • Under the Interpreter tab, select MicroPython (generic) from the dropdown menu.
  • Select the COM port corresponding to your Arduino board (e.g., COM33 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided MicroPython code and paste it into Thonny\'s editor.
  • Save the MicroPython code to your Arduino by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device and name the file main.py.
  • Click the green Run button (or press F5) to execute the code.
  • Use your hand or an object to block and unblock the light on the LDR sensor module.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot The light is present The light is present The light is NOT present The light is NOT present The light is NOT present The light is present The light is present The light is present
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

If the LED is always on or off, adjust the light sensitivity using the potentiometer. You can now modify the code to control an LED, light switch, or servo motor based on light detection. For detailed instructions, refer to the tutorials at the end of this document.

Arduino MicroPython Code - Read value from AO pin

""" 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 ADC, Pin import utime # For timing functions AO_PIN = ADC(Pin('A0')) # The Arduino Giga WiFi pin connected to the AO pin of the LDR module while True: light_value = AO_PIN.read() # Read the analog value (0-4095) print(light_value) # Print the analog value utime.sleep(1) # Add a small delay to avoid spamming the output

Detailed Instructions

Please follow these instructions one by one:

  • Copy the above code and open it in the Thonny IDE.
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Use your hand or an object to block and unblock the light on the LDR sensor module.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot 681 686 689 1229 2723 3752 3816 4026 4030 4040 4064 2561 2175 1375 683
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!