[SOLVED] Ultrasonic sensor (sr04) decoupling problems

Hi all. I'm very TIRED of this problem. I'm struttglind with it from weeks now. Here it's the problem:

I have: 1) Arduino 2) sr04 ultrasonic sensor 3) esp8266 wifi board and other parts that are not relevant to the problem.

I'm testing distance with sr04 every half seconds (now I'm using pulsein, but i tried newping library and interrupts with the same results). Sometimes (maybe 5 minutes, maybe 15 minutes) i get totally wrong distance (example: 100cm instead of 250)

I'm pretty sure it's something related to power supply, because the esp8266 drains a lot of currents. After a lot of trying I ended up doing something like: |500x341

Both Arduino and sr04 has a 100uF and a schottky diode so their capacitors aren't discharged by other components in the circuit. The main voltage regulator is set to 5.4v, to compensate the voltage drop through the diodes.

But the problem is still there. PLEASE help me

p.s. the arduino code is very stupid:

  digitalWrite(triggerPin, LOW);
  delayMicroseconds(5);  
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);  
  unsigned int dist = pulseIn(echoPin, HIGH, 800000) / 58;

as I said, I tried newping library and interrupts with the same results

Firstly the sensor you have in the Fritzing Sketch appears to be an infrared proximity sensor and not an ultrasonic sensor. Can you post a photo of your sensor or a Sparkfun link would be helpful.

You say the voltage regulator is set to 5.4V, yet u have no resistor between the output and adjustment. According to the datasheet it appears you need a fixed resistor for a set voltage output.

Your timings in the code appear on the short side and if I recall are too short for an ultrasonic sensor - if that is in fact what u are using. FWIW I would suggest not using a library and writing the full code - the code is quite short.

Thanks for your reply, but...

1) the sensor i'm using is sr04 (https://www.aimagin.com/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/h/c/hc-sr04-02.jpg)

2) i didn't put the resistor on the fritzing scheme to make it simply. I have a 1000 omh and a trimmer to set the output voltage

3) i have tested both library and not library solutions, and i get the same results...

  1. Ok so now we have established that u are using an ultrasonic sensor which has 4 pins/connections. Your diagram only shows 3 pins/connections - so u need to fix that.

  2. Well u need to post what you actually have owise you will not get correct advice - like u did from me because I cannot guess your full circuit.

  3. Post your non library code.

There is no point to try to help you further until you post your complete circuit and code.

Well, if you are concerned with power to the sr04, why not just feed the 9v supply directly to the arduino, and then power the ultrasonic sensor with the arduino's regulated 5v? It simplifies your circuit as well - you can get rid of all the diodes & capacitors pictured and lm317(assuming you can safely use the 9V @ your esp8266's regulator input)

@Suxsem - see the confusion you cause posting something that is not what your circuit is. Fritzing ought to be banned for exactly this reason. It is no good putting down components and then putting a note saying this component is really this other component and this isn't exactly wired like this it is like that.

Anyway, do not use diodes as a form of decoupling, it will not help and only generate noise, and can cause damage to the Arduino when the power is removed but the sensor is still powered. Feeding a signal into an un powered chip is a great way of damaging it. Replace the diode with an inductor and put a capacitor on both sides of the inductor. Use 0.1uF ceramic capacitors and possibly a 100uF across one side.

What makes you think it is surges from your power supply anyway? I am willing to bet it is not.

Just a thought... but if you can't get rid of the erroneous readings, maybe adjust your code to deal with it. If it's just 1 bogus reading every 5 to 15mins, maybe sample 2(or maybe more?) pings consecutively - if the readings are different, ignore them.

I finally found the issue!!!

The esp8266 (cheap) WIFI board in my circuit it's placed very close to the Arduino AND the ultrasonic sensor and it emits a lot of RF interferences!

Now I placed a copper shield (grounded) under the wifi board and I didn't get a single wrong read since 30 hours and counting!

Thanks for the help anyway :)

Grumpy_Mike: @Suxsem - see the confusion you cause posting something that is not what your circuit is. Fritzing ought to be banned for exactly this reason. It is no good putting down components and then putting a note saying this component is really this other component and this isn't exactly wired like this it is like that.

Anyway, do not use diodes as a form of decoupling, it will not help and only generate noise, and can cause damage to the Arduino when the power is removed but the sensor is still powered. Feeding a signal into an un powered chip is a great way of damaging it. Replace the diode with an inductor and put a capacitor on both sides of the inductor. Use 0.1uF ceramic capacitors and possibly a 100uF across one side.

What makes you think it is surges from your power supply anyway? I am willing to bet it is not.

Sorry about the diagram but my circuit it's A LOT COMPLEX and I didn't had time to make the full diagram. And I chose to put in the frizing diagram only the necessary components involved in the problem.

|500x375

About diodes, I tested the I/O pins when Arduino it's unpowered and they are in high impedence state, so the sr04 can't feed anything into them. I think that, as you said, inductors are a valid alternative to diodes in this case, but I find the caps+schottky solution more reliable

I tested the I/O pins when Arduino it's unpowered and they are in high impedence state,

How did you test them. That conclusion is incorrect, you could be being deceived my your meter outputting less than the forward volt drop of the diode.

but I find the caps+schottky solution more reliable

Again what test have you done to reach this conclusion? In my opinion it is a definitely bad idea.

When the Arduino is unpowered, the pins are connected by internal protection diodes to Vcc and ground.

Better a hand-drawn schematic than a bad Fritzing diagram. See my signature for some links regarding clarity in schematics.

I agree with Grumpy_Mike about the diodes and caps being a bad idea. Some parts of the circuit will lose power before others.