Internet of Things
TinkerforgeCode in ActionAbout Me
  • Course Outline
  • 1 - Getting Started
    • Overview
    • Connect to the LED
    • Getting Started
      • Glitch
      • The Application Template
    • Concepts in Programming
      • What is Programming?
      • Variables
      • Functions and Commands
      • Control Structures
      • Loops
      • Objects and Libraries
    • Programming Simple Web Apps
    • Exercises
      • 1.1 Buttons and Inputs
  • 2 - Internet of Things
    • Overview
    • IoT in our Apps
      • Getting Started
        • Hardware Kit
        • Brick Viewer and Daemon
      • Connect to the Devices
        • The Tinkerforge Device Manager
      • Program the Devices
        • RGB LED
        • RGB LED Button
        • OLED Display
        • Sensors
          • Humidity Sensor
          • Ambient Light Sensor
    • Components and Use Cases
    • Exercises
      • 2.1 Lights and Buttons
      • 2.2 Sensors
      • 2.3 Display
  • 3 - Artificial Intelligence
    • Overview
    • AI in our Apps
      • Google's Teachable Machine
      • Face Recognition
      • Training a Custom Model
    • Rules vs. Learning
    • Learning from Data
    • Use Cases
      • Computer Vision
        • Image Classification
        • Handwriting Recognition
    • Machine Learning Algorithms
      • Artificial Neural Networks
      • Decision Trees
      • Logistic Regression
    • Exercises
      • 3.1 Rules vs. Learning
      • 3.2 Fruits and Vegetables
      • 3.3 Face Recognition
      • 3.4 A Classifier for Iris
  • 4 - Cloud & APIs
    • Overview
    • APIs in our Apps
    • Cloud and APIs
      • Weather API
      • NASA Open APIs
      • EDAMAM Nutrition and Recipes API
    • Push Notifications
    • Exercises
  • 5 - App Project
    • Overview
    • Summer 2021
    • Summer 2022
  • Appendix
    • Other Devices
      • Motorized Linear Poti
      • Sound Pressure Sensor
      • NFC Reader
      • Motion Detector
    • UI Features
      • Realtime Charts
      • Countdown Timer
    • Digital Computers
      • Overview
      • The Binary System
      • Code Systems
      • Logic Gates
      • Binary Addition
      • From Analog to Digital
    • Cheat Sheets
    • Projects
      • IoT @ Pickup-Boxes
Powered by GitBook
On this page

Was this helpful?

  1. Appendix
  2. Other Devices

Motion Detector

PreviousNFC ReaderNextUI Features

Last updated 4 years ago

Was this helpful?

Working with a sensor or sensor works as described in the general article on . In this article, we assume you have successfully initialized the devices with the , and you stored all connected devices on a global variable devices. We also declared a global variable called motionDetector.

The following tutorial works for both motion detectors 1.0 and 2.0. The only thing that is different is the device identifier.

Next, we need to know the device identifier, which 292 (Motion Detector 2.0) or 233 (Motion Detector 1.0):

// Get the motion detector sensor via its device identifer
motionDetector = devices.getDeviceByIdentifier(292);

Once we have a reference to the sensor, we can register a callback function:

// We want to be informed when a new sensor value arrives
motionDetector.registerListener(motionDetected);

Of course, we have to actually define the function:

function motionDetected(val) {
   // Do something with the value object
}

Detecting a motion

In the callback function motionDetected(), we can access the information about the event via the val parameter:

function motionDetected(val) {
    var motion = val.getValue();
    
    if(motion.type === "motion_detected") {
        log("Motion detected");    
    }
    else if(motion.type === "detection_cycle_ended") {
        log("Ready to detect another motion");
    }
}

The motion detector sends to types of events:

  1. One type motion_detected, when the sensor detects a new motion.

  2. Following a few seconds after a motion was detected, we get an event detection_cycle_ended, that signals that the sensor is now ready to detect another motion.

Motion Detector 2.0
Motion Detector 1.0
how to use sensors
Tinkerforge Device Manager