safwan092

Untitled

Feb 19th, 2026
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <math.h>
  3.  
  4. // Pin and resistor configuration
  5. #define THERMISTOR_PIN 1 // Analog pin between the two resistors
  6. #define SERIES_RESISTOR 100000 // 100k ohm fixed resistor
  7. #define NOMINAL_RESISTANCE 10000 // NTC resistance at nominal temp (100k ohm)
  8. #define NOMINAL_TEMPERATURE 25 // Nominal temperature in °C (usually 25°C)
  9. #define B_COEFFICIENT 3950 // Beta coefficient of the NTC (adjust if needed)
  10. #define ADC_MAX 4095 // 12-bit ADC (ESP32)
  11. #define VCC 3.3 // Supply voltage
  12.  
  13. void setup() {
  14. Serial.begin(115200);
  15. analogReadResolution(12); // Set ADC to 12-bit resolution
  16. Serial.println("NTC Thermistor Temperature Monitor");
  17. Serial.println("-----------------------------------");
  18. }
  19.  
  20. void loop() {
  21. // Read the ADC value
  22. int adcValue = analogRead(THERMISTOR_PIN);
  23.  
  24. // Convert ADC to voltage
  25. float voltage = adcValue * (VCC / ADC_MAX);
  26.  
  27. // Calculate NTC resistance
  28. // Circuit: 3.3V -- [100k resistor] -- D8 -- [NTC] -- GND
  29. float ntcResistance = SERIES_RESISTOR * (voltage / (VCC - voltage));
  30.  
  31. // Apply Steinhart-Hart equation (Beta version)
  32. float steinhart;
  33. steinhart = ntcResistance / NOMINAL_RESISTANCE; // R/Ro
  34. steinhart = log(steinhart); // ln(R/Ro)
  35. steinhart /= B_COEFFICIENT; // 1/B * ln(R/Ro)
  36. steinhart += 1.0 / (NOMINAL_TEMPERATURE + 273.15); // + (1/To)
  37. steinhart = 1.0 / steinhart; // Invert
  38. float tempC = steinhart - 273.15; // Convert to Celsius
  39. float tempF = tempC * 9.0 / 5.0 + 32.0; // Convert to Fahrenheit
  40.  
  41. // Print results
  42. Serial.print("ADC: ");
  43. Serial.print(adcValue);
  44. Serial.print(" | Voltage: ");
  45. Serial.print(voltage, 3);
  46. Serial.print("V | NTC Resistance: ");
  47. Serial.print(ntcResistance / 1000, 2);
  48. Serial.print("kΩ | Temp: ");
  49. Serial.print(tempC, 2);
  50. Serial.print("°C / ");
  51. Serial.print(tempF, 2);
  52. Serial.println("°F");
  53.  
  54. delay(1000); // Read every second
  55. }
  56. /*
  57.  
  58. **Circuit recap (matches your description):**
  59. ```
  60. 3.3V ── [100k Ω resistor] ── D8 ── [NTC 100k Ω] ── GND
  61.  
  62. */
Advertisement
Add Comment
Please, Sign In to add comment