O'Reilly logo

Internet of Things Programming with JavaScript by Ruben Oliva Ramos

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Detecting if the door is open with a reed switch

An example has been added as an option to implement a magnetic sensor in order to detect when a door or window is open or closed.

Detecting if the door is open with a reed switch

The sensor outputs a 0 when it detects the magnetic field and when the field is far away the output would be a 1; so you can determine when the door is open or closed.

The program in the Arduino is performed as follows:

We define the input signal of the sensor, and configure the serial communication:

void setup() { 
  pinMode(sensor, INPUT_PULLUP); 
  Serial.begin(9600); 
} 

We read the state of the sensor:

void loop() { 
state = digitalRead(sensor); 

It compares the digital input and ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required