Starting Electronics Needs Your Help!
It is that time of the year when we need to pay for web hosting and buy new components and equipment for new tutorials. You can help by making a donation. Contribute to this website by clicking the Donate button. The total will be updated once daily. (You may need to clear your browser cache to see the updates.)
Target Amount: $2000
Amount Raised: $1061
Top Donor: C.C. $100
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.
This section gives an overview of how to use an Arduino with ThingSpeak, the tutorial follows.
The ThingSpeak library must be installed in the Arduino IDE software, which is easy with the Arduino library manager which handles the installation.
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.
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.
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.
To install the ThingSpeak library, open the Arduino IDE and do the following:
This video from Rob Purser shows the ThingSpeak library installation. Find more details in the article Official ThingSpeak Library for Arduino and Particle.
Can't see the video? View on YouTube →
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.
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.
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.
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.
Scroll down the WriteVoltage sketch until you find the code that sets the 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.
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.
Your channel ID will be displayed when your channel is selected on the ThingSpeak website.
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.
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.
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.