+ All Categories
Home > Documents > Create your own wireless motion sensor with Arduino · They are usually the firs Written By: Feitan...

Create your own wireless motion sensor with Arduino · They are usually the firs Written By: Feitan...

Date post: 20-May-2018
Category:
Upload: buithien
View: 216 times
Download: 2 times
Share this document with a friend
17
Create your own wireless motion sensor with Arduino If you have a friend that has an alarm system in his or her home, I am sure you’ve all seen these white motion sensors that are usually fixed above doors to detect anybody that would walk through the house when the alarm is on. They are usually the firs Written By: Feitan Create your own wireless motion sensor with Arduino © 2017 www.botsbits.org Page 1 of 17
Transcript
Page 1: Create your own wireless motion sensor with Arduino · They are usually the firs Written By: Feitan Create your own wireless motion sensor with Arduino ... Create your own wireless

Create your own wireless motion sensor withArduino

If you have a friend that has an alarm system in his or her home, I am sure you’ve all seen thesewhite motion sensors that are usually fixed above doors to detect anybody that would walk through

the house when the alarm is on. They are usually the firs

Written By: Feitan

Create your own wireless motion sensor with Arduino

© 2017 www.botsbits.org Page 1 of 17

Page 2: Create your own wireless motion sensor with Arduino · They are usually the firs Written By: Feitan Create your own wireless motion sensor with Arduino ... Create your own wireless

INTRODUCTION

In this article, we are going to see that you can reproduce exactly the same functionalities for a muchlower cost, with any sensor you want, and of course using open-source hardware. Let’s dive into theproject !

TOOLS:Arduino IDE (1)

VirtualWire library (1)just place it in the libraries/ folder of your Arduinofolder

PARTS:Arduino UNO R3 board (1)

Teensy board (1)

RF link receiver & transmitter kit (1)

PIR sensor (1)

Breadboard and jumper wires (1)

Create your own wireless motion sensor with Arduino

© 2017 www.botsbits.org Page 2 of 17

Page 3: Create your own wireless motion sensor with Arduino · They are usually the firs Written By: Feitan Create your own wireless motion sensor with Arduino ... Create your own wireless

Step 1 — Create your own wireless motion sensor with Arduino

There are two parts to build in thisproject: the receiver and thetransmitter

Receiver

The receiver, the “hub”, is thesimplest part to assemble. You needthe “big” Arduino board, and the RFreceiver module. On my modulethere were three pins: VCC, GND,and data.

Simply connect the VCC pin the 5Vpin of the Arduino board, the GND tothe GND of the Arduino board, andthe data pin to the pin number 11 ofthe Arduino board.

Transmitter

For the transmitter that is connectedto the sensor, it is also quite simple.First, connect the transmitter. It alsohas three pins to connect: VCC,GND, and data. Simply connect theVCC pin the main power supply ofthe Arduino board you are using.

Be careful, on the Teensy forexample the main power supply ofthe chip is 3.3V. Then, connect theGND to the GND of the Arduinoboard, and the data pin to the pinnumber 12 of the Arduino board.

Create your own wireless motion sensor with Arduino

© 2017 www.botsbits.org Page 3 of 17

Page 4: Create your own wireless motion sensor with Arduino · They are usually the firs Written By: Feitan Create your own wireless motion sensor with Arduino ... Create your own wireless

Finally, connect the PIR sensor. Italso has three pins like the radiomodule. Connect the VCC to themain power supply of your Arduinoboard.

Step 2

Also be careful at this point: the PIRsensor I used could work withoutproblems at 3.3V, but other requires5V because of an on-boardregulator, so you might have to dosome soldering depending on yourboard and sensor combination.

Please refer to the documentation ofyour PIR sensor to know whichvoltage you can use. Then, justconnect the ground to the ground ofthe Arduino board, and the output pinto the pin number 7 of your Arduinoboard.

Create your own wireless motion sensor with Arduino

© 2017 www.botsbits.org Page 4 of 17

Page 5: Create your own wireless motion sensor with Arduino · They are usually the firs Written By: Feitan Create your own wireless motion sensor with Arduino ... Create your own wireless

Step 3

We will take care of the radiotransmission in the last part of thistutorial, so for now, we will simplytest the motion sensor. Most of thePIR sensors simply return a “1″when they detect motion, so on theArduino it is easy to detect motionusing:

sensor_value =digitalRead(sensor_pin);

This is the sketch I used to test thesensor:

const int sensor_pin = 7;

void setup() {

Serial.begin(9600);

delay(1000);}

void loop() {

Create your own wireless motion sensor with Arduino

© 2017 www.botsbits.org Page 5 of 17

Page 6: Create your own wireless motion sensor with Arduino · They are usually the firs Written By: Feitan Create your own wireless motion sensor with Arduino ... Create your own wireless

Step 4

// Get sensor value

sensor_value =digitalRead(sensor_pin);

// Print on serial

Serial.println(sensor_value);

delay(1000);}

You can just upload this sketch,open the serial monitor and play withthe sensor to check if it is workingcorrectly.

Step 5

Now that we made sure that themotion sensor is working, we canwork on the radio transmission. Let’sdeal with the transmitter part first.As I said before, the key in thisproject is to use to VirtualWirelibrary, or you will run into serioustransmission troubles.

Create your own wireless motion sensor with Arduino

© 2017 www.botsbits.org Page 6 of 17

Page 7: Create your own wireless motion sensor with Arduino · They are usually the firs Written By: Feitan Create your own wireless motion sensor with Arduino ... Create your own wireless

For this reason, I will always useVirtualWire functions whentransmitting or receiving datathrough the RF modules. As formany of my tutorials, I will first gothrough the important parts of thecode, and then give you the full codefor the project. It starts by includingthe VirtualWire library:

#include <VirtualWire.h>

In the setup function of the Arduinosketch, we have to initialize thetransmission and set thetransmission rate:

vw_set_tx_pin(transmit_pin);

vw_setup(2000); // Transmissionrate

The transmitter can sustain muchmore than 2000 bauds, but I want tobe on the safe side here, and that’smore than enough for ourapplication. Then, we read the valueof the PIR sensor with:

sensor_value =digitalRead(sensor_pin);

Create your own wireless motion sensor with Arduino

© 2017 www.botsbits.org Page 7 of 17

Page 8: Create your own wireless motion sensor with Arduino · They are usually the firs Written By: Feitan Create your own wireless motion sensor with Arduino ... Create your own wireless

Step 6

We can then prepare the messagethat will be sent through the RF link,and change it if the sensor detectedsome motion:

// Init message

char msg[3] = {'o','f','f'};

// Change message if motion isdetected

if (sensor_value == 1){

msg[0] = 'o';

msg[1] = 'n';

msg[2] = '#';}

Create your own wireless motion sensor with Arduino

© 2017 www.botsbits.org Page 8 of 17

Page 9: Create your own wireless motion sensor with Arduino · They are usually the firs Written By: Feitan Create your own wireless motion sensor with Arduino ... Create your own wireless

Step 7

Yes, I could have transmitted just 0or 1 without any problem, but Iwanted to see how reliable was theRF link to transmit some shortwords, this is why I decided totransmit “on#” or “off”. Finally, it’stime to transmit the message everysecond (and flash the LED toindicate some transmission):

// Transmit data every second

digitalWrite(led_pin, HIGH); // Flasha light to show transmitting

vw_send((uint8_t *)msg, 3);

vw_wait_tx(); // Wait until the wholemessage is gone

digitalWrite(led_pin, LOW);

delay(1000);

Create your own wireless motion sensor with Arduino

© 2017 www.botsbits.org Page 9 of 17

Page 10: Create your own wireless motion sensor with Arduino · They are usually the firs Written By: Feitan Create your own wireless motion sensor with Arduino ... Create your own wireless

Step 8

This is the complete transmittercode:

// Include VirtualWire library

#include <VirtualWire.h>

// Define pins

const int led_pin = 13;

const int transmit_pin = 12;

const int sensor_pin = 7;

int sensor_value;

Step 9

void setup(){

// Init

vw_set_tx_pin(transmit_pin);

vw_setup(2000); // Transmissionrate

pinMode(led_pin, OUTPUT);

pinMode(sensor_pin,INPUT);}

void loop(){

// Get sensor value

Create your own wireless motion sensor with Arduino

© 2017 www.botsbits.org Page 10 of 17

Page 11: Create your own wireless motion sensor with Arduino · They are usually the firs Written By: Feitan Create your own wireless motion sensor with Arduino ... Create your own wireless

Step 10

sensor_value =digitalRead(sensor_pin);

// Init message

char msg[3] = {'o','f','f'};

// Change message if motion isdetected

if (sensor_value == 1){

msg[0] = 'o';

msg[1] = 'n';

msg[2] = '#';}

Step 11

// Transmit data every second

digitalWrite(led_pin, HIGH); // Flasha light to show transmitting

vw_send((uint8_t *)msg, 3);

vw_wait_tx(); // Wait until the wholemessage is gone

digitalWrite(led_pin, LOW);

delay(1000);}

Create your own wireless motion sensor with Arduino

© 2017 www.botsbits.org Page 11 of 17

Page 12: Create your own wireless motion sensor with Arduino · They are usually the firs Written By: Feitan Create your own wireless motion sensor with Arduino ... Create your own wireless

Step 12

Now, the receiver. It is actually quitesimilar to the transmitter code. Thesetup is slightly different:

// Initialise the IO and ISR

vw_set_rx_pin(receive_pin);

vw_setup(2000); // Transmissionrate

// Start the receiver PLL

vw_rx_start();

In the loop of the Arduino sketch, wealso have to create the buffer toreceive the data:

uint8_tbuf[VW_MAX_MESSAGE_LEN];

Step 13

uint8_t buflen =VW_MAX_MESSAGE_LEN;

Then, we actually have to check if amessage was received with:

if (vw_get_message(buf, &buflen))

To finally print the message with:

for (int i = 0; i < buflen; i++)

{ Serial.print(char(buf[i]));

Serial.print(' ');}

Create your own wireless motion sensor with Arduino

© 2017 www.botsbits.org Page 12 of 17

Page 13: Create your own wireless motion sensor with Arduino · They are usually the firs Written By: Feitan Create your own wireless motion sensor with Arduino ... Create your own wireless

Step 14

This last part will go through thereceived message and print everycharacter, which should be “on#” or“off” in our case. This is the finalreceiver code:

// Include VirtualWire library

#include <VirtualWire.h>

// Pins definition

const int led_pin = 13;

const int transmit_pin = 12;

const int receive_pin = 11;

void setup() {

Step 15

// Init

delay(1000);

Serial.begin(9600); // Debugging only

Serial.println("setup");

// Initialise the IO and ISR

vw_set_rx_pin(receive_pin);

vw_setup(2000); // Transmissionrate

// Start the receiver PLL

Create your own wireless motion sensor with Arduino

© 2017 www.botsbits.org Page 13 of 17

Page 14: Create your own wireless motion sensor with Arduino · They are usually the firs Written By: Feitan Create your own wireless motion sensor with Arduino ... Create your own wireless

Step 16

vw_rx_start();

// Set LED pin

pinMode(led_pin, OUTPUT);}

void loop(){

uint8_tbuf[VW_MAX_MESSAGE_LEN];

uint8_t buflen =VW_MAX_MESSAGE_LEN;

// Check if a message was received

if (vw_get_message(buf, &buflen)){

Step 17

// Flash a light to show receivedgood message

digitalWrite(led_pin, HIGH);

Serial.print("Got: ");

// Print message

for (int i = 0; i < buflen; i++){

Serial.print(char(buf[i]));

Serial.print(' ');}

Serial.println();

Create your own wireless motion sensor with Arduino

© 2017 www.botsbits.org Page 14 of 17

Page 15: Create your own wireless motion sensor with Arduino · They are usually the firs Written By: Feitan Create your own wireless motion sensor with Arduino ... Create your own wireless

Step 18

digitalWrite(led_pin, LOW);}}

Create your own wireless motion sensor with Arduino

© 2017 www.botsbits.org Page 15 of 17

Page 16: Create your own wireless motion sensor with Arduino · They are usually the firs Written By: Feitan Create your own wireless motion sensor with Arduino ... Create your own wireless

Step 19

It is now time to test everything !The first thing to do is to upload theright sketch to the right board. It canbe tricky if you have two boardsconnected to your computer at thesame time, so this is what I did.

First, connect the transmitter boardto your computer and upload thetransmitter sketch. Then, disconnectthe USB cable, actually put all thetransmitter part away from yourcomputer (like in real situation, right?) and plug the Arduino board to anexternal power source (for the Unoboard, between 7V and 12V.

For other boards, refer to thedocumentation of your board). Atthis point, the LED of the boardshould blink every second toindicate that a message wastransmitted.

Then, connect the receiver board toyour computer and upload thereceiver sketch. You shouldimmediately see the LED of thereceiver board blinking to indicatethat it received a message. Finally,open the serial monitor and youshould see something similar to thepicture.

Create your own wireless motion sensor with Arduino

© 2017 www.botsbits.org Page 16 of 17

Page 17: Create your own wireless motion sensor with Arduino · They are usually the firs Written By: Feitan Create your own wireless motion sensor with Arduino ... Create your own wireless

This document was last generated on 2017-06-28 12:32:35 AM.

Step 20

From that point, there are manythings you can do with this project.You can put the transmitter partwherever you want in your home(however, you might need to insertantennas on the RF module if youhave a big home), and power theArduino board with an externalpower source.

Then, on the receiver side, you canfor example implement an alarmmechanism that goes on whenmotion is detected. Or couple theproject to the Arduino GSM shield tosend you a SMS wheneversomething is detected. Thepossibilities are limitless, soexperiment & have fun !

Create your own wireless motion sensor with Arduino

© 2017 www.botsbits.org Page 17 of 17


Recommended