0

members

Medium Arduino Starter Kit: Project 6 LIGHT THEREMIN

of Arduino in UNO

Arduino Starter Kit: Project 6

Project name: LIGHT THEREMIN

Discover: making sound with the tone() function, calibrating analog sensors

Attachments: sketch

In this project, you need these parts :

1.Aruduino Uno R3 (you can also use the other version of Arduino)

2.Jumper cables

3. Resistor 1 pc (10 KOhm)

4. Breadboard half size 

5. Arduino IDE ( you can download it from here  )

6. Photoresistor (photocell) 1 pc

7. Piezo buzzer 1 pc

GENERAL

A theremin is an instrument that makes sounds based on the movements of a musician’s hands around the instrument. You have probably heard one in scary movies. The theremin detects where a performer’s hands are in relation to two antennas by reading the capacitive change on the antennas. These antennas are connected to analog circuitry that create the sound. One antenna controls the frequency of the sound and the other controls volume. While the Arduino can’t exactly replicate the mysterious sounds from this instrument, it is possible to emulate them using the tone() function. Picture below shows the difference between the pulses emited by analogWrite() and tone(). This enables a transducer like a speaker or piezo to move back and forth at different speeds. Instead of sensing capacitance with the Arduino, you will be using a photoresistor (photocell) to detect the amount of light. By moving your hands over the sensor, you will change the amount of light that falls on the photoresistor’s face, as we had in Arduino Starter Kit Project 4. The change in the voltage on the analog pin will determine what frequency note to play. You will connect the photoresistors to the Arduino using a voltage divider circuit like you did in Arduino Starter Kit Project 4. You probably noticed in the earlier project that when you read this circuit using analogRead(), your readings did not range all the way from 0 to 1023. The fixed resistor connecting to ground limits the low end of the range, and the brightness of your light limits the high end. Instead of settling for a limited range, you will calibrate the sensor readings getting the high and low values, mapping them to sound frequencies using the map() function to get as much range out of your theremin as possible. This will have the added benefit of adjusting the sensor readings whenever you move your circuit to a new environment, like a room with different light conditions. A piezo is a small element that vibrates when it receives electricity. When it moves, it displaces air around it, creating sound waves.

THE CIRCUIT

Traditional theremins can control the frequency and the volume of sound. In this example, You will be able to control the frequency only. While you can’t control the volume through the Arduino, it is possible to change the voltage level that gets to the speaker manually. On your breadboard, connect the outer bus lines to power and ground. Take your piezo, and connect one end to ground, and the other to digital pin 8 on the Arduino. Place your photoresistor on the breadboard, connecting one end to 5V. Connect the other end to the Arduino’s analogIn pin 0, and to ground through a 10-kilohm resistor. This circuit is the same as the voltage divider circuit in Arduino Starter Kit Project 4.

THE CODE

 

Create a variable to hold the analogRead() value from the photoresistor. Next, create variables for the high and low values. You are going to set the initial value in the sensorLow variable to 1023, and set the value of the sensorHigh variable to 0. When you first run the program, you will compare these numbers to the sensor’s readings to find the real maximum and minimum values. Create a constant named ledPin. You will use this as an indicator that your sensor has finished calibrating. For this project, use the on-board LED connected to pin 13. In the setup(), change the pinMode() of ledPin to OUTPUT, and turn the light on. The next steps will calibrate the sensor’s maximum and minimum values. You’ll use a while() statement to run a loop for 5 seconds. while() loops run until a certain condition is met. In this case you’re going to use the millis() function to check the current time. millis() reports how long the Arduino has been running since it was last powered on or reset.
In the loop, you’ll read the value of the sensor; if the value is less than sensorLow (initially 1023), you’ll update that variable. If it is greater than sensorHigh (initially 0), that gets updated. When 5 seconds have passed, the while() loop will end. Turn off the LED attached to pin 13. You’ll use the sensor high and low values just recorded to scale the frequency in the main part of your program. In the loop(), read the value on A0 and store it in sensorValue. Create a variable named pitch. The value of pitch is going to be mapped from sensorValue. Use sensorLow and sensorHigh as the bounds for the incoming values. For starting values for output, try 50 to 4000. These numbers set the range of frequencies the Arduino will generate. Next, call the tone() function to play a sound. It takes three arguments: what pin to play the sound on (in this case pin 8), what frequency to play (determined by the pitch variable), and how long to play the note (try 20 milliseconds to start).
Then, call a delay() for 10 milliseconds to give the sound some time to play.

Create a variable to hold the analogRead() value from the photoresistor. Next, create variables for the high and low values. You are going to set the initial value in the sensorLow variable to 1023, and set the value of the sensorHigh variable to 0. When you first run the program, you will compare these numbers to the sensor’s readings to find the real maximum and minimum values. Create a constant named ledPin. You will use this as an indicator that your sensor has finished calibrating. For this project, use the on-board LED connected to pin 13. In the setup(), change the pinMode() of ledPin to OUTPUT, and turn the light on. The next steps will calibrate the sensor’s maximum and minimum values. You’ll use a while() statement to run a loop for 5 seconds. while() loops run until a certain condition is met. In this case you’re going to use the millis() function to check the current time. millis() reports how long the Arduino has been running since it was last powered on or reset. In the loop, you read the value of the sensor; if the value is less than sensorLow (initially 1023), youl update that variable. If it is greater than sensorHigh (initially 0), that gets updated. When 5 seconds have passed, the while() loop will end. Turn off the LED attached to pin 13. You use the sensor high and low values just recorded to scale the frequency in the main part of your program. In the loop(), read the value on A0 and store it in sensorValue. Create a variable named pitch. The value of pitch is going to be mapped from sensorValue. Use sensorLow and sensorHigh as the bounds for the incoming values. For starting values for output, try 50 to 4000. These numbers set the range of frequencies the Arduino will generate. Next, call the tone() function to play a sound. It takes three arguments: what pin to play the sound on (in this case pin 8), what frequency to play (determined by the pitch variable), and how long to play the note (try 20 milliseconds to start).Then, call a delay() for 10 milliseconds to give the sound some time to play.

 

THE START

When you first power the Arduino on, there is a 5 second window for you to calibrate the sensor. To do this, move your hand up and down over the photoresistor, changing the amount of light that reaches it. The closer you replicate the motions you expect to use while playing the instrument, the better the calibration will be. After 5 seconds, the calibration will be complete, and the LED on the Arduino will turn off. When this happens, you should hear some noise coming from the piezo! As the amount of light that falls on the sensor changes, so should the frequency that the piezo plays.

THE SUMMARY

The range in the map() function that determines the pitch is pretty wide, try changing the frequencies to find ones that are the right fit for your musical style. 
The tone() function operates very much like the PWM in analogWrite() but with one significant difference. In analogWrite() the frequency is fixed; you change the ratio of the pulses in that period of time to vary the duty cycle. With tone() you are still sending pulses, but changing the frequency of them. tone() always pulses at a 50% duty cycle (half the time the pin is high, the other half the time it is low). The tone() function gives you the ability to generate different frequencies when it pulses a speaker or piezo. When using sensors in a voltage divider circuit, you probably won’t get a full range of values between 0 - 1023. By calibrating sensors, it’s possible to map your inputs to a useable range.
See one of movies on Youtube about it - link here


Other projects of Arduino

« Go back to category
Is this project fake? Report it!   
Recommend to a friend
Published at 15-08-2017
Viewed: 1461 times