Building a Smart Light Control System with Raspberry Pi Pico H

Control lights smartly with a Raspberry Pi Pico H. This project explores creating a smart light control system using motion sensors, light sensors, and Wi-Fi for remote access. Ideal for beginners in home automation, it offers a practical way to learn programming and circuit design while adding convenience to your home.

Introduction to the Smart Light Control System

Smart lighting is a practical first step into home automation. This project allows you to control lights automatically using a Raspberry Pi Pico H, motion sensors, and a light sensor. We’ll also add Wi-Fi connectivity to enable remote control. This system can be adapted for various rooms, hallways, or any location that benefits from automated lighting.

Components Needed for the Project

To build this smart light control system, you’ll need:

  • Raspberry Pi Pico H: The main microcontroller.
  • PIR Motion Sensor: To detect presence.
  • LDR (Light Dependent Resistor): For ambient light detection.
  • Relay Module: Acts as a switch for high-voltage lights.
  • ESP8266 Wi-Fi Module: For remote control (optional).
  • Breadboard and Jumper Wires: For connecting components.

These are affordable and easy to find, making this a beginner-friendly project.

Setting Up the Raspberry Pi Pico H with MicroPython

To control the system, we’ll use MicroPython on the Raspberry Pi Pico H. Connect the Pico H to your computer, open Thonny IDE, and set the interpreter to “MicroPython (Raspberry Pi Pico).” Follow Thonny’s prompts to install MicroPython if needed. Once done, you’re ready to start programming.

Understanding the Project Logic

The smart light system will work as follows:

  1. Motion Detection: If motion is detected, the light turns on.
  2. Ambient Light Detection: If it’s too bright, the light stays off even if motion is detected.
  3. Remote Control: The light can be controlled remotely via Wi-Fi.

This design offers flexibility by combining sensor-based automation and remote control.

Wiring the PIR Motion Sensor to the Raspberry Pi Pico H

The PIR sensor has three pins: VCC, GND, and OUT.

  • VCC: Connect to 3.3V on the Pico H.
  • GND: Connect to GND.
  • OUT: Connect to GPIO pin GP15 on the Pico H.

The OUT pin outputs HIGH when motion is detected and LOW otherwise.

Wiring the LDR for Ambient Light Detection

The LDR should be connected in a voltage divider circuit with a 10kΩ resistor:

  • One end of the LDR: Connect to 3.3V.
  • Other end of the LDR: Connect to one side of a 10kΩ resistor and GP26 (ADC pin).
  • Other end of the resistor: Connect to GND.

This configuration allows the Pico H to read the ambient light level.

Wiring the Relay Module

The relay module enables the Pico H to control high-voltage lights. It has three important terminals: NO (Normally Open), COM (Common), and NC (Normally Closed).

  • VCC: Connect to 3.3V.
  • GND: Connect to GND.
  • IN: Connect to GPIO pin GP14.

Connect the light to the NO and COM terminals to complete the circuit when the relay is activated.

Writing the Motion and Light Detection Code

Initialize the sensors and relay in MicroPython:

python
from machine import Pin, ADC
import time
pir_sensor = Pin(15, Pin.IN)
ldr = ADC(Pin(26))
relay = Pin(14, Pin.OUT)
LDR_THRESHOLD = 20000 # Adjust this based on testingdef check_light_conditions():
light_level = ldr.read_u16()
return light_level < LDR_THRESHOLD

def control_light():
if pir_sensor.value() == 1 and check_light_conditions():
relay.on()
print(“Light ON”)
else:
relay.off()
print(“Light OFF”)

while True:
control_light()
time.sleep(1)

This code turns on the light if motion is detected and it’s dark.

Testing the PIR and LDR Sensors

To test the sensors, print their values to the console:

python
while True:
print("Motion:", pir_sensor.value())
print("Light Level:", ldr.read_u16())
time.sleep(1)

Observe these values to understand how the sensors respond in different lighting and motion conditions.

Calibrating the LDR Threshold

Adjust the LDR_THRESHOLD based on your room’s lighting conditions. Place the LDR in your setup environment and record its readings in both dark and bright conditions to set a suitable threshold.

Adding Remote Control with the ESP8266 Wi-Fi Module

Connect the ESP8266 to the Pico H:

  • VCC: 3.3V on the Pico H.
  • GND: GND.
  • TX and RX: Connect TX to GP1 (RX on Pico H) and RX to GP0 (TX on Pico H).

Set up the ESP8266 to create a web server, allowing you to control the light remotely.

Coding the Web Server for Remote Control

Here’s code to connect to Wi-Fi and set up a basic web server:

python
import network
import socket
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(“your_SSID”, “your_PASSWORD”)while not wifi.isconnected():
pass

print(“Connected to Wi-Fi”)

addr = socket.getaddrinfo(‘0.0.0.0’, 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)

def handle_request(conn):
request = conn.recv(1024)
request = str(request)

if ‘/on’ in request:
relay.on()
elif ‘/off’ in request:
relay.off()

response = “HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n”
response += “<html><body><h1>Smart Light Control</h1>”
response += “<p><a href=’/on’>Turn ON</a></p>”
response += “<p><a href=’/off’>Turn OFF</a></p></body></html>”

conn.send(response)
conn.close()

while True:
conn, addr = s.accept()
handle_request(conn)

Replace "your_SSID" and "your_PASSWORD" with your Wi-Fi details.

Integrating Sensor and Remote Control

Add a toggle in the code to switch between sensor and remote control modes. This way, you can use the web interface to override the sensors.

python

remote_control = False

def control_light():
if remote_control:
return

if pir_sensor.value() == 1 and check_light_conditions():
relay.on()
else:
relay.off()

def handle_request(conn):
global remote_control
request = conn.recv(1024)
request = str(request)

if ‘/on’ in request:
relay.on()
remote_control = True
elif ‘/off’ in request:
relay.off()
remote_control = True
elif ‘/sensor’ in request:
remote_control = False

response = “HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n”
response += “<html><body><h1>Smart Light Control</h1>”
response += “<p><a href=’/on’>Turn ON</a></p>”
response += “<p><a href=’/off’>Turn OFF</a></p>”
response += “<p><a href=’/sensor’>Sensor Control</a></p></body></html>”

conn.send(response)
conn.close()

This code provides both sensor-based and remote-control options.

 Adding a Timer for Light Duration

Add a timer to keep the light on for a set period after detecting motion. This is useful if you want the light to stay on for a while before turning off.

python

from machine import Timer

light_timer = Timer()

def turn_off_light(timer):
relay.off()
print(“Light automatically turned OFF after timer”)

def control_light():
if pir_sensor.value() == 1 and check_light_conditions():
relay.on()
light_timer.init(mode=Timer.ONE_SHOT, period=10000, callback=turn_off_light)

This code turns the light off automatically 10 seconds after it was turned on.

Adding Notifications for Light Status Changes

If connected to Wi-Fi, you can set up notifications using a cloud service like IFTTT to alert you when the light turns on or off. Use the ESP8266 module to send requests to the cloud when the relay status changes.

python

import urequests

def notify_light_on():
url = “https://maker.ifttt.com/trigger/light_on/with/key/YOUR_IFTTT_KEY”
urequests.get(url)
print(“Notification sent: Light ON”)

def notify_light_off():
url = “https://maker.ifttt.com/trigger/light_off/with/key/YOUR_IFTTT_KEY”
urequests.get(url)
print(“Notification sent: Light OFF”)

Replace "YOUR_IFTTT_KEY" with your IFTTT webhook key.

Testing and Troubleshooting

Test the entire system by moving in front of the motion sensor and observing how the light behaves based on the ambient light level. Troubleshoot by checking connections, calibrating the LDR threshold, and ensuring Wi-Fi stability if using remote control. Fine-tune settings and re-test as needed.

The smart light control system with Raspberry Pi Pico H is a practical, flexible project that combines electronics and programming to create a functional home automation tool. By integrating sensors and remote control, you’ve learned how to design a system that reacts to environmental changes and offers user control from anywhere. This project is an excellent foundation for more advanced IoT and home automation projects.

 

Please check out our other website, where you can learn how to 3D print some of the things needed for this project. https://master3dp.com/

 

Skip to content