I'm using an HC-SR04 ultrasonic sensor for a simple project,
I used the example HC-SR04 library and uno board and it worked fine, but when I used the long wire (80cm) for the trigger pin and echo pin, I received not changeable wrong data
my question is there any software or hardware technique to avoid the attenuation of those signal
Did you also have a long ground wire?
The sensor doesn't send any "data", could you explain in detail what you mean?
The problem seems to be related to the cables used and/or their length, decreasing the signal levels. Or, more probably, some kinda interference issue: the picture shows a small board together with the sensor and some additional wires, so a detailed picture of that box together with a full description of the wires usage could help us to help you...
the box below the sensor is not connected to anything, just trying to show how big the cable is
and sensor data has not changed with any distance change,
I use this cable length with an I2C device and it works fine, however, it seems the echo or trigger signal getting too weak to process by Arduino UNO
Well, the photo isn't much clear (distant and out of focus) and the cables going to the sensor are hidden by a cable so we can't see which goes to what:

I also see "something" (that small square board) has been glued inside that box, and as the cable goes thorugh a hole of that box, it doesn't look to me to be there "just for scale". And I hope you don't close that box with the sensor inside (I can't see any holes in it, but it could be because of the blurry picture...).
Anyway, if the cable is used only for that sensor, a better and closer picture could help much more than that, together with a clear description of the cable connections, the sketch you're using to test the sensor (maybe just a sensor example code could be fine), and, most important, your meaning of "sensor data has not changed with any distance change" (what is "sensor data" for you? Make a practical example, describing what you do and what you get from the sketch/serial output).
In your code, try making the trigger pulse longer, maybe 20us
no, nothing affected
this is the source library code and I've changed the delayMicroseconds number in function afstandCM
should pulsein change as well?
AfstandsSensor::AfstandsSensor (
int triggerPin, int echoPin) {
this->triggerPin = triggerPin;
this->echoPin = echoPin;
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
}
double AfstandsSensor::afstandCM() {
//Afstanden måles ud fra en standrad temperatur på 19.307 grader celcius, hvilket svare ca til 343m/s for lyd.
return afstandCM(19.307);
}
double AfstandsSensor::afstandCM(float temperature) {
// Sikre først at triggerPin er LAV
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
//For at aktivere sensoren holdes triggerPin HØJ i 10 microsekunder.
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
//Sensoren retunere nu afstanden med at holde echoPin HØJ i en periode svarende til afstanden målt.
unsigned long durationMicroSec = pulseIn(echoPin, HIGH);
double speedOfSoundInCmPerMs = 0.03313 + 0.0000606 * temperature; // Cair ≈ (331.3 + 0.606 ⋅ ϑ) m/s
double distanceCm = durationMicroSec / 2.0 * speedOfSoundInCmPerMs;
if (distanceCm == 0 || distanceCm > 400) {
return -1.0 ;
} else {
return distanceCm;
}
}
No, just thought that a 10us pulse might not make it along a long cable
Does it work with a shot cable, 15cm?
yes, with a short cable , there is no problem
You keep hiding the wires. Show a drawing.
Hi, @mkeyno
Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.
Please do not use a cut and paste image app.
Thanks.. Tom..
![]()
I found the problem was not the length of the wire, as per the above image I use the lilypad Arduino with atmega328p chip ,
when I use the Arduino Uno board everything works perfectly, however when I use lilypad and select lilypad from the board manager, it doesn't upload the code,
but if I choose Arduino Uno in the board manager it does upload but the ultrasonic sensor sends the fixed data
any clue to solve this situation
I never had/used a Lilypad but I don't think it's fully compatible with UNO. And AFAIK Lilypad uses ATmega32U4, not 328P (are you sure?), so generally speaking you can't ever mix boards even if the IDE apparently uploads the code.
You need to get the Lilypad properly configured on the IDE first, being able to properly upload the code. If not, you can't predict its behaviour, resulting just in a loss of time. For you and us.
yes I see the mcu and its definitely 328p , and I use this board for a couple of project without any problem and always select Arduino uno in broad manager ,
That's weird, googling a bit I see Arduino docs for this (retired?) Lilypad board saying:
The LilyPad Arduino Main Board is based on the ATmega168V (the low-power version of the ATmega168) or the ATmega328V. The LilyPad Arduino was designed and developed by Leah Buechley and SparkFun Electronics.
Anyway, my 2.3.2 IDE looks like it has the specific drivers for Lilypad:
What IDE version (and OS) are you using?
yes That's weird, I might add, this is Chinese board which was a little more expensive than the Arduino mini, it is possible fuse bits are not the same as those Arduino Uno
Well, I see you could have plenty of space to install a board, and I don't think any (even compatible ones) UNO or Nano board or any other more common board could be much more expensive.
You could even consider an ESP8266 board like Wemos D1 (R2 or Mini), the only drawback is they have just a single ADC input (I don't know if you need more).
But IMHO the lastest UNO R4 Minima could be the more adequate and reliable solution, for a fair price.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.


