Created
September 21, 2023 07:52
-
-
Save kakopappa/35beffd648f962299c7df485b7f13124 to your computer and use it in GitHub Desktop.
LM335
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if defined(ESP8266) | |
#define LM_PIN A0 | |
#elif defined(ESP32) | |
#define LM_PIN 36 | |
#endif | |
#define ADC_VREF_mV 5000.0 // 5000 is the voltage provided by MCU. If you connect to 3V change to 3000 | |
#define ADC_RESOLUTION 4096.0 | |
void setup() { | |
Serial.begin(9600); | |
} | |
float getTemperature() { | |
#if defined(ESP8266) | |
int analogValue = analogRead(LM_PIN); | |
float millivolts = (analogValue / 1024.0) * ADC_VREF_mV; | |
float temperature = millivolts / 10; | |
// float fahrenheit = ((temperature * 9) / 5 + 32); | |
return temperature; | |
#elif defined(ESP32) | |
int adcVal = analogRead(LM_PIN); | |
float milliVolt = adcVal * (ADC_VREF_mV / ADC_RESOLUTION); | |
float temperature = milliVolt / 10; | |
return temperature; | |
#endif | |
} | |
void loop() | |
{ | |
float temperature = getTemperature(); | |
Serial.printf("Temperature: %2.1f °C\r\n", temperature); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment