Sensing switch state on an old 2-pin vending machine motor module with an ESP32

Thread Starter

evraknotna

Joined Jun 12, 2025
7
I'm working on a project to control an old vending machine (Automatic Products 113 Snackshop) using an ESP32, but I'm having trouble figuring out how to sense a full revolution of the 2-pin vend motor+switch module. To preface, I've already looked through countless forum posts and none so far have given me the answers I've needed, which is why I'm creating another post myself.

Below are photos of the module, and the circuit schematic I've gotten by examining the PCB:
photo_2025-06-12 12.36.56.jpeg
photo_2025-06-12 12.36.59.jpeg
photo_2025-06-12 12.37.00.jpeg
photo_2025-06-12 13.11.28.jpeg
Screenshot 2025-06-12 at 1.45.14 PM.png

As the photos show, the cam turned by the motor presses the NC switch during the revolution, and releases the switch at home position (signalling end of revolution). I've tested the effects of pressing and releasing the switch by running 24V across the module's terminals and probing with a multimeter as shown in the schematic (see voltmeter and ammeter). Here's my observations:

  • Switch not pressed (closed): voltage relatively stable around 24V
  • Switch pressed (open): voltage spikes up to 35-65V range and becomes extremely noisy
  • Whether the switch is pressed or not, there's no change in current based on multimeter readings.
I don't have easy access to an oscilloscope, so I haven't been able to get precise measurements of the voltage and current waveforms.

I've tried using voltage divider, optocoupler and comparator circuits while attempting to get an ESP32 to distinguish between the switch pressed (noisy voltage spike) and switch not pressed (relatively stable 24V) states, but I haven't had any luck. Whenever I use Vmotor as input for a voltage divider, Vout doesn't respond to any fluctuations in Vmotor, and Vmotor stops giving the expected response to the switch being toggled.

I suspect that there's some inductive/capacitive/transient black magic going on, but I can't figure out what it is, why it's happening and how I should proceed from here.

Has anybody had any luck with controlling old vend motor modules like these? I'm open to any suggestions on how I could better detect changes in the switch state.
 

dendad

Joined Feb 20, 2016
4,635
Is your circuit correct? I would think the cap is across the motor and the switch just going to a connector.
If not, try removing the capacitor and just use the switch to GND to operate the input of an opto coupler. The opto's output can drive the ESP32 i/p.
Run the opto LED "A" from the 24V via a 2K2 resistor, the i/p LED "K" to the switch. Then the o/p "E" to ESP32 gnd, "C" to the ESP32 input that has a 1K pull-up to 3V3.
 
Last edited:

Thread Starter

evraknotna

Joined Jun 12, 2025
7
Is your circuit correct? I would think the cap is across the motor and the switch just going to a connector.
If not, try removing the capacitor and just use the switch to GND to operate the input of an opto coupler. The opto's output can drive the ESP32 i/p.
Run the opto LED "A" from the 24V via a 2K2 resistor, the i/p LED "K" to the switch. Then the o/p "E" to ESP32 gnd, "C" to the ESP32 input that has a 1K pull-up to 3V3.
Thanks for the recommendation!
The schematic should be correct, unless I've completely misread the traces on the motor module's PCB. Besides, the vending machine motherboard is able to sense switch state purely based on the two motor module terminals (which is what I'm trying to replicate, to minimise the amount of additional wiring I need to do).

Instead, do you think replacing the capacitor with a resistor would work?
This would mean:
  • When the cam is not in home position -> switch pressed (opened) -> module operates as usual.
  • When cam reaches home position -> switch not pressed (closed) -> parallel resistor increases current draw through module, which I can sense using a current sensor/optocoupler with the appropriate R_led and immediately cut power to the module
 

dendad

Joined Feb 20, 2016
4,635
I think your circuit is ok. (I tried tracing it from your photos).
The capacitor is odd. I do not know what is going on there with such a small cap switched across the motor.
You could just remove the connections to the switch from the PCB and run the switch directly to the ESP32 inputs.
 

dendad

Joined Feb 20, 2016
4,635
I just had a thought. The microswitch has 3 terminals so maybe the signal is from the unused NO terminal that is switched to GND (assuming the COM is the the GND and NC is to the cap) so run that to the opto then the ESP32.
Photo on 13-6-2025 at 10.10 am.jpg
Oh, run the 2K2 from the +24V in, not from the top of the motor as I drew. Oops!
 
Last edited:

Thread Starter

evraknotna

Joined Jun 12, 2025
7
I just had a thought. The microswitch has 3 terminals so maybe the signal is from the unused NO terminal that is switched to GND (assuming the COM is the the GND and NC is to the cap) so run that to the opto then the ESP32.
Sadly this microswitch only has 2 terminals, NC and COM. My thumb was covering where the NO terminal usually is in the photo.
 

JohnSan

Joined Sep 15, 2018
124
I don't believe that capacitor is 31pF.
If the input was pulsed DC (or maybe AC even), the voltage measured by having a capacitor connected or not, would likely be different.
You'll need a scope to investigate further.
 

BobTPH

Joined Jun 5, 2013
11,480
Just add another connection as others have suggested. It is silly to delay your project just because the previous controller did it without the third connection.
 
Last edited:

Thread Starter

evraknotna

Joined Jun 12, 2025
7
Hi @dendad and @BobTPH, thanks for your replies – my plan B would be to just read off the microswitch directly, but given that there are about 50-60 motor modules in one vending machine that would just mean a lot of tedious soldering work for me. In the worst case, that's what I would've done.

Anyways, to update everybody on the outcome: a user in another forum sent me this patent: Mars 1988 patent 4,785,927 which used a motor module circuit pretty much identical to mine. The key findings from that were that the input should be PWM, which is why the circuit had a capacitor and switch in series – the capacitor absorbs inductive noise and flyback spikes from the motor when the switch is closed (hence the stable 24V) and doesn't absorb this noise when the switch is open (hence the jumpy multimeter readings).

The vending machine's motherboard has the comparator circuit in Fig.4 below which converts readings from the motor module's terminals into logic high when the switch is open (motor powered, not at home point) and a PWM-like signal when the switch is closed (motor powered, at home point), as shown in Fig.5. Whether the circuit is so complex because it needs to be, or if the company was trying to evade some other patent, who knows. Anyways, I'm gonna try to simulate this circuit and decide whether this method, or the direct-switch method makes more sense for my project. Thanks all!
Screenshot 2025-06-17 at 11.35.35 AM.png
Screenshot 2025-06-17 at 11.35.38 AM.png

Also in response to @Ya'akov, this isn't a capstone project, it's part of my summer internship :)
 

Thread Starter

evraknotna

Joined Jun 12, 2025
7
To update, I resolved the issue by replacing the series capacitor with a resistor (currently two 1/4W 1kohm resistors in parallel, but I'm planning on replacing that with a 1W 470ohm resistor for more reliability/longevity). When the switch is released (closed) in home position, the inclusion of the resistor path bumps the input current up by ~30mA, which I can easily detect using a current sensor and cut power to the module. The main benefit of this is I don't need to rewire all the switches and figure out how to run 40-50 new wires from the motor modules to my MCU.
 

JohnSan

Joined Sep 15, 2018
124
If using a resistor was reliable, I would think the original manufacturers would have employed that method.
They used a capacitor for a reason.
See what your testing shows up.
 
Top