Arduino Uno : Using "HC-SR501" to make motion-detecting sensors

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@sikul·
0.000 HBD
Arduino Uno : Using "HC-SR501" to make motion-detecting sensors
#### What Will I Learn?
<hr>

- You will learn to use HC-SR501 module
- You will learn how to integrate arduino uno and Sensor PIR HC-SR501
- You will learn how to program arduino uno and HC-SR501 to make motion-detecting sensors

##### Project Introduction
<hr>
Here is a tutorial arduino uno combined with PIR HC-SR501 sensor. HC-SR501 functions as an infrared transmitter and detects reflections (if there is an objeck nearby). When the prototype on the run, if you put something can move on the sensor or try to move your hand around the sensor and sensor will define the motion object and LED directly "ON" when detecting the motion , if no motion LED will be "OFF".


#### Requirements
<hr>

##### Hardware

- Arduino Uno
- HC-SR501 Module
- Jumper Wires Male To Male
- LED 
- Resistor 220 Ohm
- Breadboard
- USB type A to B cable
- Personal Computer (Laptop)

#### Component Description
<hr>

**Arduino Uno** - is a Atmega32-based microcontroller board. Having many digital and analog pins, the arduino can be easily programmed via a computer or laptop with USB and is designed to facilitate electronic users in various fields. As open source he comes with 2 products namely Hardware Arduino Board and Arduino IDE software.

**HC-SR501 Module** - is a PIR sensor that is often used in microcontroller applications including arduino uno for the detection of an object on the monitor. HC-SR501 has 3 pin, 1 pin GND (-), 1 pin VCC (+) and Pin OUT. The HC-SR501 senses the object with the help of infrared reflection, which is emitted in a radius of angle of (110°) distance of 5-7 meters.

##### Software
- Arduino IDE

![Arduino_IDE.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1519644592/poniq2kk10zhnf0kb2oe.png)


- Fritzing BETA

#### Difficulty

-  Basic

#### Tutorial Contents

##### Step 1 : Prepare All Parts
<hr>

You can buy from online store or directly to the Electronics Shop
![bahan.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1519571493/d1k7peu9bfg0bpkhhhek.png)

##### Step 2 : Build Circuit Architecture
<hr>

Using the fritzing software, we will create our Circuit Architecture. Open The Fritzing Beta - choose BreadBoard.

- **Prepare Module**

Open fritzing and prepare all part you need to make the Circuit Architecture , you can make the circuit with another program , like proteus, Tinkercad etc.

![1.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1519573573/vtqlrp3bqy4hzjcfnmij.png)

- Choose BreadBoard On the Fritzing (You can see the Image)
- Choose Arduino uno to the project , you can search or click the arduino Label

<hr>


- **Prepare resistor (220 Ohm) and LED**

![HC-SR501-1.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1519574341/k6vf68tqzsdoxacgdkzn.png)

- Choose the LED to BreadBoard , you can search 
- Choose the Resistor (220 Ohm) to BreadBoard , you can search in the search box
- Choose the HC-SR501 module  , you can search in the search box

<hr>

After you Prepare all the part now connect the module (LED &  HC-SR501) on the breadBoard to the arduino board using the jumper wire. To add the wire just click the port and drag to another port.

![HC-SR501-2.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1519576527/gsvqayfwdcvlxzkn7ufb.png)

- Connect Pin GND (-) Arduino Uno to (-) LED
- Connect Pin Number 13 Arduino Uno to (+) LED


![HC-SR501-4.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1519644139/ptsqxhkdqbezxaajsjh0.png)

- Connect Pin GND (-) Arduino Uno to GND (-) to HC-SR501.
- Connect Pin VCC (+) Arduino Uno to VCC (+) to HC-SR501.
- Connect the "pin number 2" of the arduino uno board to "OUT" to HC-SR501.


##### Step 3 : Programing

<hr>

- Connect Arduino to the computer with USB . 
- Open the Arduino IDE  > Chose your Board Type 
![IDE.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1518282215/dbhjjmartyvv00x8nou3.png)

<hr>

![Source_1.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1519646948/o4crm8ath57g3rl23nmn.png)

- first,initialize Variable you can use <code> #int . . </code> , <code> #float . . </code> , <code> #double . . </code> and <code> #char . . </code> in the case we will use <code> #int . . </code>

```C+

int pinLED = 13; 
int InPIR = 2; 
int stat = 0; 
int variable = 0; 

```

The explenation of the coding :

```int pinLED = 13; ``` to initialize pinLED LED

``` int InPIR = 2; ``` to initialize Vout PIR

```int stat = 0; ``` to initialize logical status

```int variable = 0; ``` to initialize temporary variable to accommodate the PIR variable

<hr>

![Source_2.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1519646984/ows6an2ofs68gcuuslqh.png)

- After Initialize, you Now Set Up the All PinMode <code>  void setup() </code>

```C+

void setup() {
Serial.begin(9600); 
pinMode(pinLED, OUTPUT);
pinMode(InPIR, INPUT); 
} 

```

In this step you will set up all command when the prototype start to begin. In the Project set <code>Serial.begin(9600); </code> As serial monitor , pin 2 as input and set pin 13 as output.

The explenation of the coding :

```Serial.begin(9600); ``` to set up serial monitor

```pinMode(pinLED, OUTPUT); ``` to set pin 13 as output

```pinMode(InPIR, INPUT); ``` to set pin 2 as input

<hr>

![Source_3.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1519646908/oq355ocd5tbgrffsy0ew.png)


- Last program is void loop() everything about the system can be run continuously.

```C+
void loop(){
variable = digitalRead(InPIR);
if (variable == HIGH) &amp;&amp; (stat == LOW)) 
{ 
 digitalWrite(pinLED, HIGH); 
 Serial.println("Motion detected!"); 
 stat = HIGH; } 
else {
  if ((variable == LOW) &amp;&amp; (stat == HIGH)){
   digitalWrite(pinLED, LOW);
   Serial.println("Motion ended!");
   stat = LOW;
   }
 }
```
 
The explenation of the coding :

```variable = digitalRead(InPIR);``` to Read The input from InPIR

```if (variable == HIGH) &amp;&amp; (stat == LOW))``` to check if there is movement or motion

```digitalWrite(pinLED, HIGH);``` to Turn On LED on The Breadboard

```Serial.println("Motion detected!");``` to Display "Motion detected!" To laptop

```stat = HIGH;``` high so as not to detect continuously

```digitalWrite(pinLED, LOW);``` to Turn Off LED on The Breadboard

```Serial.println("Motion ended!"); ``` to Display “Motion ended!” To laptop

##### Full program will be like this
![Full_Program.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1518800819/o4sykz1fhas1xkdnfpzw.png)


After write codes, click the "Verify button" to save and compile the sketch. This will also check for any errors in the program.

![IDE_1.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1518800873/jhqbot3xkao99mdue5qv.png)

If no errors is found , now you can click "Upload button" to start installing the program into the arduino uno board.

![IDE_2.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1518800908/hqo2kps4hoeat7ond3rp.png)




#### Curriculum


- [Arduino : Using "LM-35" a Termo Sensor Module](https://steemit.com/utopian-io/@sikul/5mtbxv-arduino-using-lm-35-a-termo-sensor-module)

- [Arduino : "Using TCS230 to make Color sensing"](https://steemit.com/utopian-io/@sikul/arduino-using-tcs230-to-make-color-sensing)


    


<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@sikul/4y1dmj-arduino-uno-using-hc-sr501-to-make-motion-detecting-sensors">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍 , , , ,