Raspberry Pi Pico W-Based Smart Environmental Control System with MQTT and Sensor Integration
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
The circuit is designed to operate in two modes: IoT mode and Sensor mode. In IoT mode, the fan and light are controlled via MQTT messages. In Sensor mode, the fan and light are controlled automatically based on sensor inputs from a DHT11 temperature and humidity sensor, an LDR (Light Dependent Resistor), and a Microwave Radar Motion Sensor. The Raspberry Pi Pico W serves as the microcontroller, interfacing with various sensors and actuators, and managing the logic for both modes. The circuit also includes a two-channel relay to control the fan and light, pushbuttons for manual mode switching, and an I2C module connected to an LCD display for user feedback.
Component List
- Raspberry Pi Pico W: A microcontroller board with Wi-Fi capabilities, used as the central processing unit for the circuit.
- DHT11: A temperature and humidity sensor that provides environmental data to the microcontroller.
- Two Channel Relay 5V: An electromechanical switch that allows the microcontroller to control high-power devices such as a fan and a light bulb.
- Photocell (LDR): A light sensor whose resistance varies with the intensity of light, used for detecting ambient light levels.
- Resistor: A passive component used to limit current or divide voltages in the circuit. In this circuit, 10k Ohm resistors are used.
- RCWL-0516 Microwave Radar Motion Sensor Module: A sensor that detects motion through microwave signals.
- Pushbutton: A momentary switch used to trigger certain actions in the circuit, such as changing modes.
- BULB: An electric light source controlled by the relay.
- Power Supply: Provides the necessary power to the circuit components.
- Fan: An electric fan controlled by the relay.
- I2C Module: An interface module that allows communication between the microcontroller and the LCD display using the I2C protocol.
- LCD Display (16 pin): A display device used to provide visual feedback to the user.
Wiring Details
Raspberry Pi Pico W
VSYSconnected to Two Channel Relay 5VVCCGP16connected to RCWL-0516 Microwave Radar Motion Sensor ModuleOUTGNDconnected to various components' ground pins3V3 OUTconnected to DHT11+, I2C ModuleVCC, Photocell (LDR)pin 0, and RCWL-0516 Microwave Radar Motion Sensor Module3V3GP2connected to I2C ModuleSDAGP3connected to I2C ModuleSCLGP4connected to DHT11outGP6connected to Two Channel Relay 5VIN1GP7connected to Two Channel Relay 5VIN2GP8andGP9connected to Pushbuttons and Resistors for button debouncing
DHT11
+connected to Raspberry Pi Pico W3V3 OUToutconnected to Raspberry Pi Pico WGP4-connected to Raspberry Pi Pico WGND
Two Channel Relay 5V
VCCconnected to Raspberry Pi Pico WVSYSIN1andIN2connected to Raspberry Pi Pico WGP6andGP7respectivelyGNDconnected to Raspberry Pi Pico WGNDNC2connected to Fan5VC2andC1connected to Power Supply+NC1connected to BULB+
Photocell (LDR)
pin 0connected to Raspberry Pi Pico W3V3 OUTpin 1connected to a 10k Ohm Resistorpin1
Resistor
- 10k Ohm resistors used for various purposes including LDR voltage divider and button debouncing.
RCWL-0516 Microwave Radar Motion Sensor Module
OUTconnected to Raspberry Pi Pico WGP16GNDconnected to Raspberry Pi Pico WGND3V3connected to Raspberry Pi Pico W3V3 OUT
Pushbutton
- One side connected to Raspberry Pi Pico W
GND - The other side connected to Raspberry Pi Pico W
GP8orGP9through a 10k Ohm Resistor
BULB
+connected to Two Channel Relay 5VNC1-connected to Power Supply-
Power Supply
+connected to Two Channel Relay 5VC2andC1-connected to BULB-, FanGND
Fan
5Vconnected to Two Channel Relay 5VNC2GNDconnected to Power Supply-
I2C Module
VCCconnected to Raspberry Pi Pico W3V3 OUTGNDconnected to Raspberry Pi Pico WGNDSDAconnected to Raspberry Pi Pico WGP2SCLconnected to Raspberry Pi Pico WGP3- Other pins connected to corresponding pins on the LCD Display
LCD Display (16 pin)
- Connected to the I2C Module with corresponding pin connections for data, control, and backlight.
Documented Code
/*
* This Arduino Sketch is designed for a Raspberry Pi Pico W microcontroller.
* The circuit has two modes: IoT mode and Sensor mode.
* In IoT mode, the fan and light are controlled via MQTT messages.
* In Sensor mode, the fan and light are controlled automatically based on
* sensor inputs (DHT11, LDR, and Microwave Radar Motion Sensor).
*/
#include <WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT11
#define LDRPIN 26
#define MICROWAVEPIN 16
#define LIGHTPIN 6
#define FANPIN 7
#define BUTTON1PIN 8
#define BUTTON2PIN 9
const char* ssid = "project";
const char* password = "12345678";
const char* mqtt_server = "test.mosquitto.org";
const char* mqtt_topic = "JN_17102024";
WiFiClient espClient;
PubSubClient client(espClient);
DHT dht(DHTPIN, DHTTYPE);
void setup() {
pinMode(LIGHTPIN, OUTPUT);
pinMode(FANPIN, OUTPUT);
pinMode(MICROWAVEPIN, INPUT);
pinMode(BUTTON1PIN, INPUT);
pinMode(BUTTON2PIN, INPUT);
digitalWrite(LIGHTPIN, HIGH);
digitalWrite(FANPIN, HIGH);
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(mqtt_callback);
dht.begin();
}
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected: ");
Serial.println(WiFi.localIP());
}
void mqtt_callback(char* topic, byte* payload, unsigned int length) {
String msg = "";
for (unsigned int i = 0; i < length; i++) {
msg += (char)payload[i];
}
if (msg == "a") {
digitalWrite(LIGHTPIN, LOW);
} else if (msg == "b") {
digitalWrite(LIGHTPIN, HIGH);
} else if (msg == "c") {
digitalWrite(FANPIN, LOW);
} else if (msg == "d") {
digitalWrite(FANPIN, HIGH);
}
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("raspberry_pi_pico")) {
Serial.println("connected");
client.subscribe(mqtt_topic);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void sensor_control() {
while (true) {
int movement = digitalRead(MICROWAVEPIN);
dht.read();
float temp = dht.readTemperature();
float hum = dht.readHumidity();
int ldr_value = analogRead(LDRPIN);
if (movement == HIGH) {
for (int i = 60; i > 0; i--) {
if (i >= 40) {
digitalWrite(FANPIN, LOW);
}
if (ldr_value <= 10000) {
digitalWrite(LIGHTPIN, LOW);
} else if (ldr_value >= 20000) {
digitalWrite(LIGHTPIN, HIGH);
}
if (temp >= 35) {
digitalWrite(FANPIN, LOW);
} else if (temp <= 34) {
digitalWrite(FANPIN, HIGH);