Using a Reed Switch to Control a Relay and AC/DC Loads with an Arduino

Using a Reed Switch to Control a Relay and AC/DC Loads with an Arduino

In this tutorial, we'll explore how to use a reed switch in conjunction with an Arduino to control a relay that can manage both AC and DC loads. The reed switch acts as a magnetic sensor, allowing us to trigger the relay based on the proximity of a magnet. This configuration is perfect for applications like alarms, automated lights, or any scenario where you want to control devices based on the presence or absence of a magnetic field. The outcome will be a fully operational circuit that can turn on or off a light or alarm depending on the state of the reed switch.

Reed Switch Schematic
Reed Switch module

As we build this project, we'll discuss the key components involved, including the reed switch, relay, and Arduino. The reed switch has three pins: VCC (power), GND (ground), and OUTPUT (signal). When a magnet approaches, the switch closes, allowing the signal pin to go HIGH, which can then trigger the relay. The relay acts as a switch for higher voltage loads, making it suitable for controlling devices like lamps or alarms.

For a visual demonstration and further clarification, be sure to check the associated video at (in video at 00:00).

Reed Switch module to view

Hardware Explained

The main components of this project include a reed switch, a relay module, and an Arduino board. The reed switch is a magnetic sensor that closes its contacts when a magnet is in proximity, allowing current to flow through the OUTPUT pin. This pin sends a signal to the relay, which can control high-voltage devices safely.

The relay module is designed to control larger loads. It has three main connections: Common (COM), Normally Open (NO), and Normally Closed (NC). When the relay is activated by the Arduino, it connects the COM pin to the NO pin, allowing current to flow to the connected device. This setup allows for versatile control over various electrical devices without directly interfacing the Arduino with high voltages. The main amplifier used to amplify the read switch signal is LM393 Operational Amplifier

Datasheet for module

ManufacturerUnknown
Logic/IO voltage3.3V- 5 V
Supply voltage5 V
Output current (per channel)10 A max
Peak current (per channel)16 A max
PWM frequency guidanceNot applicable
Input logic thresholds2.5 V min (HIGH)
Voltage drop / RDS(on) / saturation0.1 V typical
Thermal limits85 °C max
PackagePCB mount

Wiring Instructions

Arduino wiring for Reed switch module
Arduino wiring for Reed switch module

To wire the system, start by connecting the reed switch. Connect the VCC pin of the reed switch to the 5V output on the Arduino, and connect the GND pin to one of the ground pins on the Arduino. The OUTPUT pin of the reed switch should be connected to digital pin 2 on the Arduino. This will allow the Arduino to read the status of the reed switch.

Next, wire the relay module. Connect the VCC pin of the relay to the 5V output on the Arduino, and the GND pin to the ground. The input pin of the relay, which controls the relay operation, should be connected to digital pin 10 on the Arduino. Finally, connect the load (like a light bulb) to the relay's Common and Normally Open terminals, ensuring that the load is suitable for the relay specifications.

Code Examples & Walkthrough

The following code snippet initializes the pins and sets up serial communication for debugging. The reed switch is connected to pin 2, while the relay is controlled via pin 10.


int LD = 200; // time in milliseconds to wait before making another reading.

void setup() {
    Serial.begin(9600);
    pinMode(10, OUTPUT); // connected to relay
    pinMode(2, INPUT); // reed switch input pin 2  
    Serial.println("Robojax Test: Reed switch");
}

In the loop function, the reed switch state is continuously checked. If the switch is closed (indicating the presence of a magnet), the relay is turned on. If the switch is open, the relay is turned off after a delay of 5 seconds, allowing for a grace period before deactivating.


void loop() {
    if(digitalRead(2)){
      Serial.println("Switch ON "); 
      digitalWrite(10, LOW); // Turn the relay ON    
      delay(LD); 
    }else{
      delay(5000); // wait 5 seconds before turning the alarm off
      digitalWrite(10, HIGH); // Turn the relay OFF
    }
}

This code allows for flexibility in controlling the relay based on the reed switch's state. You can modify the timing and behavior by changing the delay values or the way the relay is controlled based on the switch input.

Demonstration / What to Expect

When the setup is complete, bringing a magnet close to the reed switch should activate the relay, turning on the connected light or alarm. If the magnet is removed, the light will remain on for a specified delay period before turning off, providing a buffer time for the user. Ensure to test the system to confirm that the relay behaves as expected and responds correctly to the reed switch.

Video Timestamps

  • 00:00 - Introduction to the project
  • 02:15 - Hardware explanation
  • 05:30 - Wiring demonstration
  • 08:45 - Code walkthrough
  • 12:00 - Expected outcomes and troubleshooting tips

Images

Reed Switch module top view
Reed Switch module top view
Reed Switch Schematic
Reed Switch Schematic
reed_switch-4
reed_switch-4
Reed Switch module
Reed Switch module
Reed Switch module to view
Reed Switch module top view
Reed Switch module
Reed Switch module
Arduino wiring for Reed switch module
Arduino wiring for Reed switch module
22-Reed Magnetic Switch Arduino with Relay code
Language: C++
Copied!

Resources & references

No resources yet.

Files📁

No files available.