004 - Brickcell Ultrasonic HCSR04 | MakeCode Microbit
Introduction
Ultrasonic sensors are devices that uses ultrasonic sound waves to measure distance and/or detect an object. They are a type of proximity sensor that works on the principle of echolocation, similar to how bats and dolphins navigate in the dark. These sensors emit high-frequency sound waves (ultrasonic waves) that are beyond the range of human hearing, and then listen for the echoes produced when these waves bounce off objects. By measuring the time it takes for the sound waves to travel to the object and back, we can calculate distances with accuracy.
Working Principle
The HC-SR04 sensor is consists of two main components: a transmitter and a receiver. The transmitter emits a burst of ultrasonic waves, while the receiver listens for the echoes. By measuring the time delay between sending the waves and receiving their echoes, the sensor can determine the distance to an object.
Measurement Range
Typically, the HC-SR04 can accurately measure distances within a range of 2 centimeters to 400 centimeters, depending on the specific model and environmental conditions.
Output
When the sensor successfully detects an object and calculates the distance, it typically provides this information as a digital signal on its output pin. The duration of the output pulse is proportional to the measured distance.
Hardware Instruction
Let's explore how to connect the HC-SR04 Ultrasonic distance sensor to a micro:bit microcontroller and utilize MakeCode for programming.
- Connect the Ultrasonic sensor GND pin to microbit GND pin.
- Connect the Ultrasonic sensor VCC pin to microbit VCC pin.
- Connect the Ultrasonic sensor trigger pin (TRIG) to microbit pin 12.
- Connect the Ultrasonic sensor echo pin (ECHO) to microbit pin 13.
Software Instruction
Now, let's start into programming the micro:bit to utilize the HC-SR04 Ultrasonic distance sensor.
- Login to https://makecode.microbit.org/ using your Microsoft account.
- Create a new project by clicking the "New Project" button. You may name it anything you want, I suggest to name it with descriptive name such as "ultrasonic-hcsr04-test".
- Click the "Extensions" block just under the "Math" block.
- Type https://github.com/gbantique/brickcell-ultrasonic-hcsr04/ on the search bar.
- Select the "brickcell-ultrasonic-hcsr04" from the search results. The "ultrasonic hcsr04" block should appear under the "Brickcell" block.
- Copy the code provided below.
1serial.setBaudRate(BaudRate.BaudRate115200);
2
3basic.forever(function () {
4 let _distance_cm = Brickcell.readDistance(DigitalPin.P12, DigitalPin.P13);
5
6 serial.writeLine("" + _distance_cm);
7 basic.showNumber(_distance_cm);
8 basic.pause(2000);
9})
- Open a Serial Monitor such as Termite terminal app https://www.compuphase.com/software_termite.htm/. Set the baud rate to 115200 bps.
Or you make a copy of my created project in your MakeCode workspace.
https://makecode.microbit.org/S43150-94172-61155-97827/
Expected Result
If you carefully follow the provided instruction above, you should be able to view the measured distance in centimeter on the serial terminal every 2000 milliseconds.
Posts in this series
- 016 - Brickcell 8x16 Dot Matrix Display | MakeCode Microbit
- 015 - Brickcell 8x8 Dot Matrix Display | MakeCode Microbit
- 014 - Brickcell CO2 SGP30 | MakeCode Microbit
- 013 - Brickcell Gesture APDS9960 | MakeCode Microbit
- 012 - Brickcell Rotary Encoder | MakeCode Microbit
- 011 - Brickcell RTC DS3231 | MakeCode Microbit
- 010 - Brickcell Gyro MPU6050 | MakeCode Microbit
- 009 - Brickcell TOF VL53L0X | MakeCode Microbit
- 008 - Brickcell Color TCS34725 | MakeCode Microbit
- 007 - Brickcell Pressure BMP280 | MakeCode Microbit
- 006 - Brickcell Weight HX711 | MakeCode Microbit
- 005 - Brickcell Fine Dust | MakeCode Microbit
- 003 - Brickcell NTC Temperature | MakeCode Microbit
- 002 - Brickcell DHT11 | MakeCode Microbit
- 001 - Brickcell LCD I2C | MakeCode Microbit
No comments yet!