Micro:bit Physical computing guides
No. 8: Ultrasonic sensor (HC-SR04 3v3 tolerant)
Connecting an Ultrasonic Sensor (HC-SR04 3v3 tolerant) to a micro:bit
Instructions on how to attach an Ultrasonic Sensor (HC-SR04 3v3 tolerant) to a BBC micro:bit.
Note: This guide is for the HC-SR04 3v3 tolerant version sold by Kitronik. If you’re using the 5v version, click here.
What you will need
Instructions on how to attach an Ultrasonic Sensor (HC-SR04 3v3 tolerant) to a BBC micro:bit.
Note: This guide is for the HC-SR04 3v3 tolerant version sold by Kitronik. If you’re using the 5v version, click here.
What you will need
- BBC micro:bit
- Micro USB cable
- Battery pack with 2 x AAA batteries
- 1 x HC-SR04 (3v3 tolerant)
- 4 x Crocodile clip leads
Understanding Ultrasonic Sensors
The ultrasonic sensor works by sending out a high-frequency sound wave and calculating the time it takes for the signal to reflect back (echo).
The sensor has 2 openings on its front; one transmits an ultrasonic wave (transmitter) and the other receives the returning signal (receiver).
It works on the principle that sound travels approximately 341m/s in the air. Using this information, along with the time it takes to send and receive the sound wave, the ultrasonic sensor determines the distance to object using the equation:
The ultrasonic sensor works by sending out a high-frequency sound wave and calculating the time it takes for the signal to reflect back (echo).
The sensor has 2 openings on its front; one transmits an ultrasonic wave (transmitter) and the other receives the returning signal (receiver).
It works on the principle that sound travels approximately 341m/s in the air. Using this information, along with the time it takes to send and receive the sound wave, the ultrasonic sensor determines the distance to object using the equation:
Where T = the time taken for the ultrasonic sensor to send and receive the sound wave.
The result is divided by 2 because the sound wave has to travel to the object and back.
The result is divided by 2 because the sound wave has to travel to the object and back.
How an Ultrasonic Sensor works
Wiring instructions
- Disconnect the micro:bit from your computer and disconnect the battery pack.
- Connect the crocodile clips to the corresponding pins:
- Trig to Pin 0.
- Echo to Pin 1.
- GND to GND.
- The HC-SR04 requires 5v to work, which is more than the 3.3 volts supplied by the BBC micro:bit. Fortunately, with the aid of a separate power source, it is possible to power the ultrasonic sensor using 3 x 1.5V (AA/AAA) batteries - thus providing 4.5V (sufficient to power the ultrasonic sensor). To connect the additional power source:
- Connect one crocodile clip from negative on the battery cage to GND on the micro:bit.
- Connect the remaining crocodile clip from positive on the battery cage to Vcc on the HC-SR04 ultrasonic sensor.
Programming your Ultrasonic Sensor
In order to program the ultrasonic sensor, you will need to add the Sonar package:
Programming your Ultrasonic Sensor
In order to program the ultrasonic sensor, you will need to add the Sonar package:
- Click on ‘Advanced’
- Click on ‘Add package’
- In the search criteria, type ‘Sonar’ and press ‘Enter’
- Click on 'sonar'
Copy the below:
Testing your code
Testing your code
- Download your program to your micro:bit.
- Reconnect your battery and run your program.