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 value 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: