Temperature to Sound

A useful use of sensor and actuator on Circuit Playground is to use the temperature sensor and the speaker.

There are two modes you might want to consider in monitoring temperature and using sound:

  • A sharp transition - for example, if the temperature hits a specific value like 79 degrees Fahrenheit / 26 degrees Celsius (a common thermostat value), a single tone is played. Or if a refridgerator/dring cooler gets above a certain temperature.
  • A changing tone - changing tyemperatures make a change in sound, useful if you are monitoring temperature and also doing something else, you can hear if a temperature is going up or down, even while working on something else.

Both of these may be programmed in the same sketch - we can use the slide switch to chose one or the other:

// Adafruit Circuit Playground - Temperature to Sound  Support Open Source, buy Adafruit
//   2016-08-07 Version 1  by Mike Barela for Adafruit Industries
// Uses the CircuitPlayground library to easily use the full functionality of the board

#include <Adafruit_CircuitPlayground.h>

const float alertTemp = 90.0;  // temperature to alert on (use 32.0 for a freezer etc.)

void setup() {
  CircuitPlayground.begin();   // initialize the Circuit Playground library
  Serial.begin(9600);
}
 
void loop() {
   float temp; 
   uint16_t sound;
   if(CircuitPlayground.slideSwitch()) {       // if the slide switch is at "+"
     temp = CircuitPlayground.temperatureF();  //   read the light sensor
     Serial.println(temp);
     sound = (int) map(temp, 70.0, 100.0, 131.0, 1760.0);   // map light to music values
     CircuitPlayground.playTone(sound, 1000);   //   play sound fora second
   }
   else {   // switch set to "-" for absolute temperature measurement
     temp = CircuitPlayground.temperatureF(); // read the light sensor
     Serial.println(temp);
     if( temp > alertTemp ) {   // if the read temperature is > your prepicked alartTemp
       CircuitPlayground.playTone(3520, 1000); // play sound for a second  
     }
   }
}

If you are more comfortable with Celcius, use the CircuitPlayground.temperature function. I picked the temperatures to be reactive around body temperature for demonstration. The temperature is printed on the Arduino serial monitor so you can read the temperature while working with the code and adjusting values.

With the slide switch at "-", you should hear no sound if the air (ambient) temperature is less than 90 degrees.  Place your fingertip on the temperature sensor (where the little thermometer is) and it will heat up and the CP will make a sound when it gets above 90 degrees (the body is at 98.6 degrees F). Take your finger off and lightly blow on the sensor, the sound should stop as the temperature goes below 90 degrees.

With the slide switch on "+", behavior changes. Circuit Playground will make a continuous tone in response to the ambient temperature. You can use your finger again to heat up the sensor. The tone should get higher as it heats up. TBlow on the sensor to cool it down, and the tone will go lower.

Last updated on 2016-08-14 at 03.40.16 AM Published on 2016-08-17 at 02.09.02 PM