How to Send Ultrasonic Sensor Data to Thingspeak

In this post we are going to learn how to upload ultrasonic sensor (HC-SR04) data to Thingspeak using Arduino and generic ESP8266 Wi-Fi module and also using NodeMCU in the second half of this article.

We will see:           

  • How to upload ultrasonic sensor data using ESP8266 and Arduino.
  • Circuit Diagram and its program code for ESP8266 and Arduino.
  • How to upload ultrasonic sensor data using NodeMCU.
  • Circuit diagram and its program code for NodeMCU.
  • Screen shots of uploaded sensor data on thingspeak.
  • Prototype image of the circuit.

Sending Measured Data to Thingspeak Using Arduino and ESP8266:

Ultrasonic sensor need no introduction among arduino hobbyists, it is one of the first sensors you might have tried. Now you are learning about IoT and you came across Thingspeak and you want to send some data to Thingspeak server and you chose ultrasonic sensor. Well you are at the right place!

Let’s begin, Shall we?

Block Diagram:

Block Diagram for Ultrasonic senor Data to Thingspeak
Block Diagram for Ultrasonic senor Data to Thingspeak

Here is the block diagram on how we are going to upload ultrasonic sensor data to Thingspeak. Firstly the Arduino will trigger the ultrasonic sensor and collects the data, after calculating the distance arduino will convey this data to ESP8266 module via UART / serial communication. The serial data consists of distance data in cm with 3 to 5 decimal places. The ESP8266 will send this data to your Thingspeak account via internet. 

Since we are using a generic ESP8266 module we have to upload an appropriate program code that makes ESP8266 to accept serial data and send it to Thingspeak. We also need to upload another program code to Arduino board, so that it will convert the measured ultrasonic sensor data in serial data.

Now let’s see how to upload the program code to ESP8266 module.

Circuit Diagram:

Uploading code to ESP8266
Uploading code to ESP8266

You need to wire up the circuit as illustrated above and remember that ESP8266 works on 3.3V and 5V will kill your module and make sure that ATmega328P is removed from Arduino board.

Once you wire-up it should look something like this:

Uploading code to ESP8266
Uploading code to ESP8266

How to upload the Program code:

ESP8266 won’t accept any new program code until you do this:

  • Press and hold the flash button now press the reset button once and leave the flash button. Pressing buttons in reverse sequence or pressing in some other way will results in fail to upload the program code.
  • Now select the board as generic ESP8266 on your IDE. You have to install the ESP8266 package to your IDE to select this board, if you haven’t done yet, Google it or watch a YouTube video on how to do so.
  • Select 115200 as baud rate and select the correct COM port.
  • Now click the upload button, it will take couple minutes to compile and another one minute to upload the code, so be patient.
  • On successful program upload you will see this:
Program uploaded to ESP8266
Program uploaded to ESP8266

Channel settings that you need to do before you upload the code:

Channel Settings
Channel Settings

Go to channel settings and name it as “Ultrasonic Sensor Value” and select field 1.

Now you need to collect some information about your channel and paste the information in the program code where necessary.

You need to your channel ID and write API key.

You Channel Settings and Write API Key
You Channel Settings and Write API Key

Download Thingspeak Library, click here.

Program Code for Generic ESP8266 Module:

#include "ThingSpeak.h"
#include <ESP8266WiFi.h>

//-------- Enter Your Wi-Fi Details -----//
char ssid[] = "xxxxxxxxxx";  //SSID
char pass[] = "yyyyyyyyyy"; //Password
//--------------------------------------//

WiFiClient  client;

unsigned long myChannelField = 123456; // Channel ID
const int ChannelField = 1; // Which To Field Write
const char * myWriteAPIKey = "xxxxxxxxxxxxxx"; // Write API Key

String value = "";
void setup()
{
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  ThingSpeak.begin(client);
}
void loop()
{
  if (Serial.available() > 0)
  {
    while (Serial.available() > 0)
    {
      int inChar = Serial.read();
      value += (char)inChar;
    }
  }
  if (WiFi.status() != WL_CONNECTED)
  {
    while (WiFi.status() != WL_CONNECTED)
    {
      WiFi.begin(ssid, pass);
      delay(5000);
    }
  }
  ThingSpeak.writeField(myChannelField, ChannelField, value, myWriteAPIKey);
  value = "";
}

Don’t forget the change the following:

//----------- Enter you Wi-Fi Details---------//
char ssid[] = "xxxxxxxx"; //SSID
char pass[] = "yyyyyyyy"; // Password
//-------------------------------------------//

unsigned long myChannelField = 123456; // Channel ID

const char * myWriteAPIKey = "xxxxxxxxxxxxxx"; // Write API Key

Once you successfully upload this code to ESP8266, you need rewire and upload another code to Arduino board.

Circuit Diagram:

Sending Ultrasonic sensor data to thingspeak
Sending Ultrasonic sensor data to thingspeak

You need to rewire the circuit as illustrated above and insert the IC back to Arduino board.

Program code for Arduino:

#include <SoftwareSerial.h>
SoftwareSerial ESP(10,11); 
const int trigger = 2;
const int echo = 3;
long T;
float distanceCM;
void setup()
{
  pinMode(trigger, OUTPUT);
  pinMode(echo, INPUT);
  Serial.begin(115200);
  ESP.begin(115200);
}
void loop()
{
  digitalWrite(trigger, LOW);
  delay(1);
  digitalWrite(trigger, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger, LOW);
  T = pulseIn(echo, HIGH);
  distanceCM = T * 0.034;
  Serial.print("Distance in cm: ");
  Serial.println(distanceCM);
  ESP.print(distanceCM);
  delay(1000);
}

After uploading this code to Arduino board, open the serial monitor at 115200 baud rate to see the measurement and open your browser to see the incoming data on Thingspeak.

Ultrasonic Sensor data on Thingspeak
Ultrasonic Sensor data on Thingspeak

You might have noticed that data updating on Thingspeak is much slower than that of serial monitor. This is normal; it takes more than 15 seconds to update the data on Thingspeak.

Prototype:

Prototype- Ultrasonic Sensor data sending to Thingspeak
Prototype- Ultrasonic Sensor data sending to Thingspeak

How to Send Ultrasonic Sensor Data using NodeMCU

NodeMCU makes IoT development a lot easier since it has enough I/O pins and built-in Wi-Fi ESP8266 module. So you no need manage with complexity that Arduino and generic ESP8266 had.

Circuit diagram:

As you can see the circuit is very simple, just connect the ultrasonic sensor and you are ready to upload the program code.

Download Thingspeak library, click here

Program code for NodeMCU:

#include "ThingSpeak.h"
#include <ESP8266WiFi.h>
	
//----------- Enter you Wi-Fi Details---------//
char ssid[] = "xxxxxxxx"; //SSID
char pass[] = "yyyyyyyy"; // Password
//-------------------------------------------//

const int trigger = 16;
const int echo = 5;
long T;
float distanceCM;
WiFiClient  client;

unsigned long myChannelField = 123456; // Channel ID
const int ChannelField = 1; // Which channel to write data
const char * myWriteAPIKey = "xxxxxxxxxxx"; // Your write API Key

void setup()
{
  Serial.begin(115200);
  pinMode(trigger, OUTPUT);
  pinMode(echo, INPUT);
  WiFi.mode(WIFI_STA);
  ThingSpeak.begin(client);
}
void loop()
{
  if (WiFi.status() != WL_CONNECTED)
  {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    while (WiFi.status() != WL_CONNECTED)
    {
      WiFi.begin(ssid, pass);
      Serial.print(".");
      delay(5000);
    }
    Serial.println("\nConnected.");
  }
  digitalWrite(trigger, LOW);
  delay(1);
  digitalWrite(trigger, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigger, LOW);
  T = pulseIn(echo, HIGH);
  distanceCM = T * 0.034;
  Serial.print("Distance in cm: ");
  Serial.println(distanceCM);
  ThingSpeak.writeField(myChannelField, ChannelField, distanceCM, myWriteAPIKey);
  delay(1000);
}

Don’t forget to change the following:

//----------- Enter you Wi-Fi Details---------//
char ssid[] = "xxxxxxxx"; //SSID
char pass[] = "yyyyyyyy"; // Password
//-------------------------------------------------//

unsigned long myChannelField = 123456; // Channel ID

const char * myWriteAPIKey = "xxxxxxxxxxx"; // Your write API Key

You need to paste your channel details in the code as mentioned in the first half of this post.

How to Upload Code to NodeMCU:

  • Connect the NodeMCU with micro USB cable to your PC.
  • Press and hold flash button and press the reset button once and now you leave the flash button.
  • Select the NodeMCU v1.0 as board.
  • Select 115200 as baud rate and select the correct COM port.
  • Press upload button, it may take 2 min to compile the code and another one minute to upload to NodeMCU.

On successful program uploaded to NodeMCU, you will see this:     

Program uploaded to ESP8266
Program uploaded to ESP8266

   Data uploaded to Thingspeak and Serial Monitor:

Ultrasonic sensor data on Thingspeak
Ultrasonic sensor data on Thingspeak

Prototype:

Ultrasonic sensor and NodeMCU
Ultrasonic sensor and NodeMCU

If you have any questions regarding this project, feel free to ask us in the comments, you will get a guaranteed reply from us.