Placeholder Image

Subtitles section Play video

  • Hi I am Massimo Banzi

  • and I like to make things

  • and welcome to another Arduino tutorial video.

  • Today we are going to build a theremin.

  • A theremin is a musical instrument

  • that produce different sounds

  • depending on the position of the hands

  • of the player around the instrument.

  • In this particular case

  • we are going to build 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 will be using a photoresistor

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

  • Here we have a piezo-buzzer.

  • The piezo-buzzer produces

  • sounds every time it is

  • turned on and off.

  • It is connected with a wire

  • to pin number 8.

  • While here we have a light sensor

  • connected to analog input zero.

  • The light sensor

  • detects the amount of light

  • that hits the surface of the sensor.

  • By moving the hand

  • from the sensor

  • we reduce or increase

  • he 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 are going to use

  • the variation of voltage to

  • gauge the distance of the

  • player's hand from the sensor,

  • and we are going to map that

  • to appropriate values of sounds

  • and then we are 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 the red and black wire

  • to the two strips

  • on the side of the breadboard.

  • Then we connect the piezo-buzzer.

  • Since the piezo-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

  • he piezo in the correct lines

  • on the breadboard.

  • Now let's place the photoresistor.

  • Here is the photoresistor,

  • we placed it on the breadboard.

  • We connect a resistor between

  • this leg of the photoresistor

  • and ground.

  • We have

  • another wire going from

  • the 5v rail to the other

  • side of the photoresistor,

  • and then one wire

  • connects the photoresistor

  • and the resistor here to the

  • analog input zero

  • of the Arduino.

  • In this case we have set up

  • a sensor that reads

  • the amount of light

  • and converts that into voltage

  • that we can measure with Arduino

  • and then we have connected an actuator,

  • the piezo-capsule

  • that produces sounds

  • and now we are going to write

  • a piece of software that

  • ties them together.

  • The software that we are going to use

  • for this project

  • starts off with a

  • 5 seconds calibration period.

  • During this time

  • you will move the hand

  • near the sensor like this

  • to let Arduino calibrate the values

  • that represent the minimum

  • and the maximum amount of light

  • that can hit the sensor.

  • After these 5 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 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 sensorValue.

  • It's an integer variable

  • that stores the values

  • read from the light sensor.

  • After that we define to variables

  • called sensorLow and sensorHigh

  • and these are used

  • in the calibration phase

  • to determine which were the minimum

  • and the maximum values read

  • from the sensor.

  • So, where is the trick?

  • As you can see we are defining

  • the variables.

  • sensorLow starts off at 1023

  • and sensorHigh starts off at zero.

  • This is done on purpose

  • o make sure that if we read

  • a value from the sensor

  • it will always be less than 1023.

  • If we start with sensorLow at 1023

  • we are making sure that

  • the first value that we read will be

  • less than that and the calibration

  • can operate correctly.

  • At the same time

  • we are using sensorHigh

  • and setting it up at zero,

  • so that any value we will read

  • from the sensor will

  • hopefully be more than zero.

  • After these variables we define

  • the classic constant ledPin

  • and we assign it the value 13,

  • because we are going to use the LED

  • to signal

  • when the calibration phase is over.

  • In the setup we have pinMode(),

  • defining pin number 13 as an OUTPUT.

  • And then we

  • digitalWrite(ledPin, HIGH),

  • so that we basically turn on the LED

  • to signal that the calibration

  • phase has began.

  • Then here we have

  • an interesting piece of code.

  • It's a while loop

  • that uses the millis() function

  • to make sure that the calibration phase

  • lasts for exactly 5 seconds.

  • How is that done?

  • The millis() function

  • is a function that

  • returns the number of milliseconds

  • that have passed since the last time

  • the Arduino board was

  • turned on or reset.

  • Every time you upload code

  • or press the reset button,

  • or you plug the power,

  • the Arduino restarts

  • from zero milliseconds

  • and millis() will return a number

  • that grows as time goes by.

  • What we are going to do here

  • is that since this code

  • is exactly at the beginning

  • of the setup(),