Pill bottle open logger
A reed switch and magnet on a bottle cap log the time each time it is opened, so you can see whether today's dose has been taken. A memory aid only, not a medical device.
Build it step by step
0 of 8 doneWire the reed switch
One contact to D2, the other to GND.
Wire the clock and display
Both on A4 and A5 with 5V and GND.
Install the libraries
RTClib, Adafruit SSD1306 and Adafruit GFX.
Upload the sketch
The clock sets from the computer on first run.
Fix the magnet to the cap
And the reed switch to the neck.
Open the cap for a second
An opening is logged.
Close and reopen
The count rises.
Power cycle
The last openings are still there.
How do you build a pill bottle open logger?
A reed switch and magnet on a bottle cap log the time each time it is opened, so you can see whether today's dose has been taken. A memory aid only, not a medical device. You need Arduino Nano, Door reed switch MC-38, DS3231 RTC module, SSD1306 OLED Display, Jumper wires male to male (40 pcs, 20 cm) and Breadboard 830 Tie Points. It takes 8 steps and about 120 minutes.
- Level
- Beginner
- Time
- About 120 min
- Parts
- 6
Parts list
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Reed switch contact 1 | Nano D2 | Closed contact reads low |
| Reed switch contact 2 | Nano GND | Closes when the magnet is near the reed |
| DS3231 SDA and OLED SDA | Nano A4 | Shared I2C data |
| DS3231 SCL and OLED SCL | Nano A5 | Shared I2C clock |
| DS3231 VCC and OLED VCC | Nano 5V | Power (check your OLED accepts 5 V) |
| DS3231 GND and OLED GND | Nano GND | Common ground |
Code
A reed switch closes when a magnet is near, so a small magnet on a bottle cap and a reed switch on the bottle neck make a simple open-or-closed sensor. While the cap is on, the magnet holds the switch closed and the pin reads low. When the cap comes off, the switch opens and the pin goes high. The sketch waits until the cap has been off for about 0.8 seconds so a bump is not counted, then stores the time from the DS3231 clock. The last five times are kept in the EEPROM, so they stay through a power cut, and the display shows the time, whether a dose has been logged today, and the time of the last opening. This is only a memory aid to answer the question of whether you already took today's dose. It cannot tell whether anyone actually took anything from the bottle, and it is not a medical device, so always follow the instructions of a doctor or pharmacist and never rely on a gadget for medicine. Keep magnets away from medical implants, and keep the electronics out of reach of children.
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 <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <EEPROM.h>
RTC_DS3231 rtc;
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
const int REED = 2;
const unsigned long OPEN_CONFIRM_MS = 800; // cap must stay off this long to count
const byte MAGIC = 0xC4;
const int N = 5; // remember the last five openings
uint32_t events[N]; // unix times, newest first
bool capOff = false, counted = false;
unsigned long offSince = 0, lastDraw = 0;
void saveEvents() {
EEPROM.update(0, MAGIC);
EEPROM.put(1, events);
}
void addEvent(uint32_t t) {
for (int i = N - 1; i > 0; i--) events[i] = events[i - 1];
events[0] = t;
saveEvents();
}
int openedToday(const DateTime &now) {
int n = 0;
for (int i = 0; i < N; i++) {
if (events[i] == 0) continue;
DateTime e(events[i]);
if (e.year() == now.year() && e.month() == now.month() && e.day() == now.day()) n++;
}
return n;
}
void setup() {
pinMode(REED, INPUT_PULLUP);
Serial.begin(9600);
if (!rtc.begin()) { Serial.println("RTC not found"); while (true) {} }
if (rtc.lostPower()) rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { while (true) {} }
if (EEPROM.read(0) == MAGIC) EEPROM.get(1, events);
else for (int i = 0; i < N; i++) events[i] = 0;
}
void loop() {
bool off = (digitalRead(REED) == HIGH); // magnet away from the reed means the cap is off
if (off && !capOff) { capOff = true; counted = false; offSince = millis(); }
if (!off) { capOff = false; }
if (capOff && !counted && millis() - offSince >= OPEN_CONFIRM_MS) {
counted = true;
addEvent(rtc.now().unixtime());
Serial.println("Opening logged");
}
if (millis() - lastDraw >= 500) {
lastDraw = millis();
DateTime now = rtc.now();
int today = openedToday(now);
char buf[24];
oled.clearDisplay();
oled.setTextColor(SSD1306_WHITE);
oled.setTextSize(1);
snprintf(buf, sizeof(buf), "%02d:%02d %02d/%02d", now.hour(), now.minute(), now.day(), now.month());
oled.setCursor(0, 0); oled.print(buf);
oled.setTextSize(2);
oled.setCursor(0, 14); oled.print(today > 0 ? "LOGGED" : "NOT YET");
oled.setTextSize(1);
oled.setCursor(0, 36); oled.print("Opened today: "); oled.print(today);
if (events[0] != 0) {
DateTime last(events[0]);
snprintf(buf, sizeof(buf), "Last: %02d:%02d %02d/%02d", last.hour(), last.minute(), last.day(), last.month());
oled.setCursor(0, 48); oled.print(buf);
}
oled.display();
}
}What the serial monitor should show
Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.
RTC not found
Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Cap on, power up | NOT YET and Opened today: 0 |
| Cap off for over one second | LOGGED and the time of the opening |
| Bump the cap briefly | No count |
| Next day | Opened today returns to 0 |
| Power cycle | The last time is still shown |
If it does not work
Opening not counted
Keep the cap off for at least a second and check D2.
Counts when the cap is on
The reed does not close, move the magnet closer.
Wrong time
Reflash once with the computer clock correct.
Blank screen
Check A4 and A5 and the OLED address.
Log empty after power-off
The EEPROM needs a first opening to be saved.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the pill bottle open logger?
Arduino Nano, Door reed switch MC-38, DS3231 RTC module, SSD1306 OLED Display, Jumper wires male to male (40 pcs, 20 cm) and Breadboard 830 Tie Points.
How long does the pill bottle open logger take to build?
About 120 minutes for a beginner maker, in 8 steps.
How do I know the pill bottle open logger works?
Cap on, power up: NOT YET and Opened today: 0. Cap off for over one second: LOGGED and the time of the opening. Bump the cap briefly: No count.
Opening not counted: what should I check?
Keep the cap off for at least a second and check D2.
Counts when the cap is on: what should I check?
The reed does not close, move the magnet closer.
Wrong time: what should I check?
Reflash once with the computer clock correct.
Blank screen: what should I check?
Check A4 and A5 and the OLED address.
