I am trying to find information about utilizing the HC-SR04 ultrasonic distance sensor in a FIRST Robotics FRC situation with the RoboRIO. Last year our seasoned programmer could not figure out how to use it and wound up giving up. He has since graduated and moved on to college and a computer science future. That was using C.
Unfortunately you missed out the one thing that gives people trouble.If the HC-SR04 does not receive an echo then the output never goes low.Devantec and Parallax sensors time out after 36ms and I think 28ms respectively. If you use Pulsin as above then with no return echo the program will hang for 1 second which is the default timeout for Pulsin. You need to use the timeout parameter.The HC-SR04 barely works to 10 feet giving a total path length of 20 feet and a path time of about 20ms so set the timeout to something above that, say 25 or 30ms.If you put a resistor, say 2k2 between E and T then only connect to T you can use the HC-SR04 from just one Arduino pin. Look up single pin operation of ultrasonic sensors.Also if you are using a HC-SR04 with a PicAxe you need to up the clockspeed to at least 8MHz otherwise they don’t see the start of the echo pulse so pulsin never starts. The HC-SR04 works fine with a BS2.
Input pins when not connected to anything are said to float and because the input impedance of them is high they can be affected by stray electrostatic voltages on anything, including your fingers. Hence voltage on them can be at an illegal value between the thresholds for logic 0 and logic 1 which can cause the internal circuitry to fail or lock up or generally do bad things. To avoid this a lot of input pins have either pull-up or pull-down resistors connected to them so that when the pin is not connected it will be in a known state.The Trig pin has a 10K pull up resistor connected between it and Vcc, on mine you can see the tiny surface mount 103 resistor on the back of the board by the header pins. So when the board is powered and no circuitry is connected to Trig the voltage on it will be at Vcc.The control computer must first set the pin to logic zero and then, when it wants to trigger the HC-04, set the pin to high for 10us before setting it low again. I’ve tried a number of different sketches with my HC-SR04 and always have the same problem.
![Frc ultrasonic sensor Frc ultrasonic sensor](/uploads/1/2/5/3/125384104/361976913.jpg)
Once the maximum range of the sensor is reached, a distance of zero is returned and then the sensor will not start working again. Once it has stopped I can move objects in front of it (like my hand) but continue to get no distance readings. It works fine until then, though. I realize no one has commented here recently, but if anyone has any idea what my problem is, please let me know. Maybe a defective sensor?. I have also had this problem with cheap HCSR04 modules I got from aliexpress ($1 each oh well!). Once an “out of range” reading occurs, the device never works again, the output from echo just stays high all the time until you power it down and back up.I am looking for a solution too and have tried quite a few things including hardware and software with no luck so I feel like the reason they were a buck each was that they are simply defective “seconds” with some hardware flaw.But if anyone has any insights on this I would appreciate hearing about it.
Some knock-off models of the HC-SR04 do not implement a timeout if no echo is detected at all. In that case, the echo pin will simply remain high forever and will not generate any new pings, regardless of what you do with the trigger pin.
The original model from Robot Electronics times out after 36 ms and does not have this problem, but many (all?) cheap knock-off models do.You can reproduce the problem easily by covering the receiver transducer with your thumb, then removing your thumb. If your sensor doesn’t have a timeout, it will stop generating pings and the only way to recover is to power cycle the sensor.A workaround that works for me so far is to test the state of the echo pin 100 ms after triggering a ping. If it’s still high, you’re in a stuck state. If you wire power to the sensor through a transistor and control that transistor via another pin, then you can power cycle the sensor via software and get back to a working state. I think I was the first person to point out that the HC-SR04 doesn’t time out if it doesn’t receive an echo unlike the SRF04 from Robot Electronics.I have used a lot of the HC-SR04 modules from different manufacturers in China and non of them have needed power-cycling if they don’t receive an echo. I can confirm without a doubt that some of the very cheap (I’m talking about 80 cents including shipping!) units do lock up even if you use both trigger and echo on different controller pins.
I can also confirm that putting a 2k2 resistor between trigger and echo to run in 1 pin mode doesn’t help (wouldn’t expect it to since 2 controller pin mode doesn’t).They are not even consistent. I bought 10 of the 80 cent modules and 5 of them are very, very unstable and lock up very easily.
Others only lock up occasionally. Another oddity (that others have also pointed out) is that sometimes if they lock up if you touch them with your finger they unlock, or even if you snap your fingers close to them.
Apparently this finally triggers the sensor to sense a ping.I upgraded to $2 devices from itead studio and they work great. They do have a 36 millisecond timeout built in and I’ve used 5 different units from them with no problems. Well I have used maybe 30 from 6 different batches with different PCBs and have yet to have one that locks up.My understanding is that the HC-SR04 was designed for a bicycle alarm where it is a good thing if nobody is near and hence no echo, so it makes no sense for them to lock up.There is always the possibility that if the Trigger pin floats because the uController pin connected to it has been made an input then the module will be susceptible to static – ie the approach of fingers. Correcting the software or using a 10k pull down would stop that.If I come across one that doesn’t work I’ll post a comment. Murtaza – of course you can’t, it is just basic electronics, it is like asking “can I get a 100m USB lead?” or will my phone work underwater?”.
100m of wire has too much inductance, resistance and capacitance for a signal from the Arduino to reach the sensor or for a reply from the sensor to reach the Arduino. You could do it with two Arduinos and a radio link between them and the HC-SR04 connected by short wires to one of them, or the two Arduinos could be connected using RS485 communication shields and 100m of suitable cable. Hi Rui,thank you for your helpful article.I just got the idea to take the last 10 distance results in a global queue variable to detect if my bot is moving at all (maybe it ran onto an “invisible” obstacle).
I used the standard deviation of the distance array for that and although i have quite different results every time i measure i found 1cm enough to detect if i’m stuck in about 1 – 5 seconds.Maybe this will work out for others, too.Do you have a method for improving the quality of measurements?I found this one but seems complex to me:ijcte.org/papers/118-G227.pdfWould you agree to take the standard deviation for that?Do you have a much better approach for motion sensing?Kind regards, Bernd. Hi David,I can also confirm, waht was said about the cheap HC-SR04. It locks up very fast and can only be resetted by power off/on.Mine also shows the effect described before, like when you cover the sensor with your finger, its stops immediately. Bought 10 of these and tested 2 so far, both are behaving the same. Seems to be just crap from china. Should have bought the real ones.Have tested it with 5 different scripts, motor shield v1 and v2, but it all comes down to the sensor failing.Cheers, Matthias.
Marza,Regarding: Can anyone please tell me, can i use this sensor 100 meters apart from arduino connected via wires.Maybe.Here are things to consider. The cable adds capacitance. For CAT5 cable the capacitance per foot might be 52pF/meter so you have 5200 pF loading on the I/O lines. It may be a problem, but I doubt it. Assuming the output impedance (driving resistance) of the I/O pins is as high as 1K Ohm, then the time constant of the I/O signals (the product of the R and the C) is about 1×10^3 x 5200.10^-12 = 5.2 x 10 ^-5. Or 0.052 mS.This will make an unmeasurebly small error.Now the long wires themselves add a litter resistance. It is less than 0.2 ohms / meter so for 100 meter it is.