Ultrasonic Distance Sensor

The Ultrasonic Distance Sensor is a very useful and clever component to use. When used with the Crumble, you are able to detect the distance of an object/obstacle. It works in a similar way to sonar; an ultrasonic ‘ping’ is sent (trigger), which is then reflected off of an object within its ‘sight’, and then detected/heard (echo). This then allows the Crumble to calculate how far away the object/obstacle is!

It requires a + and – connection, along with two I/O pads (A, B, C or D) for the Trigger and Echo pins. Connections to an Ultrasonic Distance Sensor are much easier if a Crumbliser is used.

Note: Don’t forget you will need to add batteries.

Screeny Shot 9 Nov 2017, 11.56.43

This is the block that allows you to measure the distance of an object. You can change the I/O ports by clicking the grey letters.

Screeny Shot 10 Nov 2017, 15.51.44

In this code which runs continuously, if the ultrasonic distances sensor detects something which is closer than 20cm, it turns the Sparkles to red. If not, they are green. Notice the use of a variable.

Here is the previous code working on the Crumble.

Where next?

An Ultrasonic Distance Sensor has a surprising number of uses. You could use it to set off an intruder alarm, open a servo-barrier when a ‘car’ arrives, or even make an autonomous robot that turns around if it’s going to crash!