Skip to content

Instantly share code, notes, and snippets.

@harrisonhjones
Last active September 19, 2015 19:37
Show Gist options
  • Save harrisonhjones/f83adc50c81fa7622bb2 to your computer and use it in GitHub Desktop.
Save harrisonhjones/f83adc50c81fa7622bb2 to your computer and use it in GitHub Desktop.
Example: Reads a TMP36 temperature in a loop and exposes it as a Cloud variable and sends it out via serial
// Particle TMP36 Example
// Author: [email protected]
// Description: Reads a TMP36 temperature in a loop and exposes it as a Cloud variable and sends it out via serial
// Connection: Connect VS to 3.3v, GND to GND, VOUT to A0. See http://www.analog.com/media/en/technical-documentation/data-sheets/TMP35_36_37.pdf for pin information
//
/* How it works:
The TMP36 outputs a voltage proportional ot the current temperature.
The resolution is 10 mv / degree centigrade (100 degrees centigrade / volt). There is a 500 mV offset (.5v) so negative temperatures can be measured
*/
// What pin on the Particle device is connected to the TMP36 VOUT pin (A0)
int sensorPin = A0;
// Need a global variable to hold the temperature
double tempC = 0;
/*
* setup() - this function runs once when you turn your Arduino on
* We initialize the serial connection with the computer
*/
void setup()
{
// Create a Cloud variable for the centigrade temperature
Spark.variable("temp", &tempC, DOUBLE);
// Also setup the serial port connection
Serial.begin(9600);
}
void loop()
{
// Grab the raw analog value
int rawVal = analogRead(sensorPin);
// Convert the raw analog value to voltage
float voltage = rawVal * 3.3; // First multiply by the input voltage
voltage /= 4095.0; // Then divide by the resolution of analogRead()
// Print out the voltage
Serial.print(voltage);
Serial.println(" volts");
// Convert the voltage to temperature
tempC = (voltage - 0.5) * 100 ; // (V - Offset) * 100 °C / V = °C
// Print out the temperature
Serial.print(tempC);
Serial.println(" degrees C");
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment