filmov
tv
Pt-25: How To Program the Temperature Sensor Module from the 37-in-1 Sensor Kit #arduino #tempsensor

Показать описание
How to program the Temperature Sensor Module from the KEYESTUDIO 37-in-1 Sensor Kit using the Arduino UNO, and Arduino IDE. #arduino #arduinouno #37in1sensorskit #keyestudio #temperaturesensor #thermistor #ntcthermistor #ntc
Here's the Arduino code:
FYI, for future reference, YT doesn't like angle brackets (aka 'less than' or 'greater than' symbols). If you see code with 'lt/lte' and 'gt/gte' below then substitute with the 'less than/less than equal to' and 'greater than/ greater than equal to' symbols.
***************************************
// Inverse relationship Analog reading and external temp.
// Lower numbers hotter, higher numbers colder.
// Subtract analog reading from 1023 to get the NTC analog
// that increases with hotter temperatures.
int temperatureSensorAnalogPin = A0;
int temperatureSensorDigitalPin = 8;
void setup() {
pinMode(temperatureSensorAnalogPin, INPUT);
pinMode(temperatureSensorDigitalPin, INPUT);
}
void loop() {
int tempAnalogReading = analogRead(temperatureSensorAnalogPin);
int tempDigitalReading = digitalRead(temperatureSensorDigitalPin);
int NTCtempAnalogReading = (1023-tempAnalogReading);
delay(100);
}
***************************************
#include "math.h"
#define SENSOR_PIN A0
#define RESISTANCE_FIXED 10000.0 // 10k ohms (The resistor paired with the NTC)
#define NOMINAL_RESISTANCE 9270.0 // Resistance at nominal temperature (usually 10k ohms)
#define NOMINAL_TEMPERATURE 23.0 // Temperature (in Celsius) for nominal resistance (usually 25 C)
#define B_COEFFICIENT 3950.0 // Beta coefficient (check your thermistor's datasheet!)
#define NUM_SAMPLES 10 // Read the sensor 10 times for a smoother reading
#define DELAY_BETWEEN_SAMPLES 10 // Wait 10 milliseconds between samples
void setup() {
delay(1000);
}
void loop() {
float averageReading = 0;
for (int i = 0; i 'lt' NUM_SAMPLES; i++) {
averageReading += analogRead(SENSOR_PIN);
delay(DELAY_BETWEEN_SAMPLES);
}
averageReading /= NUM_SAMPLES;
float thermistorResistance;
if (averageReading 'gt'= 1023.0) {
thermistorResistance = 0.1;
} else {
thermistorResistance = RESISTANCE_FIXED * (averageReading / (1023.0 - averageReading));
}
float steinhart;
if (thermistorResistance 'lt'= 0) {
steinhart = 0;
} else {
steinhart = thermistorResistance / 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; // Temperature in Kelvin
}
float tempC = steinhart - 273.15; // Temperature in Celsius
float tempF = (tempC * 9.0 / 5.0) + 32.0; // Temperature in Fahrenheit
if (steinhart == 0) { // Check if we had a calculation error earlier
} else {
}
delay(1000);
}
Here's the Arduino code:
FYI, for future reference, YT doesn't like angle brackets (aka 'less than' or 'greater than' symbols). If you see code with 'lt/lte' and 'gt/gte' below then substitute with the 'less than/less than equal to' and 'greater than/ greater than equal to' symbols.
***************************************
// Inverse relationship Analog reading and external temp.
// Lower numbers hotter, higher numbers colder.
// Subtract analog reading from 1023 to get the NTC analog
// that increases with hotter temperatures.
int temperatureSensorAnalogPin = A0;
int temperatureSensorDigitalPin = 8;
void setup() {
pinMode(temperatureSensorAnalogPin, INPUT);
pinMode(temperatureSensorDigitalPin, INPUT);
}
void loop() {
int tempAnalogReading = analogRead(temperatureSensorAnalogPin);
int tempDigitalReading = digitalRead(temperatureSensorDigitalPin);
int NTCtempAnalogReading = (1023-tempAnalogReading);
delay(100);
}
***************************************
#include "math.h"
#define SENSOR_PIN A0
#define RESISTANCE_FIXED 10000.0 // 10k ohms (The resistor paired with the NTC)
#define NOMINAL_RESISTANCE 9270.0 // Resistance at nominal temperature (usually 10k ohms)
#define NOMINAL_TEMPERATURE 23.0 // Temperature (in Celsius) for nominal resistance (usually 25 C)
#define B_COEFFICIENT 3950.0 // Beta coefficient (check your thermistor's datasheet!)
#define NUM_SAMPLES 10 // Read the sensor 10 times for a smoother reading
#define DELAY_BETWEEN_SAMPLES 10 // Wait 10 milliseconds between samples
void setup() {
delay(1000);
}
void loop() {
float averageReading = 0;
for (int i = 0; i 'lt' NUM_SAMPLES; i++) {
averageReading += analogRead(SENSOR_PIN);
delay(DELAY_BETWEEN_SAMPLES);
}
averageReading /= NUM_SAMPLES;
float thermistorResistance;
if (averageReading 'gt'= 1023.0) {
thermistorResistance = 0.1;
} else {
thermistorResistance = RESISTANCE_FIXED * (averageReading / (1023.0 - averageReading));
}
float steinhart;
if (thermistorResistance 'lt'= 0) {
steinhart = 0;
} else {
steinhart = thermistorResistance / 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; // Temperature in Kelvin
}
float tempC = steinhart - 273.15; // Temperature in Celsius
float tempF = (tempC * 9.0 / 5.0) + 32.0; // Temperature in Fahrenheit
if (steinhart == 0) { // Check if we had a calculation error earlier
} else {
}
delay(1000);
}