Ultrasonic Sensor Serial Monitor Code

0 views
Skip to first unread message

Alacoque Whitchurch

unread,
Aug 3, 2024, 4:52:01 PM8/3/24
to riatrekganne

Simple Arduino and HC-SR04 Example: After buying a HC-SR04 from Amazon, I could not get it to work out of the box. Not wanting to concede I had a DOA sensor on my hands, I searched for a simple example setup. After spending far too long on this than...

Try playing around with the code to get different results, such as letting it "see" further. Also, it might be easier if you didn't do the LED stuff and just connected your pins up, copied the code, took out the LED code, and ran it.

Okay, I tested it and got it working. I'll provide a new tutorial link, but don't listen to their code, but wire it up like they did. These modules a little unstable, and sometimes some of them will say like ocm or out of range even when it isn't. If this happens, just twist the cord a spec or move it and it'll go back to normal. I tested it and if you glue it all in so it's solidly connected to something that problem will go away.

Vulcan666:
Okay, I tested it and got it working. I'll provide a new tutorial link, but don't listen to their code, but wire it up like they did. These modules a little unstable, and sometimes some of them will say like ocm or out of range even when it isn't. If this happens, just twist the cord a spec or move it and it'll go back to normal. I tested it and if you glue it all in so it's solidly connected to something, like my RC Car for example, the problem will go away. Hope this helps! Sorry about the delay!

It started working properly with te code in the tutorial link you provided.I am still not able to figure out what was wrong with that sensor but I think that it was the high current draw which caused the arduino to reset.It still gives the constant OUT OF RANGE output when the objects in front of it are moving too fast.

These modules a little unstable, and sometimes some of them will say like ocm or out of range even when it isn't. If this happens, just twist the cord a spec or move it and it'll go back to normal. I tested it and if you glue it all in so it's solidly connected to something, like my RC Car for example, the problem will go away.

shadow0000:
It started working properly with te code in the tutorial link you provided.I am still not able to figure out what was wrong with that sensor but I think that it was the high current draw which caused the arduino to reset.It still gives the constant OUT OF RANGE output when the objects in front of it are moving too fast.

hey, i have the same problem, i get is 0CM all the time .
i try many code.
anther thing, before i up-lode the code to the arduino the gnd shortens the circle.
i used in the sensor one year ago and its work , but now no...

In this tutorial we will learn how the HC-SR04 ultrasonic sensor works and how to use it with Arduino. This is the most popular sensor for measuring distance and making obstacle avoiding robots with Arduino.

I have already used the HC-SR04 ultrasonic sensor for building several cool Arduino projects and you can check them out here:

  • Arduino Radar (Sonar)
  • Range Measurer and Digital Spirit Level
  • Security and Alarm System Project
  • Arduino Ant Hexapod Robot
You can watch the following video or read the written tutorial below. It has additional information and examples for the sensor, including specifications, pinouts, wiring diagrams, codes and more.

If there is no object or reflected pulse, the Echo pin will time-out after 38ms and get back to low state.If we receive a reflected pulse, the Echo pin will go down sooner than those 38ms. According to the amount of time the Echo pin was HIGH, we can determine the distance the sound wave traveled, thus the distance from the sensor to the object.For that purpose we are using the following basic formula for calculating distance:Distance = Speed x Time

Hey, well you would need to measure three dimensions, width, length and depth and multiply them to get the volume. Check my Arduino Range Measurer project where I showed something similar, or how to measure area using this sensor.

Thanks for the clear explanation. Still I dont understand. The counting starts when echoPin (input) gets the state HIGH (in this example). And the counting stops, when it gets the state LOW. Their time difference is 10us in the pingPin and their time difference is 10us in the echoPin. It is always the the same. The distance doesnt make any difference. The begin of the signal is moving as fast as the end of the signal. What actually starts the counting. It has to start, when the signal goes to pingPin. And it has to stop, when the signal come sback to echoPin. Would You please explain me more.

Hi there. Well sorry but all I can say is to check my tutorial on app inventor and in combination with this tutorial, make the project on your own.
My work is teaching the basics of a particular sensor, module or a program, and your are that one that should use the knowledge gained from those tutorials to make a specific project.
Cheers!

Dear, it possible the ultrasonic can detect the water leakage on the water pipe? mean the ultrasonic can receive the frequency or other value to show that when the water pipe has leakage, the value will changing? thanks

Dear,
I have an idea to measure the amount of water in a water tank.
It has been seen on your video experiment to Motion Sensor.
Question: Is it possible to transfer centimeter change in the screen to a percentage, for example 40%, 50%.
Thank.
Rakan

Dear Dejan, I am building a simple robot with this sensor. The problem is this: I want to avoid a collision at a distance of about 50 cm. How do I implement in code this value in the simple software I made. (the robot is programmed to drive an eight) with simple code as digitalWrite HIGH or LOW on two pins.Regards, Ton.

Can u email me or update the diagram of this project by adding the connections for the 162 lcd green screen ty in advance this website was a real help to me as my first arduino project and I use arduino uno is that OK or I have to change something in ue given project waiting for a quick response:)

The problem might be that you are using Arduino DUE, which has 3.3V pins and the ultrasonic sensors is working with 5V. Try using an resistor at the echo pin, as the 3.3V are fine for the Trig pin, but the Echo pin of the sensor will output 5V to your Arduino DUI which might damage it.

Hello,
I am trying to make a code for an ultrasonic sensor that gives us distance and if the distance is less than 50 the buzzer beeps but if the distance is continuously less than 50 then after 10sec the buzzer gets stop for further.
Please help me to make this code.

Hello,
I am trying to make a code for an ultrasonic sensor that gives us distance and if the distance is less than 50 the buzzer bips but if the distance is continuously less than 50 then after 10sec the buzzer gets stop for further.
Please help me to make this code.

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide you will find at the top of every forum category. It contains a lot of other useful information. Please read it.

If that is too big a step for you then get the Arduino IDE installed and download the example "Blink without delay". It will simply blink the LED on your Arduino board. What makes this meaningful is you know you have the IDE working and can download a program and have it work.

I am not sure but may be it helps just by checking principle of working of this sensors as it is depicted for low range distance you must be carefull about reflection from other source of ultrasonic. I think it would be a good Idea to place a delay between these two module so to make sure it get less noise from each other

the other reason for getting zero may be your controller could not measure reflection from wall due to lowspeed sampling (if you are polling) or reading time with low precision so I have a suggestion for you if you are familiar with STM32 there is a built-in hardware implementation for measure time of pulse using timers called one-pulse mode it may helps you get precision you need

I had the same problem with two diferents projects, and it happend the same to me. One with ultrasonic and other with RF24 module. In both projects, some really extrange things happens. But when i change for one original arduino, everything works perfect.

I tested your code as it currently is with my own HC-SR04s and a clone Arduino Nano (since that is, what I have here currently). It works like a charm. The values are rather good, maybe accurate about +-1cm. I checked in the change between 10 and 70cm with my desk plate and a wall as obstacle. (I just looked, if the values are plausible. I didn't investigate thoroughly, how accurate the values are).

I encountered problems trying to get readouts from the ultrasonic sensor in the mBot. After much tinkering and forum reading (with help from a post by @benjamin.m,) I was able to come up with this code that allows you to get sensor readouts using the COM window while connected via USB cable.

c80f0f1006
Reply all
Reply to author
Forward
0 new messages