Skip to content

Commit e170adf

Browse files
Merge pull request #391 from oss-slu/385-reed-switch-module-support
#385 Implementation of ReedSwitch support
2 parents d143b2e + 06c7aea commit e170adf

8 files changed

Lines changed: 383 additions & 0 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.opensourcewithslu.components.controllers;
2+
3+
import com.opensourcewithslu.inputdevices.ReedSwitchHelper;
4+
import com.opensourcewithslu.outputdevices.LEDHelper;
5+
import com.pi4j.io.gpio.digital.DigitalInput;
6+
import com.pi4j.io.gpio.digital.DigitalOutput;
7+
import io.micronaut.http.annotation.Controller;
8+
import io.micronaut.http.annotation.Get;
9+
import jakarta.inject.Named;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
13+
//tag::ex[]
14+
@Controller("/reedSwitch")
15+
public class ReedSwitchController {
16+
private final Logger log = LoggerFactory.getLogger(ReedSwitchController.class);
17+
private final ReedSwitchHelper reedSwitchHelper;
18+
private final LEDHelper ledHelper3;
19+
private final LEDHelper ledHelper4;
20+
21+
public ReedSwitchController(@Named("reed-switch-input") DigitalInput reedSwitch,
22+
@Named("led3") DigitalOutput led3,
23+
@Named("led4") DigitalOutput led4) {
24+
this.reedSwitchHelper = new ReedSwitchHelper(reedSwitch);
25+
this.ledHelper3 = new LEDHelper(led3);
26+
this.ledHelper4 = new LEDHelper(led4);
27+
}
28+
29+
@Get("/enable")
30+
public void enableReedSwitch() {
31+
reedSwitchHelper.addEventListener(e -> {
32+
try {
33+
if (reedSwitchHelper.isDetected) {
34+
ledHelper3.ledOn();
35+
ledHelper4.ledOff();
36+
} else {
37+
ledHelper3.ledOff();
38+
ledHelper4.ledOn();
39+
}
40+
} catch (Exception ex) {
41+
log.error("Error switching LED state", ex);
42+
}
43+
});
44+
}
45+
46+
@Get("/disable")
47+
public void disableReedSwitch() {
48+
reedSwitchHelper.removeEventListener();
49+
}
50+
}
51+
52+
//end::ex[]

components/src/main/resources/application.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ pi4j:
144144
pull: PULL_DOWN # Configure pull-up or pull-down as needed
145145
debounce: 100 # Optional debounce time for stable readings
146146
provider: pigpio-digital-input
147+
reed-switch-input:
148+
name: Reed Switch
149+
address: 17
150+
pull: PULL_UP
151+
debounce: 200
152+
provider: pigpio-digital-input
147153
# end::digitalInput[]
148154

149155
# tag::digitalOutput[]
@@ -160,6 +166,18 @@ pi4j:
160166
shutdown: HIGH
161167
initial: HIGH
162168
provider: pigpio-digital-output
169+
led3:
170+
name: LED Output
171+
address: 22
172+
shutdown: LOW
173+
initial: HIGH
174+
provider: pigpio-digital-output
175+
led4:
176+
name: LED Output
177+
address: 27
178+
shutdown: HIGH
179+
initial: HIGH
180+
provider: pigpio-digital-output
163181
photo-resistor-output:
164182
name: Photo Resistor Output
165183
address: 27
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
:imagesdir: img/
2+
3+
ifndef::rootpath[]
4+
:rootpath: ../../
5+
endif::rootpath[]
6+
7+
ifdef::rootpath[]
8+
:imagesdir: {rootpath}{imagesdir}
9+
endif::rootpath[]
10+
11+
==== Reed Switch
12+
13+
[.text-right]
14+
https://github.com/oss-slu/Pi4Micronaut/edit/develop/pi4micronaut-utils/src/docs/asciidoc/components/inputComponents/reedSwitch.adoc[Improve this doc]
15+
16+
===== Overview
17+
18+
This document describes a reed (magnetic) switch sensor implementation: required hardware, assembly instructions, circuit diagrams, configuration, and testing. A reed switch is a small magnetic sensor that closes a contact when a magnet is brought near.
19+
20+
===== Components
21+
22+
. Reed switch module
23+
. 2 x LED lights
24+
. 2 x 220Ω resistors
25+
. Breadboard
26+
. Jumper wires
27+
. Power source (3.3V)
28+
29+
===== Assembly Instructions
30+
31+
Connect the reed switch, LEDs, resistors, and wiring on the breadboard as shown in the circuit diagrams.
32+
33+
. Ground: connect the GND pin of the reed switch to the breadboard ground.
34+
. Power: connect VCC of the reed switch to 3.3V on the breadboard.
35+
. IO: connect the reed switch output pin (DO) to GPIO 17 on the Raspberry Pi (digital input).
36+
. LEDs: use a 220Ω resistor in series with each LED. Connect the LED cathodes to ground and the anodes to the GPIO outputs.
37+
. LED GPIOs: connect the two LED anodes to GPIO 22 and GPIO 27 respectively (digital outputs).
38+
39+
===== Circuit Diagram
40+
41+
image:reedSwitchCD.png[]
42+
43+
*Schematic Diagram:*
44+
45+
image:reedSwitch_schematic.png[]
46+
image:reedSwitch_schematic2.png[]
47+
48+
===== Functionality
49+
50+
When a magnet approaches the reed switch the internal contacts change state (close). In our example controller we read this digital input and drive one LED for detection and another for no detection.
51+
52+
===== Testing the Circuit
53+
54+
Use the following HTTP endpoints to interact with the reed switch controller.
55+
56+
* `/enable` - enable reed switch handling
57+
[source, bash]
58+
----
59+
$ curl http://localhost:8080/reedSwitch/enable
60+
----
61+
62+
* `/disable` - disable reed switch handling
63+
[source, bash]
64+
----
65+
$ curl http://localhost:8080/reedSwitch/disable
66+
----
67+
68+
===== Troubleshooting
69+
70+
- LED does not light when expected:
71+
.. Verify VCC and GND are correctly wired and the breadboard power rail is powered.
72+
.. Ensure LEDs are oriented correctly (anode to resistor -> GPIO, cathode to GND).
73+
.. Confirm resistors are 220Ω.
74+
.. Check the reed switch wiring: DO connected to GPIO 17, VCC to 3.3V, GND to ground.
75+
- False triggers or bouncing:
76+
.. Verify the configured pull-up/pull-down and debounce values in YAML.
77+
.. Move wires away from noisy power lines; shorten jumper wires where possible.
78+
79+
===== YAML Configuration
80+
The YAML configuration for the reed switch input:
81+
82+
[source, yaml]
83+
----
84+
digital-input:
85+
reed-switch-input:
86+
name: Reed Switch
87+
address: 17
88+
pull: PULL_UP
89+
debounce: 200
90+
provider: pigpio-digital-input
91+
----
92+
93+
The YAML configuration for the first LED:
94+
95+
[source, yaml]
96+
----
97+
digital-output:
98+
led3:
99+
name: LED Output
100+
address: 22
101+
shutdown: LOW
102+
initial: HIGH
103+
provider: pigpio-digital-output
104+
----
105+
106+
The YAML configuration for the second LED:
107+
108+
[source, yaml]
109+
----
110+
digital-output:
111+
led4:
112+
name: LED Output
113+
address: 27
114+
shutdown: HIGH
115+
initial: HIGH
116+
provider: pigpio-digital-output
117+
----
118+
119+
120+
121+
===== Constructor and Methods
122+
123+
To see the constructor and methods of the ReedSwitchHelper class see the javadoc:
124+
https://oss-slu.github.io/Pi4Micronaut/javadoc/com/opensourcewithslu/inputdevices/ReedSwitchHelper.html
125+
126+
===== An Example Controller
127+
128+
129+
130+
====== This controller uses the reed switch to light up LED lights when activated
131+
132+
[source, java]
133+
----
134+
include::../../../../../../components/src/main/java/com/opensourcewithslu/components/controllers/ReedSwitchController.java[tag=ex]
135+
----
293 KB
Loading
18.7 KB
Loading
13.2 KB
Loading
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.opensourcewithslu.inputdevices;
2+
3+
import com.pi4j.io.gpio.digital.DigitalInput;
4+
import com.pi4j.io.gpio.digital.DigitalStateChangeListener;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
public class ReedSwitchHelper {
9+
private static final Logger log = LoggerFactory.getLogger(ReedSwitchHelper.class);
10+
11+
private final DigitalInput reedSwitchDigitalInput;
12+
13+
private DigitalStateChangeListener reedSwitchChangeListener;
14+
15+
/**
16+
* Shows if the reed switch has detected magnetic field.
17+
*/
18+
public boolean isDetected;
19+
20+
/**
21+
* ReedSwitchHelper constructor.
22+
* @param reedSwitchDigitalInput A Pi4J DigitalInput object.
23+
*/
24+
public ReedSwitchHelper(DigitalInput reedSwitchDigitalInput)
25+
{
26+
this.reedSwitchDigitalInput = reedSwitchDigitalInput;
27+
this.isDetected = reedSwitchDigitalInput.isHigh();
28+
29+
30+
initialize();
31+
}
32+
33+
/**
34+
* Initializes the listener that keeps track of if the reed switch has detected magnetic field or not.
35+
* It is automatically called when the ReedSwitchHelper is instantiated.
36+
*/
37+
private void initialize() {
38+
39+
log.info("Initializing Reed Switch");
40+
41+
reedSwitchChangeListener = e-> {
42+
isDetected = reedSwitchDigitalInput.isLow();
43+
if (isDetected){
44+
log.info("Magnet detected!");
45+
} else {
46+
log.info("Magnet Removed");
47+
}
48+
49+
};
50+
51+
reedSwitchDigitalInput.addListener(reedSwitchChangeListener);
52+
}
53+
54+
/**
55+
* Adds an event listener to the Reed switch.
56+
* @param function A Pi4J DigitalStateChangeListener object.
57+
*/
58+
public void addEventListener(DigitalStateChangeListener function)
59+
{
60+
reedSwitchChangeListener = function;
61+
reedSwitchDigitalInput.addListener(reedSwitchChangeListener);
62+
}
63+
64+
/**
65+
* Removes the event listener from the Reed switch.
66+
*/
67+
public void removeEventListener()
68+
{
69+
if (reedSwitchChangeListener != null) {
70+
reedSwitchDigitalInput.removeListener(reedSwitchChangeListener);
71+
reedSwitchChangeListener = null;
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)