Skip to main content

Motion Sensor

Digital
input
Medium RAM
0.4.4

#Description

This sensor detects movements like the sensors that turn on lights in public hallways.

#Aliases

  • PIR Motion Sensor

#Functions



#JavaScript / Node.js

ClassButtonextends ComponentBase

Base class for button and switch components with software debouncing and hold detection.

#Constructor options

OptionTypeRequiredDescription
invertbooleanoptional
isPullupbooleanoptional
isPulldownbooleanoptional
holdTimenumberoptional
pinnumber | DigitalPinrequired

#Events

EventParametersDescription
changevalue: number
down
press
up
release
held

#Methods

updateOptions(options: { invert?: boolean })

Update runtime options such as invert direction.

get isPressedboolean

Check if the button is currently pressed

get valuenumber | undefinedfrom ComponentBase

Get the current value of the component.

#Example

JavaScript
import { Board, Button } from "@blokdots/components";

const board = new Board("/dev/ttyACM0");
await board.open();

const button = new Button({ board, pin: "D5", isPullup: false, isPulldown: false, holdTime: 1000 });

button.on("change", (value) => {
  console.log("Button changed:", value);
});

See the Button reference for the full API including all methods and events.



#C++ (Arduino)

ClassButton

#Setup

C++
#include "blokdots.h"

Button myComponent(eventBus, 5, MY_EVENT, MY_EVENT);

#Constructor

Button(bus, pin, evtDown, evtUp, pullup, debounceMs, invert, sampleIntervalMs)
ParameterTypeDefault
busEventBusrequired
pinuint8_trequired
evtDownuint8_trequired
evtUpuint8_trequired
pullupbooltrue
debounceMsuint32_t30
invertboolfalse
sampleIntervalMsuint16_t2

#Methods

isPressed()bool
emittedValue()int
onHold(holdMs, cb, ctx)int

Returns slot index, or -1 if full.

ParameterTypeDefault
holdMsuint32_trequired
cbEventBus::Handlerrequired
ctxvoidnullptr
setHoldMs(slot, ms)

Optional: update hold time later

ParameterTypeDefault
slotintrequired
msuint32_trequired
begin()

See the Button reference for the full API.