ESP32 DEVKIT and 12V NPN Sensor

ienaboard
Posts: 9
Joined: Thu May 14, 2020 9:10 am

ESP32 DEVKIT and 12V NPN Sensor

Postby ienaboard » Thu May 14, 2020 10:00 am

Hi
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:
  1. timeval tt;
  2. timeval prev_tt;
  3.  
  4. void IRAM_ATTR setTime()
  5. {
  6.     gettimeofday(&tt, NULL);
  7. }
  8.  
  9. void setup()
  10. {
  11.     pinMode(22, INPUT_PULLUP);
  12.     attachInterrupt(digitalPinToInterrupt(22), setTime, CHANGE)
  13. }
  14.  
  15. void loop()
  16. {
  17.     if(difftime(tt.tv_sec, prev_tt.tv_sec) > 5)
  18.     {
  19.         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
  20.         prev_tt = tt;
  21.     }
  22. }
All is working (I have a similar project on a Raspberry), if I move fast my hand between sensor and reflector I can see timestamp, but if I stay for some seconds in front of the sensor, the esp32 freezes and reboots.

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
Last edited by ienaboard on Wed May 20, 2020 4:17 pm, edited 1 time in total.

username
Posts: 534
Joined: Thu May 03, 2018 1:18 pm

Re: ESP32 DEVKIT and 12V NPN Sensor

Postby username » Thu May 14, 2020 3:37 pm

What voltage is sensors output ?
If it's 5v you have a problem. You need to get it to 3.3v.

ienaboard
Posts: 9
Joined: Thu May 14, 2020 9:10 am

Re: ESP32 DEVKIT and 12V NPN Sensor

Postby ienaboard » Thu May 14, 2020 5:28 pm

username wrote:
Thu May 14, 2020 3:37 pm
What voltage is sensors output ?
If it's 5v you have a problem. You need to get it to 3.3v.
The sensor is NPN. The signal wire is "floating" and, when the sensor is triggered, the wire is connected to ground. I have no voltage.

username
Posts: 534
Joined: Thu May 03, 2018 1:18 pm

Re: ESP32 DEVKIT and 12V NPN Sensor

Postby username » Fri May 15, 2020 2:32 am

Try sticking a delay(10); or delay(100); in your loop().

ienaboard
Posts: 9
Joined: Thu May 14, 2020 9:10 am

Re: ESP32 DEVKIT and 12V NPN Sensor

Postby ienaboard » Fri May 15, 2020 3:57 pm

username wrote:
Fri May 15, 2020 2:32 am
Try sticking a delay(10); or delay(100); in your loop().
I tried with no success.

I am beginnig to think that the problem is the stepdown. I am using this one and maybe connecting to ground could make some problem. Moreover, even if I wrote `FALLING` sometimes the interrupt is triggered on `RISING`. All is very strange.

If I give power to ESP32 with a powerbank and to sensor with a 12V battery, all works perfectly with no problems.

ESP_Sprite
Posts: 9739
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 DEVKIT and 12V NPN Sensor

Postby ESP_Sprite » Sun May 17, 2020 7:48 am

It may be that the internal pullup in the ESP32 just has a value that is too high, causing noise (e.g. from your stepdown) to be able to interface the line. You may want to try adding an external pullup of, say, 4.7K or so to 3.3V, see if that changes things.

ienaboard
Posts: 9
Joined: Thu May 14, 2020 9:10 am

Re: ESP32 DEVKIT and 12V NPN Sensor

Postby ienaboard » Sun May 17, 2020 6:38 pm

ESP_Sprite wrote:
Sun May 17, 2020 7:48 am
It may be that the internal pullup in the ESP32 just has a value that is too high, causing noise (e.g. from your stepdown) to be able to interface the line. You may want to try adding an external pullup of, say, 4.7K or so to 3.3V, see if that changes things.
Thank you for your answer. This could be a good hypothesis. Tomorrow I will try with an external pull-up, changing pin mode to "INPUT" instead of "INPUT_PULLUP".

Thank you.

ESP_Sprite
Posts: 9739
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 DEVKIT and 12V NPN Sensor

Postby ESP_Sprite » Sun May 17, 2020 9:04 pm

Sure! You may also want to try to add a small capacitor (1nF, for example) in parallel with that resistor to clamp down on high-frequency noise even more.

ienaboard
Posts: 9
Joined: Thu May 14, 2020 9:10 am

Re: ESP32 DEVKIT and 12V NPN Sensor

Postby ienaboard » Mon May 18, 2020 7:14 am

ESP_Sprite wrote:
Sun May 17, 2020 9:04 pm
Sure! You may also want to try to add a small capacitor (1nF, for example) in parallel with that resistor to clamp down on high-frequency noise even more.
Adding a capacitor will I add delay between sensor's signal and ESP32? I need a precision of 10ms with the signal. This project is a simple clockwatch.

ienaboard
Posts: 9
Joined: Thu May 14, 2020 9:10 am

Re: ESP32 DEVKIT and 12V NPN Sensor

Postby ienaboard » Mon May 18, 2020 7:55 am

ESP_Sprite wrote:
Sun May 17, 2020 7:48 am
It may be that the internal pullup in the ESP32 just has a value that is too high, causing noise (e.g. from your stepdown) to be able to interface the line. You may want to try adding an external pullup of, say, 4.7K or so to 3.3V, see if that changes things.
Unfortunately, nothing changed. I tryed with a 100ohm resistor too, but the behaviour is always the same.

Who is online

Users browsing this forum: No registered users and 84 guests