This project teaches students how to build a light intensity monitoring system using an ESP32 microcontroller, a photoresistor, and an OLED screen. A built-in web dashboard visualizes real-time light data using Chart.js. It demonstrates how environmental sensors and web interfaces can be combined for data monitoring and analysis.
(3.3V) —- LDR —-+—- GPIO34 (ADC)
|
[10kΩ]
|
(GND)
I2C OLED:
SCL (ESP32 GPIO22)
SDA (ESP32 GPIO21)
VCC → 3.3V
GND → GND
from machine import Pin, ADC, SoftI2C
from ssd1306 import SSD1306_I2C
import network
import socket
from time import sleep
ldr = ADC(Pin(34))
ldr.atten(ADC.ATTN_11DB)
ldr.width(ADC.WIDTH_10BIT)
i2c = SoftI2C(scl=Pin(22), sda=Pin(21))
oled = SSD1306_I2C(128, 64, i2c)
ssid = 'YOUR_WIFI_SSID'
password = 'YOUR_WIFI_PASSWORD'
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while not wlan.isconnected():
pass
ip = wlan.ifconfig()[0]
print('Connected on', ip)
html = """<!DOCTYPE html>
<html><head><title>Light Dashboard</title>
<script src='https://cdn.jsdelivr.net/npm/chart.js'></script>
<style>body{background:#111;color:white;text-align:center;font-family:sans-serif}.container{width:90%%;margin:auto}canvas{background:#222;border-radius:10px}</style>
</head><body><h1>Light Level</h1><div class='container'><canvas id='chart'></canvas></div>
<script>
const ctx=document.getElementById('chart').getContext('2d');
const chart=new Chart(ctx,{type:'line',data:{labels:[],datasets:[{label:'Light',borderColor:'lime',data:[],tension:0.3}]},options:{animation:false,responsive:true,scales:{y:{min:0,max:1023}}}});
async function update(){const r=await fetch('/light');const d=await r.json();const t=new Date().toLocaleTimeString();if(chart.data.labels.length>30){chart.data.labels.shift();chart.data.datasets[0].data.shift();}chart.data.labels.push(t);chart.data.datasets[0].data.push(d.value);chart.update();}
setInterval(update,1000);
</script></body></html>"""
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
while True:
try:
cl, addr = s.accept()
request = cl.recv(1024).decode()
if 'GET /light' in request:
cl.send('HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n')
cl.send('{{"value": {}}}'.format(ldr.read()))
else:
cl.send('HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n')
cl.send(html)
oled.fill(0)
oled.text('Light Level:', 0, 0)
oled.text(str(ldr.read()), 0, 16)
oled.text('IP:', 0, 40)
oled.text(ip, 0, 52)
oled.show()
cl.close()
except Exception as e:
print('Error:', e)
cl.close()
You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.
You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.
You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.
You can explore a wide range of microcontroller and electronics projects, including Arduino, ESP32, IoT, and more. Each project comes with downloadable code, detailed guides, and the necessary hardware list.