Making Psychedelic Music by Using Arduino

Arduino Making Music

How to use an analog sensor to play psychedelic music with Arduino theremin-like prototype !

How to use use a photoresistor to play a buzzer with Arduino
The Arduino breadboard measures the voltage on a photoresistor, by changing the intensity of light one can use the voltage information to modulate the frequency of a buzzer. Finally, playing a psychedelic theremin-like prototype !

HARDWARE
+ Arduino UNO breadbord
+ 1 LED
+ 1 photoresistor
+ 1 resistor 1 kΩ

CIRCUIT

exa04_analog_input_schem

CODE

// variable declaration
// sensor value
int value;
// low sensor value calibration
int low;
// high sensor value calibration
int high;
// digital ouput pin where the buzzer is connected to
int buzpin = 8;
// LED pin alert calibration
int led = 13;

void setup() {
pinMode (buzpin, OUTPUT);
pinMode (led, OUTPUT);
// turn LED on
digitalWrite(led, HIGH);
// calibration for the first 3 seconds after program runs
while (millis() < 3000) {
// record the maximum sensor value
value = analogRead(A0);
if (value > high) {
high = value;
}
// record the minimum sensor value
if (value < low) {
low = value;
}
}
// turn LED off
digitalWrite (led, LOW);
}

void loop() {
//read the input from A0 and store it in a variable
value = analogRead(A0);
// map the sensor values to a frequency range 50 Hz - 4000 Hz
int pitch = map(value, low, high, 50, 4000);
// play the tone for 20 ms on buzzer pin 8
tone(8, pitch, 20);
// wait for a moment
delay(10);
}

Description

Photoresistance is an analogic sensor which measures the intentity of light. In particular, the resistance of the sensor decreases when the light intensity increases. The photoresistor and the resistor of 1kΩ are put in series forming a voltage divider. In other words, between the two components there is a total voltage of 5V : in light condition the maximum voltage is on the resistance, otherwise in dark condition the maximum voltage is on the photoresistor.

Arduino breadboard measures the analogic voltage  on A0 pin using analogRead() function: it returns a number between 0 and 1023 corresponding to 0 and 5 V (in a continuos way).
The read voltage is the code instruction:


//read the input from A0 and store it in a variable
value = analogRead(A0);

In order to play psychedelic music with a buzzer you can use the function tone(pin , frequency, duration) whic sends a square wave on th pin selected. Before playing the buzzer you have to convert the read voltage in a frequency, using the code instruction


// map the sensor values to a frequency range 50 Hz - 4000 Hz
int pitch = map(value, low, high, 50, 4000);

in this way the read voltage is mapped in a sound with a frequency ranging between 50 – 4000 Hz
Note that in the program setup() it is performed a calibration of the ambient light condition, in this claibration phase the LED is turned ON.

You can play psychedelic music with arduino like in my video enjoy

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: