Hello everyone,
I am new to the forum and to the ESP32s' world. I am trying to do a home project with a few sensors. One of the things that I want to achieve is to use a reed switch to count number of rotations of a wheel. It does a full round for more than 0.5s. My idea is to use a stationary reed switch and a magnet on the wheel. When the magnet goes near the switch, I count it. Every few seconds I print the counter.
Here is my code:
You can also simplify your schematic. Since you are using INPUT_PULLUP, remove the external resistor and connect the other side of the reed switch to ground, not VCC. The pin will remain high until the magnet closes the reed switch and pulls it low
use Hall effect switch activated by a magnet as recommended by @LarryD - should give a clean signal with no bounce
also consider using the ESP32 Pulse Counter (PCNT) hardware
It is an ESP32, so an operation on a 32 bit variable will be atomic (unless it is in a packed structure). Still a good idea - you never know where code may end up.
A reed switch can be unreliable for counting rotations due to mechanical wear, slow response time, and bouncing issues, leading to missed counts. A better alternative is a Hall Effect Sensor (like A3144), which works with a magnet like a reed switch but is fully electronic, faster, and more reliable. If higher accuracy is needed, an optical sensor (like an IR obstacle sensor) or a rotary encoder can provide more precise tracking. A Hall Effect Sensor should work perfectly as a direct replacement in your setup.