1

When my ESP32 is powered with an external 5V adapter or 12V power (2596 DC-DC to 5V), and my water meter (reed switch) is connected with the ESP32, the pulse start running wild and keeps increasing at each interval (of the debounce delay).

When I connect thd ESP32 to my laptop's USB port, there are no random pulses on the ESP32 with the reed switch wires connected.

I'm reading 4 pulses, storing those counter in EEPROM, using interrupt on GPIO 25, 26, 27 and 35.

Code:

#include <EEPROM.h>
#define PULSE_ADDRESS 200

#define pin35 35 #define pin25 25 #define pin26 26 #define pin27 27

const int Pulse1 = pin25; const int Pulse2 = pin26; const int Pulse3 = pin27; const int Pulse4 = pin35;

int PulseHit = 1; const int debounceDelay = 5000; // Adjust this value based on your requirements

volatile unsigned long lastDebounceTimePulse1 = 0; volatile unsigned long lastDebounceTimePulse2 = 0; volatile unsigned long lastDebounceTimePulse3 = 0; volatile unsigned long lastDebounceTimePulse4 = 0;

volatile unsigned long pulse1Count = 0; volatile unsigned long pulse2Count = 0; volatile unsigned long pulse3Count = 0; volatile unsigned long pulse4Count = 0;

void IRAM_ATTR handlePulse1() { if ((millis() - lastDebounceTimePulse1) > debounceDelay) { PulseHit = 1; lastDebounceTimePulse1 = millis(); pulse1Count++; } }

void IRAM_ATTR handlePulse2() { if ((millis() - lastDebounceTimePulse2) > debounceDelay) { PulseHit = 1; lastDebounceTimePulse2 = millis(); pulse2Count++; } }

void IRAM_ATTR handlePulse3() { if ((millis() - lastDebounceTimePulse3) > debounceDelay) { PulseHit = 1; lastDebounceTimePulse3 = millis();

pulse3Count++;

} }

void IRAM_ATTR handlePulse4() { if ((millis() - lastDebounceTimePulse4) > debounceDelay) { PulseHit = 1; lastDebounceTimePulse4 = millis(); pulse4Count++; } }

void setup() { Serial.begin(115200); pinMode(Pulse1, INPUT); pinMode(Pulse2, INPUT); pinMode(Pulse3, INPUT); pinMode(Pulse4, INPUT);

attachInterrupt(digitalPinToInterrupt(Pulse1), handlePulse1, RISING); attachInterrupt(digitalPinToInterrupt(Pulse2), handlePulse2, RISING); attachInterrupt(digitalPinToInterrupt(Pulse3), handlePulse3, RISING); attachInterrupt(digitalPinToInterrupt(Pulse4), handlePulse4, RISING);

lastDebounceTimePulse1 = millis(); lastDebounceTimePulse2 = millis(); lastDebounceTimePulse3 = millis(); lastDebounceTimePulse4 = millis();

EEPROM.begin(512); EEPROM.get(PULSE_ADDRESS, pulse1Count); EEPROM.get(PULSE_ADDRESS + sizeof(pulse1Count), pulse2Count); EEPROM.get(PULSE_ADDRESS + 2 * sizeof(pulse1Count), pulse3Count); EEPROM.get(PULSE_ADDRESS + 3 * sizeof(pulse1Count), pulse4Count); EEPROM.end(); }

void loop() { // Print the results if (PulseHit == 1) { Serial.print("RISING Pulse on pin 25: "); Serial.println(pulse1Count);

Serial.print(&quot;RISING Pulse on pin 26: &quot;);
Serial.println(pulse2Count);

Serial.print(&quot;RISING Pulse on pin 27: &quot;);
Serial.println(pulse3Count);

Serial.print(&quot;RISING Pulse on pin 35: &quot;);
Serial.println(pulse4Count);

Serial.println(&quot; &quot;);

PulseHit = 0;

// Save pulse counts to EEPROM
EEPROM.begin(512);
EEPROM.put(PULSE_ADDRESS, pulse1Count);
EEPROM.put(PULSE_ADDRESS + sizeof(pulse1Count), pulse2Count);
EEPROM.put(PULSE_ADDRESS + 2 * sizeof(pulse1Count), pulse3Count);
EEPROM.put(PULSE_ADDRESS + 3 * sizeof(pulse1Count), pulse4Count);
EEPROM.commit();
EEPROM.end();

} }

I have gone through the similar questions below; I haven't tried the capacitor, but I added a debounce of 2 seconds in the code which should be enough, but if not, please let me know. I have tried 1k, 10k, and 330 resistors to PULL-UP as well as PULL-DOWN.

  1. How to avoid electromagnetic interference on ESP32 input
  2. https://forum.arduino.cc/t/pulse-counting-with-an-external-device/316432/11
  3. https://forum.arduino.cc/t/rain-gauge-reed-switch-interference/466087/6
  4. https://forum.arduino.cc/t/interrupt-problems-on-arduino/496335/11
  5. Measure revolutions per second with reed switch

Are there any other ways to suppress the interferences on thd ESP32?

Edited:

void setup() {
  for (int i = 0; i < numleds; i++) {
    pinMode(ledPins[i], OUTPUT);
  }

pinMode(Pulse1, INPUT); pinMode(Pulse2, INPUT); pinMode(Pulse3, INPUT); pinMode(Pulse4, INPUT); }

void loop() { if (digitalRead(Pulse1) == HIGH) { digitalWrite(ledPins[0], HIGH); } else { digitalWrite(ledPins[0], LOW); }

ocrdu
  • 1,795
  • 3
  • 12
  • 24

0 Answers0