The HC-SR04 Ultrasonic Distance Sensor is a low-cost module you can use to measure distance. It "provides 2cm to 400cm of non-contact measurement functionality with a ranging accuracy that can reach up to 3mm"
Review:
Operation:
Send a 10 μ sec pulse to the Trigger input
Use the "PulseIn" function on the Echo pin to measure the time for the pulse to return to the sensor.
Calculate the distance
(2) jumper wires
breadboard
Grove cable
Insert the HC-SR04 Ultrasonic Distance Sensor into the breadboard
Make a Grove-to-breadboard cable:
Cut a Grove cable in half
Method 1:
Cut (2) different color jumper wires in half to create (4) half-jumper wires
Solder the (4) half-jumper wires to each of the Grove cable wires. Insulate the connections with heat-shrink tubing or electrical tape
Method 2: (this photo ➡)
Connect male Dupont pins to the cut wires on one half of the Grove cable.
Method 3:
Connect the Grove cable to the D7 socket
Connect the jumper wires to the HC-SR04 Ultrasonic Distance Sensor:
VCC VCC
GND GND
D8 Trig
D7 Echo
Build this sketch ➡
Or, to save some time, click to download this file and save it to your computer (remember where you saved it!)
Ultrasonic_Distance_Sensor_HCSR04.cdc
Then, in Codecraft, use "Files ... Open local projects" to open the file you saved.
Upload and test:
Move your hand in front of the sensor and look at the distance numbers displayed on the Serial Monitor.
Do the numbers match the distance between your hand and the sensor (in cm.)?