Ardumotive Arduino Greek Playground
  • Home
    • About us
    • Contact
    • License
  • Arduino
    • Tutorials
    • Workshop
  • Raspberry Pi
  • DIY 3D Printer
  • News
    • Events >
      • Unboxing & Review
    • Blog
The 1st Arduino Playground in Greece - Open Source Hardware

Magnetic contact switch (door sensor)​

Available Languages
Picture
Picture

Introduction 

Picture

In this tutorial we will use one magnetic - contact door sw / sensor and a buzzer for making a "noise" when a door is opened. Normally the reed is 'open' (no connection between the two wires). The other half is a magnet. When the magnet is less than 13mm (0.5") away, the reed switch closes.

What you will need - Hardware

Picture
For this project you will need:

  • Arduino uno
  • Magnetic Contact Switch - Door Sensor
  • Buzzer
  • Breadboard and some cables

The Circuit

Ardumotive Tutorial - Arduino Shake Vibration Sensor
The connections are pretty easy, see the above image with the breadboard circuit schematic.

The code using Codebender

​Here's the code, embedded using Codebender!
Try downloading the Codebender plugin and clicking on the "Run on Arduino" button to program your Arduino board. And that's it, you've programmed your Arduino with this sketch!
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/* Arduino Tutorial: How to use a magnetic contact switch 
   Dev: Michalis Vasilakis // www.ardumotive.com // Date: 4/8/2016 */

const int buzzer = 3; 
const int sensor = 4;

int state; // 0 close - 1 open wwitch

void setup()
{
	pinMode(sensor, INPUT_PULLUP);
}

void loop()
{
	state = digitalRead(sensor);
	
	if (state == HIGH){
		tone(buzzer, 400);
	}
	else{
		noTone(buzzer);
	}
	delay(200);
}
Download the code from here and open it with Arduino IDE. ​
magneticcontactsw_tutorial.zip
File Size: 0 kb
File Type: zip
Download File

Well done!

Picture
You have successfully completed one more "How to" tutorial and you learned how to use a magnetic contact switch with Arduino.
​
I hope you liked this, let me know in the comments.
Picture

Search Engine

Picture

Licence 

Picture

Help us to grow up!

Picture


Donate us
About us
License
Cookies policy

Visit the biggest Arduino Shop in Greece!

Picture
find us on dwrean.net
find us on Codebender
find us on Instructables
Developed and designed by Vasilakis Michalis Copyright © 2013 Ardumotive All Rights Reserved
All trademarks referenced herein are properties of their
Powered by Create your own unique website with customizable templates.
Design by DivTag Templates