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.
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.
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.