// . Write an Arduino/ Raspberry pi program for interfacing with PIR sensor Experiment
int led = 13; // pin where LED is connected
int pir_sensor = 3; // pin where PIR sensor is connected
//integer variables that store the pin numbers for the LED and PIR sensor,
void setup()
{
pinMode(led, OUTPUT); // set LED pin for output
pinMode(pir_sensor, INPUT); // set PIR sensor pin for reading input
digitalWrite(led, LOW); // keep LED off initially
}
// pinMode(led, OUTPUT);: Configures the LED pin as an output, so you can send a signal to it.
// pinMode(pir_sensor, INPUT);: Configures the PIR sensor pin as an input, so the program can read signals from it.
// digitalWrite(led, LOW);: Ensures the LED is turned off initially by setting its state to LOW (0 volts).
void loop()
{
int sensor_val = digitalRead(pir_sensor); // read PIR sensor value
if (sensor_val == HIGH) // if motion is detected
{
digitalWrite(led, HIGH); // turn LED on
}
else // if no motion is detected
{
digitalWrite(led, LOW); // turn LED off
}
}
// digitalRead(pir_sensor);: Reads the value from the PIR sensor. If it detects motion, it returns HIGH; otherwise, it returns LOW.
// if (sensor_val == HIGH): Checks if the sensor detected motion (i.e., HIGH). If motion is detected, the LED is turned on.
// digitalWrite(led, HIGH);: Turns on the LED by setting the LED pin to HIGH (5 volts).
// digitalWrite(led, LOW);: Turns off the LED if no motion is detected.
// ### Data Flow
// 1. **PIR Sensor (Input)**: The PIR sensor detects motion. When it senses movement, it sends a HIGH signal to the pin it’s connected to (pin 3).
// 2. **Arduino Board**: The board reads the sensor value using `digitalRead(pir_sensor);`.
// - If the value is HIGH (motion detected), it sends a HIGH signal to the LED pin (pin 13) to turn the LED on.
// - If the value is LOW (no motion), it sends a LOW signal to the LED pin to keep the LED off.
// 3. **LED (Output)**: The LED responds to the HIGH or LOW signals, lighting up if there's motion or staying off otherwise.
// ### Key Concepts and Requirements
// 1. **Understanding Digital I/O Pins**:
// - **Digital Input**: Used to read data from sensors like the PIR sensor, which returns either HIGH (motion detected) or LOW (no motion).
// - **Digital Output**: Used to control devices like LEDs, which can be set to HIGH (on) or LOW (off).
// 2. **Working with the PIR Sensor**:
// - Know that PIR sensors detect infrared radiation, which increases when a person or animal moves in front of it.
// - When the sensor detects motion, it outputs a HIGH signal; otherwise, it outputs LOW.
// - The PIR sensor typically has a delay after detecting motion; it won’t constantly send a HIGH signal but instead stays HIGH for a short duration even after movement has stopped.
// 3. **Circuit Setup**:
// - **LED Setup**: Connect the LED’s positive (longer) leg to pin 13 and the negative (shorter) leg to ground (through a resistor if needed to limit current).
// - **PIR Sensor Setup**: The PIR sensor usually has three pins: **VCC (power)**, **GND (ground)**, and **OUT (output)**.
// - Connect **VCC** to 5V (or the required voltage for the sensor) on the Arduino.
// - Connect **GND** to a ground pin on the Arduino.
// - Connect **OUT** to the digital input pin defined in the code (pin 3).
// 4. **Powering the Arduino**:
// - Make sure your Arduino board is powered, either via USB from your computer or an external power source if required for standalone operation.
// 5. **Debugging and Testing**:
// - Check your wiring connections to ensure they match the pin assignments in the code.
// - Make sure your PIR sensor is functioning correctly. Some sensors have a sensitivity adjustment or a small LED that lights up when it detects motion.
// - Use the **Serial Monitor** in the Arduino IDE to print sensor values (e.g., `Serial.println(sensor_val);`) if you need help troubleshooting.
// By understanding this setup and these concepts, you’ll be ready to perform the practical and make modifications if needed.
// This setup—using a PIR sensor to control an LED—has a range of real-life applications, especially in areas involving security, automation, and energy efficiency. Here are a few key examples:
// ### 1. **Motion-Activated Lighting**
// - **Application**: Automatic lights in hallways, staircases, or outdoor areas.
// - **How It Works**: Lights turn on only when movement is detected, providing illumination for as long as someone is present. This improves convenience and saves electricity, as the lights automatically turn off when no one is around.
// ### 2. **Intruder Detection and Security Systems**
// - **Application**: Security alarms and CCTV systems in homes or businesses.
// - **How It Works**: A PIR sensor detects any unauthorized movement in a designated area and triggers alarms, alerts, or even cameras to record footage. This is a common feature in security systems to detect intruders.
// ### 3. **Automatic Door Control**
// - **Application**: Automatic doors in offices, shopping malls, or supermarkets.
// - **How It Works**: PIR sensors detect people approaching, signaling the door mechanism to open. This provides easy access for users and helps control airflow and temperature inside the building.
// ### 4. **Energy Management in Smart Buildings**
// - **Application**: Controlling lighting, HVAC (heating, ventilation, and air conditioning), and other energy-consuming devices.
// - **How It Works**: PIR sensors detect the presence of people in a room. For instance, if a room is unoccupied, the HVAC system can reduce its energy consumption, and the lights can automatically turn off. This is popular in "smart building" setups.
// ### 5. **Automated Restrooms and Sanitary Control**
// - **Application**: Motion-sensing lights and exhaust fans in public or private restrooms.
// - **How It Works**: The PIR sensor activates lights, hand dryers, or exhaust fans only when someone enters the restroom, helping with sanitation and energy efficiency.
// ### 6. **Home Automation Systems**
// - **Application**: Smart home hubs that control various devices.
// - **How It Works**: In smart homes, PIR sensors can be used to trigger multiple devices, like turning on the lights, activating the thermostat, or even notifying the homeowner of unexpected motion in specific areas. This adds convenience and security to everyday life.
// ### 7. **Pet and Wildlife Monitoring**
// - **Application**: Monitoring movement of pets or wildlife.
// - **How It Works**: The PIR sensor can detect motion and trigger a camera or light to record activity. This is useful for observing pets’ behavior in certain areas or monitoring wildlife activity near your home, such as in gardens or farms.
// ### 8. **Industrial Automation and Safety**
// - **Application**: Motion-activated warning lights or alarms in factories.
// - **How It Works**: In industrial settings, PIR sensors detect workers’ presence near potentially dangerous equipment and trigger warning lights or sounds to ensure their safety. It’s also useful in warehouse areas to detect movement and adjust lighting