Sunday, January 11, 2009

Arduino and Sound

What about making the Arduino and a simple loudspeaker produce a tone?

Quite simple:
http://www.uchobby.com/index.php/2007/11/14/arduino-sound-part-2-hello-world/

i quickly made a small change in the script to get a lightsensor alter the pitch:

#define SOUNDOUT_PIN 13
#define potPin 0
int val = 0;

void setup(void){
//Set the sound out pin to output mode
pinMode(SOUNDOUT_PIN,OUTPUT);
//output the serial port to check the values
Serial.begin(9600);
}

void loop(void){
//Generate sound by toggling the I/O pin High and Low
//Generate a 1KHz tone. set the pin high for 500uS then
//low for 500uS to make the period 1ms or 1KHz.

//Set the pin high and delay for 1/2 a cycle of 1KHz, 500uS.
digitalWrite(SOUNDOUT_PIN,HIGH);
delayMicroseconds(val);//val instead of the fixed 500

//Set the pin low and delay for 1/2 a cycle of 1KHz, 500uS.
digitalWrite(SOUNDOUT_PIN,LOW);
delayMicroseconds(val);//val instead of the fixed 500

val = analogRead(potPin);
Serial.print(val, DEC); //reading a lightsensor from a second volt divider, here checking values
Serial.print("\n");//to get an idea of what is read in and make a calibration of the pitch
}

Now I had a tone from a speaker, steered from a lightsensor, which could be replaced by other sensors. The volume can be regulated by a potentiometer in the first volt divider.

Then I used a small laser (the one to hang pictures really horizontally) and really could make the pitch jump, when the laser was on the lightsensor.
This could be the start of a low budget laserharp. Just make the tone only when a certain level of light is detected.

Here are the simple connecting drawings, the values of resisters and lightsensors must be experimentally tried out, for me it just worked with a 1 K resistor, lying around.

0 comments: