This Arduino project integrates a PIR (Passive Infrared) sensor, an LDR (Light-Dependent Resistor), and a current sensor (ACS). The system adjusts the brightness of an LED based on the LDR reading, calculates the current flowing through the LED, and sends the current data, along with the LED brightness percentage, to a NodeMCU using serial communication.
- PIR Sensor
- LDR
- ACS (Current Sensor)
- LED
- Arduino board
- NodeMCU
- PIR Sensor: Digital Pin 2
- LDR: Analog Pin A0
- ACS: Analog Pin A1
- LED: Digital Pin 3
MAX_PWM
andMIN_PWM
: Define the PWM range for LED brightness control.MAX_PIR
andMIN_PIR
: Define the range of PIR sensor readings.MIN_PERSON_TIME
: Minimum duration for which the PIR sensor keeps the detection state.START
: Start byte for serial communication.
_pirEnabled
: Flag indicating whether a person has been detected by the PIR sensor._now
: Keeps track of the current time usingmillis()
._pirTime
: Records the time when a person is detected by the PIR sensor._current
: Current flowing through the LED._voltageResistor
: Voltage across the resistor in the current calculation._pwm
: Pulse Width Modulation value for LED brightness control._percentage
: LED brightness percentage.buffer
: Data buffer for serial communication.
- Configure pin modes and set up serial communication.
- Initialize the LED state.
- Continuously reads data from the sensors.
- Adjusts the LED brightness based on LDR readings.
- Calculates the current flowing through the LED using the ACS.
- Sends current and LED brightness percentage data to the NodeMCU through serial communication.
- The
calculateCurrent
function estimates the current flowing through the LED based on the PWM value.
- An interrupt service routine (
pir_process
) is triggered on a rising edge of the PIR sensor signal. This routine updates_pirTime
and sets_pirEnabled
to true.
- Upload the code to your Arduino board.
- Connect the sensors and components as per the provided pin configuration.
- Ensure the NodeMCU code correctly expects and interprets the data format sent by the Arduino.
An ESP8266 module and facilitates communication with an MQTT broker. It connects to a specified Wi-Fi network, establishes a connection with the MQTT broker, and publishes data received from a serial device in JSON format.
ESP8266WiFi
WiFiClient
SoftwareSerial
PubSubClient
ArduinoJson