Interfacing Reed Switch with Arduino

If you noticed that when we are turn the screen on and off the laptop will automatically shut down and starts. How it is possible. If you think it is possible using some type of sensor then you are right. Here we are using reed switches. We will discuss about reed switches in this tutorial.
These switches are cheap and reliable. These are very easy to use. These switches turn on and off when there is any magnet nearby.

Reed Switch Overview

A reed switch consists of pair of magnetic plates or reeds made of ferromagnetic material. The surface of reeds are coated with some type of material such as palladium etc. which gives life to the switch as they open and close for millions of times. These reeds are then enclosed in the glass capsule to prevent them from dust and air.

How Does a Reed Switch Work?

It works on the principle of magnetic field. When you bring the magnet close to the switch then switch becomes the part of the circuit include magnets.
When you bring the magnet close to the switch the plates gives contact to each other as a result the current flows. When you bring magnet away from the switch the plates move far from each other. Now current cannot flow through them.

Wiring up a Reed Switch to an Arduino

The connection of reed switch is very simple. First of all you have to inset reed switch in the breadboard and using jumper connect one wire with Arduino GND and the other goes to any digital pin of Arduino.

const int REED_PIN = 2;	// Pin connected to reed switch
const int LED_PIN = 13;	// LED pin

void setup() {
	Serial.begin(9600);
	pinMode(REED_PIN, INPUT_PULLUP);	// Enable internal pull-up for the reed switch
	pinMode(LED_PIN, OUTPUT);
}

void loop() {
	int proximity = digitalRead(REED_PIN); // Read the state of the switch
	
	// If the pin reads low, the switch is closed.
	if (proximity == LOW) {
		Serial.println("Switch closed");
		digitalWrite(LED_PIN, HIGH);	// Turn the LED on
	}
	else {
		Serial.println("Switch opened");
		digitalWrite(LED_PIN, LOW);		// Turn the LED off
	}
}

Code explanation

Firs initialize Arduino pins that we are using.

Now in void setup we declare led pin as output and digital pin 2 as input enabling internal pull up.

In void loop the led will turn on when switch is low otherwise the led remains off.

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
YouTube
× Contact us