How to measure DC voltage with reverse polarity (-9v / 0 / +9V) with an ESP32 and ESPHOME

Hello

I would like to determine the direction of rotation of 2 small DC motors with an ESP32 and ESPHOME as well as the voltage (-9V / 0V /+9V). The two small motors are managed externally by another mechanism (not by the ESP) and their power supply would be connected in parallel to some GPIOs (not directly obviously). I had thought using an INA226 but apparently the wiring to the VCC and GND terminals are fixed and we cannot reverse the wiring (I think I fried an INA226 doing that).

Is there a module (like INAxxx) that can be connected to an ESP and allows this type of measurement to be made?

My project is to find out if the motor power supplies are sufficient (+/- 9V) and to have the ESP perform some actions depending on the direction of rotation.

Electronics is not my field, I might be asking myself stupid questions.
Thank you in advance for your advice.

Do you mean really -9 / +9 voltage referred to GND or just 9V with polarity swap?

I think it is just a swap

here my testbed:


2 Lego motors driven by Lego Tx/Rc Remote Control devices

the 2 pairs of wires (green/blue and yellow/orange) will be connected to the “measuring module” this one will be connected to the ESP

1/ measure for forward direction

2/ measure for backward direction

If you measure the voltage on motor contacts multimeter reading is swapping between +9V and -9V because polarity is swapping. But the voltage is just 9V.
If you measure between the power supply GND and motor contact there is only voltage +9V or 0.
I can’t see from your images where you could most easily connect multimeter to the power supply GND.

Anyway, you can detect the direction just trough simple voltage divider, no need for circuits for negative voltage. But you need to find power supply GND.

Here is the pinout of wires plug of the Lego Motor (Power Functions models)


The motor runs when there is a current between C1 and C2

My tests:



Results:

Does that answer your question?
What kind of voltage divider should I need?

You need 2:1 voltage divider, but first you need to find the supply gnd. Not the one that is swapping by the motor controller.
I’m not so familiar with Lego components to even recognize what is the power supply , but power supply GND needs to be connected to Esp GND. Thereon motor contact voltages referred to GND are +9V or 0V, swapping when direction changes. From there with two voltage dividers (one per each pole) you can detect if motor is on or off and what is the direction.

Nouvelle image (4)

For direction measure these two pins with respect to ground with separate GPIOs connected via a voltage divider.

C1 on = forward

C2 on = backward.

Both off = not moving

You can combine these states into one template sensor in ESPHome if needed.

To measure them the ESP needs 3.3v maximum for on. So using this calculator:

Vs = 9V (from C1 or C2)
R1 = 10k Ohms
R2 = 5.6K Ohms
Vout = 3.2V (to GPIOs)

Check you got it wired correctly and that it is outputting about 3.2v with your multimeter before connecting it to the ESP GPIOs.

Thank you for your advice.
I have built a test bench using your suggestions.

I use 1K & 470+100 ohms resistances (since I do not have 10K)


Good.
If you measure the voltage between supply GND and motor contacts, you get either 0 or +9.

Sorry I have not completed my message. Give me few minutes

/edit/
@Karosm : message completed below

1 Like

The results

no rotation:


note the -0V on the right multimeter
The battery gives 7.8V (battery a little low)

forward:


2.74V on left, 0.02 on right

backward:


0.001 on right, 2.75 on right

During the transition I get -0.01 for a brief time (less 1 second) I don’t know if this will be a problem on the input GPIOs of the ESP also the -0V without rotation.

If you are using 9V “box” battery for your experiments with motors, may I suggest to replace it with 6AA setup.
9V “box” is good for fire alarms etc, not for high current (like motors) applications.
Your discharged battery’s open circuit voltage of 7.8V can drop to 2.7V when you draw little bit current.

@Karosm
I have replaced the Lego battery with another one with 6 AA batteries.


It provides 9V on output, this is the voltage necessary for these Lego motors (Power Function models)
The outputs of my “voltage dividers” are 3V around now

If I understood well , it’s good then?

I think it’s good (except the -0.01V during the direction change. I don’t know what the ESP will do with this voltage on a GPIO input).
I start the wiring with the ESP and the ESPHOME script

Honestly, I’m not able to follow your hardware setup.
But if you have voltage levels between 0 and 3, you are good to go.
-0.01 is not a problem, consider it 0.

I will make a clear drawing of my installation