/*
Reed Switch + Servo Example
---------------------------
WIRING NOTES
1. REED SWITCH
- One leg of reed switch -> Arduino D2
- Other leg of reed switch -> GND
This code uses INPUT_PULLUP, so:
- switch open = HIGH
- switch closed = LOW
When a magnet comes near the reed switch,
the switch closes and the pin reads LOW.
2. SERVO
- Servo signal wire -> Arduino D9
- Servo VCC -> External 5V supply
- Servo GND -> GND
3. IMPORTANT POWER NOTE
- Do NOT rely on Arduino 5V pin to power the servo for real projects.
- Use an external 5V supply for the servo.
- Connect Arduino GND and external supply GND together.
4. BEHAVIOR
- No magnet detected -> servo goes to default position
- Magnet detected -> servo moves to triggered position
*/
#include <Servo.h>
const int reedPin = 2;
const int servoPin = 9;
Servo sorterServo;
// Adjust these for your mechanism
const int defaultAngle = 60;
const int triggeredAngle = 120;
void setup() {
pinMode(reedPin, INPUT_PULLUP);
sorterServo.attach(servoPin);
sorterServo.write(defaultAngle);
Serial.begin(9600);
Serial.println("Reed switch + servo system ready");
}
void loop() {
int reedState = digitalRead(reedPin);
// With INPUT_PULLUP:
// HIGH = no magnet
// LOW = magnet detected
if (reedState == LOW) {
sorterServo.write(triggeredAngle);
Serial.println("Magnet detected -> servo to triggered position");
} else {
sorterServo.write(defaultAngle);
Serial.println("No magnet -> servo to default position");
}
delay(100);
}