HC-SR501 PIR Motion Sensor Module – Infrared

25,00 DH 28,00 DH

Availability:

997 in stock

SKU: 0122 Categories: ,

Description

HC-SR501 PIR Motion Sensor Module – Infrared

The HC-SR501 motion detector uses a passive infrared sensor to detect movement within a field of view of up to 7 metres and an angle of 120 degrees.

Detailed Specifications

  • Supply voltage: 4.5V to 20V DC
  • Output: Digital HIGH (3.3V) or LOW (0V) signal
  • Detection angle: < 120 degrees
  • Detection distance: Up to 7 metres
  • Delay time: Adjustable from 5 seconds to 5 minutes
  • Dimensions: 32 mm x 24 mm
  • Current consumption: < 60µA
  • Operating temperature: -15°C to 70°C

Operation

The HC-SR501 PIR sensor detects changes in infrared levels within its field of view. When a warm object, such as a person or animal, enters this field, the sensor sends a HIGH signal. Two potentiometers on the module adjust sensitivity and delay time.

Applications

  • Security systems
  • Automatic lighting
  • People counting
  • Alarm systems
  • Home-automation projects

Pinout

  • VCC: Power supply (4.5V to 20V)
  • OUT: Digital output
  • GND: Ground

How to Use It

To use the HC-SR501 with an Arduino, connect VCC to the Arduino 5V supply, GND to ground, and OUT to an Arduino digital input (for example, D2).

How to Use It with an Arduino Uno

Basic Arduino Code Example

#define PIR_PIN 2
#define LED_PIN 13


void setup() {
  pinMode(PIR_PIN, INPUT);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
}


void loop() {
  int pirState = digitalRead(PIR_PIN);
  if (pirState == HIGH) {
    digitalWrite(LED_PIN, HIGH);
    Serial.println("Mouvement détecté !");
  } else {
    digitalWrite(LED_PIN, LOW);
    Serial.println("Pas de mouvement.");
  }
  delay(1000);
}

Purpose of the Code

This code reads the motion sensor state and turns on an LED when motion is detected. It also displays the sensor state in the Serial Monitor.

Components Required for the Basic Example

  • Arduino Uno
  • HC-SR501 motion sensor module
  • Connecting wires

Detailed Explanation of the Basic Code

  1. Initialization: Serial communication is set to 9600 baud. PIR_PIN and LED_PIN are configured as input and output, respectively.
  2. Reading data: The value from PIR_PIN is read into pirState. If motion is detected, the LED turns on and a message appears in the Serial Monitor. Otherwise, the LED turns off and a different message appears.

Basic Example Wiring

  • VCC: Connect to the Arduino 5V supply
  • GND: Connect to Arduino ground
  • OUT: Connect to Arduino D2

Advanced Arduino Example: LCD Display, Error Handling, and Improvements

#include <LiquidCrystal_I2C.h>


// Constantes
const int PIR_PIN = 2; // Broche du capteur PIR
const int LED_PIN = 13; // Broche de la LED interne de l'Arduino
const int DELAY_TIME = 1000; // Délai entre les lectures en millisecondes


LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialisation de l'écran LCD I2C avec l'adresse 0x27 et une taille de 16x2


void setup() {
  pinMode(PIR_PIN, INPUT); // Configurer la broche PIR_PIN en entrée
  pinMode(LED_PIN, OUTPUT); // Configurer la broche LED_PIN en sortie
  lcd.begin(); // Initialisation de l'écran LCD
  lcd.backlight(); // Allumer le rétroéclairage de l'écran LCD
  Serial.begin(9600); // Initialisation de la communication série à 9600 bauds
}


void readSensorValue(int& pirState) {
  pirState = digitalRead(PIR_PIN); // Lire l'état du capteur PIR
}


void displayValuesOnLCD(int pirState) {
  static int lastPirState = -1; // Variable pour stocker le dernier état du capteur PIR affiché


  if (pirState != lastPirState) { // Met à jour l'affichage uniquement si l'état a changé
    lcd.clear(); // Efface l'écran LCD
    lcd.setCursor(0, 0); // Positionne le curseur à la première ligne
    lcd.print("PIR Sensor: "); // Affiche le texte "PIR Sensor: "
    if (pirState == HIGH) {
      lcd.setCursor(0, 1); // Positionne le curseur à la deuxième ligne
      lcd.print("Mouvement!"); // Affiche "Mouvement!"
    } else {
      lcd.setCursor(0, 1); // Positionne le curseur à la deuxième ligne
      lcd.print("Pas de mouv."); // Affiche "Pas de mouv."
    }
    lastPirState = pirState; // Met à jour le dernier état affiché
  }
}


void displayValuesOnSerialConsole(int pirState) {
  if (pirState == HIGH) {
    Serial.println("Mouvement détecté !"); // Affiche "Mouvement détecté !" sur la console série
  } else {
    Serial.println("Pas de mouvement."); // Affiche "Pas de mouvement." sur la console série
  }
}


void loop() {
  int pirState;
  readSensorValue(pirState); // Lire l'état du capteur PIR
  digitalWrite(LED_PIN, pirState); // Allumer ou éteindre la LED en fonction de l'état du capteur PIR
  displayValuesOnLCD(pirState); // Afficher l'état sur l'écran LCD
  displayValuesOnSerialConsole(pirState); // Afficher l'état sur la console série
  delay(DELAY_TIME); // Attendre avant de refaire une lecture
}

Components Required for the Advanced Example

  • Arduino Uno
  • HC-SR501 motion sensor module
  • 1602 I2C LCD
  • Connecting wires

Detailed Explanation of the Improved Code

  1. Including the library: LiquidCrystal_I2C.h enables communication with an LCD using the I2C protocol.
  2. Defining constants: The constants define the PIR sensor pin (PIR_PIN), LED pin (LED_PIN), and interval between readings (DELAY_TIME).
  3. Initializing the LCD: setup() initializes the LCD and serial communication at 9600 baud. PIR_PIN and LED_PIN are configured as input and output, respectively.
  4. Reading sensor values: readSensorValue(int& pirState) reads the PIR sensor state and stores it in pirState.
  5. Displaying values on the LCD: displayValuesOnLCD(int pirState) displays the PIR state and refreshes the display only when the state changes. A static variable stores the last displayed state.
  6. Displaying values in the Serial Monitor: displayValuesOnSerialConsole(int pirState) sends the PIR state to the serial console.
  7. Main loop: loop() reads the PIR state, updates the LED, displays the state on the LCD and serial console, then waits one second before reading again.

Installing Libraries

Basic and advanced code: The basic example requires no special library. For the advanced example with an I2C LCD, install LiquidCrystal_I2C through the Arduino IDE Library Manager.

  1. Open the Arduino IDE.
  2. Go to Sketch -> Include Library -> Manage Libraries…
  3. Search for LiquidCrystal_I2C and click Install.

 
Keywords: motion sensor, passive infrared, PIR detection, HC-SR501, home security, industrial automation, energy saving.

Thank you for your loyalty!

EN