Range finder with display (problems with the display)

I'm making a small device just for fun where I use the ultrasonic range finder (HC-SR04) to measure distance and a 7 segment led display (driven by a TM1637) to display the distance. To run the display I use a library called (TM1637Display). For distances longer than about 50cm or so it more or less throws random numbers out (both on serial and display), and I just can't seem to find out why.

When I detach the led display it seems to display the correct distances (displayed with serial output).

Both the display and the sonar are connected to the 5V pin on the arduino, could it be that they interfer with each other?

Code:

#include <TM1637Display.h>

#define CLK 2
#define DIO 3

#define triggerPin 7
#define echoPin 8

TM1637Display display(CLK, DIO);


void setup() {
 Serial.begin(9600);
display.setBrightness(0x0a);
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop() {
digitalWrite(triggerPin, LOW);
delay(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
unsigned long pulseDuration = pulseIn(echoPin, HIGH);
unsigned long distance = pulseDuration / 58;
display.showNumberDec(distance);
Serial.println(distance);
delay(200);
}

My wild guess is a power problem.
How are you powering everything?

Pauly:
My wild guess is a power problem.
How are you powering everything?

That's my concern as well. I've tried powering the arduino with the USB and battery (2S lipo). Same result with both.

Could it be that the led display and the ultrasonic sensor draw more current than the 5V voltage regulator can handle?

Okay, so I've tried something else, more precisely to turn off all led segments before measuring and then turn them on again after measuring and calculating the distance in case it overloaded the 5V regulator. It still behaves as before, throwing random numbers when the distance is more than halv a metre.

#include <TM1637Display.h>

#define CLK 2
#define DIO 3

#define triggerPin 7
#define echoPin 8

#define SERIALDEBUG

TM1637Display display(CLK, DIO);
byte clearDisplay[] = {0x00, 0x00, 0x00, 0x00};

void setup() {
#ifdef SERIALDEBUG
Serial.begin(9600);
#endif
display.setBrightness(0x0a);
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop() {
display.setSegments(clearDisplay);
delay(10);
digitalWrite(triggerPin, LOW);
delay(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
unsigned long pulseDuration = pulseIn(echoPin, HIGH);
unsigned long distance = pulseDuration / 58;
delay(10);
display.showNumberDec(distance);
#ifdef SERIALDEBUG
Serial.println(distance);
#endif
delay(200);
}

Do you have a link to the 7 segment display or the sensor?
Or maybe links to datasheets?

Pauly:
Do you have a link to the 7 segment display or the sensor?
Or maybe links to datasheets?

http://www.micropik.com/PDF/HCSR04.pdf

According to the datasheet it draws 15mA current.

Btw, I found out it worked by clearing all segments before measuring, however doing that I get an annoying flicker so there should be a way to avoid this.

I also looked at the power pin on the sonar with an oscilloscope and it shows no dips in voltage. When looking at the echo pin with the scope it seems to be pulled low too early or at random intervals (the length of the pulse is proportonal to the measured time interval between trigger and echo). Somehow connecting the led display affects the echopin, even though the supply voltage is stable.

Lars81:
When looking at the echo pin with the scope it seems to be pulled low too early or at random intervals

Sounds like the echo pin is floating. Try adding a 10k resistor to ground.

aisc:
Sounds like the echo pin is floating. Try adding a 10k resistor to ground.

Tried, but to no effect.

Could it be that the TM1637 library uses interrupts that interfer with the pulseIn() function and if the display is not connected it doesn't trigger the interrupts?

I think perhaps that it uses pwm since it's possible to set the brightness of the display and that this uses the same timer as the pulseIn(), could that be it? If so, is there a way to circumvent this?

Looking at a similar sensor I see this note:

Note: For close-up measurements, the Ping))) sensor only needs to be roughly 8 to 10 cm above your working surface. However, if you are measuring objects that are more than a half a meter away, make sure to keep your Ping))) sensor at least half a meter above the floor. Please read the Ping))) documentation (398k pdf) for more practical considerations.

Good luck figuring it out.

Pauly:
Looking at a similar sensor I see this note:

Good luck figuring it out.

I pointed it towards the ceiling with nothing around it to obstruct the signal.

Even if this was the problem it wouldn't explain that it works when the display is not attached (even though it's included in the code).

I will try some other libraries for the led display, maybe that will help.

Which Arduino?
How many digits in the 7 segment display - is it 4?

I have a similar setup with an HC-SR04 + 4 Digit 7 segment display.

This would be my approach to trouble-shoot this problem....

From your comments it seems when the 7 segment display is not connected, the serial display is correct.
When the 7 segment display is connected the problem appears.
Ergo something about the 7 segment display is causing an issue.

As a test I would run 2 count experiments:

  1. Count up in integers starting at 0.
  2. Count up in integers divided by 58 starting at 58.

See how the count displays and if u can learn anything from it.

I am purely speculating, but...
You are calculating the distance in cm.
When u divide (by 58), you may not necessarily get an integer.
I am wondering if the decimal points are making the numbers too long for the 7 segment display to handle.
If this is the problem, I would define the serial display variable as a float and the 7 segment display variable as an integer i.e. 2 separate variables.
I would then manipulate the distance calc further for the 7 segment display value to be an integer.