Example Code for Arduino-Ambient Noise Analysis (Based on ThingSpeak)
This tutorial demonstrates how to read data from a sound sensor, upload it to ThingSpeak, and generate graphs to analyze environmental noise levels. By monitoring the data, you can identify the periods with the highest noise levels during the day.
Hardware Preparation
- Arduino UNO board x 1
- IO Expansion board x1
- WiFi IoT module x1
- Analog Sound sensor
- Wires
Software Preparation
- Arduino IDE
- Download and install the WiFi IoT Module Library. (About how to install the library?)
Wiring Diagram

How to use ThingSpeak
Open ThingSpeak website,, click the right-upper corner to enter the register and login interface.

When using for first time, register an account in the following interface. Fill in the correct email address and vertify the mail box.


The following interface will appear after finishing the registeration.

The above interface can also be found by clicking "Channels->My Channel". Now click new channel.

Enter the interface below then.

Here, we need to fill in the name, channel description, and tick the number of fields. In the makecode library, the fields are the same as the fields in the channel. In the routine, only the noise analog value is transferred to the channel. So only one is ticked here, and the field name can be customized. We modify the field name here as noise. Save the channel now. Here is the channel I created.

After saving, the channel is created. The created channel is shown in the figure below, the parameters uploaded can be seen here.

If more data needs to be uploaded, you need to click channel settings, click to enter the figure below, tick the "box" behind the field, and save it. You can also delete channel and clear channel data here.

Channel added successfully.

Click API Keys then we can see the related password of the channel. The password in the red box should be filled in the corresponding block in programming.

Sample Code
#include "DFRobot_WiFi_IoT_Module.h"
#include <SoftwareSerial.h>
//I2C Mode
DFRobot_WiFi_IoT_Module_I2C IoT;
//configure WiFi account and password
const char *WIFI_SSID = "hitest";
const char *WIFI_PASSWORD = "12345678";
//Configure ThingSpeak
const char *ThingSpeak_ADDRESS = "api.thingspeak.com"; //ThingSpeak address, do not need to revise here
const char *THINGSPEAK_KEY = "RXARKPLSDMR3G8MA"; //Fill in the event password created on IFTTT
//Send message to ThingSpeak
const char *THINGSPEAK_VALUE_1 = "Value1";
const char *THINGSPEAK_VALUE_2 = "Value2";
const char *THINGSPEAK_VALUE_3 = "Value3";
/*******************************************************
Function: getNoise
Description: Get the value of sound sensor
Params:
Return: Current sound value
******************************************************/
int getNoise()
{
int val;
val = analogRead(0);
return val;
}
void setup() {
//use serialport 115200 as print port
Serial.begin(115200);
//init communication port
while(IoT.begin() != 0){
Serial.println("init ERROR!!!!");
delay(100);
}
Serial.println("init Success");
//Connect to WiFi
while(IoT.connectWifi(WIFI_SSID, WIFI_PASSWORD) != 0){
Serial.print(".");
delay(100);
}
Serial.println("Wifi Connect Success");
while(IoT.HTTPBegin(ThingSpeak_ADDRESS) != 0){
Serial.println("Parameter is empty!");
delay(100);
}
Serial.println("HTTP Configuration Success");
while(IoT.thingSpeakBegin(THINGSPEAK_KEY) != 0){
Serial.print("Parameter is empty!");
delay(100);
}
Serial.println("ThingSpeak Configuration Success");
}
void loop() {
String data =(String)getNoise();
//Upload current sound value to ThingSpeak every 1s
IoT.thingSpeakSendMessage(data.c_str());
delay(1000);
}
Result
Check the uploaded data in private view of thingSpeak platform, the sound value read by analog sound sensor will be uploaded to IoT platform in real time, and displayed in the form of line graph.

Was this article helpful?
