Temperature to sound

Hello!

I'm trying to make an art installation but I don't have a lot of experience with arduino. I'm trying to convert temperature to tone. So, for example, when it's 23 degrees you can hear a C note, and when it's 26 degrees you can hear an E note, and so forth.

I found the pitches.h library, which works perfect for what I want. And I found this code:

const int hot = 87; //set hot parameter
const int cold = 75; //set cold parameter
void setup() {
pinMode(A2, INPUT); //sensor
pinMode(2, OUTPUT); //blue
pinMode(3, OUTPUT); //green
pinMode(4, OUTPUT); //red
Serial.begin(9600);
}
void loop() {
int sensor = analogRead(A2);
float voltage = (sensor / 1024.0) * 5.0;
float tempC = (voltage - .5) * 100;
float tempF = (tempC * 1.8) + 32;
Serial.print("temp: ");
Serial.print(tempF);
if (tempF < cold) { //cold
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
Serial.println(" It's Cold.");
}
else if (tempF >= hot) { //hot
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
Serial.println(" It's Hot.");
}
else { //fine
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
Serial.println(" It's Fine.");
}
delay(10);
}

Which is used to read temperature with LEDs. But I don't really understand how I can replace this with the notes from the pitches.h library. So instead of the lights turning on, you hear sound.

The arduino is connected to a speaker and a temperature sensor. I've got the temperature sensor to work and the tones to work on the speaker, but I don't get them to work together. Does anyone know how I can do this?

Thank you very much! :slight_smile:

The pitches.h file just defines the frequency associated with a note name (C, G, etc.).

So, if you want to play a C when the tempC is 23, there is no reason to calculate the temperature in Fahrenheit.

   if(tempC == 23)
   {
      tone(pin, C, howeverLongYouWant); // or something like that
   }

One small change to PaulS code (since tempC is a float)

if((22.999<tempC) & (24.00>tempC))
{
tone(pin, C, howeverLongYouWant); // or something like that
}