How to use: HC-SR501

Are you a starter with Croduino? Or a newbie when it comes to electronics? You fancy a specific module, but you don't know how to use it? Don't worry, we have our How To Use series!

How to Use is a series of blog tutorials by e-radionica where you will find everything you need to begin working with your favorite modules. Tutorials include technical characteristics, operating principles, instructions on how to connect the module with Croduino and basic coding. Everything else is up to you and your imagination.

Basic characteristics

HC-SR501, aswell known as PIR (Passive Infrared), Pyroelectric or IR motion sensor, enables motion detection within the sensor's range. It's exactly that sensor you can find in lighting at your/ neighbours doorway. The basic part is a pyroelectric sensor that detects the amount of infrared radiation. Each being, objects radiate with a certain amount of radiation. Warmer something is, it radiates more. Therefore, this sensor is mainly used to detect the movements of living beings. Keep in mind that this sensor cannot give you information about the distance on which something is detected. If you need this particular option, check out a HC-SR04 module.

DSC_8529
Characteristics:
Voltage: DC 4,5 - 20V
Current: 50uA
Angle: < 120
Distance: 3m - max7m
Lens diameter: 23mm
Dimensions 32 x 24mm

Basic working principle

As already mentioned, the main part is the pyroelectric sensor that has two slits/halves. If one slit senses more radiation than the other, the sensor will automatically switch from HIGH to LOW, or vice versa. For example, the sensor is enclosed and reads the radiation of walls and other objects in its range. When a warm body passes (human or animal), it affects one half of the sensor that reads the change of radiation in the range. It is opposite when body leaves the range of the sensor. Exactly those changes are registered by the sensor. For more details, check out the datasheet.
Screen Shot 2015-05-18 at 16.24.48

Second most important part of the HC-SR501 sensor is its lens. It may vary depending on curvature, width, material, pattern, etc., so most of the magic of this sensor can happen with optics. Everything already mentioned greatly changes the final characteristics of the sensor which opens space for detailed adjustments.

DSC_8530
Use the sensor without lens if you want to have a small detection angle(around 15 degrees)

How to connect module with Croduino

Before you connect modules pins with Croduino, I would draw attention to two potentiometers which are mounted on the module. By adjusting them, the sensitivity (on the board marked "Sx") and the time of a delay ("Tx") are adjusted as follows: Turning clockwise to increase sensitivity and prolong the delay, while turning counter-clockwise to reduce sensitivity and to short the delay. This means that you can use the module even without a controller. Be careful, the module gives HIGH in the idle state and gives LOW when movement is detected. For the purpose of this tutorial, the module is tested inside a closed environment and sensitivity potentiometer is set to medium and delay to shortest.
We connect pins as follows:

Vcc - +5V
OUT - digital PIN8
GND - gnd

Progetto PIR con accensione LED_bb

Code for module

As always, we provide a basic code that puts this module in operation. The output results are read via the Serial Monitor and the Digital PIN 13 (orange led embedded in the Croduino), or if we merged an extra LED as shown in the picture. The following code can be written by you aswell. From the tutorials, we know that the sensor has a LOW value at the output pin during reading. Everything else is left to the imagination.

int inPin = 9; // signal s HC-SR501 na pinu9
int led = 13;  // ledica na pinu13

boolean ocitanje;

void setup() {
  pinMode(inPin, INPUT);
  pinMode(led, OUTPUT);
}

void loop() {
  // kada je HC-SR501 aktivan salje LOW signal,
  // pošto će HIGH signal upaliti ledicu promjeniti cemo mu vrijednost  
  ocitanje != digitalRead(inPin); // ocitanje je suprotno od stanja pina9
  
  // konačno, stanje led pina, postavljamo s obzirom na ocitanje
  digitalWrite(led, ocitanje);
}
Leave a Reply