Sign In SIGN UP UPGRADE

Arduino + ML8511 UV Sensor

ML8511 UV Sensor splash image

Getting started

The ML8511 UV sensor from Sparkfun is awesome! It's so easy to grab UV readings and pipe them directly to Plotly.

Materials

  • Arduino-c]ompatible board
  • Your choice of Arduino internet shield
    (WIFI, CC3000, Ethernet, GSM)
  • ML8511 UV 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.

ML8511 UV Sensor Parts

Hooking it up

ML8511 EN ML8511 3.3V
ML8511 OUT Arduino A0
ML8511 GND Arduino GND
ML8511 3.3V Arduino 3.3V
ML8511 3.3V ML8511 EN

ML8511 UV 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 uvsensor.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();

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 uvsensor.js script.

Open uvsensor.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. Then, every time our UV sensor gets a "data" event (set to the sensor's 'freq' value, we'll fire off that data to our stream.

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

board.on("ready", function() {
    var uvOut = new five.Sensor({pin: "A0", freq: 1000, thresh: 0.5});
    var ref_3V3 = new five.Sensor({pin: "A1", freq: 1000, thresh: 0.5});
    var uvLevel = 0;
    var refLevel = 0;

    plotly.plot(data,layout,function (err, res) {
        var stream = plotly.stream('25tm9197rz', function (err, res) {
            console.log(res);
        });
        var stream_2 = plotly.stream('unbi52ww8a', function (err, res) {
            console.log(res);
        });

        uvOut.on("data", function() {
            uvLevel = this.value;

            var data = {
                x : getDateString(),
                y : uvLevel
            };
            // write the data to the plotly stream
            stream.write(JSON.stringify(data)+'\n');
        });

        ref_3V3.on("data", function() {
            refLevel = this.value;
            var outputVoltage = 3.3 / refLevel * uvLevel;

            var data = {
                x : getDateString(),
                y : outputVoltage
            };
            // write the data to the plotly stream
            stream_2.write(JSON.stringify(data)+'\n');
        });
    });
});

Wrapping it up

Using the Arduino sketch

By now you've got your Arduino wired up to the UV 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 UV 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 uvsensor.js

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