This code is an Arduino sketch that controls and logs...
This code is an Arduino sketch that controls and logs the energy usage of two devices (a TV and a geyser) while monitoring motion and inactivity using a PIR sensor. Here's a detailed breakdown of what it does:
Summary:
The code monitors motion using a PIR sensor, turns a TV off after 2 hours of inactivity, logs the current (sensor readings) and relay states (ON/OFF status) of the TV and geyser to an SD card, and timestamps the logs using an RTC (Real-Time Clock).
Key Components and Features:
- Hardware Setup:
-
Relay control for two devices:
TV_RELAY
(Pin 8) controls the TV.GEYSER_RELAY
(Pin 9) controls the geyser.- The relays are assumed to be active LOW (they are ON when the pin is LOW and OFF when the pin is HIGH).
-
Current sensors:
TV_SENSOR
(Analog pin A1) measures the current (or some value) for the TV.GEYSER_SENSOR
(Analog pin A0) measures the current (or some value) for the geyser.
-
PIR motion detection:
PIR_PIN
(Digital pin 7) detects motion. When motion is detected, the PIR sensor's output is HIGH.
-
SD card for logging:
- Connected via SPI (
SD_CS
pin 3) to log data into a CSV file namedenergy.csv
.
- Connected via SPI (
-
RTC (Real-Time Clock):
- The RTC (DS1307) provides the current date and time for timestamping the log entries.
-
-
Code Execution Flow:
setup()
:- Sets up serial communication, pin modes for relays, and initializes the RTC, SD card, and log file:
- Writes the CSV header (
Time,TV_Current,Geyser_Current,TV_Status,Geyser_Status
) toenergy.csv
on the SD card.
- Writes the CSV header (
- Ensures proper functioning of the RTC and SD card or halts the program.
loop()
:-
Motion Detection & Inactivity Handling:
- Monitors the PIR sensor for motion (
PIR_PIN
). If motion is detected, it updateslastMotionTime
with the current time. - If no motion is detected for more than 2 hours (
MAX_INACTIVE_TIME
), the TV is turned off by settingTV_RELAY
to HIGH.
- Monitors the PIR sensor for motion (
-
Sensor Readings:
- Reads raw analog sensor values for
TV_SENSOR
andGEYSER_SENSOR
to estimate current usage for the TV and geyser.
- Reads raw analog sensor values for
-
Logging Data:
- Retrieves the current timestamp from the RTC (
rtc.now()
). - Logs the following information into
energy.csv
:- Current timestamp (time).
- Current readings (
TV_SENSOR
andGEYSER_SENSOR
). - TV relay state (
ON
orOFF
). - Geyser relay state (
ON
orOFF
).
- Retrieves the current timestamp from the RTC (
-
Logs data every 5 seconds (
delay(5000)
).
- Sets up serial communication, pin modes for relays, and initializes the RTC, SD card, and log file:
- Key Functionalities:
- Power Management:
- Automatically turns off the TV after 2 hours of inactivity, conserving energy.
- Data Logging:
- Records the energy usage (via current sensor readings) and the ON/OFF status of devices into a file on an SD card, providing a history for monitoring.
- Timestamping:
- Uses a real-time clock (RTC) to timestamp the logged data for accurate records.
- Motion Tracking:
- Tracks motion using a PIR sensor to determine when the TV should remain ON or turn OFF due to inactivity.
- Power Management:
Example Log Entry:
A log entry might look like this (assuming placeholders for sensor data):
2023-10-12T10:45:30,345,123,ON,OFF
This means:
- Timestamp:
2023-10-12T10:45:30
. - TV current:
345
(raw sensor reading). - Geyser current:
123
(raw sensor reading). - TV status:
ON
(relay is LOW). - Geyser status:
OFF
(relay is HIGH).
Purpose:
This code automates controlling and monitoring energy usage and logs the relevant data for analysis. It is useful for building energy-efficient systems where monitoring and optimization of device usage is important.