Class 3 · Time and schedule¶
The strip can do a sunrise, but only when you press reset. Today the lamp learns what time it is. A DS3231 clock chip keeps the time, with a coin battery so it doesn't forget when the power goes out. Then you write the schedule: sunrise in the morning, night light in the evening, off at bedtime.
By the end of class
The lamp reads the real time from the DS3231 and switches between sunrise, on, night light and off by itself, at the times written in your code.
New parts today¶
| Part | What it's for |
|---|---|
| DS3231 RTC module | Keeps the time |
| CR2032 battery | Keeps the DS3231 running with the power off (Dr. Alvear checks the board first) |
Build it¶
The DS3231 talks to the ESP32 over I²C, a two-wire bus. Power off first.
| From (DS3231) | To (ESP32) |
|---|---|
| VCC | 3V3 |
| GND | GND |
| SDA | GPIO 21 |
| SCL | GPIO 22 |
GPIO 21 and 22 are the ESP32's default I²C pins, so the library finds them without being told. Leave the strip wiring from Class 2 as it is.
Tool: I²C
I²C uses two wires for many devices: SDA carries the data, SCL carries a clock that keeps both sides in step. Every device on the bus has an address, a number it answers to. The ESP32 calls an address, and only that device replies.
A scanner calls every address and prints the ones that answer. It's the first thing to run when you connect an I²C part.
#include <Arduino.h>
#include <Wire.h>
void setup() {
Serial.begin(115200);
Wire.begin(); // SDA 21, SCL 22 on the ESP32
delay(500);
for (int address = 1; address < 127; address++) {
Wire.beginTransmission(address);
if (Wire.endTransmission() == 0) { // 0 means "someone answered"
Serial.print("Found a device at 0x");
Serial.println(address, HEX);
}
}
Serial.println("Scan done.");
}
void loop() {}
Run it with the DS3231 connected. The DS3231 answers at 0x68. Many DS3231 boards also carry a memory chip, which shows up as a second address (often 0x57). If nothing shows up, check SDA and SCL aren't swapped.
Tool: RTClib
Add the library to platformio.ini:
Example, not your project: a clock that prints the time every second and counts down to 15:00.
#include <Arduino.h>
#include <RTClib.h>
RTC_DS3231 rtc;
void setup() {
Serial.begin(115200);
if (!rtc.begin()) {
Serial.println("No DS3231 found. Check the wiring.");
while (true) {}
}
// Run this line ONCE to set the clock to the time you compiled,
// then comment it out and upload again.
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
void loop() {
DateTime now = rtc.now();
Serial.print(now.hour());
Serial.print(":");
Serial.print(now.minute());
Serial.print(":");
Serial.println(now.second());
int minutesNow = now.hour() * 60 + now.minute();
int minutesTo3pm = 15 * 60 - minutesNow;
if (minutesTo3pm > 0) {
Serial.print("Minutes until 15:00: ");
Serial.println(minutesTo3pm);
}
delay(1000);
}
Why comment out rtc.adjust after the first upload? If you leave it in, every restart sets the clock back to the moment you compiled.
rtc.lostPower() returns true if the DS3231 lost its battery power and doesn't know the time any more.
Mission 1 · The lamp knows the time
Set the DS3231 and print the time to the Serial Monitor every second. Then unplug the ESP32 for a minute and plug it back in.
Done when
After the unplug, the Serial Monitor shows the right time (within a few seconds of your phone) without re-uploading.
Tool: minutes since midnight
Comparing times with hours and minutes separately gets messy: is 6:45 after 6:30? Turn every time into one number, minutes since midnight: hour * 60 + minute. 06:30 is 390, 21:00 is 1260. Now "is it later than" is just >.
Example, not your project: which school period is it?
const int FIRST_BELL = 8 * 60; // 08:00
const int LUNCH = 11 * 60 + 30; // 11:30
const int LAST_BELL = 15 * 60; // 15:00
int minutesNow = now.hour() * 60 + now.minute();
if (minutesNow < FIRST_BELL) {
Serial.println("Before school");
} else if (minutesNow < LUNCH) {
Serial.println("Morning classes");
} else if (minutesNow < LAST_BELL) {
Serial.println("Afternoon classes");
} else {
Serial.println("School's out");
}
The order of the ifs matters: each one only runs when the ones above it were false, so each test only needs one end of the range.
Mission 2 · The schedule decides
Write your schedule with constants at the top of your code. These are the starting numbers; change them to what you want:
| Setting | Starting value |
|---|---|
| Sunrise starts | 06:30 |
| Sunrise length | 20 minutes |
| Light stays on until | 07:30 |
| Night light on | 21:00 |
| Off | 22:00 |
Write a function that looks at the time and decides which mode the lamp is in: sunrise, on, night light or off. Print the mode once a second, next to the time.
Done when
You change the constants to times a minute or two from now, and the printed mode switches at exactly those times.
Mission 3 · The lights follow the schedule
Connect the modes to the strip: off is dark, night light is a dim warm colour, on is your warm white, and sunrise is your Class 2 fade. In the sunrise, the progress now comes from the clock, not from millis(): how many seconds since the sunrise started, out of the sunrise length in seconds.
Done when
With the times set a few minutes ahead and a 2-minute sunrise, the strip goes through every mode on its own, at the right times, while you just watch. Then put the real times back.
If you finish early¶
- Make the night light fade out over the last 10 minutes instead of switching off.
- Make "off" at 00:30 work. (Hint: what happens to your comparisons when a time range crosses midnight?)
- Print a warning in
setup()ifrtc.lostPower()is true. - Different sunrise times on weekends:
now.dayOfTheWeek()returns 0 for Sunday through 6 for Saturday.
Sign-off with Dr. Alvear
- I²C scanner finds the DS3231
- Time survives an unplug
- Strip runs through all four modes on a test schedule
- Build log updated (photo + two sentences)