Sign In SIGN UP UPGRADE

Arduino + Water Flow Sensor

Water Flow Sensor splash image

Getting started

We wanted to gather some data that would be compelling from the get-go. Unlike temperature, humidity and various other atmospheric variables, the amount of water we're using changes quickly, and we can view those changes in real-time. This little snippet will show you how to setup a real-time water flow sensor, so you can keep track of how much water you're using!

Materials

  • Arduino UNO
  • Your choice of Arduino internet shield
    (WIFI, CC3000, Ethernet, GSM)
  • Water flow sensor
  • Hookup wires

If you're going to use the Node.js route, you do not need an internet shield — just a USB connection from your Arduino to your computer.

Water Flow Sensor Parts

Hooking it up

WATERFLOW PIN1 Arduino 5V
WATERFLOW PIN2 Arduino D2
WATERFLOW PIN3 Arduino GND

Water Flow Sensor hookup

Software

Download and install all libraries into your usual Arduino Library Folder.
The default is usually at /User/Documents/Arduino/Libraries.

If you're using the Arduino wifi shield, make sure to use version 1.0.3 of the Arduino IDE!

The script

The choice is yours! You can use an Arduino sketch for this project, or a Node.js script.

Using the Arduino sketch

Grab your choice of Arduino script here:

Open up waterflow.ino and let's go through it. First we initialize a plotly graph. If you're using a wifi shield, remember to change these wireless credentials to your own in the sketch!

plotly graph("username", "api_key", tokens, "filename", nTraces);

In the setup() function, we're going to initialize the graph. Once it's initialized, we're going to open the stream. graph.openStream() opens the plotly stream and graph.plot() is where all the data gets piped in. Let's check it out:

graph.init();
graph.openStream();

In the loop() function, we're going to start streaming our sensor data!

In this example, we are incrementing pulses as water flows through the sensor. Based on time, we can calculate the number of liters used.

void loop () {
    float liters = pulses;
    liters /= 7.5;
    liters /= 60.0;

    graph.plot(millis(), liters, tokens[0]);
    delay(100);
}

Your code is now all set up!

Using the Node.js script

First, you need to set up your Arduino for Node.js interaction.

Next, download the waterflow.js script.

Open waterflow.js, and let's go through it! First we create a new plotly object with your credentials:

var plotly = require('plotly')("username", "api_key");

The init_data object is what we send to plotly when we initialize the graph. We are sending empty arrays to act as containers for our data. You can also choose the maximum number of points to display.

Don't forget to change the 'streamtoken' to your own!

var init_data = [{
    x : [],
    y : [],
    stream : {
        token : 'streamtoken',
        maxpoints : 5000
    }
}];

We're going to use plotly.plot() to initialize our graph and plotly.stream() to pipe our data.

First we'll set up our board (let johnny-five connect to the Arduino over serial). Once that's successful, we'll initialize the graph.

When our Plotly graph is ready, we'll initialize a stream!

In this example, we'll be reading Digital Pin 2, and each time there is a pulse, we will pass it to flowSignal(value).

This will convert the pulses to liters used. In our setInterval() function, we're going to be piping our data to the stream once every second. flow_stream will emit data to the Plotly stream we initialized!

Check out the full waterflow.js for better error handling practices.

board.on("ready", function() {
    this.pinMode(2, five.Pin.INPUT);
    lastFlowPinState = 0;

    var x = this.digitalRead(2, function(value) {
        flowSignal(value);
        console.log(value);
    });

    setInterval(function() {
        var litres = pulses;
        litres /= 7.5;
        litres /= 60;
        var data = {x:getDateString(), y:litres};
        flow_stream.emit('data', JSON.stringify(data)+'\n');
    }, 1000);

    plotly.plot(data,layout,function (err, msg) {
        console.log(msg);
        var stream = plotly.stream('streamtoken', function (err, res) {
            console.log(res);
        });
        flow_stream.pipe(stream);
    });
});

Wrapping it up

Using the Arduino sketch

By now you've got your Arduino wired up to the water flow sensor, you've got it set up for internet connectivity, you've uploaded your sketch, and you're ready to go.

Start streaming! Plug in your Arduino, and go check out your live results!

Using the Node.js script

By now you've got your Arduino wired up to the water flow sensor, you've modified your node script, and you're ready to go.

Start streaming! Plug in your Arduino to your USB port, and run the script from the command line:

$ node waterflow.js

You can watch the console to see the url returned from Plotly's server. Now go check out your live stream!