Table of Contents
CODE BLOCKS FOR PIR SENSOR AND ARDUINO IN TINKERCAD
In this section, you will see code blocks for PIR sensor and arduino in tinkercad. So let understand what is PIR sensor.
PIR sensor (Pyroelectric Infrared Radial Sensor or Passive Infrared Sensor) is a low-cost sensor used to detect infrared-emitting objects such as the presence of human beings and animals. It is also known as a motion detector sensor.
This sensor has three output pins which are power (VCC), ground (GND) and output or signal. Its signal/output pin is 3.3 V TTL logic thus it can be used with various kinds of microcontroller boards like Arduino, Raspberry pi, 8051, PIC, etc.
REQUIRED HARDWARE OR COMPONENT
S.N. | COMPONENTS | QUANTITY |
1. | Arduino Uno | 1 |
2. | PIR Sensor | 1 |
3. | LED | 1 |
4. | Resistor | 1 |
CIRCUIT DIAGRAM
CONNECTION TABLE FOR CODE BLOCKS FOR PIR SENSOR AND ARDUINO IN TINKERCAD
S.N. | ARDUINO | LED | PIR SENSOR |
1. | 13 | Anode (+) | |
2. | GND | Cathode (–) | |
3. | 2 | Signal | |
4. | 5 V | Power | |
5. | GND | Ground (GND) |
CIRCUIT EXPLANATION
Here, we are using a PIR sensor which has three pins known as signal, power and GND. These pins are connected to pins 2, 5V and GND of Arduino respectively.
An LED whose anode terminal is connected to Arduino’s output pin 13 and the cathode terminal is connected to the ground pin of Arduino and a resistor is placed between the LED anode terminal and pin number 13 which help us to limit the current and prevent LED from burning it.
In this circuit when the PIR sensor detects any infrared signal then the LED will Light up otherwise it will be off.
ARRANGEMENTS OF BLOCKS AND TEXT CODE FOR PIR SENSOR AND ARDUINO IN TINKERCAD
CODE BLOCKS AND TEXT CODE EXPLANATION
BLOCK EXPLANATION
Step 1: First of all click on the variables blocks then click on the create a variable option
then we have to write a new variable name as “pirsensor”
then click on OK. After that, we get three command blocks in the variable blocks like this
Then we will drag the set command block
and drop it at the work area.
Step 2: After that, go to the input blocks option drag “read digital pin” command block
and drop it into set command block like this
After that, change “read digital pin” from 0 to 2 from the dropdown menu, where the PIR sensor output pin is connected.
Step 3: Click on the control block and drag the “if else” command block from it and drop it on the work area below the set command block.
Step 4: Then, go to the math blocks and drag the second block
and drop it over the “if” hexagon shape command block at the work area like this
Step 5: Then again go to the variable blocks and drag the variable “pirsensor”
and drop it on the first elliptical shape inside the math block under “if” command block
after that select equal sign from the dropdown menu
Step 6: Then, we will go to the output blocks and drag and drop “set pin” command block
under the if – statement block. After that, change “set pin” from 0 to 13 (the pin on which the LED is connected in the above circuit diagram) from the dropdown menu and select HIGH
from the dropdown menu.
Step 7: Go to the output blocks and drag the “print to the serial monitor” command block from it and drop it at the work area below the set pin block and write “Motion detected, presence of human or animals” at the place of default “hello world” written.
Step 8: Now, again select the “set pin” command block from it and drop it under the else-command block. After that, click the “set pin” from 0 to 13 from the dropdown menu and select LOW from the next dropdown menu.
Step 9: Go to the output block and drag print to the serial monitor command block from it and drop it at the work area below the set pin block and write “Motion not detected” at the place of default “hello world” written.
Step 10: At the end go to the output block and drag the “wait” command block from it and drop it at the work area below the “if-else” command block
This wait command block is used to pause the program for 1 second you can vary the wait time.
TEXT CODE EXPLANATION
Step 1: Here the first global declaration integer type variable is created and set to 0, using this we will be able to read the value from pin 2. That is,
int pirsensor = 0;
Step 2: In the setup() function it automatically generates lines of code as soon as we select input or output blocks.
The Function “pinMode( )” has been defined here in the pin working mode as an input/output.
In the first pinMode() function two arguments are passing.
- First one is pin number.
- And the second is its INPUT mode.
pinMode(2, INPUT);
In the second pinMode() function two arguments are passing.
- First one is pin number.
- And the second is its OUTPUT mode.
pinMode(13, OUTPUT);
Step 3: In the third line code “Serial.begin()” function is used to set the baud rate for serial communication.
Serial.begin(9600);
Step 4: In the loop() function, the digitalRead() function is used to read the value (state) from pin number 2 of the Arduino and we will store the value using variable pirsensor. This will be either High or LOW.
Depending on the variable pirsensor value, we will write High or LOW on pin 13 of Arduino using the digitalWrite() function.
In the “if-else” statement, here we check the variable “pirsensor” value, if it is HIGH, (this means statement becomes true) the block of “if” will execute and we write the HIGH value in digitalWrite() function for pin 13 of Arduino.
If this statement is false, the block of “else” will execute and we write the LOW value in digitalWrite() function for pin 13 of Arduino.
/* Tutorial: https://pijaeducation.com/tinkercad/ */ int pirsensor = 0; void setup() { pinMode(2, INPUT); pinMode(13, OUTPUT); Serial.begin(9600); } void loop() { pirsensor = digitalRead(2); if (pirsensor == 1) { digitalWrite(13, HIGH); Serial.println("Motion detected , presence of human or animals"); } else { digitalWrite(13, LOW); Serial.println("Motion not detected"); } delay(1000); // Wait for 1000 millisecond(s) }
START SIMULATION
Click on the start simulation button to perform the PIR sensor program and to test the circuit.
[pad]
PREVIOUS POST
CODE BLOCKS FOR ULTRASONIC RANGE FINDER PROJECT USING ARDUINO IN TINKERCAD