Updated November 2026. This page started back when ThingSpeak’s Arduino library needed Arduino 1.6 and a Wi-Fi shield. A lot has changed since then. Arduino boards come with Wi-Fi built in, the Arduino IDE is on version 2, and the ThingSpeak library can send over HTTPS. So I rewrote it for a current board and today’s ThingSpeak.
This project sends two analog readings from your Arduino to a ThingSpeak channel every 20 seconds. There’s nothing to wire. Once a reading shows up on a chart, you know your board, your network, and your channel all work. Then you can hook up any sensor you like.
Pick your board
You need an Arduino that can get on the internet, either with Wi-Fi on the board or with a shield.
- Arduino UNO R4 WiFi: Wi-Fi is built in. The sketch below works as written. I wrote about getting started with this board in Learning How to Use the Arduino UNO R4 WiFi Board.
- Arduino MKR WiFi 1010 or Nano 33 IoT: use the same sketch and change one line.
- ESP32 or ESP8266: follow Building the Internet of Things with the ESP8266, ESP32, and ThingSpeak.
- Arduino UNO R3 or Mega with an Ethernet shield: follow Using an Arduino Ethernet Shield to Update a ThingSpeak Channel.
One catch: the Wi-Fi on these boards only works on 2.4 GHz networks.
What you need
- One of the boards above and a USB cable
- The Arduino IDE, version 2
- A free ThingSpeak account at thingspeak.mathworks.com
Create a ThingSpeak channel
1. Sign in to ThingSpeak with your MathWorks account, or create one. 2. Go to Channels, My Channels, New Channel. 3. Name the channel, set Field 1 to A0, check Field 2 and set it to A1, and click Save Channel. 4. Copy the Channel ID from the top of the channel page. 5. On the API Keys tab, copy the Write API Key.
Install the board and the library
1. In the Arduino IDE, open the Boards Manager and install the package for your board: Arduino UNO R4 Boards for the UNO R4 WiFi, or Arduino SAMD Boards for the MKR WiFi 1010 and Nano 33 IoT. 2. Open the Library Manager, search for ThingSpeak, and install ThingSpeak by MathWorks. This page uses version 2.1.1. 3. On a MKR WiFi 1010 or Nano 33 IoT, also install the WiFiNINA library.
The sketch
The sketch has two files. Create a new sketch, paste this in, and save it as ThingSpeakArduino:
// Send two analog readings to a ThingSpeak channel every 20 seconds, over HTTPS.
// Board: Arduino UNO R4 WiFi. For a MKR WiFi 1010 or Nano 33 IoT, change the WiFiS3.h line to
// #include <WiFiNINA.h>; nothing else changes.
// Put your Wi-Fi name and password and your channel's number and Write API Key in arduino_secrets.h.
#include <WiFiS3.h>
#include "arduino_secrets.h"
#define TS_ENABLE_SSL // send over HTTPS; this line goes before the ThingSpeak include
#include <ThingSpeak.h> // the ThingSpeak library always comes after the Wi-Fi include
WiFiSSLClient client;
const unsigned long SEND_EVERY_MS = 20000; // a free ThingSpeak account takes one update every 15 seconds
void connectWiFi() {
while (WiFi.status() != WL_CONNECTED) {
Serial.print("Connecting to ");
Serial.println(SECRET_SSID);
WiFi.begin(SECRET_SSID, SECRET_PASS);
delay(5000);
}
Serial.println("Wi-Fi connected");
}
void setup() {
Serial.begin(115200);
while (!Serial && millis() < 3000) {
// wait up to 3 seconds for the Serial Monitor, then carry on without it
}
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("No Wi-Fi module found. Check the board you selected.");
while (true) {
}
}
ThingSpeak.begin(client);
}
void loop() {
connectWiFi();
int a0 = analogRead(A0);
int a1 = analogRead(A1);
ThingSpeak.setField(1, a0);
ThingSpeak.setField(2, a1);
int status = ThingSpeak.writeFields(SECRET_CH_ID, SECRET_WRITE_APIKEY);
if (status == 200) {
Serial.print("Sent A0=");
Serial.print(a0);
Serial.print(" A1=");
Serial.println(a1);
} else {
Serial.print("ThingSpeak error ");
Serial.println(status);
}
delay(SEND_EVERY_MS);
}
Then add a second tab (the … menu at the top right of the editor, New Tab), name it arduino_secrets.h, and fill in your own values:
// Your Wi-Fi network and your ThingSpeak channel. Keep this file out of anything you share.
#define SECRET_SSID "your-wifi-name"
#define SECRET_PASS "your-wifi-password"
#define SECRET_CH_ID 0 // your channel number, like 1417
#define SECRET_WRITE_APIKEY "XXXXXXXXXXXXXXXX" // your channel's Write API Key
A few things to know about the sketch:
- HTTPS.
#define TS_ENABLE_SSLwith aWiFiSSLClientmakes the library send over HTTPS on port 443, so your Write API Key doesn’t cross the network in plain text. The define has to come before#include <ThingSpeak.h>. - Several fields at once.
ThingSpeak.setField()stages a value for each field, andThingSpeak.writeFields()sends them together as one update. A channel has up to 8 fields. - Nothing wired. With nothing connected, the analog pins float, so the numbers wander around. That’s fine for a first test (you’re checking the plumbing, not the readings). Put a potentiometer or a sensor on
A0when you’re ready. - One value. To send a single reading, skip
setField()and useThingSpeak.writeField(SECRET_CH_ID, 1, a0, SECRET_WRITE_APIKEY);.
Upload it and watch the chart
1. Plug in the board, then pick it and its port in the board menu at the top of the IDE. 2. Click Upload. 3. Open the Serial Monitor at 115200 baud. You should see Wi-Fi connected, then a Sent A0=... A1=... line every 20 seconds. 4. Open your channel’s Private View in ThingSpeak. The two charts fill in as the updates arrive.
Troubleshooting
writeFields() returns 200 when ThingSpeak accepts the update. Anything else prints as ThingSpeak error followed by one of these codes from the library:
400or404: the Write API Key or the channel number is wrong. Copy both again from the channel page.-401: ThingSpeak didn’t store the update. Most of the time that’s because it came less than 15 seconds after the last one. A free account takes one update every 15 seconds.-301: the board couldn’t connect to ThingSpeak. Check that the board is on Wi-Fi and that your network allows outgoing connections on port 443.-304: ThingSpeak didn’t answer in time. The next update usually goes through.- It never gets past
Connecting to: check the network name and password, and that the network is 2.4 GHz. No Wi-Fi module found: the board selected in the IDE doesn’t match the board you have, or the include line doesn’t match it (WiFiS3.hfor the UNO R4 WiFi,WiFiNINA.hfor the MKR WiFi 1010 and Nano 33 IoT).
What changed from the old version
- Current boards. The old page was written for Arduino 1.6 and boards with a Wi-Fi shield. This one uses the UNO R4 WiFi, with one-line changes for the MKR WiFi 1010 and Nano 33 IoT, and sends the ESP and Ethernet boards to their own tutorials.
- Encrypted by default. The sketch sends over HTTPS. The old examples sent over plain HTTP.
- One sketch, two fields. The single-value and multiple-value examples are now one sketch that writes two fields, with a one-line change for a single value.
- Your settings in one file. The Wi-Fi password and the Write API Key live in
arduino_secrets.h, not in the sketch.
