In this lesson, you will learn how to display a bar graph that is proportional to the distance of an object using an HC-SR04 ultrasonic sensor.
This module includes an ultrasonic transmitter, receiver and all of the control circuity to make it simple to use. When you look at the device you will see two round cans, one with a T for Transmitter and one with an R for Receiver.
Parts List:
- micro:bit
- USB Cable
- 2xAAA Switched Battery Pack
- 3xAA Switched Battery Pack
- Breadboard
- Edge Connector
- Jumper Wires
- HC-SR04
Connections:
The first thing to note is the HC-SR04 requires 5Volts DC to operate. This is important as the micro:bit only need 3Volts.
The HC-SR04 only has 4 pins, so it is very easy to connect up. VCC is connected to the 5 Volts supply and GND is connected to the 0 Volt or Ground.
Now be sure to connect both grounds from your power supplies together!
This is so there is a reference between the 3 Volts on the micro:bit and the 5 Volts on the sensor. Luckily the micro:bit pins can tolerate the 5 Volt signal coming into it.
Now because the HC-SR04 only has 4 pins, you can use the banana plug default pins 0, 1, 2 and GND. In the code example, the trigger pin is connected to the micro:bit P1 and the Echo pin is connected to the micro:bit P2
Now depending on which breakout board you use the way it is assembled on a breadboard may differ. In this example, the Elecfreaks breadboard adapter is used.
Software:
To program your HC-SR04 using the micro:bit it has been made super easy for you through the use of the sonar extension.
Start by making a new program. Then click on the advanced tab
and then click on extensions...
Sonar should be one of the blocks that come up, but if it does not just type sonar into the search box. To install it, just click on it!
Now you will be able to use the code below!
Leave a comment (all fields required)