Dec.01

Push Notification from Arduino Yún with motion sensor (Part 1/2)

Due to the surprising popularity of my first post and the fact that it’s content was targeted mainly to people that are already a bit experienced (at least enough to admit IntelliJ’s superiority over Arduino IDE 🙂 ), I’ve decided to keep on researching about Internet of Things from my Android dev’s perspective.

Prerequisites

Circuit preparation

Wire up your Arduino with motion sensor using below schematic (made in Fritzing).

 

 

Simple as that, now we have to…

Make sure it’s up

Initialize a new Arduino project (I recommend catching up with my last post before doing that).

mkdir YunPush
cd YunPush
platformio init --board=yun --ide=clion

Now create a new file in /src directory:

#include "Arduino.h"

#define PIR_PIN 7
#define LED_PIN 13

int pirState = 0;

void setup() {
    pinMode(PIR_PIN, INPUT);          // Set pin as input
    pinMode(LED_PIN, OUTPUT);         // Set pin as output
}

void loop() {
    pirState = digitalRead(PIR_PIN);
    if(pirState > 0) {
        digitalWrite(LED_PIN, HIGH);  // Set the LED on
    } else {
        digitalWrite(LED_PIN, LOW);   // Set the LED off
    }
}

This is the desired effect at this point.

 

Theoretically, you could keep on debugging your code using this one LED (and your own, original Morse code variation 😉 ) but let’s prepare for next steps a little bit better, and….

Monitor your Arduino’s Serial Port

Modify your code so it writes the desired output using Arduino’s Serial.

#include "Arduino.h"

#define PIR_PIN 7

int pirState = 0;

void setup() {
    Serial.begin(9600); // Initialize 9600 bits per second connection
    delay(1000);
}

void loop() {
    pirState = digitalRead(PIR_PIN);
    if(pirState > 0) {
        Serial.write("Movement detected!\n");
    } else {
        Serial.write("Nothing.\n");
    }
}

Build & upload your code then start monitoring the output in reference to PlatformIO’s documentation. In my case it’s:

platformio serialports list
platformio serialports monitor -p /dev/tty.usbmodem1421

Press (Ctrl + ]) to stop. Now when you’ve got a nice insight into Yún’s state you can optionally…

Understand and calibrate PIR

PIR sensor has two variable resistors marked as Ch1 and RL2.

  • Ch1 let’s you set the time that the output of the sensor will be active (one of the main limitations of this module is that the minimum time is about 3 seconds).
  • RL2 allows you to set the detection range, which could vary between 3-7m.

Note (partly from Wikipedia): PIR (Passive Infrared) sensor detects changes in levels of infrared radiation. The term passive in this instance refers to the fact that PIR devices do not generate or radiate any energy for detection purposes. All objects with a temperature above absolute zero emit heat energy in the form of radiation. PIR changes output voltage level when a contrast (to background objects at room temperature) is detected – that’s why it needs few seconds to autocalibrate after turning on.

 

Continue to part 2 ->

 

Arduino,Programming,Android
Share this Story:
  • facebook
  • twitter
  • gplus

Leave a comment

Comment