Arduino and Raspberrypi collaboration for motion detection

The above video will show the final assembled part of the motion detector.
I will try to have additional videos on how to setup each device from the scratch.

Below you will find the code that is used in the arduino part.

The basic part would be
– The arduino will detect the motion
– It will hit the API inside the raspberry pi
– The raspberry pi will log the motion being detected into the database.

After this, any other system can make the use of the data that has been logged into the database.


 Web client
 This sketch connects to a raspberry pi RoR websever
 using an Arduino Wiznet Ethernet shield. 
 * Ethernet shield attached to pins 10, 11, 12, 13
 Modified for motion by Kaleb Woldearegay
 created 18 Dec 2009
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe, based on work by Adrian McEwen

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
byte ip[] = {192,168,2,4}; //shield's ip
byte geteway[] = {192,168,0,1};
// Set the static IP address to use if the DHCP fails to assign
IPAddress server(192,168,2,9); //pi's ip

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

//sensor and LED
int calibrationTime = 30; 

//the time when the sensor outputs a low impulse
long unsigned int lowIn; 

//the amount of milliseconds the sensor has to be low 
//before we assume all motion has stopped
long unsigned int pause = 5000; 

boolean lockLow = true;
boolean takeLowTime; 

int pirPin = 7; //the digital pin connected to the PIR sensor's output
int ledPin = 13;

//end sensor and LED

void setup() {
 // Open serial communications and wait for port to open:
 while (!Serial) {
 ; // wait for serial port to connect. Needed for Leonardo only
 //sensor and LED
 pinMode(pirPin, INPUT);
 pinMode(ledPin, OUTPUT);
 digitalWrite(pirPin, LOW);
 //give the sensor some time to calibrate
 Serial.print("calibrating sensor ");
 for(int i = 0; i < calibrationTime; i++){
 Serial.println(" done");
 Serial.println("SENSOR ACTIVE");
 // start the Ethernet connection:
 if (Ethernet.begin(mac) == 0) {
 Serial.println("Failed to configure Ethernet using DHCP");
 // no point in carrying on, so do nothing forevermore:
 // try to congifure using IP address instead of DHCP:
 Ethernet.begin(mac, ip);
 // give the Ethernet shield a second to initialize:

void loop()
 //sensor and LED stuff
 if(digitalRead(pirPin) == HIGH){
 digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
 //makes sure we wait for a transition to LOW before any further output is made:
 lockLow = false; 
 Serial.print("motion detected at ");
 Serial.println(" sec"); 
 takeLowTime = true;

 if(digitalRead(pirPin) == LOW){ 
 digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state

 lowIn = millis(); //save the time of the transition from high to LOW
 takeLowTime = false; //make sure this is only done at the start of a LOW phase
 //if the sensor is low for more than the given pause, 
 //we assume that no more motion is going to happen
 if(!lockLow && millis() - lowIn > pause){ 
 //makes sure this block of code is only executed again after 
 //a new motion sequence has been detected
 lockLow = true; 
 Serial.print("motion ended at "); //output
 Serial.print((millis() - pause)/1000);
 Serial.println(" sec");
 // if you get a connection, report back via serial:
 if (client.connect(server, 3000)) {
 // Make a HTTP request:
 client.println("GET /increment");
 client.println("Connection: close");
 else {
 // kf you didn't get a connection to the server:
 Serial.println("connection failed");