Step 1: Build the Circuit.
Build the circuit following this scheme.
Step 2: Install Python Libraries and Make the Program
Step 3: Scratch Program I
Now we write a Scratch program that converts the distance from the sensor to a MIDI musical note and plays it.
The distance of the sensor is given in meters, so we calculate the musical note by converting the distance into cm and adding a value of 50 (you can play with this value depending on your taste). The program shows the note that is playing with the cat sprite.
Adding the sensor value to the Scratch program is a bit tricky, so we do it in five steps.
The first step is to write a version of the program (see image) not using the sensor value (it uses a zero constant value as a distance).
Step 4: Scratch Program Il
Click mouse right button on the "Sensor value" block, and select "Enable remote sensor connections".
Step 5: Scratch Program Ill
Now, we execute the python program from the terminal. It will show the
distance in meters at the terminal screen and it will send the data to the Scratch program (Like in the first image). Return to Scratch (be careful not to close the terminal, the Python program needs to be running) and go to the "Sensing" tray (light blue). We select "UltrasonicSensor", this is our sensor, as shown in the second image.
Step 6: Scratch Program IV
Next, we add a tick to the "Ultrasonic sensor sensor value" block, making a left click on the mouse, as shown in the image.
Step 7: Scratch Program V
Now, add the "Ultrasonic sensor sensor value" block to the Scratch program, as in the images.
Step 8: Run the Program...
Now you can run the Scratch program, pressing the green flag.
You have to see the cat saying "Playing note <the note now is playing>" and a light blue and orange variables on the top (In orange the note, and in light blue the sensor value).