I would appreciate it if someone could help point me in the right direction. For context, my electronics and microcontroller knowledge is mostly limited to dabbling and following guides albeit programming is a stronger area for me. I will try and keep this as brief as possible.
I wanted to achieve what I perceived would be a fairly simple project, an ESP32 acting as a web server to serve the value/state of a float switch. I grabbed an unbranded ESP-32S, connected a float switch (chunky sewage style one on an unshielded 5m cable) and connected it to ground and Pin 18, wrote my code to set the pin to INPUT_PULLUP and perform digital reads on the pin when requested.
Very brief testing on the desk worked well, so (given I was trying to solve an issue) I deployed it into use. I am grabbing a reading from the web-page every 1.5 minutes. A few hours after install, the float switch floated. What I observed was periods of several minutes (ranging say 5 - 45) where I was receiving the expected value (0) from the digitalRead punctuated with individual reads of an unexpected value (1). Initially I chalked this up to turbulence in the water causing the switch to change.
Some time later, the float switch stopped floating, and I received several hours of expected values (1), punctuated with the very occasional unexpected value (0) which was not caused by the switch as it is suspended in free air.
I started to wonder whether I would need to de-bounce the switch which I hadn't previously done because I thought my periodic checks wouldn't really suffer from bouncing. Before I could consider implementing this, my digitalRead started to read 0 again, this time however, there is still no chance of the switch floating (double checked with resistance on multimeter) and the digitalRead has just been stuck returning 0 through resets.
I have now moved to another pin, and I am working again, albeit obviously expecting the above to happen again.
I have several thoughts on what my issues could be, but not really enough knowledge to know whether they apply.
- 5m unscreened cable, perhaps interference of some kind, there was a coiled up (and powered) extension cable nearby, not sure if that could cause interference enough to give sporadic results (and perhaps kill the pin)?
- Using internal pullup resistors, especially over a longer cable, I have seen some suggestions that with a longer cable an external resistor might be more appropriate?
- Bad luck? A dodgy pin or microcontroller?
- Other things I have not come across?
Any help would be really appreciated here, I am totally happy to research and learn, but feel I might benefit from somebody more experienced to point me in the right direction.
Thanks in advance.