""" Program 7-5: Data logging of the reading from the LM34 temperature sensor. """ from machine import ADC, Pin, RTC, Timer LM34 = ADC(Pin(27)) # instantiate an ADC object using GP27 pin. rtc = RTC() # instantiate the RTC fh = open('log.txt', 'w') # open the log file for write def get_temp(): """Function to read the LM34 and convert to temperature""" value = LM34.read_u16() # read a conversion result value &= 0xFFF0 # mask out the lower bits mV = value * 3300 / 65520 # convert to millivolts return mV / 10 # LM34 has the output of 10mV per degree F def log_an_entry(_): """Timer callback function""" temp = get_temp() # take a temperature reading tm = rtc.datetime() # take a timestamp # logline = '{:2d}:{:02d},{:3.1f}\n'.format(tm[4], tm[5], temp) # v1.16 or earlier logline = f'{tm[4]:2d}:{tm[5]:02d},{temp:3.1f}\n' # v1.17 or later print(logline) # print an entry fh.write(logline) # write the entry to the log file # Start a periodic timer to log an entry every minute tmr = Timer(period = 60000, mode = Timer.PERIODIC, callback = log_an_entry) halt = input('Hit any key to stop logging.\n') tmr.deinit() # stop the timer fh.close() # close the log file print('Data logging terminated')