Now we will explore using a different kind of input – an ultrasonic sensor. These sensors bounce sound waves off of objects. The Arduino calculates distance based on this data.
Click here for a video description of this assignment, circuit and program.
YOU can use this data to measure distance, trigger a robot to stop before an obstacle OR trigger an alarm if an object gets too close.
Some sensors have three pins, some have four pins. We will use a four pin sensor that you can find in Tinkercad.
Connect the four pins to the Arduino/breadboard – these pins are labeled in between the two “eyes”:
VCC – 5 volts
TRIG – a pin on the Arduino
ECHO – a pin on the Arduino (not the same as TRIG)
GND – ground (you guessed it!)
Here’s the overall tinkercad wiring:

Now let’s add the code. Click here for some starter code that you can copy paste into Tinkercad or the Arduino IDE.