Send Sensor’s Data to ThingSpeak Using Raspberry Pi

Use ThingSpeak in your Raspberry Pi IoT projects.


Data acquisition is important to further do analysis. ThingSpeak allows us to send the data and display it in the graph. For the student, the best thing is, it also offer a free account for you to try. So in this tutorial, I will share with you how to send sensor’s data to ThingSpeak using Raspberry Pi. You may need to refer the following tutorial to get familiar with other IoT platforms.

  1. Displaying Sensor’s Data on Blynk App Using Raspberry Pi
  2. Getting Started With Favoriot IoT Platform Using Raspberry Pi


This video will show you how to send sensor’s data to ThingSpeak IoT platform using Raspberry Pi.

Hardware Preparation

This is the list of items used in the video.

Sample Program

Below is the sample program for Thonny Python IDE used in the video. Before that please install the DHT sensor python library from Adafruit. Write the following command on the terminal.

git clone
cd Adafruit_Python_DHT
sudo python3 install
from gpiozero import Buzzer
import Adafruit_DHT
from time import time, sleep
from urllib.request import urlopen
import sys
WRITE_API = "Your ThingSpeak Write API" # Replace your ThingSpeak API key here
BASE_URL = "{}".format(WRITE_API)
buzzer = Buzzer(26)
SensorPrevSec = 0
SensorInterval = 2 # 2 seconds
ThingSpeakPrevSec = 0
ThingSpeakInterval = 20 # 20 seconds
while True:
if time() SensorPrevSec > SensorInterval:
SensorPrevSec = time()
humidity, temperature = Adafruit_DHT.read_retry(SENSOR_TYPE, SENSOR_PIN)
print("Humidity = {:.2f}%\tTemperature = {:.2f}C".format(humidity, temperature))
if time() ThingSpeakPrevSec > ThingSpeakInterval:
ThingSpeakPrevSec = time()
thingspeakHttp = BASE_URL + "&field1={:.2f}&field2={:.2f}".format(temperature, humidity)
conn = urlopen(thingspeakHttp)
print("Response: {}".format(
buzzer.beep(0.05, 0.05, 1)
except KeyboardInterrupt:

view raw
hosted with ❤ by GitHub

Thank You


Thanks for reading this tutorial. If you have any technical inquiries, please post at Cytron Technical Forum.

2 thoughts on “Send Sensor’s Data to ThingSpeak Using Raspberry Pi”

  1. Thank you for this. Had loads of errors trying to use old urllib libraries (newbie you see) . Using a sensehat and now have it writing data to thingspeak. Question though how do I get the script to keep sending data when there has been an interruption in the internet connection?
    Many thanks in advance

  2. hi change library
    from urllib.request import urlopen
    except ImportError:
    from urllib2 import urlopen
    and error
    Traceback (most recent call last):
    File “”, line 29, in
    humidity, temperature = Adafruit_DHT.read_retry(SENSOR_TYPE, SENSOR_PIN)
    File “build/bdist.linux-armv7l/egg/Adafruit_DHT/”, line 94, in read_retry
    File “build/bdist.linux-armv7l/egg/Adafruit_DHT/”, line 81, in read
    File “build/bdist.linux-armv7l/egg/Adafruit_DHT/”, line 213, in read
    RuntimeError: Error accessing GPIO. Make sure program is run as root with sudo!

Leave a Comment

Your email address will not be published.

Share this Tutorial

Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest
Share on facebook
Share on whatsapp
Share on email
Share on print
Share on twitter
Share on pinterest

Latest Tutorial

BLTouch Installation for Ender 3 with 32-bit V4.2.2 Board
Pick and Send Random Meal’s Option and Locations through Telegram Bot Using Grove WiFi 8266 on micro:bit
DIY Automated Vacuum Cleaner Using REKA:BIT With Micro:bit
Rainbow Spark in Mini House using Maker Uno.
TinyML on Arduino using Edge Impulse
Tutorials of Cytron Technologies Scroll to Top