Skip to main content

Electromagnet

Digital
output
Small RAM
1.1.1

#Description

The electromagnet can create a magnetic field when power is applied to it. The magnetic field can then manipulate objects or materials affected by magnetism. Under ideal circumstances, the Grove Electromagnet can hold up to 1kg.

The Grove component has a protective circuit in front of the electromagnet. Do not connect your own electromagnets without a circuit protecting against blowback voltage or you might damage your board.

#Units

  • State

#Functions



#JavaScript / Node.js

Base class for digital output components (on/off control, blink, toggle).

#Constructor options

OptionTypeRequiredDescription
invertbooleanoptional
pinnumber | DigitalPinrequired

#Methods

on()this

Turn the component on (no arguments) or register an event listener.

off()this

Turn the component off

toggle()this

Toggle the component state

set(value: 0 | 1)this

Set the value of the component

blink(interval: number = 500)this

Blink the component at a given interval (Arduino-side animation)

stop()this

Stop any running animation (blink, pulse)

get isOnboolean

Check if the component is currently on

get isAnimatingboolean

Check if an animation is running

get valuenumber | undefinedfrom ComponentBase

Get the current value of the component.

#Example

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

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

const digitalActor = new DigitalActor({ board, pin: "D5" });

digitalActor.on();
digitalActor.off();
digitalActor.toggle();

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



#C++ (Arduino)

#Setup

C++
#include "blokdots.h"

DigitalActor myComponent(5);

#Constructor

DigitalActor(pin, invert, startOn)
ParameterTypeDefault
pinuint8_trequired
invertboolfalse
startOnboolfalse
DigitalActor(bus, pin, evtChange, invert, startOn)

Constructor with EventBus for trigger support

ParameterTypeDefault
busEventBusrequired
pinuint8_trequired
evtChangeuint8_trequired
invertboolfalse
startOnboolfalse

#Methods

on()
off()
toggle()
emittedValue()int

Get the current state value (0 or 1) for triggers

stop()DigitalActor&

Stop any running animation (blink/pulse)

blink(intervalMs, durationMs)DigitalActor&

durationMs=0 => blink forever

ParameterTypeDefault
intervalMsuint16_t500
durationMsuint32_t0
blink(onMs, offMs, durationMs)DigitalActor&

durationMs=0 => blink forever

ParameterTypeDefault
onMsuint16_trequired
offMsuint16_trequired
durationMsuint32_t0
pulse(onForMs)DigitalActor&

One-shot pulse: ON for onForMs then OFF (no repeating)

ParameterTypeDefault
onForMsuint32_trequired

See the DigitalActor reference for the full API.