An ultrasonic sensor is a device that detects an object and measures the distance to it. It measures the distance by emitting ultrasound and receiving the wave that the object reflects. By recording the time elapsed between the sound wave being generated and the sound wave bouncing back, it is possible to calculate the distance between the sensor and the object.
HC-SR04 Ultrasonic Sensor Module
The HC-SR04 ultrasonic sensor uses sonar to measure the distance to an object. It emits ultrasound at 40 000 Hz which travels through the air and if there is an object or obstacle in its path the wave will bounce back to the module.
The HC-SR04 Ultrasonic Module has 4 pins:
- Ground (GND)
The Ground (GND) and the VCC pins of the module must be connected to the Ground and the 5V pins on evive respectively and the Trig and Echo pins to any 2 Digital I/O pins.
Interfacing Ultrasonic Sensor
Activity: Displaying the Distance Measured on the TFT Screen
Follow the steps below to connect the Ultrasonic sensor to evive:
- Take the four male-to-female jumper wires and connect their female ends to the 4 pins of the sensor.
- Take the free male end of the wire connected to the VCC pin and connect it to the 5V pin on evive.
- Connect the free male end of the wire connected to the Trig pin and connect it to Digital I/O pin 2.
- Connect the free male end of the wire connected to the Echo pin and connect it to Digital I/O pin 3.
- Take the free male of the wire connected to the GND pin and connect it to the GND pin on evive.
Below is the circuit diagram:
Follow the steps below to write the script:
- Connect evive to your computer and open PictoBlox; if already open and working on a project, save that project. Then, click on New.
- Go to the toolbar and click on the Board menu. Then select evive as your board.
- Next, click on the Connect menu and from the fly-out menu, select the Port to which evive is connected e.g. COMXX or ttyXX.Once you select the port, the icon beside the Connect tab will become connected.
- Go to the Events palette and drag and drop the when flag clicked block.
- Next, go to the Display palette and drag and drop the fill screen with () color block. You can change the background color if you want to by clicking on the color and moving the sliders in the drop-down menu.
- From the Control palette, drag and drop the forever block below the fill screen with () color block.
- Go to the Display palette and drag and drop the set cursor at () () block inside the forever block.
- Then, drag and drop the write () block below the set cursor at () () block.
- Go to the Operators palette and drag and drop the join () () block inside the space of the write () block. In the first space of the join () () block, write ‘Distance = ‘ and in the second space drag and drop another join () () block.
- From the same palette, drag and drop the round () block in the first space of the second join () () block.
- Now, to read the distance with the help of the ultrasonic sensor, you need to add a new palette/extension from the extension library. Click on the purple button in the bottom-left corner of the window and add the Sensors extension. Now from this extension, drag and drop the get ultrasonic sensor distance (cm) | trig () , echo () block inside the round () block.
- Next, go to the Looks palette and drag and drop the say () for () secs block below the write () block. Then, repeat steps 8 through 10.
Below is the complete script:
Click on the green flag to run the script.
Click on the red octagon, next to the green flag to stop the script.
It this tutorial, you learned how to interface an Ultrasonic sensor with evive using PictoBlox.
Hope you had fun! 🙂