Skip to content

Project 1.9.1

Cloud Light Intensity Logger

Overview

Build a light logger that reads an LDR module and shows the current value plus recent history on a web page.

This project demonstrates analog input, a rolling history list, and browser-based data display.

In this beginner version, the data is shown on a web page on your local Wi-Fi network. The final result should show the current light level, the last 10 readings, and their average.

Required Components

Pico 2 W
Raspberry Pi Pico 2 W
LDR
LDR (light dependent resistor)
Breadboard
Breadboard
Jumper Wires
Jumper wires
2.4 GHz Wi-Fi network Phone or computer browser

Circuit Connections

Component Pin Connects To Pico GPIO / Physical Pin Number Notes
LDR leg 1 3.3V Physical pin 36 Top side of the voltage divider
LDR leg 2 GPIO 26 GPIO 26 / physical pin 31 Shared junction point
LDR leg 2 GND Physical pin 38 Bottom side of the voltage divider

Step-by-Step Assembly

Step 1: Place the Raspberry Pi Pico 2W

Place the Raspberry Pi Pico 2W on the breadboard so it sits across the center gap. Keep the USB port facing outward so you can easily connect it to your computer.

Circuit 2

Step 2: Place the LDR

Place the LDR module on the breadboard or position it where it can be exposed to room light.

Identify VCC, GND, and AO/AOUT on the LDR module before wiring.

Check the printed labels on your module because some modules may arrange the pins differently.

Circuit 3

Step 3: Connect the LDR to 3.3V

Connect LDR module VCC to 3.3V.

Connect LDR module GND to GND.

Circuit 4

Step 4: Connect the LDR Signal Junction

Connect LDR module AO / AOUT to GPIO 26 / ADC0.

GPIO 26 reads the changing analog signal from the LDR module.

Circuit 5

Wiring Check

Pico 2W is placed correctly across the breadboard center gap

Pico 2W USB port faces outward

LDR module VCC connects to 3.3V

LDR module GND connects to GND

LDR module AO/AOUT connects to GPIO 26 / ADC0

No loose jumper wires

Beginner Note

An LDR module detects light using a light-dependent resistor and an onboard resistor circuit. The Pico reads the module’s AO/AOUT pin through GPIO 26 as an analog value. The reading changes when the light level changes.

Testing Individual Components

Before running the full project, test each part separately. This makes it easier to find wiring or code problems.

LDR ADC test

Check that the LDR voltage divider changes when the light changes.

from machine import ADC, Pin
import time
adc = ADC(Pin(26))
while True:
    print(adc.read_u16())
    time.sleep(0.5)

Expected test result: The raw ADC value should change when you cover the LDR or shine more light on it.

Wi-Fi connection test

Check that the Pico connects to Wi-Fi and prints its IP address.

import network
import time
SSID = 'YOUR_WIFI_NAME'
PASSWORD = 'YOUR_WIFI_PASSWORD'
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
for _ in range(15):
    if wlan.isconnected():
        break
    print('Connecting...')
    time.sleep(1)
print('Connected:', wlan.isconnected())
if wlan.isconnected():
    print('IP address:', wlan.ifconfig()[0])

Expected test result: The Shell should show Connected: True and print an IP address.

Full Project Code

Upload and run this code after the individual tests work correctly.

import network
import socket
import time
from machine import ADC, Pin

SSID = 'YOUR_WIFI_NAME'
PASSWORD = 'YOUR_WIFI_PASSWORD'

adc = ADC(Pin(26))
light_log = []

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)

print('Connecting to Wi-Fi...')
for _ in range(15):
    if wlan.isconnected():
        break
    time.sleep(1)

if not wlan.isconnected():
    raise RuntimeError('Wi-Fi connection failed')

ip_address = wlan.ifconfig()[0]
print('Connected. Open http://{} in your browser'.format(ip_address))

def read_light_percentage():
    raw = adc.read_u16()
    percent = int((raw / 65535) * 100)
    return raw, percent

def web_page(current_raw, current_percent, history):
    rows = ''
    for index, value in enumerate(history, 1):
        rows += '<tr><td>{}</td><td>{}%</td></tr>'.format(index, value)
    average = sum(history) / len(history) if history else 0
    return """<!DOCTYPE html>
<html>
<head>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <meta http-equiv='refresh' content='3'>
    <title>Light Logger</title>
</head>
<body style='font-family:Arial;padding:30px'>
    <h1>Light Intensity Logger</h1>
    <h2>Current light: CURRENT_PERCENT%</h2>
    <p>Raw ADC: CURRENT_RAW</p>
    <h3>Last RECENT_COUNT readings</h3>
    <table border='1' cellpadding='8' cellspacing='0'>
        <tr><th>#</th><th>Light %</th></tr>
        HISTORY_ROWS
    </table>
    <p>Average: AVG_VALUE%</p>
    <p>Page refreshes every 3 seconds</p>
</body>
</html>""".replace('CURRENT_PERCENT', str(current_percent)).replace('CURRENT_RAW', str(current_raw)).replace('RECENT_COUNT', str(len(history))).replace('HISTORY_ROWS', rows).replace('AVG_VALUE', '{:.1f}'.format(average))

address = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
server = socket.socket()
server.bind(address)
server.listen(1)

while True:
    client, client_address = server.accept()
    print('Client connected from', client_address)
    client.recv(1024)

    current_raw, current_percent = read_light_percentage()
    light_log.append(current_percent)
    if len(light_log) > 10:
        light_log.pop(0)

    response = web_page(current_raw, current_percent, light_log)
    client.send('HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n'.encode())
    client.sendall(response.encode())
    client.close()

How the Code Works

Code Section What It Does Why It Matters
Voltage divider + ADC Reads the LDR signal as a 16-bit number The Pico needs ADC to measure changing light levels
read_light_percentage() Converts the raw value to a simple percentage Percent values are easier for beginners to understand
light_log list Stores only the last 10 readings This creates a rolling history instead of an endlessly growing list
web_page() Builds the HTML page with the current value, history, and average The browser page becomes the simple data dashboard

Expected Result

After entering your Wi-Fi details and running the code, the Shell should print an IP address. Opening that address in a browser should show the current light level, a table of the last 10 readings, and the average. Covering or lighting the LDR should change the values.

Troubleshooting

Problem Possible Cause Solution
ADC stays near 0 or 65535 Voltage divider is wired incorrectly Check that the LDR and 10kΩ resistor form a proper divider with GPIO 26 at the junction
History never grows Page has not refreshed enough times yet Wait for a few refreshes because one new reading is added each page load
Wi-Fi does not connect Wrong network details or unsupported network Recheck the SSID and password and use a 2.4 GHz network