I bet George Clooney doesn't know exactly how many coffees is drinking per day.

This little device made with Arduino MKR Zero will record every coffee made by just "listening" to the machine. If the sound is higher than a configurable threesold, a new record will be added to the CSV file into the microSD card with date and time.

It will also keep record of the number of coffees made since the last water refill so you can be alerted to refill and avoid damaging your coffee maker.

Connections are the following: A1 to read the sound module, D6 for the push button and D5 for the led.

Since MKR has RTC and an integrated SD reader, there is nothing more to connect but a VIN input to VIN/GND (not shown in the image below) Or you can still use the USB cable.

Configuration

Before uploading the code edit the threesold and the number of servings to refill water. Remember also to edit date and time, so it will be passed by to RTC

Reports

To create reports you have to read the microSD and group records per day. Then you will be able to make a nice chart from Excel or LibreCalc.

Project code

/*
Nespresso Shield 
Data logger and water refill alerts
Rodrigo S. A. Steadman Thompson 
steadmanthompson@gmail.com

*/

#include <RTCZero.h>
#include <SD.h>

RTCZero rtc;

const int  SD_CHIP_SELECT = SDCARD_SS_PIN;
bool SDAvailable = false;
String logFile = "DATALOG.CSV";
int soundPin=7;
int buttonPin=6;
int ledPin=5;

unsigned long StartTime = millis();
int working=0;
int counter=0;
int waterLimit=5;
int threesold=800;

/* Change these values to set the current initial time */
const byte seconds = 00;
const byte minutes = 13;
const byte hours = 12;

/* Change these values to set the current initial date */
const byte day = 24;
const byte month = 04;
const byte year = 21;

void setup()
{
  Serial.begin(9600);

  rtc.begin(); 

  pinMode(soundPin, INPUT); 
  pinMode(buttonPin, INPUT_PULLUP);  
  pinMode(ledPin, OUTPUT);    

  digitalWrite(ledPin, HIGH);
  
  // Set the time  
  rtc.setHours(hours);
  rtc.setMinutes(minutes);
  rtc.setSeconds(seconds);

  // Set the date
  rtc.setDay(day);
  rtc.setMonth(month);
  rtc.setYear(year);

  // initialize SD card:
  SDAvailable = SD.begin(SD_CHIP_SELECT);
  Serial.println("Card working: " + String(SDAvailable));
  digitalWrite(ledPin, LOW);
  
}

void loop()
{

  // digital read
  int soundVal1 = digitalRead(soundPin); 
  // analog read
  int soundVal2 = analogRead(1);

  int buttonVal=digitalRead(buttonPin); 
  
  Serial.println("Reading: ");
  Serial.println(soundVal2);
  Serial.println(" Button: ");
  Serial.println(buttonVal);  
  Serial.println(" Counter: ");
  Serial.println(counter);  
  Serial.println("");
  
  // Print date...
  print2digits(rtc.getDay());
  Serial.print("/");
  print2digits(rtc.getMonth());
  Serial.print("/");
  print2digits(rtc.getYear());
  Serial.print(" ");
  print2digits(rtc.getHours());
  Serial.print(":");
  print2digits(rtc.getMinutes());
  Serial.print(":");
  print2digits(rtc.getSeconds());

  Serial.println();

  if (counter>waterLimit){
    digitalWrite(ledPin, HIGH);
    Serial.println("Change water");
   }
   else
   {
    digitalWrite(ledPin, LOW);
   }
  
  if (buttonVal==0){
    // reset Coffe counter
    counter=0;
  }
   
   if (soundVal2>threesold){  
    
    //for digital comparison use if (soundVal1==1){  
    
        if (working==0){
         working=1;
         counter++;
         StartTime = millis();
         Serial.println("Making coffee");   
         digitalWrite(ledPin, HIGH);      
        } // working
        
    }// sound detected
    else
    {

      if (working==1){
        
        working=0;
        
        unsigned long CurrentTime = millis();
        unsigned long ElapsedTime = CurrentTime - StartTime; 
        float ElapsedSeconds = ElapsedTime/1000;

        Serial.println("Elapsed: ");
        Serial.print(ElapsedSeconds);
        Serial.print(" seconds");

        // Write to SD
         if (SDAvailable) {
          File dataFile = SD.open(logFile, FILE_WRITE);
          dataFile.print("\n"); 
          dataFile.print(counter); 
          dataFile.print(","); 
          dataFile.print(rtc.getDay());
          dataFile.print("/");
          dataFile.print(rtc.getMonth());
          dataFile.print("/");
          dataFile.print(rtc.getYear());
          dataFile.print(",");
 dataFile.print(rtc.getHours());...
Read more »