Remote-controlled RGB mood light
Warm, cool, rainbow or breathing: cycle lighting moods from your sofa.
Shop by category
Shop all componentsDraw a traditional round clock face on the OLED, with hour, minute and second hands that sweep like a real watch. A clock hand is a line from the centre outwards at a particular angle, so the whole face is trigonometry. Twelve hours cover 360 degrees, giving 30 degrees per hour and 6 degrees per minute. sin() and cos() convert that angle into an x and y offset, but they expect radians, hence the multiplication by PI / 180. The hour hand also advances a little within the hour, that is the minute / 2 term, which is what stops it jumping.
The ESP32 is a 3.3V board: never connect a 5V signal straight to a GPIO pin.
Every part for this build comes in the Geekobot Titan Kit box, a Geekobot exclusive, together with the parts for all 120 projects, from a first blinking LED to a smart fan and a reaction game. The wiring below is the kit's own wiring.
Tick anything you already have and the total updates.
ESP32 DevKit V1 (38-pin): the brain of every Titan Kit project, with Wi-Fi and Bluetooth
Male-to-female and female-to-female leads for the modules
ESP32 expansion board from the kit: every GPIO has its own S, V, G header
Follow the colours: red is power, dark grey is ground, blue is a signal.
titan_kit_036_oled_analog_clock.inoVerify & upload in Electrolark#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
int centreX = 64;
int centreY = 32;
int radius = 30;
int hours = 10;
int minutes = 10;
int seconds = 0;
unsigned long lastTick = 0;
int setHour = 13;
int setMinute = 12;
void drawHand(float degrees, int length, int thickness)
{
float radians = (degrees - 90) * PI / 180.0;
int x = centreX + cos(radians) * length;
int y = centreY + sin(radians) * length;
display.drawLine(centreX, centreY, x, y, SSD1306_WHITE);
if (thickness > 1)
{
display.drawLine(centreX + 1, centreY, x, y, SSD1306_WHITE);
display.drawLine(centreX, centreY + 1, x, y, SSD1306_WHITE);
}
}
void drawFace()
{
display.drawCircle(centreX, centreY, radius + 2, SSD1306_WHITE);
for (int mark = 0; mark < 12; mark++)
{
float radians = (mark * 30 - 90) * PI / 180.0;
int xOuter = centreX + cos(radians) * radius;
int yOuter = centreY + sin(radians) * radius;
int xInner = centreX + cos(radians) * (radius - 4);
int yInner = centreY + sin(radians) * (radius - 4);
display.drawLine(xInner, yInner, xOuter, yOuter, SSD1306_WHITE);
}
}
void setup()
{
pinMode(setHour, INPUT_PULLUP);
pinMode(setMinute, INPUT_PULLUP);
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
{
Serial.println("OLED not found");
while (true) { }
}
display.clearDisplay();
display.display();
lastTick = millis();
}
void loop()
{
if (millis() - lastTick >= 1000)
{
lastTick = lastTick + 1000;
seconds = seconds + 1;
if (seconds >= 60) { seconds = 0; minutes = minutes + 1; }
if (minutes >= 60) { minutes = 0; hours = hours + 1; }
if (hours >= 12) { hours = 0; }
}
if (digitalRead(setHour) == LOW) { hours = (hours + 1) % 12; delay(250); }
if (digitalRead(setMinute) == LOW) { minutes = (minutes + 1) % 60; seconds = 0; delay(250); }
display.clearDisplay();
drawFace();
drawHand(hours * 30 + minutes / 2, radius - 14, 2); // Hour hand
drawHand(minutes * 6, radius - 7, 2); // Minute hand
drawHand(seconds * 6, radius - 4, 1); // Second hand
display.fillCircle(centreX, centreY, 2, SSD1306_WHITE);
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 56);
display.print(hours);
display.print(":");
if (minutes < 10) display.print("0");
display.print(minutes);
display.display();
delay(50);
}
Use a micro-USB data cable (some cables only charge), install the CP210x or CH340 driver if no port appears, pick Tools > Board > ESP32 Dev Module and hold the BOOT button on the board until the upload starts.
The sketch uses INPUT_PULLUP, so one leg goes to the GPIO and the other to GND. Use two legs on opposite sides of the button.
Check SDA on GPIO21 and SCL on GPIO22, power from 3V3, and that the sketch uses address 0x3C (some boards are 0x3D).
A clock hand is a line from the centre outwards at a particular angle, so the whole face is trigonometry. Twelve hours cover 360 degrees, giving 30 degrees per hour and 6 degrees per minute. sin() and cos() convert that angle into an x and y offset, but they expect radians, hence the multiplication by PI / 180. The hour hand also advances a little within the hour, that is the minute / 2 term, which is what stops it jumping.
A circular clock face appears with twelve tick marks. The second hand steps round once per second. Hour and minute hands sit at the correct angles.
Yes. It is project 36 of 120 in the Geekobot Titan Kit, in level 8 (OLED graphics). Every part on the list comes in the kit box, and you can also buy the parts one by one from the list above.
No. Every Titan Kit project is wired on the ESP32 expansion board and the breadboard with jumper wires.
7 parts: ESP32-WROOM-32 38-Pin Development Board, USB Cable, Solderless Breadboard, Dupont Jumper Wires (40 pcs), 2 × Tactile Push Button Switch, 0.96" OLED Display 128x64 and ESP32 38-Pin Expansion Board. On this page you can tick the parts you already have and add only the rest to your cart.
It is a intermediate project and takes about 30 minutes. The build on this page has 5 steps.
No. This page has the complete, tested code (titan_kit_036_oled_analog_clock.ino).
About ₹831 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
I2C graphics on an OLED display; Trigonometry for clock hands with sin() and cos(); Converting degrees to radians; Redrawing a whole frame each tick with clearDisplay().
The ESP32 is a 3.3V board: never connect a 5V signal straight to a GPIO pin.
Warm, cool, rainbow or breathing: cycle lighting moods from your sofa.
Press to roll: the pips tumble and slow down before landing on a number.
Wait for the light, hit the button: how many milliseconds can you beat?
A bright 4-digit clock that keeps time through power cuts.
Lessons are free to read. Maker (₹499/month) and Pro add quizzes, worked problems and certificates, paid with your Geekobot account. See plans
Learning project. Check every circuit and the datasheet of each part before you power it up.