.. 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 [|link_sf_facebook|] and join today!
.. _2.2.2_js:
2.2.2 Thermistor
================
.. note::
.. image:: ../img/mcp3008_and_adc0834.jpg
:width: 25%
:align: left
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.
.. image:: ../img/list_2.2.2_thermistor.png
It's definitely convenient to buy a whole kit, here's the link:
.. list-table::
:widths: 20 20 20
:header-rows: 1
* - Name
- ITEMS IN THIS KIT
- LINK
* - Raphael Kit
- 337
- |link_Raphael_kit|
You can also buy them separately from the links below.
.. list-table::
:widths: 30 20
:header-rows: 1
* - COMPONENT INTRODUCTION
- PURCHASE LINK
* - :ref:`cpn_gpio_board`
- |link_gpio_board_buy|
* - :ref:`cpn_breadboard`
- |link_breadboard_buy|
* - :ref:`cpn_wires`
- |link_wires_buy|
* - :ref:`cpn_resistor`
- |link_resistor_buy|
* - :ref:`cpn_thermistor`
- |link_thermistor_buy|
* - :ref:`cpn_adc0834`
- \-
Schematic Diagram
------------------
.. image:: ../img/image323.png
.. image:: ../img/image324.png
Experimental Procedures
-----------------------
**Step 1:** Build the circuit.
.. image:: ../img/image202.png
**Step 2:** Go to the folder of the code.
.. raw:: html
.. code-block::
cd ~/raphael-kit/nodejs/
**Step 3:** Run the code.
.. raw:: html
.. code-block::
sudo node thermistor.js
With the code run, the thermistor detects ambient temperature which will
be printed on the screen once it finishes the program calculation.
**Code**
.. code-block:: js
const Gpio = require('pigpio').Gpio;
const ADC0834 = require('./adc0834.js').ADC0834;
exports.ADC0834 = ADC0834;
const adc = new ADC0834(17, 18, 27);
setInterval(() => {
adc.read(0).then((value) => {
var Vr = 5 * value / 255;
var Rt = 10000 * Vr / (5 - Vr);
var temp = 1 / ((Math.log(Rt/10000) / 3950)+(1 / (273.15 + 25)));
var cel = (temp - 273.15).toFixed(2);
var Fah = (cel * 1.8 + 32).toFixed(2);
console.log(`Celsius: ${cel} C Fahrenheit: ${Fah} F\n`);
}, (error)=>{
console.log("Error: " + error);
});
}, 1000);
**Code Explanation**
.. code-block:: js
setInterval(() => {
adc.read(0).then((value) => {
var Vr = 5 * value / 255;
var Rt = 10000 * Vr / (5 - Vr);
var temp = 1 / ((Math.log(Rt/10000) / 3950)+(1 / (273.15 + 25)));
var cel = (temp - 273.15).toFixed(2);
var Fah = (cel * 1.8 + 32).toFixed(2);
console.log(`Celsius: ${cel} C Fahrenheit: ${Fah} F\n`);
}, (error)=>{
console.log("Error: " + error);
});
}, 1000);
We can read the ``value`` of the thermistor through the statement ``adc.read(0).then((value) => {...})``
.. code-block:: js
var Vr = 5 * value / 255;
var Rt = 10000 * Vr / (5 - Vr);
var temp = 1 / ((Math.log(Rt/10000) / 3950)+(1 / (273.15 + 25)));
var cel = (temp - 273.15).toFixed(2);
var Fah = (cel * 1.8 + 32).toFixed(2);
console.log(`Celsius: ${cel} C Fahrenheit: ${Fah} F\n`);
These operations convert the thermistor value to a Celsius temperature value.
.. code-block:: js
var Vr = 5 * value / 255;
var Rt = 10000 * Vr / (5 - Vr);
These two lines of code are used to calculate the voltage distribution from the read values, resulting in Rt (resistance of the thermistor).
.. code-block:: js
var temp = 1 / ((Math.log(Rt/10000) / 3950)+(1 / (273.15 + 25)));
This code refers to substituting Rt into the formula **TK=1/(ln(RT/RN)/B+1/TN)** to get the temperature in Kelvin.
.. code-block:: js
var cel = (temp - 273.15).toFixed(2);
This paragraph is to convert the Kelvin temperature to Celsius with two decimal places.
.. code-block:: js
var Fah = (cel * 1.8 + 32).toFixed(2);
This paragraph converts Celsius to Fahrenheit with two decimal places.
.. code-block:: js
console.log(`Celsius: ${cel} C Fahrenheit: ${Fah} F\n`);
Print Celsius, Fahrenheit and their units on the terminal.
Phenomenon Picture
------------------
.. image:: ../img/image203.jpeg