Smart Home System
This project is meant to function as a multi functional home system built using an Arduino UNO R3. The main objective is to provide key safety and monitoring functions found in a real home, all combined into one integrated circuit with data being displayed through an I2C OLED screen. Specifically, it uses motion detection, gas monitoring, temperature sensing, and IR remote control into a single interactive OLED I2C display. This instructible is a intermediate friendly and fully documented at each step, to ensure a full replication of the circuit.
Supplies
Core Components
Sensors (Inputs)
- PIR motion sensor
- MQ-2 Gas Sensors
- DHT11 temperature & humidity Sensor
- IR Receiver module
- IR Remote control
Outputs
Tools
Project Idea & Purpose
Many home safety projects often use a single sensor at a time. This project combines multiple sensors into one interactive system that is easy to interpret and simulates real world automation concepts. The system monitors:
- Burglary / Intrusion using a PIR motion Sensor
- Temperature (Thermostat) using a Temperature Sensor
- Carbon monoxide detection using a MQ-series gas sensor
An IR remote and a IR receiver are used in order to switch between display modes on the OLED display, allowing the user to view different sensor readings on demand.
Learning Outcomes
- Reading digital and analog sensors
- Clear input > processing > output logic
- Communication using libraries
- Displaying output on I2C oled display
Circuit Overview
Inputs: PIR motion sensor, MQ-2 gas sensor, DHT11 temperature sensor, IR receiver
Outputs: OLED i2c display
System Logic
- Sensors continuously feed their readings (data) into the Arduino
- Arduino processes readings and displays them on the OLED screen
- IR remote buttons switch display modes or toggle system ON/OFF
Testing Concept
- Each sensor will be tested individually before combining into a full system
- IR HEX codes are logged for proper remote functionality
Sensor Explanation
PIR Motion Sensor
- Detects changes in infrared radiation caused by movements
- Outputs HIGH digital signal when motion is being detected
- Used for: burglary or intrusion alerts
MQ-2 Gas Sensor
- Detects gases such as smoke, propane, methane, and carbon monoxide
- Analog voltage output proportional to gas concentration
- Used for: gas leak detection
Temperature Sensor (DHT11)
- Outputs digital readings for temperature (C) and humidity (%)
- Simple library functions allow for easy coding
- Used for: monitoring room temperature
IR Receiver
- Detects infrared signals from the remote
- Each button has a unique HEX code
- Used for: Switching OLED display modes and toggling the system ON/OFF
OLED Display (i2C)
- This step is to verify that the OLED screen is wired correctly and is able to display text before adding any sensors
Pin Connections
OLED -> Breadboard
VCC -> 5V (power rail)
GND -> ground rail
SDA -> A4
SCL -> A5
- Ensure library "Adafruit_GFX.h", "Adafruit_SSD1306.h", are both included in the library within the Arduino files
Code reasoning
- #include <Adafruit_GFX.h> and #include <Adafruit_SSD1306.h> are required libraries to draw text and graphics on the screen
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C); starts the OLED at address 0x3C, which is the most common address for 128x64 OLEDs
- display.clearDisplay() clears any random pixels left in memory
- setTextSize, setCursor, and println control how and where the text appears
- display.display() sends everything to the screen, without this code nothing will show up
Use this code to test:
PIR Motion Sensor
- This step tests the motion detection using a PIR sensor and confirms the Arduino can read a digital HIGH/LOW signal.
Pin Connections
PIR -> Breadboard
VCC -> 5V (power rail)
GND -> ground rail
OUT -> digital pin 2
Code reasoning
- pinMode(pir, INPUT); tells the arduino the PIR sensor is sending (INPUT) data into the board
- digitalRead(pir); reads either HIGH (motion) or LOW (no motion).
- The if statement checks the sensor state and prints the result to the LED screen
- delay(500); slows the output so messages are readable
Noticeable Observations:
- "Motion detected" when movement is in front of PIR sensor
- "No motion detected" movement is still near PIR sensor
Gas Sensor
- This step is to confirm the MQ-2 gas sensor is providing analog readings to the Arduino
Pin Connections
MQ-2 -> Breadboard
VCC -> 5V (power rail)
GND -> ground rail
A0 -> Analog pin A0
D0 -> no output
Code reasoning
- pinMode(pir, INPUT); tells the arduino the PIR sensor is sending (INPUT) data into the board
- digitalRead(pir); reads either HIGH (motion) or LOW (no motion).
- The if statement checks the sensor state and prints the result to the LED screen
- delay(500); slows the output so messages are readable
Noticeable Observations:
- Values changing when gas (carbon monoxide) or breathing near the sensor
- Higher number = more detected gas
Key Notes:
When the gas sensor is disconnected, the Arduino will still output values as the analog pin will be left floating. Floating analog inputs can pick up electrical noises, which result in irregular readings.
Sensor Temperature (DHT11)
- This step is to verify temperature readings from the DHT11 sensor using simple integer values
Pin Connections
DHT11 -> Breadboard
VCC -> 5V (power rail)
GND -> ground rail
DATA -> digital pin 4
- 10K ohm resistor between DATA and VCC
Code reasoning
- #include <dht.h> loads the DHT sensor library
- DHT.read11(DHTPIN); tells the sensor to take a new measurement
- DHT.temperature;, and DHT.humidity; store the integer value from the sensor
- These values are printed to the serial monitor
IR Receiver (Finding HEX Codes)
- Every remote sends different HEX codes, you must record these codes before writing the final program. This step is to identify the HEX codes sent by each button on the IR remote, which are required for ON/OFF and mode switching.
Pin Connections
IR receiver -> Breadboard
VCC -> 5V (power rail)
GND -> ground rail
OUT -> digital pin 7
Code reasoning
- #include <IRremote.h> This library allows the Arduino to receive and decode infrared signals from a remote control
- int IR_PIN = 7; Stores the pin number where the IR receiver's OUT pin is connected
- IRrecv irrecv(IR_PIN); Creates an IR receiver object, tells the library which pin to listen to.
- decode_results results; Stores the decoded IR signal data, results.value will contain the HEX code of the button pressed
- irrecv.enableIRIn(); Starts the IR receiver
- if (irrecv.decode(&results)) Checks if a button was pressed, runs the code inside only when a IR signal is detected
- long value = results.value; Saves the received HEX code into a variable, long is used as the IR HEX code numbers are large numbers
- display.println(value, HEX); Prints the button's HEX code to the OLED screen
- irrecv.resume(); Clears the last signal, allows the receiver to detect the next button press
Observations to be made:
- Press each remote button > Write down HEX codes for:
- Temperature, gas, reset, on, off
Final Step
In this step, all sensors, the OLED display, and the IR remote are combined into one complete system. The remote is used to turn the system on/off and switch between temperature, gas, and home screens, while sensors update live data on the OLED
Sensor Pins
- int is used because pin numbers are simple whole numbers
- Makes wiring easy to change later without rewriting code
Remote button HEX codes
- const prevents accidental changes during runtime
System State Variables
- boolsystemOn, if true than the system will start, if false, OLED will be blank
- screenmode, 0 = home, 1 = temperature screen, 2 = gas
PinMode Setup
- Starts the serial monitor
- Allows the PIR to be input
- Starts the OLED and IR receiver
IR remote controlling system
- Reads the HEX code from the remote
- Each button changes the system screen
- irrecv.resume(); allows the receiver to prepare for the next button press
Sensor Readings
- Uses integers for stability
- MQ-2 outputs raw analog values
- map() converts this into a rough PPM estimate
- Capped between 0 - 200 ppm to prevent false spikes
- High = motion detected
Temperature Screen
- Large centered temperature
- Smaller humidity display
- Note: setcursor must always go before .print
Gas screen
- Large CO ppm value
- Raw sensor value shown
- Vertical Bar graph for visuals
Home page / Reset (END)
- Default screen
- Displays motion alert when PIR is triggered
- Acts as the systems dashboard
FULL CODE
Conclusion (Reflection)
This project is to show how an Arduino is able to use multiple sensors to monitor home safety and home features. The PIR, DHT11, and MQ-2 sensors are able to provide real time data on motion, temperature, humidity, and carbon monoxide, all displayed on a OLED screen. Using an IR remote to control modes and ON/OFF states shows the importance of assigning the correct HEX codes. Through this project, I personally learnt how to handle analog and digital inputs, process sensor data, and manage a clean display without glitches. I was also able to build my code through testing of the physical (sensor) components, as each test provided the knowledge and understanding of the code for the final assembly.