im port RPi. GPIO as GPIO
im port smbus
im port time
im port math
address = 0x48
bus=smbus.SMBus(1)
cmd=0x40
def analogRead(chn):
value = bus.read_byte_data(address,cmd+chn)
r eturn value
def analogWrite(value):
bus. write_byte_data(address,cmd,value)
def setup():
GPIO. setmode(GPIO.BOARD)
def loop():
w hile Tr ue:
value = analogRead(0) #read A0 pin
voltage = value / 255.0 * 3.3 #calculate voltage
Rt = 10 * voltage / ( 3.3 - voltage) #calculate resistance value of thermistor
tempK = 1/(1/(273.15 + 25) + math.log(Rt/10)/3950.0) #calculate temperature
(Kelvin)
tempC = tempK -273.15 #calculate temperature (Celsius)
print('ADC Value : %d, Voltage : %.2f, Temperature : %.2f'%(value,voltage,tempC))
time.sleep(0.01)
def destroy():
GPIO. cleanup()
if __name__ == '__main__':
p rint ('Program is starting ... ')
setup()
try:
loop()
e xcept KeyboardInterrupt:
destroy()
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.