Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
2.2.2 Thermistor(MCP3008)
Note
Depending on your kit version, please identify whether you have ADC0834 or MCP3008 and proceed with the matching section.
Introduction
Just like photoresistor can sense light, thermistor is a temperature sensitive electronic device that can be used for realizing functions of temperature control, such as making a heat alarm.
Required Components
In this project, we need the following components.
Schematic Diagram
T-Board Name |
physical |
WiringPi |
BCM |
|---|---|---|---|
SPICE0 |
pin24 |
10 |
8 |
SPIMOSI |
pin19 |
12 |
10 |
SPIMISO |
pin21 |
13 |
9 |
SPISCLK |
pin23 |
14 |
11 |
Experimental Procedures
Step 1: Build the circuit.
Step 2: Go to the folder of the code.
cd ~/davinci-kit-for-raspberry-pi/nodejs/
Step 3: Run the code.
sudo node thermistor-2.js
With the code run, the thermistor detects ambient temperature which will be printed on the screen once it finishes the program calculation.
Code
const mcpadc = require('mcp-spi-adc');
// Open MCP3008 channel 0 (CH0), analog input from thermistor voltage divider
const adc = mcpadc.openMcp3008(0, { speedHz: 1350000 }, (err) => {
if (err) {
console.error('Failed to open MCP3008 channel:', err);
process.exit(1);
}
console.log('MCP3008 thermistor channel opened.');
setInterval(() => {
adc.read((err, reading) => {
if (err) {
console.error('ADC read error:', err);
return;
}
const adcValue = reading.value; // Float: 0.0–1.0
const raw = Math.round(adcValue * 1023); // 10-bit integer value
const Vr = 3.3 * raw / 1023; // Convert to voltage (assuming 3.3V Vref)
const R0 = 10000; // Fixed resistor: 10k
const B = 3950; // B constant
const Rt = R0 * Vr / (3.3 - Vr); // Thermistor resistance
const tempK = 1 / ((Math.log(Rt / R0) / B) + (1 / (273.15 + 25))); // Kelvin
const tempC = tempK - 273.15; // Celsius
const tempF = tempC * 1.8 + 32; // Fahrenheit
console.log(`Celsius: ${tempC.toFixed(2)} °C | Fahrenheit: ${tempF.toFixed(2)} °F`);
});
}, 1000);
});
Code Explanation
setInterval(() => {
adc.read((err, reading) => {
...
});
}, 1000);
Sets up a loop to read from MCP3008 channel 0 every 1000 milliseconds (1 second). The read function returns an analog value between 0.0 and 1.0.
const raw = Math.round(reading.value * 1023);
Converts the normalized float ADC value into a raw 10-bit integer (range 0–1023).
const Vr = 3.3 * raw / 1023;
Calculates the voltage at the thermistor (Vr) using the ADC reading. Assumes MCP3008 reference voltage is 3.3V.
const Rt = R0 * Vr / (3.3 - Vr);
Uses the voltage divider formula to calculate the thermistor resistance Rt, where R0 is a fixed resistor (10kΩ) in series.
const tempK = 1 / ((Math.log(Rt / R0) / B) + (1 / (273.15 + 25)));
This applies the B-parameter equation (a simplified form of the Steinhart-Hart equation) to estimate the temperature in Kelvin.
const tempC = tempK - 273.15;
const tempF = tempC * 1.8 + 32;
These convert the Kelvin temperature to Celsius and then Fahrenheit.
console.log(`Celsius: ${tempC.toFixed(2)} °C | Fahrenheit: ${tempF.toFixed(2)} °F`);
Prints both the Celsius and Fahrenheit temperature values with two decimal points of precision to the console.