Problem with HC-SR04 distance sensors

hc-sr04_nano_oled_bb.png (310430Bytes)
disp4_ultra.c (1320Bytes)

Hi Everyone,

I have been finding that I get erratic distance measurements on my tests with the HC-SR04 sensors. I put together a circuit with 128x64 OLED SSD1306 I2C display (Really like that module, by the way) connected on a breadboard to Arduino Nano v3.

The idea behind the circuit is:

Use the NewPing library to generate a distance measurement in centimeters.

Print the distance value to the screen, and keep on repeating.

As the sensor is moved around (8V lipo battery connected to Vin and Ground) you can see the distance updated on the tiny screen.

So, the circuit and arduino sketch works reasonably well. I was very happy on my first run, that I was lining up a ruler next to the sensor, and getting a matching distance value being displayed (from approx 40cm down to approx 1cm, with best values in the range 35cm to 2cm). However I noticed: "But this isn't producing any numbers for greater than 50cm" Why's that?

I made little tweaks and changes to my sketch, such as adding/removing the max_distance restriction to the library call. I thought, suppose it could be "this" sensor, and as I have about 4 of these, I decide to turn off, swap sensor, and check the new sensor's measurements. Sensor 2 and 3 gave garbage - fairly random numbers returned from 0-190cm (with no effect from the actual distance to the wall). Pretty unhappy about that - It takes about 25 days for new deliveries from Hong Kong/China. The last HC-SR04 sensor gave decent values again, but only from ~ 1cm to 50cm, like the first one I tested.

All the tests have been done with I2C ports connected to OLED, and GPIO 7 and 8 connected to Echo and Ping on the sensor. The Arduino is 5 Volt model, ATmega328.

Have any of you experienced similar problems? Any idea what I can do to improve performance - Either the garbage sensors or the sensors which aren't giving a decent range. Would it be better if I switched to GPIO 2 and 3 (which are commonly used in examples)? Any of your new sensors not working from first use? Maybe use another library or custom code?

Looking forward to what your comments will uncover.

========

Some more comments about this:

I have just tried out the 1602 LCD module for displaying the distance given by the Ultrasonic sensor. This is behaving much better than the 128x64 OLED display, and isn't interfering with the displayed distance data. Please see the attached code sample. I've been getting decent measurements from approx 3cm to 300cm.

On the OLED display I did get an improvement in the results, by doing the following: The OLED is supposed to support 3.3V - 5V, so I decided to connect it to 3.3V on the Arduino, rather than 5V (which the Ultrasonic distance sensor is using). I also used 10k pull-up resistors on the SDA and SCL lines, and 1 microF capacitor across 3.3V and GND. I have observed some improvement in the distance measurement results being displayed, but still not perfect. I suspect the I2C bus is affecting the accurate timing required by the Ultrasonic Trigger/Echo pulse measurement.

5v regulated they need

These sensors are picky with voltage provided. Random values may be sign of fried chips, improper alimentation or missing connection.

The code has a big impact on the range, which can go far beyond 50cm, but losing precision.

Don’t think it is a power problem…

I have tried powering over USB cable (5V) and separately over 8V Lipo connected to Vin and Ground. In each case, I had the same sensor results.

Unless I’m vastly mistaken, the Arduino Nano v3 manages voltage regulation of 5V-12V coming in on Vin, and puts out 5V on the 5V pin, which is being used to power the HC-SR04 sensor.

I’ll possibly post pictures, circuit diagram, code at a later date. All I’ve done is as described.

 

Circuit diagram added

Hi All,

Thanks for your comments so far. I’ve used Fritzing to draw a circuit diagram of what I have put together, but note there are a couple of differences to the real thing, which I’m mentioning here:

There is a 8V Lipo instead of 9V Battery. When powering by usb cable, I disconnect the Lipo.

The display is a four-connection I2C 128x64 OLED SSD1306 display, not the one built by Adafruit, in the image file.

Oddbot: Not sure why you are stating the thing you did. I never connected 8V directly to 5V port on Arduino Nano - I connected 8V to the regulated Vin port - Please see the circuit I’ve attached - Hope that makes it clear. If I’m mistaken about this, then feel free to berate me further.

 

Latest findings

Hi all,

Some more information. I followed Oddbot’s advice, and set up a test system using serial port for viewing the distance measurements of the HC-SR04 using the NewPing library, but with no I2C activity. This has had the desired effect. The range being detected (without I2C) is 1cm - 150cm (and higher). The I2C seems to be interfering with the longer range measurements, and I was getting approx 1cm - 50cm (accurate values).

Also, I’ve been comparing my set of HC-SR04 sensors. Plug one into the test breadboard, test it for a while, then plug a different one in and test that. Some of my sensors give bad results. That could be due to misadventure (e.g. chip frying), or it could be that they were poor quality ones. I’ve bought a new batch, so I can test out, decide which ones I wish to use, and avoid bad sensing.

Not entirely clear what I need to do to turn off I2C interrupt vector during the NewPing distance measurement, however I shall go out and hunt for more information. Once I’ve managed to do that, I can update this forum entry with more details.

Thanks for your suggestions, Oddbot.

More findings - Trying a 1602 LCD

Added to the bottom of the main post, are some more findings of mine. Also some code has been attached to the post.

SPI OLED 128x64 pixel SSD1306 works
Another update. I’ve finally got an SPI version of the SSD1306 128x64 pixel display. I’m not having the problem of interference with this being used to display calculated distance, which I was getting with I2C version. Maybe it is because SPI is quicker at handling display updates than I2C.

With SPI display, and HC-SR04 sensor, i can get decent distance measurements from approx 3cm - 450cm. With the I2C display things started to misbehave above 55cm.

Ordered myself a second SPI display. By the way, I own 5 I2C displays and noticing the same problem across more than 1.