Sunday, 29 March 2026

Clock and temp using TM1637

import machine import dht import time import tm1637 # SETUP HARDWARE # Display connected to GP26 (CLK) and GP27 (DIO) display = tm1637.TM1637(clk=machine.Pin(26), dio=machine.Pin(27)) # Temperature sensor connected to GP18 sensor = dht.DHT11(machine.Pin(18)) # Set display brightness display.brightness(3) print("Clock started (using synced laptop time)...") # MAIN LOOP while True: # Get current time. # Returns a tuple: (year, month, mday, hour, minute, second, weekday, yearday) t = time.localtime() # Extract Hour (index 3) and Minute (index 4) hh = t[3] mm = t[4] # Show the time for 10 seconds display.numbers(hh, mm, True) # Colon ON time.sleep(10) # Measure and show temperature for 5 seconds try: sensor.measure() temp = sensor.temperature() display.temperature(temp) # Format example: "25 C" time.sleep(5) except: print("Sensor error") time.sleep(1)

No comments: