hi I am Massimo Banzi and I like to make
things and welcome to another of do we
know tutorial video today we're going to
be building a theremin the theremin is a
musical instrument that produces
different sounds depending on the
position of the hands of the player
around the instrument in this particular
case we're going to be building a very
simple theremin using a light sensor as
a way to capture the position of the
hand of the player from the Arduino
you'll be using a photo resistor to
detect the amount of light and from the
amount of light we are going to guess
the distance of the player's hand from
the sensor so here we have a piezo
buzzer the piezo buzzer produces sounds
every time it's turned on and off so and
it's connected with the wire to P number
eight while here we have a light sensor
connected to analog input zero so the
light sensor detects that the amount of
light that hits the surface of the
sensor so by moving the hand from the
sensor we reduce or increase the amount
of light that hits the sensor and in
turn this information goes into the
Arduino as a variation of voltage in our
code we're going to use the variation of
voltage to gauge the distance of the
player's hand from the sensor and we're
going to map that to the appropriate
values of sounds and then we're going to
drive the piezo capsule using the tone
function in Arduino let's start building
the circuit the first thing to do is to
connect the power bus with a red and
black wire to the two strips on the side
of the breadboard then we connect the
pier
the buzzer but since the pizza buzzer is
a bit tricky to mount we have to prepare
the two wires at the right distance on
the breadboard and then plug in the
piezo in the correct lines on the
breadboard
now let's place the photoresist or here
is the photo resistor we placed on the
breadboard we connect a resistor between
this leg of the photo resistor and
ground then we have another wire going
from the five volt rail to the other
side of the photo resistor and then one
wire connects the photo resistor and the
resistor here to the analog input zero
of the Arduino so in this case we have
set up a sensor there reads the amount
of light and convert that into voltage
that we can measure with Arduino and
then we have connected an actuator the
piezo Capshaw they produces sounds and
now we're going to write a piece of
software that ties that together the
software that we are going to be using
with this project starts off with a five
second calibration period so during this
time you will move your hand near the
sensor like this to let the air do we
don't calibrate the values that
represent a minimum and the maximum
amount of light that can hit the sensor
after those five seconds Arduino will
start the main loop and during the main
loop we have a very simple structure we
read the amount of light in terms of
voltage applied to the analog in and
then we convert that to a suitable
frequency to play on the piezo speaker
and then we use the tone function in
order to play that sound let's look at
the code we start off at the beginning
defining a few variables the first one
is called sensor value it's an integer
variable that stores the
you read from the light sensor after
that we define two variables called
sensor low and sensor high and these are
used in the calibration phase to
determine which one was the minimum and
the maximum value read from the sensor
so what's the trick as you can see we
are defining the variables sensor low
starts off as 1023 and sensor high
starts off with zero this is done on
purpose to make sure that if we read a
value from the sensor it will always be
less than 1023 so if we start with
sensor low at 1053 we'd make sure that
the first value that we read will be
less and the calibration can operate
correctly at the same time we are using
sensor high and setting it up at zero so
that the any value that we read from the
sensor will hopefully be more than zero
after these variables we define the
classic constant LED pin and we assign
13 because we're going to use the LED to
signal when the calibration phase is
over in the setup we have pin mode
defining pin number 13 as an output and
then we digital right on 20d pin high so
we basically turn on the LED to signal
that the calibration phase has begun
then here we have an interesting piece
of code it's a while loop they uses the
Milly's function to make sure that the
calibration phase lasts for exactly five
seconds so how's that done so the me
list function is a function that returns
the number of milliseconds that have
passed since the last time the D Arduino
board was turned on or reset so every
time you upload code or you press the
reset button or you plug the power the
Arduino starts from zero me
seconds and me listen an umber that
grows as time goes by what we're gonna
do here is that since this code is
exactly at the beginning of the setup
it's happening in a very few
milliseconds right after the board was
turned on so by doing while Millie's
less than five thousand that we have
here in the while loop we make sure that
the code within the while loop is
executed only during the first five
seconds that the birth the board has
been turned on or reset so what is
happening here so we read through analog
read we read input zero we place that
into sensor value and then with a very
simple algorithm we check if sensor
value is more than sensor high then we
make sense or high equal to sensor value
so essentially we are saying is the
value that I'm reading from the sense of
right now higher than the highest value
I have read until now if that's the case
then that value becomes the highest
value that we have read until now the
same thing we do for selves or value and
sense or low so again we do we we check
if sensor value is less than sense or
low then sense or low becomes equal to
sensor value this code get executed as
many times as possible within the five
seconds after the ball was turned on or
reset so if I move my hand on the sensor
like this during the first five seconds
that the program has started I basically
let the light sensor experience all the
possible values of minimum and maximum
light and after that I come out of this
calibration phase with the maximum and a
minimum value stored in sensor low and
sensor high then you can see here with
digital right LED pin low we are turning
off the LED to signal that the
calibration phase
is over and then the loop is very simple
during the loop we read the analog input
zero and we store that into sense or
value then we are going to use an
interesting function called map because
we have a minimum and a maximum value
that the the sensor can read in the
current light conditions and then we
have the minimum and maximum audio
frequency that we want to playback so
instead of having to calculate manually
the matching between those values and
the frequency value that we want to play
back on the piezo speaker then what
we're gonna do we're gonna use this
function called map map is very simple
to use we specify a value that we want
to map the first parameter is sensor
value then two parameters follow sense
of value they determine which is the
value range that the input value can
have so sense of low end sense or high
determine this is the range of values
that sense of value can assume the last
two parameters of the map function they
determine which is the output range that
we expect from map so very simply we say
this is a value this value can be
between a minimum and a maximum value
and depending on which value I am
processing I want to produce a value
which is within this other range so our
aim is to basically say for any value
within sensor low and sensor high we
have to produce a number between 50 and
4000 that represents the frequency of
the audio signal we want to produce that
the result of this calculation goes into
the pitch variable in the next line of
code you can see that tone is producing
a sound on the piezo speaker connected
to pin number 8 and the pitch is the
value that we calculate
previously using map and then 20 is the
duration of this sound after this sound
has been produced we delay for 10
milliseconds and then we continue
so now let's upload the code onto the
board and see what happens you can see
there is if you gauge your movement
properly you can produce a lot of
different sounds I'm sure you will find
this project funny probably for the
first five minutes then the sound will
become too annoying and you will be
prompted to do this so thank you for
listening
so now you have to build it hack it and
share it because remember that Arduino
is you