The Official Raspberry Pi Projects Book - Projects_Book_v4

(singke) #1

Tutorial WALKTHROUGH


(^146) The Official Raspberry Pi Projects Book raspberrypi.org/magpi
THE RASPBERRY PI
ULTRASONIC THEREMIN
Build your very own theremin musical instrument using an ultrasonic
distance sensor and a little bit of Python and Sonic Pi code
theremin is a unique musical instrument that
produces sound without being touched.
In this tutorial, you will use an ultrasonic
distance sensor to control the notes played by Sonic Pi.
An ultrasonic distance sensor has four pins: Gnd
(ground), Trig (trigger), Echo (echo), and Vcc (power).
To use the sensor, you need to connect its Gnd pin to
a GND (ground) pin on the Raspberry Pi, the Trig pin
to a GPIO pin on the Pi, and the Vcc pin to the 5 V pin
on the Pi.
The Echo pin is a little more complicated. It needs
to be connected through a 330 Ω resistor to a GPIO
pin on the Raspberry Pi, and that pin needs to be
grounded through a 470 Ω resistor. The diagram above
shows one suggested arrangement. If you’ve wired
up the sensor as shown in the diagram, your echo pin
is 17 and your trigger pin is 4.
Click on Menu > Programming > Python 3 (IDLE), to
open a new Python shell. Click on New > New File. The
code to detect distance is listed in theremin1.py. Type
it into your new file, then save and run it.
The sensor.distance is the distance in metres
between the object and the sensor. Run your code and
move your hand backwards and forwards. You should
see the distance changing, as it is printed in the shell.
Getting Sonic Pi ready
Sonic Pi will receive messages from your Python
script. Open Sonic Pi by clicking on Menu >
Programming > Sonic Pi. In the buffer that is open,
you can begin by writing a live_loop. This is a loop
that runs forever, but can easily be updated, allowing
you to experiment. You can add a line to reduce the
time it takes for Sonic Pi and Python to talk.
live_loop :listen do
set_sched_ahead_time! 0.1
end



HC-SR04 A
ultrasonic
distance sensor
magpi.cc/
2q3cZ9J
Breadboard
magpi.cc/
2q34ZFz
Jumper wires
magpi.cc/
2q2Kqcm
You’ll
Need
MARC SCOTT
Marc used to run a Raspberry Pi and
Minecraft club at his old school, where he
taught Computer Science, and Systems
and Control. He's now Head of Curriculum
at the Raspberry Pi Foundation.
magpi.cc/2pOFrJB
(^12345678910111213141516171819202122232425)
6789101112131415161718192021222324252627282930
2627282930



  • _


  • _




  • _




  • _
    a b c d e
    f g h i j
    a b c d e
    f g h i j
    3V3GP2GP3GP4GNDGP17GP27GP223V3GP10GP09GP11GNDDNCGP5GP6GP13GP19GP26GND
    5V5VGNDGP14GP15GP18GNDGP23GP24GNDGP25GP8GP7DNCGNDGP12GNDGP16GP20GP21
    12345
    Below Play the musical theremin by
    moving your hand up and down over the kit
    A voltage divider, comprising two
    resistors, is used to reduce the voltage
    of the Echo pin current to 3.3 V
    HC-SR04 ultrasonic distance sensor
    detects the distance to an object,
    such as your hand
    The sensor has four connections to
    the GPIO header on the Pi, including
    5 V power and GND




Free download pdf