Disclaimer: I'm an absolut electronics beginner (obviously) with an interest in IOT that tries not to electrecute himself (or others) and / or burn down the house.
For the last couple of days I have been wandering around the Interweb, looking for some guidance on how to properly connect an external circuit (switch) to an "arduino"1.
The circuit in question is a motorised garage door opener2 that uses two reed switches3 to limit / detect the movement of the door and offers a screw terminal for a momentary switch4.
While I am already able to open or close the garage door via a relay (shield) which is connected to the same screw terminal as the momentary switch, I am struggling to find and implement the best possible approach for reading the reed switches.
Given that the D1 mini has an operating voltage of 3.3V and accepts 3.2V on its single analog pin, my first thought was to build a voltage devider with 100kΩ for R1 and ~160kΩ for R2 for each switch and then either poll them via the analog pin or to use an interrupt service routine.
Would that be an okay-ish thing to do?
Since I'm not really interested in the voltage level but just the high or low state of the two switches, wouldn't it make more sense to use two digital pins instead of the single analog input of the D1?
<rubber_duck_debugging>
On the software side of things, I just realised that I don't really know if it is possible to use "digitalRead" on a pin that is also declared to be an interrupt "pin"?! That said, both "endpoint" switches are only low when the door is either fully open or fully closed. Wouldn't an ISR (falling) approach constantly and unnecessarily trigger the registered function?
</rubber_duck_debugging>
I've read that not only I will have to connect the ground of both circuits together, but that it is also wise to protect the microcontroller from "back powering". It is at this point that I'm out of my comfort zone and everything I read about the electronic components involved like (zener) diodes and / or (PNP) transistors is out of my grasp and sadly only leads to more open browser tabs.
In short (tl/dr):
- Is it okay or maybe mandatory to use a voltage divider or is there a better method?
- Could you please give me an example of how to protect the microcontroller from back powering and / or other harmful scenarios that I did not think of?
- Is there anything in particular that speaks for or against the use of polling or interrupts in this project?
1 D1 mini - separate 5V power supply
2 230V / 24V 2.5A internally
3 Two switches - 5V while open or in between for both switches, 0V if closed or fully opened
4 12V (switch has an internal led)
To awoid misunderstandings, please grab pen and paper, and draw a wiring diagram showing all involved voltages, and attache it.
I've attached a simple sketch to my original post.
Yes You have. My mistake.
Does the 2 read switches and the manual button have same GND? I Think there is a good chance for that.
Removing mains and measuring the resistance with a digital resistance meter, a DMM, could verify that. Check marks on the main unit to see if they give any help.
If they have a common ground, GND, You could use a transistor instead of the relay. Jacking into the +5 volt side of the reads would also work, without voltage dividers as Your controller is a 5 powered.
Thank you for your feedback.
No mistake on your part, I've added the picture after your request.
The momentary switch and the reed switches are connected right next to each other on the same circuit board inside the garage door opener which is powered by a 230V / 24V 2.5A power supply.
I used a relay shield for "convenience" because, as you can see in the sketch, the momentary switch runs on 12V instead of the 5V of the reed switches. In that sense, the relay part was not my main concern.
Railroader:
[...] Jacking into the +5 volt side of the reads would also work, without voltage dividers as Your controller is a 5 powered. [...]
Although the D1 mini is powered via a 5V USB power supply, the operating voltage is not 5V but 3.3V and the maximum analog input voltage 3.2V (according to the documentation).
Unless I totally missed your point, the following questions remain:
curiosys:
[...]
In short (tl/dr):
- Is it okay or maybe mandatory to use a voltage divider or is there a better method?
- Could you please give me an example of how to protect the microcontroller from back powering and / or other harmful scenarios that I did not think of?
- Is there anything in particular that speaks for or against the use of polling or interrupts in this project?
[...]
Okey. Then You need voltage dividers bringing down the 5 volt level below 3.2 volt.
You will not need interrupts. The purpose of an interrupt is to say to the processor,
"STOP whatever you are dong RIGHT NOW and listen to MY instruction. Process it and then you can carry on."
You don't need that, the loop will poll your switches far faster than you need with a simple digitalRead on the inputs.
If you are reading a logical signal, you would instinctively use a digital input unless you have none left.
My feeling would be to use opto isolators to connect into your chosen processor.
Thank you (both) for your input.
AJLElectronics:
You will not need interrupts. The purpose of an interrupt is to say to the processor,
"STOP whatever you are dong RIGHT NOW and listen to MY instruction. Process it and then you can carry on."
[...]
Background
The overall plan is to include the system into a Node-RED / MQTT network and to be able to control / monitor the garage door via "any" device in the network (if needed ~ lost 1 of the 2 rf remotes already).
The microcontroller, depending on any available pins left, might also report other sensor data in the future, like from a PIR sensor and / or a DHT22.
I regarded the interrupt-method as an external confirmation for the opening / closing instruction triggered either programmatically (MQTT) or manually (Switch). As far as I know, it is also a valid option, if I ever wanted to use power saving or deep sleep modes, right? After all, we open / close the garage door maybe three times a week.
AJLElectronics:
[...]
My feeling would be to use opto isolators to connect into your chosen processor.
If I understand you correctly, the reed switch connects to the "led side" of the optocoupler. For the second reed switch, as shown in the attached sketch, this would mean that it would be "on" like "99%" of the time, since the circuit is only interrupted when the garage door is fully open.
Is this acceptable / normal for such a component? What would be a suitable choice for my project and could you point me in the direction where to find an example implementation, please?
In the end I used one 4N35 optocoupler / phototransistor each to decouple the 5V signals of both reed switches.
For that I used a 1kΩ current limiting resistor for the diode and a 10kΩ pull-down resistor from the emitter to ground and to a digital input pin of the microcontroller.