Build it step by step
0 of 6 doneSet the clock
Upload the RTClib ds3231 example once to set the time, then upload the logger.
Set the boost converter
Power the MT3608 from the cell and turn its trimmer until the output is 5.0 V on a multimeter.
Build the backup path
Adapter to the TP4056, cell to the holder, TP4056 output to the MT3608, MT3608 to the Nano.
Add the sense divider
Two 10 kΩ resistors from the adapter's 5 V to GND with the midpoint on A0.
Format the SD card
Use FAT32. Insert it in the module.
Test with the adapter
Unplug the adapter for 20 seconds, plug it back and read cuts.csv on a computer.
How do you build a power cut logger?
A battery-backed Arduino notices when mains power goes and comes back, and logs the time and length of each power cut to an SD card. You need Arduino Nano, DS3231 RTC module, MicroSD Card Module, 5 V 2 A USB adapter, TP4056 module with protection (USB-C), 18650 cell 2600 mAh (branded), Battery holder 18650 single, MT3608 Boost Module, Resistor 10 kΩ × 2, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 6 steps and about 90 minutes.
- Level
- Beginner
- Time
- About 90 min
- Parts
- 11
Wiring
| From | To | Why |
|---|---|---|
| 5 V USB adapter | TP4056 IN+ and IN- | Charges the cell (use a phone adapter, mains stays inside it) |
| 18650 cell holder | TP4056 B+ and B- | Backup cell |
| TP4056 OUT+ and OUT- | MT3608 IN+ and IN- | Cell feeds the boost converter |
| MT3608 OUT+ (set to 5.0 V first) | Nano 5V | Backup supply |
| MT3608 OUT- | Nano GND | Common ground |
| Adapter 5 V | 10 kΩ then A0, with 10 kΩ from A0 to GND | Divider, 2.5 V at A0 when power is on |
| RTC SDA and SCL | Nano A4 and A5 | Clock |
| RTC VCC and GND | Nano 5V and GND | Supply |
| SD module MOSI, MISO, SCK, CS | Nano D11, D12, D13, D10 | SPI |
| SD module VCC and GND | Nano 5V and GND | Supply (use a 5 V module with level shifting) |
Code
Arduino IDE, board "Arduino Nano" (processor ATmega328P, or ATmega328P (Old Bootloader) for clones). Install "RTClib" by Adafruit. SD and EEPROM come with the IDE. Times are Unix seconds. Set the RTC with the RTClib example sketch "ds3231" first. Set the MT3608 to exactly 5.0 V with a multimeter before connecting the Nano, because its default output can be higher.
Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
#include <Wire.h>
#include <RTClib.h>
#include <SD.h>
#include <EEPROM.h>
RTC_DS3231 rtc;
const byte SENSE = A0, CS = 10;
const int PRESENT = 400; // A0 around 512 when power is on, near 0 when it fails
bool outage = false;
void logLine(uint32_t start, uint32_t end) {
File f = SD.open("cuts.csv", FILE_WRITE);
if (f) {
f.print(start); f.print(','); f.print(end); f.print(','); f.println(end - start);
f.close();
}
}
void setup() {
Serial.begin(9600);
rtc.begin();
SD.begin(CS);
uint32_t saved; EEPROM.get(1, saved);
if (EEPROM.read(0) == 1) { // the Nano restarted during a cut and is now powered again
outage = true;
}
}
void loop() {
bool present = analogRead(SENSE) > PRESENT;
uint32_t now = rtc.now().unixtime();
if (!present && !outage) {
outage = true;
EEPROM.update(0, 1);
EEPROM.put(1, now); // keep the start in case the backup runs out
Serial.println("Power cut");
} else if (present && outage) {
outage = false;
uint32_t start; EEPROM.get(1, start);
logLine(start, now);
EEPROM.update(0, 0);
Serial.print("Power back after "); Serial.print(now - start); Serial.println(" s");
}
delay(200);
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Adapter plugged in | A0 reads near 512 on the serial monitor |
| Adapter unplugged | A0 falls to near 0 and the serial monitor prints Power cut |
| Plug back after 20 s | A line appears in cuts.csv with a duration near 20 |
| Cut for a minute | Duration near 60 and the Nano never restarts |
If it does not work
Nano restarts when the adapter is pulled
The boost converter or cell cannot supply it fast enough. Check the cell charge and the TP4056 wiring.
No file on the SD card
Check CS is on D10, the card is FAT32 and the module is 5 V compatible.
Durations are wrong
The RTC time is not set or its battery is flat.
The TP4056 gets hot
It is charging a cell while powering a load. Keep the cell near 1 A charge and the Nano load small.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the power cut logger?
Arduino Nano, DS3231 RTC module, MicroSD Card Module, 5 V 2 A USB adapter, TP4056 module with protection (USB-C), 18650 cell 2600 mAh (branded), Battery holder 18650 single, MT3608 Boost Module, Resistor 10 kΩ × 2, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).
How long does the power cut logger take to build?
About 90 minutes for a beginner maker, in 6 steps.
How do I know the power cut logger works?
Adapter plugged in: A0 reads near 512 on the serial monitor. Adapter unplugged: A0 falls to near 0 and the serial monitor prints Power cut. Plug back after 20 s: A line appears in cuts.csv with a duration near 20.
Nano restarts when the adapter is pulled: what should I check?
The boost converter or cell cannot supply it fast enough. Check the cell charge and the TP4056 wiring.
No file on the SD card: what should I check?
Check CS is on D10, the card is FAT32 and the module is 5 V compatible.
Durations are wrong: what should I check?
The RTC time is not set or its battery is flat.
The TP4056 gets hot: what should I check?
It is charging a cell while powering a load. Keep the cell near 1 A charge and the Nano load small.
