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:
- How to connect the LDR light sensor module to an Arduino.
- How to write MicroPython code for the Arduino to detect light using the digital signal from the LDR light sensor.
- How to write MicroPython code for the Arduino to measure light intensity using the analog signal from the LDR light sensor.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
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.
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.
This image is created using Fritzing. Click to enlarge image
Arduino MicroPython Code - Read value from DO pin
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.
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
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.