4.2 Basic: Build your Sensor
On this page:
Create an Ultrasonic Distance Sensor
The HC-SR04 ultrasonic distance sensor works by transmitting a pulse of sound and listening for the echo. The sensor consists of two ultrasonic transducers. Ultrasonic refers to the frequency, 40 kHz (40-thousand cycles per second), which is a higher pitch than humans are capable of hearing. One transducer acts as a transmitter which converts electrical signal into pulses of sound when a signal is received by the “Trigger” pin. The receiver listens for the echo of the transmitted sound pulses. If it receives them, it produces an output signal sent to the “Echo” pin. The time between when the signal is sent and received is used to determine the distance the sound pulse traveled as explained above.
In this activity you will build a distance sensor using the HC-SR04 . You can do this section without a computer and make a portable sensor, or with a computer which enables you to interact with the sensor more through the use of computer coding. Or you can try it both ways!
Note: For more information on the HC-SR04 sensor, you can see the sensor datasheet.
Building your HC-SR04 ultrasonic distance sensor
- Create a circuit to read an HC-SR04 sensor from your microcontroller. Start by connecting the VCC pin on the HC-SR04 to the BAT pin of the microcontroller by first connecting the BAT pin on the microcontroller to the (+) rail on the breadboard. Then connect the VCC pin on the HC-SR04 to anywhere on the same (+) rail on the breadboard.
- Connect the GND pin (black wire) on the HC-SR04 to your microcontroller by first connecting the GND pin on the microcontroller to the (-) rail on the breadboard. Then connect the GND pin on the HC-SR04 to anywhere on the same (-) rail on the breadboard.
- Connect the TRIG pin on the HC-SR04 to Pin 12 on the microcontroller.
- Connect the ECHO pin on the HC-SR04 to Pin 11 on the microcontroller.
Note: it helps to keep the colors of the jumper wires constant to keep track of your connections. For example, all ground wires should be a black or dark color.
To use without a computer…
Setting up the LCD Screen and Button
4. The LCD screen uses 4 pins that get connected to the microcontroller. Use Male-Female jumper wires to make these connections (See Figure 2.5 below):
a. Connect “VCC” on the LCD to “BAT” on the microcontroller. You can do this by connecting the “BAT” pin on the microcontroller to the “(+)” rail on the breadboard, then connecting the “VCC” on the LCD to the same rail.
b. Connect “GND” on the LCD to “GND” on the microcontroller
c. Connect “SDA” on the LCD to “SDA” on the microcontroller
d. Connect “SCL” on the LCD to “SCL” on the microcontroller
5. Connect your button. Connect one side of the button to the “GND” pin on your microcontroller using a jumper. Connect the other side of the button to Pin “13” on your microcontroller using a jumper.
6. Before connecting the microcontroller to the battery, double check all of your connections to make sure it matches the diagram above (Figure 2.5).
7. Insert the battery into the battery holder so that the positive side, indicated by the “+” symbol, is on the side of the holder with the red wire, and the negative side of the battery is on the side of the holder with the black wire.
8. Connect the black wire from the battery holder to the “GND” pin on the microcontroller. You can also do this by connecting the black wire from the battery holder to the “(-)” rail that the microcontroller “GND” pin is also connected to.
9. Connect the red wire from the battery holder to the pin labeled “BAT” on the microcontroller (see Figure 2.6 below). Once the battery is connected, the LCD should light up and display “Hello!”. For instructions on charging the battery, see the Charging the Battery page.
10. You can start taking measurements once the LCD display changes and displays “Ready!” after 5-10 seconds. To take a single distance measurement, press the button once. A distance measurement in cm will appear on the first line of the LCD screen.
11. To stop powering the instrument, remove the red battery wire from the “BAT” connection. To try using it with a computer, remove the battery connections and follow the directions below.
To use with a computer…
12. Before connecting the microcontroller to the computer, double check all of your connections. If you have completed the “To use without a computer section”, unplug the LCD, battery, and the button and make sure it matches the diagram in Figure 2.3
13. Connect your microcontroller to your computer using the USB cable.
14. Open the Beagle Term Chrome App (for additional instructions on setting up Beagle Term, see ‘Connect to a Microcontroller’) and type “import read_dist” and press enter. This library contains all of the functions needed to measure distance with your HC-SR04 sensor
15. Before we make a measurement, we need to initialize the distance sensor. To do this, type “sensor = read_dist.read_dist()” then press enter. Now we can use “sensor” to measure the distance.
16. On a new line in Beagle Term, type “sensor.print_dist()” and press enter. Beagle Term will print the distance measured by the sensor. The distance will also appear on the LCD if you have it connected to the microcontroller.
a. You can also take measurements continuously for N number of measurements by typing “sensor.print_dist_start(N)” and pressing enter. Beagle Term will print the distance every 5 seconds. For example, if you want to take 100 measurements, type “sensor.print_dist_start(100)” then press enter. You can stop taking measurements at any time by pressing CTRL+C. The measurements will also appear on the LCD every 5 seconds if you have it connected to the microcontroller.