Skip to content

Instantly share code, notes, and snippets.

@lostsh
Last active June 27, 2022 20:54
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 lostsh/0cd40b5c9e9491cdacc320db81bb52cd to your computer and use it in GitHub Desktop.
Save lostsh/0cd40b5c9e9491cdacc320db81bb52cd to your computer and use it in GitHub Desktop.
NodeMCU RTC OLED (128x64 0.96inch I2C)
/**
* Wiring
*
* Component : Arduino
*
* OLED 128x64 - 0.96 I2C
* VCC => 3V3
* GND => GND
* SCL => D1
* SDA => D2
*
* DS3231
* GND => GND
* VCC => 3V3
* SDA => D2
* SCL => D1
*/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DS3231.h>
RTClib myRTC;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(9600);
Wire.begin();
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.display();
delay(2000);
display.clearDisplay();
display.display();
delay(2000);
}
void loop() {
display.clearDisplay();
DateTime now = myRTC.now();
simpleDrawClock(int(now.hour()), int(now.minute()));
// Draw digital time
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(66, 0);
char timeBuffer[6];
sprintf(timeBuffer, "%02d:%02d", now.hour(), now.minute());
display.println(timeBuffer);
// Draw digital date
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(64, display.height()-12);
char dateBuffer[11];
sprintf(dateBuffer, "%02d/%02d/%04d", now.day(), now.month(), now.year());
display.println(dateBuffer);
display.display();
delay(500);
}
void simpleDrawClock(int hour, int minute){
drawClock(32, 32, 31, hour, minute);
}
/**
* Draw The clock
* clockSize is the diameter of the clock
*/
void drawClock(int xCenter, int yCenter, int clockSize, int hour, int minute){
display.drawCircle(xCenter, yCenter, clockSize, SSD1306_WHITE);
float alpha = (PI/2)-((PI/30)*minute);
// Draw minute hand
int handMinuteGap = 4;
int x = int(cos(alpha)*(clockSize-handMinuteGap));
int y = int(sin(alpha)*(clockSize-handMinuteGap));
display.drawLine(xCenter, yCenter, xCenter+x, yCenter-y, SSD1306_WHITE);
// Draw hour hand
alpha = (PI/2)-((2*PI/24)*hour);
int handHourGap = 8;
x = int(cos(alpha)*(clockSize-handHourGap));
y = int(sin(alpha)*(clockSize-handHourGap));
display.drawLine(xCenter, yCenter, xCenter+x, yCenter-y, SSD1306_WHITE);
}
@lostsh
Copy link
Author

lostsh commented Jun 27, 2022

Ugly fun debug

IMG_4877.MOV

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment