You will learn how to interface Ultrasonic Sensor HC-SR04 with Arduino. It can be an Ultrasonic Range Sensor or any purposes.
1984 MG Maestro 2.0 EFi: The MG Maestro 2.0 EFi is a front wheel drive vehicle, with its engine located in the front, and a 5 door hatchback bodyshell. The 2 litre engine is a naturally aspirated, single overhead camshaft, 4 cylinder unit that develops 115 bhp (117 PS/86 kW) of power at 5600 rpm, and maximum torque of 182 Nm (134 lbft/18.6 kgm) at 2800 rpm.
| × | 1 | ||
| × | 1 | ||
| × | 1 | ||
| × | 4 | ||
| × | 1 |
|
|
Ultrasonic Sensor HC-SR04 is a sensor that can measure distance. It emits an ultrasound at 40 000 Hz (40kHz) which travels through the air and if there is an object or obstacle on its path It will bounce back to the module. Considering the travel time and the speed of the sound you can calculate the distance.
The configuration pin of HC-SR04 is VCC (1), TRIG (2), ECHO (3), and GND (4). The supply voltage of VCC is +5V and you can attach TRIG and ECHO pin to any Digital I/O in your Arduino Board.
The materials that we need to make this project:
1. Arduino UNO R3 CH340 (you can use any Arduino Boards)
2. Ultrasonic Sensor HC-SR04
3. Male to Male Jumper Wires
4. Breadboard
The connection of Arduino and Ultrasonic Sensor HC-SR04
In order to generate the ultrasound we need to set the Trigger Pin on a High State for 10 µs. That will send out an 8 cycle sonic burst which will travel at the speed sound and it will be received in the Echo Pin. The Echo Pin will output the time in microseconds the sound wave traveled.
For example, if the object is 20 cm away from the sensor, and the speed of the sound is 340 m/s or 0.034 cm/µs the sound wave will need to travel about 588 microseconds. But what you will get from the Echo pin will be double that number because the sound wave needs to travel forward and bounce backward. So in order to get the distance in cm we need to multiply the received travel time value from the echo pin by 0.034 and divide it by 2.
For the programming code, first we need to define the Trigger Pin and Echo Pin that connected to Arduino board. In this project EchoPin is attached to D2 and TrigPin to D3. Then define variables for the distance (int) and duration (long).
In the loop first you have to make sure that the trigPin is clear so we have to set that pin on a LOW State for just 2 µs. Now for generating the ultrasound wave we have to set the trigPin on HIGH State for 10 µs. Using the pulseIn()function you have to read the travel time and put that value into the variable “duration”. This function has 2 parameters, the first one is the name of the echo pin and for the second one you can write either HIGH or LOW. In this case, HIGH means that the pulseIn() function will wait for the pin to go HIGH caused by the bounced sound wave and it will start timing, then it will wait for the pin to go LOW when the sound wave will end which will stop the timing. At the end the function will return the length of the pulse in microseconds. For getting the distance we will multiply the duration by 0.034 and divide it by 2 as we explained this equation previously. At the end we will print the value of the distance on the Serial Monitor.
Steps :
1. First do the wiring as shown in the picture
2. Open Arduino IDE Software and write down your code, or download the code below and open it
3. Choose your own Arduino board (in this case Arduino Uno), by selecting Tools > Board > Arduino/Geniuno Uno
4. Choose your COM Port (usually it appears only one existing port), Tools > Port > COM.. (If there are more than one ports, try it one by one)
5. Upload your code by pressing Ctrl + U or Sketch > Upload
6. To display the measurement data you can use Serial Monitor by pressing Ctrl + Shift + M (make sure that the baudrate speed is 9600)
Results:
After uploading the code, display the data with Serial Monitor. Now try to give an object in front of the sensor and see the measurement.
For the consideration, you can use your manual tape meter to measure the distance and compare it with the distance on Serial Monitor. If you want to display it on LCD, you can follow the second wiring diagram and upload the code below.
and 27 others
See similar projectsFrequency is how often something occurs.
The frequency was 2 on Saturday, 1 on Thursday and 3 for the whole week.
By counting frequencies we can make a Frequency Distribution table.
These are the numbers of newspapers sold at a local shop over the last 10 days:
22, 20, 18, 23, 20, 25, 22, 20, 18, 20
Let us count how many of each number there is:
Papers Sold | Frequency |
---|---|
18 | 2 |
19 | 0 |
20 | 4 |
21 | 0 |
22 | 2 |
23 | 1 |
24 | 0 |
25 | 1 |
It is also possible to group the values. Here they are grouped in 5s:
Papers Sold | Frequency |
---|---|
15-19 | 2 |
20-24 | 7 |
25-29 | 1 |
We just saw how we can group frequencies. It is very useful when the scores have many different values.
Alex measured the lengths of leaves on the oak tree (to the nearest cm):
9,16,13,7,8,4,18,10,17,18,9,12,5,9,9,16,1,8,17,1,
10,5,9,11,15,6,14,9,1,12,5,16,4,16,8,15,14,17
Let's try to group them, but what groups should we use?
To get started, put the numbers in order, then find the smallest and largest values in your data, and calculate the range (range = largest - smallest).
In order the lengths are:
1,1,1,4,4,5,5,5,6,7,8,8,8,9,9,9,9,9,9,10,10,11,12,12,
13,14,14,15,15,16,16,16,16,17,17,17,18,18
The smallest value (the 'minimum') is 1 cm
The largest value (the 'maximum') is 18 cm
The range is 18−1 = 17 cm
Now calculate an approximate group size, by dividing the range by how many groups you would like.
Then round that group size up to some simple value (like 2 instead of 1.83 or 5 instead of 4.26).
Let us say we want about 5 groups.
Divide the range by 5:
Then round that up to 4
Pick a starting value that is less than or equal to the smallest value. Try to make it a multiple of the group size if you can.
In our case a start value of 0 makes the most sense.
Now calculate the list of groups. (We must go up to or past the largest value).
Starting at 0 and with a group size of 4 we get: 0, 4, 8, 12, 16
Write down the groups.
Include the end value of each group that must be less than the next group:
Length (cm) | Frequency |
---|---|
0-3 | |
4-7 | |
8-11 | |
12-15 | |
16-19 |
The last group goes to 19 which is greater than the largest value. That is OK: the main thing is that it must include the largest value.
(Note: If you don't like the groups, then go back and change the group size or starting value and try again.)
Even though Alex only measured in whole numbers, the data is continuous, so '4 cm' means the actual value could have been anywhere from 3.5 cm to 4.5 cm. Alex just rounded the numbers to whole centimeters.
Here are the groups with the Lower and Upper limits shown:
Length | Lower/Upper | Frequency |
---|---|---|
0-3 cm | 0-3.5 | |
4-7 cm | 3.5-7.5 | |
8-11 cm | 7.5-11.5 | |
12-15 cm | 11.5-15.5 | |
16-19 cm | 15.5-19.5 |
Now tally the results to find the frequencies. And do a total.
1,1,1,4,4,5,5,5,6,7,8,8,8,9,9,9,9,9,9,10,10,11,12,12,
13,14,14,15,15,16,16,16,16,17,17,17,18,18:
Length | Lower/Upper | Frequency |
---|---|---|
0-3 cm | 0-3.5 | 3 |
4-7 cm | 3.5-7.5 | 7 |
8-11 cm | 7.5-11.5 | 12 |
12-15 cm | 11.5-15.5 | 7 |
16-19 cm | 15.5-19.5 | 9 |
Total: | 38 |
Done!
You might also like to make a Histogram of your data.