🔍 How to connect a reed to Arduino

The name "reed switch" comes from the phrase "sealed contact." And that explains his device. In fact, a reed switch is a two open (or closed) contacts that are in a vacuum flask, which change their state to the opposite when exposed to a magnetic field. 
1_1

The name “reed switch” comes from the phrase “sealed contact.” And that explains his device. In fact, a reed switch is a two open (or closed) contacts that are in a vacuum flask, which change their state to the opposite when exposed to a magnetic field. Reed switches are very popular sensors that are used in many applications. This includes the control of opening/closing doors, various counters of operations, speed counters, etc. Let’s see how to connect the reed switch to the Arduino and see how it works.

Instructions for connecting the reed switch to the Arduino
We will need:

  • Arduino UNO or another compatible card;
  • reed switch or module with reed switch;
  • permanent magnet;
  • connecting wires (I recommend this set);
  • breadboard.

Connection diagram reed switch to Arduino

We use this module with a reed switch. Connect it to the Arduino in the given scheme. Power is supplied from 5 V or from 3.3 V. The signal is connected to the digital pin D2.

The module with the reed switch contains a variable resistor of 10 kΩ. This resistor can set the trigger threshold of the reed switch and thus adjust the sensitivity. The module also contains an LM393 comparator for generating a digital signal when a magnetic sensor is triggered.

Sketch reed trigger processing

We will write a sketch of the processing of the reed switch. Everything is simple.

int switchPin = 2; 
int ledPin = 13; 

void setup() {
  pinMode(switchPin, INPUT); 
  digitalWrite(switchPin, HIGH); 
  pinMode(ledPin, OUTPUT); 
  Serial.begin(9600); 
}

void loop() {
  int g = digitalRead(switchPin); 
  digitalWrite(ledPin, !g); 
  Serial.println(g);  
  delay(20); 
}

Set the pin number to which we connect the output of the module – “2”, and turn it on to “wiretap”. Activate the pull-up resistor on the leg “2”. Set 13 pin as a holiday. Turn on the serial port at 9600 baud. And then every 20 milliseconds we read the reed switch readings and give the value to the port. If the reed switch is open, “1” is output; if it is closed, “0” is displayed. In addition, the LED on the 13th leg of the Arduino is lit while the reed switch contacts are closed.

Pay attention to the inversion of the signal read from the sensor.

Checking the sensor with reed switch

Connect power to the Arduino. The LED on the module will light up indicating that the module has power.

Now we bring a permanent magnet to the reed switch – the contacts of the reed switch will close, and the LED will light, indicating the activation of the reed switch. Again, remove the magnet – the reed switch opens, and the LED goes out. If we turn on the monitor of the serial port, we will see the triggering of the reed switch in the form of zeros among the stream of units with the contact open.

Connect reed switch to Arduino directly

Take the usual reed switch, without a module (for example, like this), and connect it to the Arduino. The reed switch is connected in the same way as a button. The sketch code will remain the same.

Let’s assemble the circuit as in the illustration, turn on the Arduino power supply. If you bring a permanent magnet to the reed switch – the Arduino LED will light and shine while the reed switch contacts are closed.

With the help of the reed switch you can do, for example, a door or window opening sensor, a wheel speed sensor or a water level sensor, and much more. But the reed switch, nevertheless, is an electromechanical device, and its service life depends on the intensity of use. Therefore, a magnetic Hall sensor can be used instead of the reed switch.

Share on facebook
Facebook
Share on google
Google+
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on pinterest
Pinterest