Arduino MicroPython Light Sensor

This guide will show you how to use an LDR light sensor (also known as a photoresistor, light-dependent resistor, or photocell) with an Arduino and MicroPython. We’ll cover the following:

Arduino MicroPython light sensor

If you are looking for a module with a light sensor, check the tutorial Arduino MicroPython LDR Light Sensor Module.

Hardware Preparation

1×Arduino Giga R1 WiFi
1×USB Cable Type-C
1×Light Sensor
1×10 kΩ resistor
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino Uno/Mega/Giga
1×(Recommended) Breadboard Shield For Arduino Mega/Giga
1×(Recommended) Enclosure For Arduino Giga
1×(Recommended) Power Splitter 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 Light Sensor

This guide explains how to use a photoresistor, also known as a Light-Dependent Resistor (LDR), to measure the brightness of surrounding light.

Pinout

A photoresistor has two pins. Since it is a resistor, you do not need to distinguish these pins. They are identical.

Light Sensor Pinout

How It Works

A photoresistor is a unique resistor that changes its resistance in response to light. Its resistance decreases in bright light and increases in darkness or no light. By measuring the photoresistor's resistance, we can determine the brightness or darkness of the surrounding light, making it useful for measuring light levels in different environments.

How Light Sensor Works

WARNING

The light sensor value provides a general indication of light brightness, but it's not a precise measurement. Use it when exact measurements are not required.

Wiring Diagram

How to connect Arduino and light sensor

The wiring diagram between Arduino MicroPython Light Sensor

This image is created using Fritzing. Click to enlarge image

Arduino MicroPython Code

This code checks the light amount using a photocell and describes how bright it is.

""" 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-light-sensor """ from machine import ADC, Pin import time # Initialize ADC (Analog to Digital Converter) adc = ADC(Pin('A0')) # The Arduino Giga WiFi pin A0 connected to the light sensor while True: # Read the input on analog pin ADC0 (value between 0 and 4095) value = adc.read_u16() >> 4 # Read the 16-bit analog value from the sensor and convert back to 12-bit ADC description = "" # We'll have a few thresholds, qualitatively determined if value < 49: description = "Dark" elif value < 829: description = "Dim" elif value < 2038: description = "Light" elif value < 3267: description = "Bright" else: description = "Very bright" print(f"Analog reading: {value} - {description}") time.sleep(0.5) # delay for 500 milliseconds

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 LDR light sensor to the Arduino 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.
  • Direct light onto the sensor.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Analog reading: 654 - Dim Analog reading: 604 - Dim Analog reading: 742 - Dim Analog reading: 756 - Dim Analog reading: 915 - Light Analog reading: 1324 - Light Analog reading: 1607 - Light Analog reading: 2451 - Bright Analog reading: 2619 - Bright Analog reading: 3918 - Very bright Analog reading: 3922 - Very bright Analog reading: 3976 - Very bright
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

※ 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!