HC-SR501 motion sensor with Arduino

FunduinoLast updated on

Bewegungen erkennen und ein Warnsignal auslösen

Bewegungsmelder sind aus unserem Alltag nicht wegzudenken:

  • Sie öffnen und schließen automatisch Türen.
  • Sie schalten die Beleuchtung in Räumen oder im Freien.
  • Sie setzen Wasserhähne, Seifenspender und Händetrockner in Gang.
  • Sie schalten die Sirene einer Alarmanlage.

Aufgabe: Ein Lautsprecher soll piepen, sobald eine Bewegung erkannt wird.

Lerninhalt: Die Spannung eines Bewegungsmelders (HC-SR501) auslesen und für eine Ausgabe verwenden.

HC-SR501 motion detector without plastic lens, with visible infrared detector
The infrared detector is located beneath the plastic lens – the contact labels are at the top edge.

You need this

HC-SR501 motion detector - PIR sensor, 3-7 m, 140°, 5 V HC-SR501 motion detector - PIR sensor, 3-7 m, 140°, 5 V 1,99 € *Item no. FUN-1010565About the product — HC-SR501 motion detector - PIR sensor, 3-7 m, 140°, 5 V
5 V buzzer - electromagnetic sounder, 2400 Hz 5 V buzzer - electromagnetic sounder, 2400 Hz 0,90 € *Item no. F23108736About the product — 5 V buzzer - electromagnetic sounder, 2400 Hz
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
830-point Breadboard - solderless breadboard for Arduino 830-point Breadboard - solderless breadboard for Arduino 2,90 € *Item no. FUN-1010553About the product — 830-point Breadboard - solderless breadboard for Arduino
Breadboard Cables Male-to-Male - 40 Pieces, 20 cm Breadboard Cables Male-to-Male - 40 Pieces, 20 cm 1,74 € *Item no. FUN-1010558About the product — Breadboard Cables Male-to-Male - 40 Pieces, 20 cm
Breadboard cables male-female - 40 pieces, 20 cm Breadboard cables male-female - 40 pieces, 20 cm 1,74 € *Item no. FUN-1010554About the product — Breadboard cables male-female - 40 pieces, 20 cm

All 6 parts together 20,39 € *

* All prices include statutory VAT, plus shipping costs.

Choose a microcontroller

Pins, schematic, and sketch in the steps change with your selection. It remains saved.

Get to know the motion detector

The HC-SR501 motion detector, also called a PIR sensor (Passive Infrared Sensor), has a very simple design. As soon as it detects movement, it outputs a voltage of 5 volts on one pin. You just need to read it and have the microcontroller process it.

Use the two rotary controls to set the duration of the output signal (left control) and the sensitivity (right control).

The plastic lens is only lightly attached. If you lift it off, you’ll see the infrared detector and, from the labeling underneath the lens, how to wire the sensor: GND (–), OUT (signal output), VCC (+).

HC-SR501 motion sensor with two potentiometers for duration and sensitivity
The two rotary controls: duration on the left, sensitivity on the right
HC-SR501 motion detector without plastic lens, with visible infrared detector
The infrared detector is located beneath the plastic lens – the contact labels are at the top edge.
1 / 2

Set the jumper correctly

The jumper has two positions:

  • Jumper all the way out: The output signal remains active for a certain period after motion is detected, then switches off regardless—even if movement is still detected within the motion sensor's range. After a certain period, the sensor generates the output signal again.
  • Jumper slightly inward: The output signal remains continuously active as long as the motion sensor detects movement.

The latest version of the motion detector does not require a jumper. You calibrate the sensor directly using the potentiometers.

Jumper positions on the HC-SR501 motion sensor
The two possible jumper positions

Wire and program

The motion sensor connects to pin 7, and the speaker to pin 5.

Circuit diagram with HC-SR501 connected to pin 7 and speaker connected to pin 5 of the Arduino UNO
Schematic: Motion Sensor and Speaker on the Arduino UNO
Program code – Arduino UNO
// Das Wort "Piezo" steht jetzt für den Wert 5.
int Piezo = 5;
// Das Wort "Bewegungsmelder" steht jetzt für den Wert 7.
int Bewegungsmelder = 7;
// Das Wort "Bewegungsstatus" steht jetzt zunächst für den Wert 0.
// Später wird unter dieser Variable gespeichert, ob eine Bewegung erkannt wird oder nicht.
int Bewegungsstatus = 0;
// Hier beginnt das Setup.
void setup()
{
  // Der Pin mit dem Lautsprecher (Pin 5) ist jetzt ein Ausgang.
  pinMode(Piezo, OUTPUT);
  // Der Pin mit dem Bewegungsmelder (Pin 7) ist jetzt ein Eingang.
  pinMode(Bewegungsmelder, INPUT);
}
// Der loop-Teil beginnt
void loop()
{
  // Hier wird Pin 7 ausgelesen. Das Ergebnis wird in der Variablen "Bewegungsstatus"
  // mit dem Wert "HIGH" für 5 Volt oder "LOW" für 0 Volt gespeichert.
  Bewegungsstatus = digitalRead(Bewegungsmelder);
  // Wenn eine Bewegung erkannt wird (das Signal ist HIGH), soll der Lautsprecher piepsen.
  if (Bewegungsstatus == HIGH)
  {
    digitalWrite(Piezo, HIGH);
    delay(5000);              // ... und zwar für 5 Sekunden.
    digitalWrite(Piezo, LOW); // ... danach wird er ausgeschaltet.
  }
  // ansonsten, wenn das erkannte Signal LOW ist ...
  else
  {
    // ... soll der Lautsprecher aus sein.
    digitalWrite(Piezo, LOW);
  }
}
// Mit dieser letzten Klammer wird der loop-Teil geschlossen.

Download: program-code-arduino-uno.ino

Board in the Arduino IDEArduino Uno

You don’t need an additional board package for this. The UNO is included in the Arduino IDE by default.

Done!

Cover und Gehäuse aus dem 3D-Drucker

Für den Bewegungsmelder findest du bei Thingiverse diverse Cover und Gehäuse zum Ausdrucken:

Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.