Chapter 11 Thermistor
132
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
int
adcValue
;
float
tempK
,
tempC
;
float
voltage
,
Rt
;
if
(
wiringPiSetup
()
==
-
1
){
//when initialize wiring failed,print message to screen
printf
(
"setup wiringPi failed !"
);
return
1
;
}
pcf8591Setup
(
pinbase
,
address
);
while
(
1
){
adcValue
=
analogRead
(
A0
);
//read A0 pin
voltage
=
(
float
)
adcValue
/
255.0
*
3.3
;
// calculate voltage
Rt
=
10
*
voltage
/
(
3.3
-
voltage
);
//calculate resistance value of thermistor
tempK
=
1
/(
1
/(
273.15
+
25
)
+
log
(
Rt
/
10
)/
3950.0
);
//calculate temperature (Kelvin)
tempC
=
tempK
-
273.15
;
//calculate temperature (Celsius)
printf
(
"ADC value : %d ,\tVoltage : %.2fV,
\tTemperature : %.2fC\n"
,
adcValue
,
voltage
,
tempC
);
delay
(
100
);
}
return
0
;
}
In the code, read the ADC value of PCF8591 A0 port, and then calculate the voltage and the resistance of
thermistor according to Ohms law. Finally, calculate the current temperature. according to the front formula.
Python Code 11.1.1 Thermometer
First observe the project result, and then analyze the code.
1.
Use cd command to enter 11.1.1_Thermometer directory of Python code.
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/Python_Code/11.1.1_Thermometer
2.
Use python command to execute python code “Thermometer.py”.
python Thermometer.py
After the program is executed, the terminal window will print out the current ADC value, voltage v alue and
temperature value. Try to pinch the thermistor (do not touch pin) with hand lasting for a while, then the
temperature value will be increased.
The following is the code:
Summary of Contents for Ultimate Starter Kit
Page 1: ...Free your innovation Freenove is an open source electronics platform www freenove com ...
Page 117: ...117 Chapter 9 Potentiometer RGBLED www freenove com support freenove com Hardware connection ...
Page 155: ...155 Chapter 14 Relay Motor www freenove com support freenove com Hardware connection OFF 3 3V ...
Page 173: ...173 Chapter 16 Stepping Motor www freenove com support freenove com Hardware connection ...
Page 239: ...239 Chapter 22 Matrix Keypad www freenove com support freenove com Circuit Schematic diagram ...
Page 240: ...Chapter 22 Matrix Keypad 240 www freenove com support freenove com Hardware connection ...