Skip to content

Instantly share code, notes, and snippets.

@sankarcheppali
Created February 25, 2024 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sankarcheppali/45f03b82abd66302ba3610872b4877f5 to your computer and use it in GitHub Desktop.
Save sankarcheppali/45f03b82abd66302ba3610872b4877f5 to your computer and use it in GitHub Desktop.
ESP32 based digital clock
#include <WiFi.h>
#include "time.h"
const char* ntpServer1 = "time.google.com";
const char* time_zone = "IST-5:30";
const uint8_t CLK_CYCLE_DELAY = 100;// us
const uint8_t CLK_PIN = 22;
const uint8_t DIO_PIN = 19;
const uint8_t NUM_DIGITS = 4;
const char* ssid = "****";
const char* password = "****";
// LED segment patterns.
const uint8_t NUM_PATTERNS = 10;
const uint8_t PATTERNS[NUM_PATTERNS] = {
0x3F, // 0
0x06, // 1
0x5B, // 2
0x4F, // 3
0x66, // 4
0x6D, // 5
0x7D, // 6
0x07, // 7
0x7F, // 8
0x6F, // 9
};
void setDataPin(uint8_t _bit){
digitalWrite(DIO_PIN,_bit);
}
void setClkPint(uint8_t _bit){
digitalWrite(CLK_PIN,_bit);
}
void sleep(int sleep){
delayMicroseconds(sleep);
}
void start() {
setDataPin(0);
sleep(CLK_CYCLE_DELAY);
setClkPint(0);
sleep(CLK_CYCLE_DELAY);
}
void stop() {
setDataPin(0);
sleep(CLK_CYCLE_DELAY);
setClkPint(1);
sleep(CLK_CYCLE_DELAY);
setDataPin(1);
}
void writeByte(uint8_t _byte){
for(uint8_t i=0;i<8;i++){
setDataPin(_byte >> i & 0x01) ;
sleep(CLK_CYCLE_DELAY);
setClkPint(1);
sleep(CLK_CYCLE_DELAY);
setClkPint(0);
sleep(CLK_CYCLE_DELAY);
}
setClkPint(0);
sleep(CLK_CYCLE_DELAY);
setClkPint(1);
sleep(CLK_CYCLE_DELAY);
setClkPint(0);
sleep(CLK_CYCLE_DELAY);
}
void writeDataCmd(uint8_t cmd){
start();
writeByte(cmd);
stop();
}
void sendByteStream(uint8_t *bytes,uint8_t len) {
writeDataCmd(0x40);
start();
for(uint8_t i=0;i<len;i++){
writeByte(bytes[i]);
}
stop();
}
void updateTime()
{
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("No time available (yet)");
return;
}
char printBuffer[10];
int strLen = sprintf(printBuffer, "%02d%02d",
timeinfo.tm_hour, timeinfo.tm_min);
uint8_t tokenBuffer[NUM_DIGITS+1] = {0};
tokenBuffer[0]= {0xC0}; // first byte will be the register address
uint8_t showColon = timeinfo.tm_sec % 2;
for(int i=0;i<strLen && i<NUM_DIGITS ;i++){
uint8_t digit = ((uint8_t)printBuffer[i])-48;
tokenBuffer[i+1] = PATTERNS[digit];
if(showColon == 1){
tokenBuffer[i+1] = 0x80 | tokenBuffer[i+1];
}
}
sendByteStream(tokenBuffer,NUM_DIGITS+1);
sprintf(printBuffer, "%02d:%02d:%02d",
timeinfo.tm_hour, timeinfo.tm_min,timeinfo.tm_sec);
Serial.println(printBuffer);
}
void setup() {
Serial.begin(115200);
configTzTime(time_zone, ntpServer1);
pinMode(CLK_PIN,OUTPUT);
pinMode(DIO_PIN,OUTPUT);
stop();
// switch on the display
uint8_t displayControlCmd[1]={0x8F};// display on with full brightness
sendByteStream(displayControlCmd,1);
//connect to WiFi
Serial.printf("Connecting to access point");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" CONNECTED");
}
void loop() {
delay(1000);
updateTime();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment