Difference between revisions of "4-Digital Display"

From
Jump to: navigation, search
(Code Exemple->DigitalTube->ClockDisplay)
(Code - TM1637 Display - Full Test)
 
(56 intermediate revisions by the same user not shown)
Line 18: Line 18:
 
==== Librairies ====
 
==== Librairies ====
  
[https://code.google.com/archive/p/arduino-timerone/downloads TimerOne library]<br>
+
[https://code.google.com/archive/p/arduino-timerone/downloads TimerOne library] <br>
  
[https://github.com/Seeed-Studio/Grove_4Digital_Display Grove_4Digital_Display Library]<br>
+
[https://github.com/Seeed-Studio/Grove_4Digital_Display Grove_4Digital_Display Library] <b>TM1637.h</b> Seeed-Studio/Grove_4Digital_Display<br>
  
[https://github.com/Seeed-Studio/Four_Digit_Display_Suli Four-Digit Display Suli Library]
+
[https://github.com/Seeed-Studio/Four_Digit_Display_Suli Four-Digit Display Suli Library] <b>Four_Digit_Display.h</b> Seeed-Studio/Four_Digit_Display_Suli
  
 
=== Voltage ===
 
=== Voltage ===
Line 29: Line 29:
 
=== Code Exemple->DigitalTube->ClockDisplay ===
 
=== Code Exemple->DigitalTube->ClockDisplay ===
  
 +
sur le Port <b>D2</b><br>
  
[[File:Pinout Grove Shield Arduino Nano.JPG| 100px]]<br>
+
[[File:Pinout Grove Shield Arduino Nano.JPG| 100px]]
[[File:Seeeduino-nano-connect.png | 150px]]<br>
+
[[File:Seeeduino-nano-connect.png | 200px]]
 
[[File:4-Digital Display recto.jpg | 150px]]<br>
 
[[File:4-Digital Display recto.jpg | 150px]]<br>
  
Line 110: Line 111:
  
 
</pre>
 
</pre>
il affiche 12:00 et ensuite 12:01 aisi de suite...
+
il affiche <font color =red>12:00</font> et ensuite <font color =red>12:01</font> ainsi de suite...
  
=== Code Wokwi ===
+
----
 +
 
 +
=== Code Wokwi 1 &#128732; ===
  
 
wokwi-esp32-devkit-v1, Anonymous<br>
 
wokwi-esp32-devkit-v1, Anonymous<br>
https://wokwi.com/projects/376726394500720641<br>
+
https://wokwi.com/projects/376726394500720641<br> (GMT+7 for Bangkok, Thailand)
 +
 
 +
#define CLK_PIN 2  // D2 sur XIAO-ESP32S3<br>
 +
#define DIO_PIN 3  // D3 sur XIAO-ESP32S3<br>
 +
 
 +
[[File:Grove Base for XIAO 1.PNG|200px]]
 +
[[File:XIAO-ESP32S3 1.PNG|125px]]
 +
[[File:4-Digital Display recto.jpg|125px]]
 +
 
 +
Grove Base for XIAO (Port <b>D1</b>) &#9989;<br>
 +
XIAO-ESP32S3 <br>
 +
4-Digital Display
  
 
<pre>
 
<pre>
#if defined(ESP8266)
 
  #include <ESP8266WiFi.h>
 
#elif defined(ESP32)
 
  #include <WiFi.h>
 
#endif
 
  
 +
#include <WiFi.h>
 
#include <TM1637Display.h>
 
#include <TM1637Display.h>
 
#include <NTPClient.h>
 
#include <NTPClient.h>
 
#include <WiFiUdp.h>
 
#include <WiFiUdp.h>
  
// Define the pins for TM1637
+
#define CLK_PIN 2 // GPIO2
#if defined(ESP8266)
+
#define DIO_PIN 3 // GPIO3
  #define CLK_PIN 0 // GPIO0
 
  #define DIO_PIN 2 // GPIO2
 
#elif defined(ESP32)
 
  #define CLK_PIN 4
 
  #define DIO_PIN 5
 
#endif
 
  
 
// Initialize the TM1637 display
 
// Initialize the TM1637 display
 
TM1637Display display(CLK_PIN, DIO_PIN);
 
TM1637Display display(CLK_PIN, DIO_PIN);
  
const char* ssid = "Wokwi-GUEST";
+
const char * ssid="Wokwi-GUEST";
const char* password = "";
+
const char * password="";
  
 
// Define NTP Server
 
// Define NTP Server
Line 147: Line 151:
 
// Initialize UDP client for NTP
 
// Initialize UDP client for NTP
 
WiFiUDP ntpUDP;
 
WiFiUDP ntpUDP;
// Define time zone (GMT+7 for Bangkok, Thailand)
+
// Define time zone (GMT+2 for Zurich Switzerland)
const long utcOffsetInSeconds = 7 * 3600;
+
const long utcOffsetInSeconds = 2 * 3600;
 
NTPClient timeClient(ntpUDP, ntpServerName, utcOffsetInSeconds);
 
NTPClient timeClient(ntpUDP, ntpServerName, utcOffsetInSeconds);
  
Line 193: Line 197:
 
   delay(1000); // Update the display every second
 
   delay(1000); // Update the display every second
 
    
 
    
 +
}
 +
</pre>
 +
<br>
 +
----
 +
 +
=== Code Wokwi 2 &#128732; ===
 +
code modifié ajout du <b>clignotement</b> des 2 points toutes les secondes
 +
 +
[[File:Grove Base for XIAO 1.PNG|200px]]
 +
[[File:XIAO-ESP32S3 1.PNG|125px]]
 +
[[File:4-Digital Display recto.jpg|125px]]
 +
 +
Grove Base for XIAO (Port <b>D1</b>) &#9989;<br>
 +
XIAO-ESP32S3 <br>
 +
4-Digital Display
 +
 +
<pre>
 +
#include <WiFi.h>
 +
#include <TM1637Display.h>
 +
#include <NTPClient.h>
 +
#include <WiFiUdp.h>
 +
 +
#define CLK_PIN 2 // GPIO2
 +
#define DIO_PIN 3 // GPIO3
 +
 +
// Initialize the TM1637 display
 +
TM1637Display display(CLK_PIN, DIO_PIN);
 +
 +
const char* ssid = "Wokwi-GUEST";
 +
const char* password = "";
 +
 +
// Define NTP Server
 +
const char* ntpServerName = "pool.ntp.org";
 +
// Initialize UDP client for NTP
 +
WiFiUDP ntpUDP;
 +
// Define time zone (GMT+2 for Zurich Switzerland)
 +
const long utcOffsetInSeconds = 2 * 3600;
 +
NTPClient timeClient(ntpUDP, ntpServerName, utcOffsetInSeconds);
 +
 +
void setup() {
 +
  Serial.begin(115200);
 +
 +
  // Initialize the TM1637 display
 +
  display.setBrightness(0x0a); // Set the brightness (0x00 to 0x0f)
 +
 +
  WiFi.begin(ssid, password);
 +
  while (WiFi.status() != WL_CONNECTED) {
 +
    delay(250);
 +
    Serial.print(".");
 +
  }
 +
 +
  Serial.println("");
 +
  Serial.println("WiFi connected");
 +
  Serial.print("IP address: ");
 +
  Serial.println(WiFi.localIP());
 +
 +
  // Initialize the time client
 +
  timeClient.begin();
 +
  timeClient.setTimeOffset(utcOffsetInSeconds);
 +
}
 +
 +
void loop() {
 +
  // Update the time from the NTP server
 +
  timeClient.update();
 +
 +
  // Get the current time
 +
  uint8_t hours = timeClient.getHours();
 +
  uint8_t minutes = timeClient.getMinutes();
 +
  uint8_t seconds = timeClient.getSeconds();
 +
 +
  // Adjust brightness based on time of day
 +
  if (hours >= 7 && hours < 18) {
 +
    display.setBrightness(0x0f); // High brightness
 +
  } else {
 +
    display.setBrightness(0x07); // Medium brightness
 +
  }
 +
 +
  // Blink the colon every second: ON if even second, OFF if odd
 +
  uint8_t colonState = (seconds % 2 == 0) ? 0b11100000 : 0b00000000;
 +
 +
  // Display time with blinking colon
 +
  display.showNumberDecEx(hours * 100 + minutes, colonState, true);
 +
 +
  // Print time to serial monitor
 +
  Serial.println((String)hours + ":" + minutes + ":" + seconds);
 +
 +
  delay(1000); // Update every second
 +
}
 +
</pre>
 +
 +
<br>
 +
----
 +
 +
=== Code Wokwi 3 &#128732; ===
 +
Code modifié ajout du <b>DaylightSaving</b> heure d’été / heure d’hiver
 +
 +
https://github.com/PaulStoffregen/Time <b>TimeLib.h</b>
 +
 +
[[File:Grove Base for XIAO 1.PNG|200px]]
 +
[[File:XIAO-ESP32S3 1.PNG|125px]]
 +
[[File:4-Digital Display recto.jpg|125px]]
 +
 +
Grove Base for XIAO (Port <b>D1</b>) &#9989;<br>
 +
XIAO-ESP32S3 <br>
 +
4-Digital Display
 +
 +
<pre>
 +
#include <WiFi.h>
 +
#include <TM1637Display.h>
 +
#include <NTPClient.h>
 +
#include <WiFiUdp.h>
 +
#include <TimeLib.h> // Nécessaire pour manipuler les dates
 +
 +
#define CLK_PIN 2 // GPIO2
 +
#define DIO_PIN 3 // GPIO3
 +
 +
TM1637Display display(CLK_PIN, DIO_PIN);
 +
 +
const char * ssid = "Mon-ssid";
 +
const char * password = "Mon-password";
 +
 +
const char* ntpServerName = "pool.ntp.org";
 +
WiFiUDP ntpUDP;
 +
NTPClient timeClient(ntpUDP, ntpServerName, 0); // Offset initial à 0
 +
 +
// Fonction pour déterminer si on est en heure d’été en Suisse
 +
bool isDaylightSaving(int day, int month, int weekday) {
 +
  // Dernier dimanche de mars → début heure d’été
 +
  if (month == 3 && (day - weekday) >= 25) return true;
 +
  if (month > 3 && month < 10) return true;
 +
  // Dernier dimanche d’octobre → fin heure d’été
 +
  if (month == 10 && (day - weekday) < 25) return true;
 +
  return false;
 +
}
 +
 +
void setup() {
 +
  Serial.begin(115200);
 +
  display.setBrightness(0x0a);
 +
 +
  WiFi.begin(ssid, password);
 +
  while (WiFi.status() != WL_CONNECTED) {
 +
    delay(250);
 +
    Serial.print(".");
 +
  }
 +
 +
  Serial.println("\nWiFi connected");
 +
  Serial.print("IP address: ");
 +
  Serial.println(WiFi.localIP());
 +
 +
  timeClient.begin();
 +
}
 +
 +
void loop() {
 +
  timeClient.update();
 +
 +
  // Récupère l’heure brute UTC
 +
  unsigned long rawTime = timeClient.getEpochTime();
 +
  setTime(rawTime); // Utilise TimeLib pour extraire jour/mois/semaine
 +
 +
  // Calcule l’offset selon heure d’été
 +
  int offset = isDaylightSaving(day(), month(), weekday()) ? 2 * 3600 : 1 * 3600;
 +
  timeClient.setTimeOffset(offset);
 +
 +
  // Recalcule l’heure locale
 +
  timeClient.update();
 +
  uint8_t hours = timeClient.getHours();
 +
  uint8_t minutes = timeClient.getMinutes();
 +
  uint8_t seconds = timeClient.getSeconds();
 +
 +
  // Luminosité selon l’heure
 +
  display.setBrightness((hours >= 7 && hours < 18) ? 0x0f : 0x07);
 +
 +
  // Clignotement des deux points
 +
  uint8_t colonState = (seconds % 2 == 0) ? 0b11100000 : 0b00000000;
 +
  display.showNumberDecEx(hours * 100 + minutes, colonState, true);
 +
 +
  Serial.println((String)hours + ":" + minutes + ":" + seconds);
 +
  delay(1000);
 +
}
 +
</pre>
 +
 +
<br>
 +
----
 +
 +
==== Code Simple Countdown Timer ====
 +
Compte à rebours (30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19)
 +
Bibliotheque : https://github.com/avishorp/TM1637<br>
 +
Source : https://arduinoyard.com/tm1637-display-with-arduino/<br>
 +
 +
Testé avec : <br>
 +
 +
[[File:Grove Base for XIAO 1.PNG|200px]]
 +
[[File:XIAO-ESP32S3 1.PNG|125px]]
 +
[[File:4-Digital Display recto.jpg|125px]]
 +
 +
Grove Base for XIAO (Port <b>D1</b>) &#9989;<br>
 +
XIAO-ESP32S3 <br>
 +
4-Digital Display
 +
 +
Le résultat : (30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19)
 +
 +
[[File:Grove Base for XIAO 1.PNG|200px]]
 +
[[File:XIAO RA4M1 4.PNG|125px]]
 +
[[File:4-Digital Display recto.jpg|125px]]
 +
 +
Grove Base for XIAO (Port <b>D2</b>) &#9989;<br>
 +
XIAO-RA4M1 <br>
 +
4-Digital Display
 +
 +
<pre>
 +
// TM1637 Display with Arduino - Countdown Timer
 +
#include <TM1637Display.h>
 +
 +
#define CLK 2
 +
#define DIO 3
 +
 +
TM1637Display display(CLK, DIO);
 +
 +
void setup() {
 +
  display.setBrightness(7);
 +
}
 +
 +
void loop() {
 +
  for (int i = 30; i >= 0; i--) {
 +
    display.showNumberDec(i, true);  // Display with leading zero (e.g., 09)
 +
    delay(1000);
 +
  }
 +
 
 +
  display.clear();
 +
  delay(2000);
 +
}
 +
</pre>
 +
 +
----
 +
 +
==== Code - TM1637 Display - Full Test ====
 +
 +
Le code est executé une seule fois, reset pour faire à nouveau<br>
 +
Source : https://arduinoyard.com/tm1637-display-with-arduino<br>
 +
 +
[[File:Grove Base for XIAO 1.PNG|200px]]
 +
[[File:XIAO-ESP32S3 1.PNG|125px]]
 +
[[File:4-Digital Display recto.jpg|125px]]
 +
 +
Grove Base for XIAO (Port <b>D1</b>) &#9989;<br>
 +
XIAO-ESP32S3 <br>
 +
4-Digital Display
 +
 +
<pre>
 +
// TM1637 Display with Arduino - Full Test
 +
#include <TM1637Display.h>
 +
 +
// Define TM1637 module connection pins (Digital Pins)
 +
#define CLK 2  // Clock Pin
 +
#define DIO 3  // Data Pin
 +
 +
// Delay time between different test cases
 +
#define TEST_DELAY 2000 
 +
 +
// Custom segment encoding for displaying "DONE"
 +
const uint8_t SEG_DONE[] = {
 +
  SEG_B | SEG_C | SEG_D | SEG_E | SEG_G,          // d
 +
  SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F,  // O
 +
  SEG_C | SEG_E | SEG_G,                          // n
 +
  SEG_A | SEG_D | SEG_E | SEG_F | SEG_G            // E
 +
};
 +
 +
// Create display object
 +
TM1637Display display(CLK, DIO);
 +
 +
void setup() {
 +
  // Set brightness level (0x0F = max brightness)
 +
  display.setBrightness(0x0F);
 +
}
 +
 +
void loop() {
 +
  int k;
 +
  uint8_t data[] = { 0xff, 0xff, 0xff, 0xff }; // All segments ON
 +
  uint8_t blank[] = { 0x00, 0x00, 0x00, 0x00 }; // All segments OFF
 +
 +
  // 1. Display all segments ON
 +
  display.setSegments(data);
 +
  delay(TEST_DELAY);
 +
 +
  // 2. Display numbers 0123
 +
  data[0] = display.encodeDigit(0);
 +
  data[1] = display.encodeDigit(1);
 +
  data[2] = display.encodeDigit(2);
 +
  data[3] = display.encodeDigit(3);
 +
  display.setSegments(data);
 +
  delay(TEST_DELAY);
 +
 +
  // 3. Display partial digits
 +
  display.clear();
 +
  display.setSegments(data + 2, 2, 2); // Display "23__"
 +
  delay(TEST_DELAY);
 +
 +
  display.clear();
 +
  display.setSegments(data + 2, 2, 1); // Display "_23_"
 +
  delay(TEST_DELAY);
 +
 +
  display.clear();
 +
  display.setSegments(data + 1, 3, 1); // Display "_123"
 +
  delay(TEST_DELAY);
 +
 +
  // 4. Show numbers with and without leading zeros
 +
  display.showNumberDec(0, false);  // Expect: ___0
 +
  delay(TEST_DELAY);
 +
  display.showNumberDec(0, true);  // Expect: 0000
 +
  delay(TEST_DELAY);
 +
  display.showNumberDec(301, false); // Expect: _301
 +
  delay(TEST_DELAY);
 +
  display.showNumberDec(301, true);  // Expect: 0301
 +
  delay(TEST_DELAY);
 +
 +
  // 5. Display negative numbers
 +
  display.showNumberDec(-1, false);  // Expect: __-1
 +
  delay(TEST_DELAY);
 +
  display.showNumberDec(-12);        // Expect: _-12
 +
  delay(TEST_DELAY);
 +
  display.showNumberDec(-999);      // Expect: -999
 +
  delay(TEST_DELAY);
 +
 +
  // 6. Show hexadecimal numbers
 +
  display.showNumberHexEx(0xf1af);        // Expect: f1Af
 +
  delay(TEST_DELAY);
 +
  display.showNumberHexEx(0x2c);          // Expect: __2C
 +
  delay(TEST_DELAY);
 +
  display.showNumberHexEx(0xd1, 0, true); // Expect: 00d1
 +
  delay(TEST_DELAY);
 +
 +
  // 7. Display all possible dot positions
 +
  for (k = 0; k <= 4; k++) {
 +
    display.showNumberDecEx(0, (0x80 >> k), true);
 +
    delay(TEST_DELAY);
 +
  }
 +
 +
  // 8. Brightness test (gradual increase)
 +
  for (k = 0; k < 4; k++) {
 +
    data[k] = 0xff; // All segments ON
 +
  }
 +
  for (k = 0; k < 7; k++) {
 +
    display.setBrightness(k);
 +
    display.setSegments(data);
 +
    delay(TEST_DELAY);
 +
  }
 +
 +
  // 9. On/Off Test
 +
  for (k = 0; k < 4; k++) {
 +
    display.setBrightness(7, false);  // Turn off
 +
    display.setSegments(data);
 +
    delay(TEST_DELAY);
 +
    display.setBrightness(7, true);  // Turn on
 +
    display.setSegments(data);
 +
    delay(TEST_DELAY);
 +
  }
 +
 +
  // 10. Display "DONE"
 +
  display.setSegments(SEG_DONE);
 +
 +
  // Halt execution
 +
  while (1);
 
}
 
}
 
</pre>
 
</pre>

Latest revision as of 18:00, 17 October 2025

Grove - 4-Digital Displays (Seeed-Studio)

Caractéristiques

Grove compatible interface (3.3V/5V)

connect to D2/D3

4 digit red alpha-numeric display
8 adjustable luminance levels

Support

4-Digital Display recto.jpg 4-Digital Display verso.jpg 4-digit display interface.jpg

Drivers

chip - TM1637

Librairies

TimerOne library

Grove_4Digital_Display Library TM1637.h Seeed-Studio/Grove_4Digital_Display

Four-Digit Display Suli Library Four_Digit_Display.h Seeed-Studio/Four_Digit_Display_Suli

Voltage

3.3V ~ 5.5V

Code Exemple->DigitalTube->ClockDisplay

sur le Port D2

Pinout Grove Shield Arduino Nano.JPG Seeeduino-nano-connect.png 4-Digital Display recto.jpg


Copier les deux librairies dans C:\Program Files (x86)\Arduino\libraries, exécuter Arduino IDE, chercher l'exemple, choisir Arduino UNO, et le port COM

	//  Author:Frankie.Chu
	//  Date:9 April,2012

	#include <TimerOne.h>
	#include "TM1637.h"
	#define ON 1
	#define OFF 0

	int8_t TimeDisp[] = {0x00,0x00,0x00,0x00};
	unsigned char ClockPoint = 1;
	unsigned char Update;
	unsigned char halfsecond = 0;
	unsigned char second;
	unsigned char minute = 0;
	unsigned char hour = 12;


	#define CLK 2//pins definitions for TM1637 and can be changed to other ports    
	#define DIO 3
	TM1637 tm1637(CLK,DIO);

	void setup()
	{
	  tm1637.set();
	  tm1637.init();
	  Timer1.initialize(500000);//timing for 500ms
	  Timer1.attachInterrupt(TimingISR);//declare the interrupt serve routine:TimingISR  
	}
	void loop()
	{
	  if(Update == ON)
	  {
		TimeUpdate();
		tm1637.display(TimeDisp);
	  }
	  
	}
	void TimingISR()
	{
	  halfsecond ++;
	  Update = ON;
	  if(halfsecond == 2){
		second ++;
		if(second == 60)
		{
		  minute ++;
		  if(minute == 60)
		  {
			hour ++;
			if(hour == 24)hour = 0;
			minute = 0;
		  }
		  second = 0;
		}
		halfsecond = 0;  
	  }
	 // Serial.println(second);
	  ClockPoint = (~ClockPoint) & 0x01;
	}
	void TimeUpdate(void)
	{
	  if(ClockPoint)tm1637.point(POINT_ON);
	  else tm1637.point(POINT_OFF); 
	  TimeDisp[0] = hour / 10;
	  TimeDisp[1] = hour % 10;
	  TimeDisp[2] = minute / 10;
	  TimeDisp[3] = minute % 10;
	  Update = OFF;
	}


il affiche 12:00 et ensuite 12:01 ainsi de suite...


Code Wokwi 1 🛜

wokwi-esp32-devkit-v1, Anonymous
https://wokwi.com/projects/376726394500720641
(GMT+7 for Bangkok, Thailand)

  1. define CLK_PIN 2 // D2 sur XIAO-ESP32S3
  2. define DIO_PIN 3 // D3 sur XIAO-ESP32S3

Grove Base for XIAO 1.PNG XIAO-ESP32S3 1.PNG 4-Digital Display recto.jpg

Grove Base for XIAO (Port D1) ✅
XIAO-ESP32S3
4-Digital Display


#include <WiFi.h>
#include <TM1637Display.h>
#include <NTPClient.h>
#include <WiFiUdp.h>

#define CLK_PIN 2 // GPIO2
#define DIO_PIN 3 // GPIO3

// Initialize the TM1637 display
TM1637Display display(CLK_PIN, DIO_PIN);

const char * ssid="Wokwi-GUEST";
const char * password="";

// Define NTP Server
const char* ntpServerName = "pool.ntp.org";
// Initialize UDP client for NTP
WiFiUDP ntpUDP;
// Define time zone (GMT+2 for Zurich Switzerland)
const long utcOffsetInSeconds = 2 * 3600;
NTPClient timeClient(ntpUDP, ntpServerName, utcOffsetInSeconds);

void setup() {
  Serial.begin(115200);

  // Initialize the TM1637 display
  display.setBrightness(0x0a); // Set the brightness (0x00 to 0x0f)

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(250);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
 
  // Initialize the time client
  timeClient.begin();
  timeClient.setTimeOffset(utcOffsetInSeconds);
}


void loop() {
  // Update the time from the NTP server
  timeClient.update();
  // Get the current time
  uint8_t hours = timeClient.getHours();
  uint8_t minutes = timeClient.getMinutes();
 
  if (hours >= 7 && hours < 18) {
    display.setBrightness(0x0f); // Set the brightness to high
  } else {
    display.setBrightness(0x07); // Set the brightness to medium
  }
  // Display the time on the TM1637 display
  display.showNumberDecEx(hours * 100 + minutes, 0b11100000, true); //Flash every 2 seconds
  
  uint8_t seconds = timeClient.getSeconds();
  Serial.println((String)hours + ":" + minutes + ":" + seconds);
  delay(1000); // Update the display every second
  
}



Code Wokwi 2 🛜

code modifié ajout du clignotement des 2 points toutes les secondes 

Grove Base for XIAO 1.PNG XIAO-ESP32S3 1.PNG 4-Digital Display recto.jpg

Grove Base for XIAO (Port D1) ✅
XIAO-ESP32S3
4-Digital Display

#include <WiFi.h>
#include <TM1637Display.h>
#include <NTPClient.h>
#include <WiFiUdp.h>

#define CLK_PIN 2 // GPIO2
#define DIO_PIN 3 // GPIO3

// Initialize the TM1637 display
TM1637Display display(CLK_PIN, DIO_PIN);

const char* ssid = "Wokwi-GUEST";
const char* password = "";

// Define NTP Server
const char* ntpServerName = "pool.ntp.org";
// Initialize UDP client for NTP
WiFiUDP ntpUDP;
// Define time zone (GMT+2 for Zurich Switzerland)
const long utcOffsetInSeconds = 2 * 3600;
NTPClient timeClient(ntpUDP, ntpServerName, utcOffsetInSeconds);

void setup() {
  Serial.begin(115200);

  // Initialize the TM1637 display
  display.setBrightness(0x0a); // Set the brightness (0x00 to 0x0f)

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(250);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  // Initialize the time client
  timeClient.begin();
  timeClient.setTimeOffset(utcOffsetInSeconds);
}

void loop() {
  // Update the time from the NTP server
  timeClient.update();

  // Get the current time
  uint8_t hours = timeClient.getHours();
  uint8_t minutes = timeClient.getMinutes();
  uint8_t seconds = timeClient.getSeconds();

  // Adjust brightness based on time of day
  if (hours >= 7 && hours < 18) {
    display.setBrightness(0x0f); // High brightness
  } else {
    display.setBrightness(0x07); // Medium brightness
  }

  // Blink the colon every second: ON if even second, OFF if odd
  uint8_t colonState = (seconds % 2 == 0) ? 0b11100000 : 0b00000000;

  // Display time with blinking colon
  display.showNumberDecEx(hours * 100 + minutes, colonState, true);

  // Print time to serial monitor
  Serial.println((String)hours + ":" + minutes + ":" + seconds);

  delay(1000); // Update every second
}



Code Wokwi 3 🛜

Code modifié ajout du DaylightSaving heure d’été / heure d’hiver

https://github.com/PaulStoffregen/Time TimeLib.h

Grove Base for XIAO 1.PNG XIAO-ESP32S3 1.PNG 4-Digital Display recto.jpg

Grove Base for XIAO (Port D1) ✅
XIAO-ESP32S3
4-Digital Display

#include <WiFi.h>
#include <TM1637Display.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <TimeLib.h> // Nécessaire pour manipuler les dates

#define CLK_PIN 2 // GPIO2
#define DIO_PIN 3 // GPIO3

TM1637Display display(CLK_PIN, DIO_PIN);

const char * ssid = "Mon-ssid";
const char * password = "Mon-password";

const char* ntpServerName = "pool.ntp.org";
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, ntpServerName, 0); // Offset initial à 0

// Fonction pour déterminer si on est en heure d’été en Suisse
bool isDaylightSaving(int day, int month, int weekday) {
  // Dernier dimanche de mars → début heure d’été
  if (month == 3 && (day - weekday) >= 25) return true;
  if (month > 3 && month < 10) return true;
  // Dernier dimanche d’octobre → fin heure d’été
  if (month == 10 && (day - weekday) < 25) return true;
  return false;
}

void setup() {
  Serial.begin(115200);
  display.setBrightness(0x0a);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(250);
    Serial.print(".");
  }

  Serial.println("\nWiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  timeClient.begin();
}

void loop() {
  timeClient.update();

  // Récupère l’heure brute UTC
  unsigned long rawTime = timeClient.getEpochTime();
  setTime(rawTime); // Utilise TimeLib pour extraire jour/mois/semaine

  // Calcule l’offset selon heure d’été
  int offset = isDaylightSaving(day(), month(), weekday()) ? 2 * 3600 : 1 * 3600;
  timeClient.setTimeOffset(offset);

  // Recalcule l’heure locale
  timeClient.update();
  uint8_t hours = timeClient.getHours();
  uint8_t minutes = timeClient.getMinutes();
  uint8_t seconds = timeClient.getSeconds();

  // Luminosité selon l’heure
  display.setBrightness((hours >= 7 && hours < 18) ? 0x0f : 0x07);

  // Clignotement des deux points
  uint8_t colonState = (seconds % 2 == 0) ? 0b11100000 : 0b00000000;
  display.showNumberDecEx(hours * 100 + minutes, colonState, true);

  Serial.println((String)hours + ":" + minutes + ":" + seconds);
  delay(1000);
}



Code Simple Countdown Timer

Compte à rebours (30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19)

Bibliotheque : https://github.com/avishorp/TM1637
Source : https://arduinoyard.com/tm1637-display-with-arduino/

Testé avec :

Grove Base for XIAO 1.PNG XIAO-ESP32S3 1.PNG 4-Digital Display recto.jpg

Grove Base for XIAO (Port D1) ✅
XIAO-ESP32S3
4-Digital Display

Le résultat : (30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19)

Grove Base for XIAO 1.PNG XIAO RA4M1 4.PNG 4-Digital Display recto.jpg

Grove Base for XIAO (Port D2) ✅
XIAO-RA4M1
4-Digital Display

// TM1637 Display with Arduino - Countdown Timer
#include <TM1637Display.h>

#define CLK 2
#define DIO 3

TM1637Display display(CLK, DIO);

void setup() {
  display.setBrightness(7);
}

void loop() {
  for (int i = 30; i >= 0; i--) {
    display.showNumberDec(i, true);  // Display with leading zero (e.g., 09)
    delay(1000);
  }
  
  display.clear();
  delay(2000);
}

Code - TM1637 Display - Full Test

Le code est executé une seule fois, reset pour faire à nouveau
Source : https://arduinoyard.com/tm1637-display-with-arduino

Grove Base for XIAO 1.PNG XIAO-ESP32S3 1.PNG 4-Digital Display recto.jpg

Grove Base for XIAO (Port D1) ✅
XIAO-ESP32S3
4-Digital Display

// TM1637 Display with Arduino - Full Test
#include <TM1637Display.h>

// Define TM1637 module connection pins (Digital Pins)
#define CLK 2  // Clock Pin
#define DIO 3  // Data Pin

// Delay time between different test cases
#define TEST_DELAY 2000  

// Custom segment encoding for displaying "DONE"
const uint8_t SEG_DONE[] = {
  SEG_B | SEG_C | SEG_D | SEG_E | SEG_G,           // d
  SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F,   // O
  SEG_C | SEG_E | SEG_G,                           // n
  SEG_A | SEG_D | SEG_E | SEG_F | SEG_G            // E
};

// Create display object
TM1637Display display(CLK, DIO);

void setup() {
  // Set brightness level (0x0F = max brightness)
  display.setBrightness(0x0F);
}

void loop() {
  int k;
  uint8_t data[] = { 0xff, 0xff, 0xff, 0xff }; // All segments ON
  uint8_t blank[] = { 0x00, 0x00, 0x00, 0x00 }; // All segments OFF

  // 1. Display all segments ON
  display.setSegments(data);
  delay(TEST_DELAY);

  // 2. Display numbers 0123
  data[0] = display.encodeDigit(0);
  data[1] = display.encodeDigit(1);
  data[2] = display.encodeDigit(2);
  data[3] = display.encodeDigit(3);
  display.setSegments(data);
  delay(TEST_DELAY);

  // 3. Display partial digits
  display.clear();
  display.setSegments(data + 2, 2, 2); // Display "23__"
  delay(TEST_DELAY);

  display.clear();
  display.setSegments(data + 2, 2, 1); // Display "_23_"
  delay(TEST_DELAY);

  display.clear();
  display.setSegments(data + 1, 3, 1); // Display "_123"
  delay(TEST_DELAY);

  // 4. Show numbers with and without leading zeros
  display.showNumberDec(0, false);  // Expect: ___0
  delay(TEST_DELAY);
  display.showNumberDec(0, true);   // Expect: 0000
  delay(TEST_DELAY);
  display.showNumberDec(301, false); // Expect: _301
  delay(TEST_DELAY);
  display.showNumberDec(301, true);  // Expect: 0301
  delay(TEST_DELAY);

  // 5. Display negative numbers
  display.showNumberDec(-1, false);  // Expect: __-1
  delay(TEST_DELAY);
  display.showNumberDec(-12);        // Expect: _-12
  delay(TEST_DELAY);
  display.showNumberDec(-999);       // Expect: -999
  delay(TEST_DELAY);

  // 6. Show hexadecimal numbers
  display.showNumberHexEx(0xf1af);        // Expect: f1Af
  delay(TEST_DELAY);
  display.showNumberHexEx(0x2c);          // Expect: __2C
  delay(TEST_DELAY);
  display.showNumberHexEx(0xd1, 0, true); // Expect: 00d1
  delay(TEST_DELAY);

  // 7. Display all possible dot positions
  for (k = 0; k <= 4; k++) {
    display.showNumberDecEx(0, (0x80 >> k), true);
    delay(TEST_DELAY);
  }

  // 8. Brightness test (gradual increase)
  for (k = 0; k < 4; k++) {
    data[k] = 0xff; // All segments ON
  }
  for (k = 0; k < 7; k++) {
    display.setBrightness(k);
    display.setSegments(data);
    delay(TEST_DELAY);
  }

  // 9. On/Off Test
  for (k = 0; k < 4; k++) {
    display.setBrightness(7, false);  // Turn off
    display.setSegments(data);
    delay(TEST_DELAY);
    display.setBrightness(7, true);   // Turn on
    display.setSegments(data);
    delay(TEST_DELAY);
  }

  // 10. Display "DONE"
  display.setSegments(SEG_DONE);

  // Halt execution
  while (1);
}

Liens internes

Main page


Liens externes

https://www.seeedstudio.com/Grove-4-Digit-Display.html shop

http://wiki.seeedstudio.com/Grove-4-Digit_Display wiki