Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <math.h>
- // Pin and resistor configuration
- #define THERMISTOR_PIN 1 // Analog pin between the two resistors
- #define SERIES_RESISTOR 100000 // 100k ohm fixed resistor
- #define NOMINAL_RESISTANCE 10000 // NTC resistance at nominal temp (100k ohm)
- #define NOMINAL_TEMPERATURE 25 // Nominal temperature in °C (usually 25°C)
- #define B_COEFFICIENT 3950 // Beta coefficient of the NTC (adjust if needed)
- #define ADC_MAX 4095 // 12-bit ADC (ESP32)
- #define VCC 3.3 // Supply voltage
- void setup() {
- Serial.begin(115200);
- analogReadResolution(12); // Set ADC to 12-bit resolution
- Serial.println("NTC Thermistor Temperature Monitor");
- Serial.println("-----------------------------------");
- }
- void loop() {
- // Read the ADC value
- int adcValue = analogRead(THERMISTOR_PIN);
- // Convert ADC to voltage
- float voltage = adcValue * (VCC / ADC_MAX);
- // Calculate NTC resistance
- // Circuit: 3.3V -- [100k resistor] -- D8 -- [NTC] -- GND
- float ntcResistance = SERIES_RESISTOR * (voltage / (VCC - voltage));
- // Apply Steinhart-Hart equation (Beta version)
- float steinhart;
- steinhart = ntcResistance / NOMINAL_RESISTANCE; // R/Ro
- steinhart = log(steinhart); // ln(R/Ro)
- steinhart /= B_COEFFICIENT; // 1/B * ln(R/Ro)
- steinhart += 1.0 / (NOMINAL_TEMPERATURE + 273.15); // + (1/To)
- steinhart = 1.0 / steinhart; // Invert
- float tempC = steinhart - 273.15; // Convert to Celsius
- float tempF = tempC * 9.0 / 5.0 + 32.0; // Convert to Fahrenheit
- // Print results
- Serial.print("ADC: ");
- Serial.print(adcValue);
- Serial.print(" | Voltage: ");
- Serial.print(voltage, 3);
- Serial.print("V | NTC Resistance: ");
- Serial.print(ntcResistance / 1000, 2);
- Serial.print("kΩ | Temp: ");
- Serial.print(tempC, 2);
- Serial.print("°C / ");
- Serial.print(tempF, 2);
- Serial.println("°F");
- delay(1000); // Read every second
- }
- /*
- **Circuit recap (matches your description):**
- ```
- 3.3V ── [100k Ω resistor] ── D8 ── [NTC 100k Ω] ── GND
- */
Advertisement
Add Comment
Please, Sign In to add comment