Skip to content

Instantly share code, notes, and snippets.

@JasonParmenter
Last active July 29, 2022 10:07
Show Gist options
  • Save JasonParmenter/aa556107c18e38796326 to your computer and use it in GitHub Desktop.
Save JasonParmenter/aa556107c18e38796326 to your computer and use it in GitHub Desktop.
Sample Arduino sketch for reading temperature from MCP9700A sensor
/**
This is a simple example sketch that reads the analog output voltage from the MCP9700A temperature sensor.
Further information about the MCP9700A can be found here:
http://ww1.microchip.com/downloads/en/DeviceDoc/20001942F.pdf
Two methods used in this example: tempF(), tempC() utilizing conversion formula found in the
MCP9700A data sheet.
*****************************************************************
Wiring:
RED <--> 3.3V or 5.5V pin (power over I/O pins is OK).
BLACK <--> Any analog pin on the arduino (A0) in this example.
Bare <--> GND
*****************************************************************
created 21 Aug. 2014
by Jason Parmenter
*/
// Any analog pin will do, but we will use analog pin 0.
const int sensorPin = A0; // BLACK wire from temp sensor (analog voltage)
float voltage = 3.62; // Typically 5V on Arduino (This is the reference voltage used by your boards A/D Converter).
float tmpC = 0;
float tmpF = 0;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
pinMode(sensorPin,INPUT);
}
void loop() {
// read the analog in value:
tmpC = getTempC();
tmpF = getTempF();
// print the results to the serial monitor:
Serial.print("TempC = " );
Serial.print(tmpC);
Serial.print(", ");
Serial.print("TempF = ");
Serial.println(tmpF);
delay(1000);
}
/*********************************
Method to get temperature (C)
**********************************/
float getTempF()
{
float tmp = analogRead(sensorPin);
tmp = tmp*voltage/1023;
return (tmp/0.01) - .5;
}
/*********************************
Method to get temperature (F)
**********************************/
float getTempC()
{
float tmp = getTempF();
return (tmp - 32.0) * (5.0/9.0);
}
@07et842
Copy link

07et842 commented Jul 29, 2022

in line #54, should it not be return (tmp - 0.5) / 0.01; ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment