This article is the fourth in a series on Internet of Things (IoT) sensors. It teaches you about various sensors available and how to read data from them.


  • Arduino Uno
  • Motion Detection Sensor
  • Jumper Cables

CodifyThings - Internet of Things (IoT) Sensors



Step 1: Make sure your Arduino is not connected to a power source.

Step 2: Using jumper cables connect power (VNC) and ground (GND) ports on Arduino to power (+) and ground (-) ports on the breadboard.

Tip: It is a good practice to use red jumper cable for power (+ / VNC) and black jumper cable for ground (- / GND).

Step 3: Now that your breadboard has a power source, using jumper cables connect power (+) and ground (-) ports of your breadboard to power and ground ports of the Motion Detection sensor.

Step 4: To read motion sensor values, you will need to connect a jumper cable from signal (middle) port of Motion Detection sensor to D3 (Digital) port of your Arduino.

Your circuit is now complete and it should look similar to figures below.

CodifyThings - Internet of Things (IoT) Sensors

Arduino Code

Next, you are going to write Arduino code that will read sensor data. Start your Arduino IDE and either type the code provided below or download it from GitHub. All the code goes into a single source file (*.ino) but in order to make it easy to understand and reuse, it has been divided into 3 sections.

  • External Libraries: includes all libraries required to run the program
  • Sensor Setup: code for reading sensor data
  • Standard Arduino Functions: implementation of standard Arduino functions setup() and loop()
* External Libraries

#include <SPI.h>

* Sensor Setup - Variables & Functions

int calibrationTime = 30;
long unsigned int lowIn;
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;
int pirPin = 3;

void calibrateSensor()
pinMode(pirPin, INPUT);
digitalWrite(pirPin, LOW);

Serial.println("[INFO] Calibrating Sensor ");

for(int i = 0; i < calibrationTime; i++) { Serial.print("."); delay(1000); } Serial.println(""); Serial.println("[INFO] Calibration Complete"); Serial.println("[INFO] Sensor Active"); delay(50); } void readSensorData() { if(digitalRead(pirPin) == HIGH) { if(lockLow) { lockLow = false; Serial.print("[INFO] Activty Detected @ "); Serial.print(millis()/1000); Serial.print(" secs"); Serial.println(""); delay(50); } takeLowTime = true; } if(digitalRead(pirPin) == LOW) { if(takeLowTime) { lowIn = millis(); takeLowTime = false; } if(!lockLow && millis() - lowIn > pause)
lockLow = true;

Serial.print("[INFO] Activty Ended @ "); //output
Serial.print((millis() - pause)/1000);
Serial.print(" secs");


* Standard Arduino Functions - setup(), loop()

void setup()
// Initialize serial port

// Calibrate motion sensor

void loop()

Final Product

Make sure your Arduino is powered on and code from above has been deployed. As soon as the code has been deployed, open serial monitor.

CodifyThings - Internet of Things (IoT) Sensors