You are a beginner with Croduino. Or electronics? A specific module caught your eye, but you do not know how to use it? Do not worry, HUM is here for you! How to Use Module (HUM) is a blog tutorials series by e-radionica where you will find all you need in order to begin working with your favorite module. Tutorials include: technical characteristics, work principle, instructions on how to connect module with Croduino and the basic code. Everything else is left to your imagination.


In this tutorial, we will introduce you to the magnetic switch and some of its applications. Magnetic switch consists of two components, a magnet and a reed switch located in plastic housings that can be easily attached to doors, windows or drawers. When the door opens or closes, the magnetic switch detects the change.

• Voltage: max. 200V, best to use 5V of Croduino/Arduino
• Maximum current through reed: 100mA
• Maximum distance for detection: 15mm, 13mm recommended
• Dimensions: 29 x 14 x 9mm
• Cable lenght: 30cm


This switch works in a very simple way. When the magnet and reed switch are close to each other, to be precise 13mm or less, the reed switch closes under the influence of the magnetic field and thus closes the circuit. The circuit opens again as the magnet moves away from the reed switch.

In the following example we will show you a simple connection with the magnetic switch. We will make a detector with a LED diode which will light up when the magnet and switch are separated.



For this example we need:
LED diode, resistor (220Ohm in this example) and the magnetic switch.
Magnetic switch has no polarity, which means that it does not matter if we reverse its cables while connecting. One conductor of the magnetic switch is connected to digital pin 2, and the other end to GND. When connecting the LED diode, you must pay attention to the polarity. The longer part of the diode, anode, is connected to digital pin 13, and the cathode to the resistor and from the resistor to GND.


const int switchPin = 2;   
const int ledPin = 13; 
void setup() {
pinMode(switchPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(switchPin, HIGH);
void loop() {
if(digitalRead(switchPin) == LOW){  //If the switch is Low - switch separated
digitalWrite(ledPin, HIGH);         //LED diode lights up
digitalWrite(ledPin, LOW);          //Otherwise it does not light up - when the switch is connected