Advertisement
microrobotics

Temperature Sensor, Steel Head 10K NTC

Mar 31st, 2023
2,493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. The Steel Head 10K NTC thermistor is a temperature sensor whose resistance changes with temperature. Here's a simple Arduino code to read the sensor's output and display the temperature on the Serial Monitor using the Steinhart-Hart equation:
  3.  
  4. To wire the Steel Head 10K NTC thermistor, connect one leg of the thermistor to the 5V pin on the Arduino and the other leg to the NTC_PIN (analog pin A0) on the Arduino. Connect a 10KΩ resistor between the NTC_PIN (analog pin A0) and the ground.
  5.  
  6. This code will continuously read the sensor's output and print the raw readings, the calculated resistance, and the temperature in Celsius on the Serial Monitor. Make sure to adjust the R0, BETA, and SERIES_RESISTOR values according to your specific thermistor and the resistor used in the voltage divider circuit.
  7. */
  8.  
  9. const int NTC_PIN = A0; // NTC thermistor connected to analog pin A0
  10.  
  11. // Thermistor parameters
  12. const float T0 = 298.15; // Reference temperature in Kelvin (25°C)
  13. const float R0 = 10000.0; // Resistance at reference temperature (10K)
  14. const float BETA = 3950.0; // Beta constant for the thermistor (check datasheet)
  15.  
  16. const float VOLTAGE_REF = 5.0;
  17. const float SERIES_RESISTOR = 10000.0; // Series resistor value (10K)
  18.  
  19. void setup() {
  20.   Serial.begin(9600);
  21. }
  22.  
  23. void loop() {
  24.   int sensorValue = analogRead(NTC_PIN);
  25.  
  26.   // Calculate the voltage across the thermistor
  27.   float voltage = sensorValue * (VOLTAGE_REF / 1023.0);
  28.  
  29.   // Calculate the thermistor resistance
  30.   float resistance = SERIES_RESISTOR * (VOLTAGE_REF / voltage - 1.0);
  31.  
  32.   // Calculate the temperature using the Steinhart-Hart equation
  33.   float temperatureK = 1.0 / (1.0 / T0 + (1.0 / BETA) * log(resistance / R0));
  34.   float temperatureC = temperatureK - 273.15;
  35.  
  36.   Serial.print("Sensor Value: ");
  37.   Serial.println(sensorValue);
  38.   Serial.print("Resistance: ");
  39.   Serial.print(resistance);
  40.   Serial.println(" Ohms");
  41.   Serial.print("Temperature: ");
  42.   Serial.print(temperatureC);
  43.   Serial.println(" °C");
  44.  
  45.   delay(1000); // Wait 1 second between readings
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement