I have a strange problem with an ESP32 connected to an sensor with an open collector output (NPN). I try to explain.
This sensor is a photoelectric retroreflective switch (E30-R4NA with reflector), working with 6to36V DC
Because is a "mobility project", I need to make it working with a 12V battery. So i bought a 12V to 5V stepdown and I connect esp32 VIN to 5V output and the sensor's brown wire to battery V+
All the grounds (ESP32 and E30-R4NA) are connected to battery's negative.
Then i connected the signal (black) wire to GPIO pin 26 and, between GPIO and wire I put a diode (cathode to GPIO).
Programmatically I simply did this:
- timeval tt;
- timeval prev_tt;
- void IRAM_ATTR setTime()
- {
- gettimeofday(&tt, NULL);
- }
- void setup()
- {
- pinMode(22, INPUT_PULLUP);
- attachInterrupt(digitalPinToInterrupt(22), setTime, CHANGE)
- }
- void loop()
- {
- if(difftime(tt.tv_sec, prev_tt.tv_sec) > 5)
- {
- Serial.println(tt); // this is a seplification, in my program I set the correct time in my local format and I display it on a 20x4 LCD
- prev_tt = tt;
- }
- }
I really cannot understand why.
If I connect ESP32 to the USB of my PC (so I can read the Serial output) and the sensor to the battery, all is working without any kind problems.
Maybe I am wrong-wiring sensor to ESP32?
Thank you