Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Arduino Sketch for data logging water flow to an SD card. Project at
#include <SD.h>
This script is a simple combination of two existing example files.
The goal is to measure water flow coming from a rain collection barrel
The project progress can be found at
This is example code for using the Adafruit liquid flow meters.
Tested and works great with the Adafruit plastic and brass meters
Connect the red wire to +5V,
the black wire to common ground
and the yellow sensor wire to pin #2
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, check license.txt for more information
All text above must be included in any redistribution
SD card datalogger
This example shows how to log data from three analog sensors
to an SD card using the SD library.
The circuit:
* analog sensors on analog ins 0, 1, and 2
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
created 24 Nov 2010
modified 9 Apr 2012
by Tom Igoe
This example code is in the public domain.
// which pin to use for reading the sensor? can use any pin!
const int chipSelect = 4;
// count how many pulses!
volatile uint16_t pulses = 0;
// track the state of the pulse pin
volatile uint8_t lastflowpinstate;
// you can try to keep time of how long it is between pulses
volatile uint32_t lastflowratetimer = 0;
// and use that to calculate a flow rate
volatile float flowrate;
// Interrupt is called once a millisecond, looks for any pulses from the sensor!
uint8_t x = digitalRead(FLOWSENSORPIN);
if (x == lastflowpinstate) {
return; // nothing changed!
if (x == HIGH) {
//low to high transition!
lastflowpinstate = x;
flowrate = 1000.0;
flowrate /= lastflowratetimer; // in hertz
lastflowratetimer = 0;
void useInterrupt(boolean v) {
if (v) {
// Timer0 is already used for millis() - we'll just interrupt somewhere
// in the middle and call the "Compare A" function above
OCR0A = 0xAF;
} else {
// do not call the interrupt function COMPA anymore
void setup() {
Serial.println("Flow sensor test!");
//lcd.begin(16, 2);
Serial.println("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
Serial.println("card initialized.");
lastflowpinstate = digitalRead(FLOWSENSORPIN);
void loop() // run over and over again
//lcd.setCursor(0, 0);
//Serial.print("Pulses:"); Serial.print(pulses, DEC);
//Serial.print(" Hz:");
Serial.print("Freq: "); Serial.println(flowrate);
Serial.print("Pulses: "); Serial.println(pulses, DEC);
// if a plastic sensor use the following calculation
// Sensor Frequency (Hz) = 7.5 * Q (Liters/min)
// Liters = Q * time elapsed (seconds) / 60 (seconds/minute)
// Liters = (Frequency (Pulses/second) / 7.5) * time elapsed (seconds) / 60
// Liters = Pulses / (7.5 * 60)
float liters = pulses;
liters /= 7.5;
liters /= 60.0;
Serial.print(liters); Serial.println(" Liters");
float gallons=liters * 0.2642;
Serial.print(gallons); Serial.println(" Gallons");
File dataFile ="datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.print("Liters: ");
dataFile.print(" Gallons: ");
// print to the serial port too:
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment