Simple `pulse_counter` device rebooting unexpectedly (during high speed)

Use the pulse total, this reports the total pulses, not pulses / min: https://esphome.io/components/sensor/pulse_counter.html#counting-total-pulses

One edge, rising or falling. Not both.

Unit of mm.

One pulse = one mm. No need for any calibration factor.

I saw that and tried it out but I couldnā€™t get it to work and gave up in favor of the separate integration option which I have used before with the pulse_counter. Regardless, of all of this, I like your idea to try only rising/falling as a debug step so Iā€™ll probably give it a try, though in order to use that and get the correct value Iā€™ll have to use a filter with math to double the results.

Itā€™s the same integration.

I was serious when I asked if youā€™ve used the pulse_counter. Everything it reports is normalized to 60s. Try it out.

Please see this post I made in 2020.

Did you even look at the link I posted?

omg, please calm down. I read it. Iā€™m just using it as a ā€œrate meterā€ as nickrout said in his response: ESPHome pulse_counter update_interval less than 60s (maybe be a bug?) - #6 by nickrout

pulse_counter reports a rate (ie pulses per minute which you multiply by a factor to get litres per minute etc)

Anyway, this is all beside the point, who cares about the units when I canā€™t get the thing to be stable. Iā€™m trying it with the default count_mode right now, by the way: rising_edge: INCREMENT and falling_edge: DISABLE.

EDIT: ā€¦It still rebooted. :frowning:

You clearly said you wanted to measure a length, not a rate:

You are not using the correct component to do this. Iā€™m done trying to convince you.

Yeah, I tried it with a 120Vac to 5V 3A dedicated USB micro supply but that didnā€™t seem to affect the problem, still rebooted. It very well could be that the plastic wheel is scraping the body of the encoder.

Iā€™ve got the o-scope hooked up to it now and Iā€™m watching the pulses and to be honest, they look pretty darn clean! Now at least I know that running my power drill at full tilt (on ā€˜1ā€™ speed) the shortest pulses I get are around 250Āµs.


This is 1s/DIV, so a long capture with variable slow speeds on the drill. When the plastic vane stops between the sensor, the output is 3.3V. When the vane does not block the sensor, the output is 0V.


500ms/DIV


2ms/DIV


2ms/DIV running the drill a little faster


500Āµs/DIV I tried to get this one at the top speed.


200Āµs/DIV I also tried to get this one at the top drill speed.

Interesting to see that there is definitely a duty cycle to it, which means that the vanes arenā€™t balancedā€¦

anyway, the output of the sensor looks decent.

No answer from thereā€¦
Did you ever try what I suggested on post#14, removing the pulse counter from your code and running it otherwise same setup?

Oh! No, I hadnā€™t, I guess I didnā€™t see that one or maybe I misinterpreted it. Iā€™ll give that a try and see if itā€™s the actual pulse_counter implementation causing the reboot or something external.

I was just about to go on an excursion building a small faraday cage to see if that would help! LOL.

Iā€™ll give the ā€œno pulse_counter definitionā€ experiment a try first, since that should be pretty easy and straight forward to prove out.

Okay, that was a good call. I just tried it without anything configured:

esphome:
  name: respooler
  friendly_name: Respooler
  build_path: ./build/respooler

esp32:
  board: nodemcu-32s
  framework:
    type: arduino

logger:
  level: DEBUG

safe_mode:

and the device still reboots. Iā€™m going to try another ā€œshotgun approachā€ with a faraday cage unless you have any other ideasā€¦

You could play with the oscilloscope until it reboots. You could also try with scope connected to 3.3V pin when it occurs.

Thatā€™s a good call, too. Iā€™ll have to see if I can find another probe to look at both pins simultaneously.

I built a little faraday cage out of old screen door, a small cardboard box, and a stapler but it still rebooted. :face_with_symbols_over_mouth:

I even put a ferrite ring over the wires coming from the sensor, thinking that might help reduce the noise.

Giving hard time, made me curious now :grinning:
At least you have scope, sooner or later youā€™ll find something.
Your wiring is quite long, nice antenna as wellā€¦ Twisting them (or better shortening) might offer some benefits.
Obviously you tried some other Gpio pinā€¦?

With short USB cable, right?

I hadnā€™t until you just said that but I did try a completely different ESP32 ā€“ same result and I just tried GPIO27 instead of GPIO14 and the problem persists.

I swapped the USB cable for a shorter one and this one has a ferrite bead on it but that didnā€™t seem to help either.

I got another probe on the VDD but this scope is REALLY crappy and I canā€™t really zoom into it too much vertically since the base of the signal must be displayed in the scope window. Anyway, this is a zoomed out view (2 seconds/DIV) and the failure happened just to the left of when the high-speed pulses stopped.

Hereā€™s another one with the max voltage scaling that I can apply before the stupid waveform goes off the top of the screen. VDD looks okay.

Wow, itā€™s getting even betterā€¦ :open_mouth:
What about the lambda on your yaml: if (id(battery_drill).rpm) >666 {rebootā€¦
Seriously, there is only esp32 and optocoupler .Esp is now without code for interrupts and stabile voltages confirmed by scopeā€¦?

Yeah, Iā€™m wondering if I should just try to implement my own counter from the GPIO pin with straight C code using the Arduino IDE and see if I can get it to work with something besides ESPHome. Iā€™ve only tinkered a little with that before, so itā€™d take some time and googling but Iā€™m not really sure what else I can try.

Stab in the dark here: does the board youā€™re using have a watchdog timer somewhere?

Any pattern to how much uptime the board has before it reboots? (Try adding something to toggle a GPIO every boot and measure pulse width, maybe.)

not that I know of. The failure points seem to be random ā€“ not based on a time since last reboot. I am trying it again, this time without the powerdrill just hand cranking the thing and I can still cause reboots. Iā€™m cranking it pretty slowly, the first time I got to almost 8 meters before the thing rebooted. If I try cranking it more it counts up between the reboots ā€“ the value that it reboots at is random. :thinking:

I asked Gemini to produce some interrupt-driven pulse counting ESP32 source code and it responded with this:

#include <Arduino.h>

const int pulsePin = 13;
const int mmPerPulse = 1;

volatile int pulses = 0;
volatile int totalPulses = 0;

void setup() {
  Serial.begin(115200);
  pinMode(pulsePin, INPUT);

  attachInterrupt(digitalPinToInterrupt(pulsePin), countPulse, RISING);
}

void loop() {
  static unsigned long previousMillis = 0;
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= 1000) {
    previousMillis = currentMillis;

    int currentPulses = pulses;
    pulses = 0;
    totalPulses += currentPulses;

    Serial.print("Current Pulses: ");
    Serial.print(currentPulses);
    Serial.print("  Total Pulses: ");
    Serial.print(totalPulses);
    Serial.print("  Distance: ");
    Serial.print(totalPulses * mmPerPulse);
    Serial.println(" mm");
  }
}

void countPulse() {
  pulses++;
}

I took that code, compiled it on the Arduino IDE and sent it to the device. It isnā€™t great but it definitely shows pulses and a total in the Arduino Serial Monitor. However, after hand cranking it for about a minute, I can get the thing to reboot, just like it does with ESPHome:

image

I am at a loss here.