Logging Data and Displaying Graphs over the Internet with Arduino

Created on: 16 March 2016

In this tutorial, an Arduino and Ethernet shield are used to send data over the Internet which is then logged and displayed in a graph using an external IoT (Internet of Things) service called ThingSpeak.

Using an external IoT service is a lot easier than using the Arduino and Ethernet shield as a webserver that hosts its own webpage to display the logged data in graphs. When using ThingSpeak, the Arduino is set up as a web client instead of a web server. This simplifies connecting the Arduino to the Internet. The ThingSpeak server also handles capturing the data and displaying the graphs.

Any data can be logged, for example, voltage on Arduino analog pins, temperature from a temperature sensor, pressure, humidity, etc. Just set up the Arduino with the desired sensor or sensors and send the data over the Internet for logging and display in a line graph.

This tutorial uses an Arduino Uno R3 and Ethernet shield, but any Arduino that can connect to the Internet should work, such as an Arduino MEGA 2560 and Ethernet shield, Arduino Ethernet board, Arduino Yun and ESP8266. No SD card is needed.

Principle of Operation of Arduino with ThingSpeak

This section gives an overview of how to use an Arduino with ThingSpeak, the tutorial follows.

Install the ThingSpeak Library

The ThingSpeak library must be installed in the Arduino IDE software, which is easy with the Arduino library manager which handles the installation.

Create a ThingSpeak Account

In order to create a "channel" that the Arduino can send data over and to view the logged data in graphs, an account at ThingSpeak must be created (free of charge).

After an account is created, one or more channels can be created. Each channel can have multiple fields, so you can send for example, temperature and humidity over one channel to your ThingSpeak account using separate temperature and humidity fields.

Creating an Arduino Sketch to Send Data to ThingSpeak

Once you have created a channel on ThingSpeak, you can send data from an Arduino to your ThingSpeak account by writing a sketch that uses functions from the ThingSpeak library installed in Arduino. There are several example sketches that are installed when the ThingSpeak library is installed and this tutorial shows how to use one of the examples to send voltage from Arduino analog pin A0 to ThingSpeak to be displayed in a graph.

ThingSpeak Server

To avoid any confusion, it is not necessary to set up a ThingSpeak server if you have created an account at ThingSpeak. This option is available if you want to host your own server, which I am sure most users will not want to do.

1. Installing the ThingSpeak Library in the Arduino IDE

To install the ThingSpeak library, open the Arduino IDE and do the following:

  • From the top Arduino menu, select Sketch → Include Library → Manage Libraries...
  • In the Filter your search... field of the Library Manager dialog box, enter ThingSpeak.
  • Click ThingSpeak in the dialog box to select it.
  • Click the Install button to install the ThingSpeak library which will then be downloaded from the Internet.

2. Create a ThingSpeak Account

Go to the ThingSpeak website and click the Sign Up link. You will need to provide a user name (user ID), email address and password and also agree to the terms of use and privacy policy to create an account.

3. Create a Channel and Field

In order to send data from the Arduino to ThingSpeak, you need to create a channel. When you log into your ThingSpeak account for the first time, you should see the New Channel button. If it is not visible, click Channels → My Channels.

ThingSpeak New Channel button

You will now be able to fill in a name for the new channel, description and one or more field names. With this example, we are going to use the channel to display the voltage from pin A0 of the Arduino.

ThingSpeak new channel details

In the image above, the new channel is given a name so that it can be identified when logged into ThingSpeak. A description can be filled in so that you remember what the channel is for. A single field is created that contains the name that the data from the Arduino will be found under – in this example it is voltageA0.

When finished entering the details, scroll to the bottom of the page and click the Save Channel button. Leave the ThingSpeak page open that now displays the new channel – we will need to get the channel ID number and API key from this page to insert into the Arduino sketch.

4. Open and Modify an Example Sketch

This tutorial uses the WriteVoltage sketch that can be found on the Arduino IDE menu under File → Examples → ThingSpeak → WriteVoltage after the ThingSpeak library is installed. You can modify this sketch or copy the contents from it to a new sketch if you don't want to overwrite the original.

You now need to modify the sketch as follows.

Changing the MAC Address

Scroll down the WriteVoltage sketch until you find the code that sets the Ethernet MAC address.

Changing the Arduino Ethernet MAC address

Change the hex numbers in the above line for the numbers found on the sticker on the bottom of your Ethernet shield.

Change the Channel Number and Write API Key

Scroll further down the sketch and replace the channel number and write API key with your own from your channel on the ThingSpeak website. The code to modify is shown below.

Changing the channel number and API write key

Your channel ID will be displayed when your channel is selected on the ThingSpeak website.

ThingSpeak channel ID

Copy this channel ID and paste it in the code in place of 31461 shown in the above code snippet.

Get your Write API Key by clicking the API Keys tab on the ThingSpeak website. Copy this string and replace the string in the Arduino sketch at const char * myWriteAPIKey = "LD79EOAAWRVYF04Y"; with this key.

Finding the ThingSpeak API write key

5. Load and Run the WriteVoltage Sketch

Now that the sketch is modified, it can be loaded to the Arduino. Connect the Arduino Ethernet shield Ethernet connector to your network using a network cable. A potentiometer can be used to vary the voltage on the A0 pin. Connect the Arduino to the PC USB port for power and programming.

Load the sketch to the Arduino and check that the data is being sent through to the channel set up on the ThingSpeak website. You will find the graph that displays the data under the Private View tab. Data will be updated every 20 seconds.

The maximum and minimum values of the y axis of the graph can be changed by clicking the pencil icon on the graph at the top right of the graph. This will open a dialog box allowing various graph or chart options to be changed.

ThingSpeak graph plotting Arduino voltage

The minimum and maximum values for the y-axis can be changed to 0 and 5 to fit the 0V to 5V range of the Arduino analog input pin. Click the Save button after changes have been made.

Chaning ThingSpeak graph axis values