from machine import Pin, I2C
import utime
import tm1637
import ds3231
#Setup for TM1637 4-digit display
clk Pin(20) # Clock pin (GPIO 20)
dio Pin(21) # Data pin (GPIO 21)
display tm1637.TM1637(clk-clk, dio-dio)
#Setup for DS3231 RTC (I2C)
#Use I2C on GPIO (SDA) and GPIO 1 (SCL) for I2C bus 0
12c I2C(0, scl=Pin(1), sda Pin(0)) # Correct I2C bus and pins for the Pico rtc ds3231.DS3231(12c)
# Function to display time on terminal and 7-segment display
def display_time():
while True:
#Get the current datetime from the DS3231
current_timertc.datetime()
#Extract the hour, minute, and second
hour = current_time[4]
#Hour is at index 4
minute = current_time [5] # Minute is at index 5 second = current_time[6] # Second is at index 6
#Print the time to the terminal in HH:MM:SS format
print(f"Current Time: {hour:02):(minute:02): {second:02}")
# Display the hour and minute on the TM1637 display display.numbers (hour, minute) #Wait for 1 second before updating the display utime.sleep(1)
try:
display.brightness (7) # Set brightness level (0-7)
display_time() # Start displaying time on both terminal and TM1637 except KeyboardInterrupt: display.clear() # Clear the display when exiting. print("Program stopped.")
#include <Wire.h>
#include <RTClib.h> // DS1307 RTC library from RTClib
// Define TM1637 pins for Wokwi simulator
#define CLK_PIN 20
#define DIO_PIN 21
// Create an RTC object
RTC_DS1307 rtc;
void setup() {
// Initialize Serial Monitor for debugging
Serial.begin(9600);
// Initialize the DS1307 RTC
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
// Check if the RTC is connected and has the correct time
if (rtc.lostPower()) {
Serial.println("RTC lost power, setting time...");
// Uncomment the following line if you want to set the time
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Use compile time to set the RTC time
}
// Initialize the TM1637 display pins (using Wokwi's built-in functions)
pinMode(CLK_PIN, OUTPUT);
pinMode(DIO_PIN, OUTPUT);
}
void loop() {
// Get current time from RTC
DateTime now = rtc.now();
// Extract hour, minute, and second
int hour = now.hour();
int minute = now.minute();
int second = now.second();
// Print the current time to the Serial Monitor
Serial.print("Current Time: ");
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.println(second);
// Display time on TM1637 (hour and minute)
// Wokwi supports direct TM1637 display manipulation, so we use the following function
displayTimeOnTM1637(hour, minute);
// Wait for 1 second before updating the display
delay(1000);
}
// Function to display time on TM1637
void displayTimeOnTM1637(int hour, int minute) {
// Display hour and minute on TM1637 in HH:MM format
int displayValue = (hour * 100) + minute;
// Split the display value into individual digits and display them on the TM1637
for (int i = 0; i < 4; i++) {
int digit = displayValue % 10;
displayValue /= 10;
displayDigitOnTM1637(3 - i, digit); // Display on the correct position (starting from 0 to 3)
}
}
// Function to simulate the display of a single digit on TM1637
void displayDigitOnTM1637(int position, int digit) {
// Here you can add your own code to interface with the display,
// or use Wokwi's built-in function to control the display (adjust for Wokwi environment).
// This is just a placeholder function for simulating the display action.
}