Abstract
Integration of the latest technological advancements such as Internet of Things (IoT) and Computational Intelligence (CI) techniques is an active research area for various industrial applications. The rapid urbanization and exponential growth of vehicles has led to crowded traffic in cities. The deployment of IoT infrastructures for building smart and intelligent traffic management system greatly improves the quality and comfort of city dwellers. This work aims at building a cost effective IoT enabled traffic forecasting system using deep learning techniques. The case study experimentation is done in a real time traffic environment. The main contributions of this work include: (i) deploying road side sensor station built with ultrasonic sensor and Arduino Uno controller for obtaining traffic flow data (ii) building an IoT cloud system based on open source Thingspeak cloud platform for monitoring real time traffic (iii) performing short term traffic forecast using Recurrent Neural Network (RNN) models such as Long Short Term Memory (LSTM) and Gated Recurrent Unit (GRU). The performance of the prediction model is compared with the traditional statistical methods such as Autoregressive Integrated Moving Average (ARIMA), Seasonal ARIMA (SARIMA) and Convolutional Neural Network (CNN). The results show good performance metrics with RMSE of 5.8, 7.9, 10.2 for LSTM model and 6.7, 8.6, 10.9 for GRU model for three different scenarios such as whole day, morning congested hour and evening congested hour datasets.
Introduction
Traffic Management is one of the key challenge in the past few decades, particularly in the metropolitan cities. Both the public and personal transportation vehicles are increasing day by day due to urbanization [1]. Ultimately this leads to a congested traffic in the cities. Integration of information and communication techniques (ICT) is very much crucial for Intelligent Transportation System (ITS) [2]. The rapid advancement in technologies such as Sensors, IoT, Cloud Computing and Artificial Intelligence (AI) enables strategic planning of optimal vehicle routing. Traffic flow prediction is very much helpful for building ITS [3]. The objectives of ITS includes Traffic state prediction, optimal route estimation, incident or accident detection, bottleneck positioning, dynamic and adaptive traffic signal control, surveillance and security services.
Variety of sensors [4–6] such as ultrasonic sensor, radar, magnetic sensor, optical sensors, inductive loop sensor, weight in motion, piezoelectric sensor etc. are used for detecting the vehicle flow, vehicle speed and vehicle classification. Millions of physical objects such as these sensors shall be connected to the internet [7] using gateway devices [8]. The traffic related data obtained from these sensors are stored in cloud platform [9]. ITS rely on this traffic data such as traffic flow, density, speed, road occupancy rate, vehicle’s geographical location for performing the data analytics to give useful results such as traffic forecasting, travel time estimation, route optimization, etc.
The short term traffic prediction [10] helps in traffic forecasting for the near future. With this short term traffic forecast, ITS shall suggest optimal routes to the city dwellers [11]. Data driven short term traffic forecasting shall be done with the Statistical as well Computational Intelligent techniques [12]. In the recent times Recurrent Neural Network (RNN) [13] models such as Long Short Term Memory(LSTM) and Gated Recurrent Unit(GRU) are gaining attention among the researchers for traffic forecasting which is a time series prediction method.
The proposed work focusses on Building a low cost IoT enabled vehicle detection system. Integrating the vehicle detection system to the open source cloud platform. Performing short term traffic forecast with the collected data using GRU and LSTM RNN.
The remainder of the paper is organized as follows. Section II discusses the related works in literature in the field of ultrasonic sensor and Arduino based vehicle detectors, IoT cloud system, statistical and computational techniques for traffic prediction. Next we briefly elaborate the background required for the proposed work Section III. In section IV our proposed methodology is described. Next in section V the experimental set up and data acquisition is explained. Finally section VI deals with the results and discussion of our work followed by the conclusion in section VII.
Related works
There are many works done by researchers in literature in the area of vehicle detection, IoT cloud system and time series forecasting techniques.
Ultrasonic sensor for vehicle detection
In the literature, ultrasonic sensors is used widely for vehicle detection, since it reduces the cost and the power consumption of the system. Youngtae et al. design a traffic flow detection system using ultrasonic Sensor [14]. Obed Appiah et al. experiment with the detection of vehicle with ultrasonic sensor [15]. Similarly, Youngtae Jo et al. use ultrasonic sensor and ATMega128 L microcontroller for vehicle detection by placing the setup on the roadside [16]. Vivek Agarwal et al. propose a cost effective driver assist system for obstacle avoidance in the road traffic using ultrasonic sensor [17]. Zhijun Li et al. use ultrasonic sensor for real time traffic flow detection using MCS-51 single chip microcomputer and control the traffic light, based on the real time traffic flow in different lanes [18]. Soobin Jeon et al. [19] also use ultrasonic sensor for traffic flow measurement on a multiple lane road. Even through there are many sensors available for vehicle detection, the ultrasonic sensor is chosen as a low cost and low power consuming alternative by the researchers.
IoT cloud system
IoT edge devices generate large volume data and thanks to the cloud platform which supports the big data [20].
Zheng et al. use IoT based Hadoop Cloud Platform and perform data mining on the sensor data collected in the IoT cloud [21]. Celesti et al. build an IoT cloud system using openGTS and MangoDB for traffic monitoring based on mobile sensor data [22]. Sathish Pasika et al. [23] use Thingspeak IoT Cloud for monitoring the sensor data collected using Microcontroller Unit (MCU). Geng Yang et al. propose an IoT Cloud based system for patient pain monitoring [24]. Luca Catarinucci et al. use IoT based Cloud platform for collecting RFID sensor data for intelligent waste management [25]. AbhijitPathak et al. use Thingspeak IoT cloud to collect parameters related to the agriculture field for appropriate crop selection [26].
Weiguang Li et al. [27] design an IoT based system for traffic prediction using neural networks. Jiaona Chen et al. implement an intelligent traffic control system using IoT and machine learning techniques by collecting vehicle flow data [28]. Bhabendu Kumar et al. propose an accident severity classification enabled with IoT for proving alert message [29]. Thus the researchers use various cloud platform such as Hadoop, MangoDB, Thingspeak, etc for building IoT cloud.
Statistical techniques for traffic prediction
The traffic flow forecasting is a time series prediction problem. In the literature, researchers use statistical techniques such as Autoregressive Moving Average (ARMA), Autoregressive Integrated Moving Average (ARIMA), Seasonal Auto-Regressive Integrated Moving Average (SARIMA). Ergin Erdem et al. use ARMA model for forecasting the wind speed and direction for efficient energy harvesting [30]. Jinquan Guo et al. predict road gradient as well the vehicle speed for efficient energy management in electric vehicles [31]. Snezhana et al. perform time series analysis and forecasting of air pollution with SARIMA model [32].
Artificial intelligence techniques for traffic prediction
In the recent times with the advancement in the computation techniques, time series prediction problem using Artificial Intelligence techniques has gained focus among the researchers. The machine learning techniques such as Support Vector Machines (SVM), K Nearest Neighbors (KNN), Conventional Neural Networks (CNN), Recurrent Neural Network (RNN), etc. show good prediction results for the time sequence models such as language processing [33], video recognition [34], speech recognition [35], electricity load forecasting [36], stock prediction [37], traffic prediction [38] etc. Nowadays, researchers prefer such computational techniques for traffic prediction in ITS, since these techniques provide good prediction results.
Xinxin Feng et al. [39] propose an SVM based algorithm named Adaptive Multi-kernel Support Vector Machine with Spatial–Temporal Correlation (AMSVM-STC) considering the relationship between the spatial and temporal data for short term traffic prediction. Bin Yu et al. [40] develop a KNN based multi time-step traffic prediction model considering the spatial and temporal characteristics. Yuxiu Hua et al. [41] propose a stochastic connectivity LSTM to reduce the computation cost and time for applications with power and time constraints such as traffic predictions. A multivariate LSTM model [42] considering traffic flow, speed and occupancy rate shows better performance in traffic flow forecasting. Peng Sun et al. [43] propose a GRU based traffic prediction model considering the spatial characteristics for a multi road traffic flow prediction.
Thus in the literature most of the proposed works on traffic prediction are evaluated using dataset that are available online. Also, traffic data acquisition system is implemented with different sensor technologies. Most of the works in the literature either focuses on traffic acquisition system alone or the works focuses on prediction techniques alone using online traffic datasets. Among various sensing techniques, the ultrasonic sensors are cost effective for obtaining traffic flow data. Also, on comparing the performance metrics of the statistical and machine learning models, the LSTM and GRU RNN shows superior performance. Therefore, to provide a cost effective solution for the ITS, an IoT enabled traffic data acquisition system using the ultrasonic sensor and traffic forecasting using LSTM and GRU RNN deep learning models is proposed and implemented in this study as shown in Fig. 1.

Proposed IoT based traffic forecast system.
Arduino UNO
The Arduino UNO is an open-source microcontroller board which is based on the Microchip ATmega328P microcontroller. The board is equipped with sets of digital and analog input/output pins that may be interfaced to various components.
Ultrasonic sensor
An ultrasonic sensor is an electronic device that measures the distance of a target object by emitting ultrasonic sound waves, and converts the reflected sound into an electrical signal. Ultrasonic waves travel faster than the speed of audible sound. Two ultrasonic sensors are used in this work for a single lane road to get an accurate average number of vehicles that crosses in a particular time limit.
Kalman Filter based signal processing
Kalman filter algorithm is used for eliminating the noise in the ultrasonic sensor measurements. The kalman filter determine the state of a dynamically behaving system by recursive estimation process. The mathematical formulation of Kalman filter is as follows:
W
K
, V
K
are the input and output measurement noises. X
K
, Y
K
, U
K
are the state, output and feedback vectors. A, B, C, D, H are the constant values. The objective of the Kalman filter is to remove the noise and determine true measurement
Time update is done based on Equation (4). The measurement update is done with Equations (5)–(6).
M x , M y are the gains for minimizing the covariance of the estimation errors.
The ESP8266 is a low-cost Wi-Fi microchip, with a full TCP/IP stack and microcontroller capability. This microchip makes the overall system as an IoT device. The data collected from the ultrasonic sensor is stored in the Arduino board and transferred to the cloud for each and every minute with the help of WiFi Module.
Thingspeak cloud platform
To collect the live data and to visualize the data, Thingspeak is used as an IoT middleware platform. It uses HTTP APIs as a RESTful architecture for accessing the network devices [44]. The platform offers dashboard for real-time data visualization and analyzing in the forms of graphs, charts, plots, etc
Recurrent neural network (RNN)
In Convolutional Neural Network (CNN) the computation at each layer is based on the output of previous layer, weight and the bias input. There is no connectivity between the nodes in a particular layer. Rather in RNN, the neuron at the hidden layer does computation based on the current state input and the feedback from the previous state which makes it remember the temporal information. This feature makes the RNN to gain attention in the areas of time sequence applications such as Natural Language Processing, speech recognition, video processing, traffic forecasting, etc.
1D-CNN
The Convolutional Neural Network (CNN) is classical approach that can also be deployed for time series prediction. The architecture of 1D-CNN for time series prediction is shown in Fig. 2.

Architecture of 1D CNN.
It has Input layer, Convolutional layer, Pooling layer, Flatten layer, Fully connected layer and Output Layer for making the time sequence prediction by extracting the correlation among the data.
The RNN suffers from vanishing and exploding gradient problem due to the back propagation error. Long Short Term Memory [45], a variant of RNN shall overcome this problem.
As shown in Fig. 3, the LSTM memory cell has three gates namely - forget gate, input gate and output gate for its computation.

Architecture of LSTM Memory cell.
At each time step t the memory cell output is computed with output gate function and the current cell state as shown in Equation (7).
The output gate which provides controlled input for output computational function is computed with previous cell hidden state and the input sequence as given by Equation (8)
The current cell state is computed with forget gate function, previous cell state, input gate function, previous cell hidden state and the input sequence as shown in Equation (9)
The forget gate eliminates the unwanted historical information for the memory cell computation and it is computed as shown in Equation (10)
W o , W c , W f , W i are the weights. b0, b c , b f and b i are the biases
Gated Recurrent Unit (GRU) [46] is another variant of RNN which is simpler and faster compared to the LSTM. GRU also solves the vanishing gradient problem due to short term memory dependencies. Each computational unit has two gates - an update gate and a reset gate as shown in Fig. 4.

Architecture of GRU computation unit.
At each i-th unit of GRU at time t the cell state
W z , W c , W r , U z , U c , U r are the weights. b z , b c and b r are the biases.
Thus in our proposed work we use ultrasonic sensor interfaced with Arduino Uno for detecting the vehicles. The collected vehicle count data is sent to the Thingspeak cloud through the ESP8266 WiFi module. The collected data is a time sequence traffic count data. Hence the time series forecasting techniques such as LSTM and GRU are used for traffic prediction and their results are compared.
The IoT enabled traffic data acquisition system is built with HC-SR04 ultrasonic sensor coupled with ESP8266 Node MCU. Multiple sensing stations are deployed across a road section. The average traffic flow data is obtained from all these sensing station deployed in a road section. This method of average flow data acquisition enables the minimization of the error due to the detection of non-vehicle objects.
The system is setup in such a way that the sensors cover a range of about 0.3 to 6 meters. Whenever the sensor detects a vehicle, a counter value is incremented which is initially set to zero. To demonstrate the proposed methodology, two sensing stations built with HC-SR04 ultrasonic sensors and ESP8266 Wifi module considered.
The two sensor stations are placed apart at a distance of 6 meter. The setup is done in such a way that the vehicle passes the sensor station-1 first and then the sensor station-2. The average count detected from these two sensors is estimated to be the vehicle count. Thus, this way of averaging the two sensors count, minimizes the error due to any wrong detection and thereby increases the accuracy of traffic flow data.
The distance data obtained from these ultrasonic sensors for detecting vehicles contains unwanted noises. These noises has to be removed by the preprocessing techniques. The Kalman filtering technique is used for signal conditioning.
This average vehicle count is sent to the cloud through the ESP 8266 WiFi module which acts as the gateway device to the cloud. After successfully uploading the vehicle count to the cloud, the counter value is reset to zero and the counter is incremented again on detecting the vehicles. This process is repeated every one minute in order to obtain traffic flow data as “number of vehicles per minute” and this entire process is illustrated in Fig. 5.

Algorithm for IoT based traffic data acquisition system.
Thus, by determining the average vehicle count passing by every one minute, the average traffic flow of the road shall be determined. The real time traffic data captured using the sensor set up is stored in the cloud. Finally, with this traffic flow data the real time traffic forecasting is made using the deep learning models built with LSTM and GRU RNN. Thus, the traffic forecasting enable intelligent control of city traffic and make the transportation system to be smarter.
Madurai is the Temple city of Tamil Nadu in India. Due to heavy traffic, the roads in the city are very much congested especially during the peak hours. We choose the Iyer Bungalow Road in Madurai, a busy road section in the city for conducting a case study experiment as shown in the map in Fig. 6.

Road section in Google Map on which sensor stations are placed.
Two sensor stations are placed at two different places at about 6 meters apart from each other. The placement of the sensor stations on the road side are shown in Figs. 7 8.

Road side placement of sensor station-1.

Road side placement of sensor station-2.
Each sensor station detects the vehicles passing by and the vehicle count is estimated after signal conditioning with filtering method. The average of the vehicle count is uploaded to the cloud with the ESP8266 WiFi module interfaced with the sensor station. Thus this experimental set up collects and communicate the real time traffic flow data to the Thingspeak cloud for predicting the traffic flow in real time.
The real time average traffic flow data is obtained in the Thingspeak cloud platform through the NodeMCU.
MATLAB R2020b is used for implementing the LSTM and GRU RNN based deep learning models for time series forecasting of traffic data. The observed traffic flow data is plotted in Fig. 9. Initially the data preprocessing is done before applying it to the deep learning model. During pre-processing the mean and standard deviation of the data is estimated. Each data point is subtracted by the mean and divided by the standard deviation for normalizing the data.

Visualized original traffic flow data.
The adam optimizer [47], one of the effective optimization algorithm is used for parameters tuning. This combines both the Adagrad and RMS prop techniques for having an adaptive learning rate for its computation. The dataset is split into 90% training and 10% test dataset. The learning rate is initialized as 0.005 and learning rate drop factor is 0.2. The number of hidden units is chosen as 200 with maximum epochs of 250. Also the bias weights of the LSTM models are regularized in order to eliminate over-fitting problem.
The evaluation of the LSTM and GRU models for our traffic forecast is done with the performance metrics such as: mean square error (MSE), root mean square error (RMSE), mean absolute error (MAE), mean absolute percentage error (MAPE), Pearson Correlation Coefficient (R).
Taken N samples the metrics are calculated with actual of traffic flow data A and forecasted traffic flow F, the metrics can be estimated by the equations Equations (16)–(21).
μ A , σ A are mean and standard deviation of actual traffic flow data. μ F , σ F are mean and standard deviation of forecasted traffic flow data.
The performance metrics are calculated for both GRU and LSTM forecast results. These metrics are compared with ARIMA, SARIMA, SVM, CNN models for the whole day data as shown in the Table 1.
Comparison of performance metrics for whole day traffic data
Similarly, the experiments are conducted with peak time dataset which are collected during congested hours for a week. The metrics for the forecast results based on morning peak time data collected between 9.00 A.M. to 10 A.M. is illustrated in Table 2.
Comparison of performance metrics for morning congested hour traffic data
Also, the performance metrics for the forecast results based on evening peak time data collected between 5.00 P.M. to 6 P.M. is illustrated in Table 3.
Comparison of performance metrics for evening congested hour traffic data
The observed and forecasted traffic flow for the whole day data using 1D-CNN, GRU and LSTM are shown in the Fig. 10(a)–(c), respectively. Also the forecast results for the morning peak hour data using 1D-CNN, GRU and LSTM are shown in the Fig. 11(a)–(c), respectively. The forecast results for the evening congested hour using using 1D-CNN, GRU and LSTM are shown in the Fig. 12(a)–(c), respectively.

Visualized observed and forecasted traffic flow for whole day data using –(a) 1D-CNN (b) GRU (c) LSTM.

Visualized observed and forecasted traffic flow for morning congested hour data using –(a) 1D-CNN (b) GRU (c) LSTM.

Visualized observed and LSTM forecasted traffic flow (a) Morning congested hour data (b) Evening congested hour data.
The correlation coefficients are 0.49, 0.61 and 0.49 for 1D-CNN forecast with whole day, morning peak and evening peak hour data set respectively. Similarly, the correlation coefficients are 0.56, 0.66 and 0.55 for GRU forecast with whole day, morning peak and evening peak hour data set respectively. The correlation coefficients are 0.61, 0.69 and 0.59 for LSTM forecast with whole day, morning peak and evening peak hour data set respectively. The correlation coefficients value clearly shows that the forecast results of LSTM and GRU RNN models possess more correlation with actual values.
The MSE is 56.5, 67.5, 117.8 for 1D-CNN forecast; 53.2, 64.2, 111.6 for GRU forecast and 43.4, 62.6, 109.4 for LSTM forecast, for the three datasets –whole day, morning peak and evening peak respectively. The RMSE is 7.7, 9.1, 11.5 for 1D-CNN model; 6.7, 8.6, 10.9 for GRU model and 5.8, 7.9, 10.2 for LSTM model for those scenarios. The MAE is 5.9, 12.8, 16.8 for 1D-CNN; 5.6, 11.6, 15.1 for GRU and 4.3, 10.8, 11.8 for LSTM for the three datasets. The MAPE is 6.3, 13.9, 14.1 for 1D-CNN; 5.9, 9.8, 13.9 for GRU and 5.1, 8.3, 11.1 for LSTM for the three cases. These results show that the RNN based models such as LSTM and GRU show good performance metrics. In particular, the LSTM model provides good forecast results compared to GRU model.
In this study, a cost effective IoT system is built with easily deployable sensing station for obtaining the traffic data. The system collect the real time traffic flow data and store it in the cloud platform. The traffic flow forecasting is then done using LSTM and GRU RNN based deep learning models. The prediction results show good performance metrics with RMSE of 5.8, 7.9, 10.2 for LSTM model and 6.7, 8.6, 10.9 for GRU model for three different scenarios such as whole day, morning congested hour and evening congested hour datasets. Thus our proposed IoT based traffic forecasting system really exhibits less complex implementation in real time with reduced cost and improved forecast accuracy. As a future work, other sensing methods will be implemented and explored with a wide range of deep learning algorithms to build a reliable and robust traffic forecasting system.
Footnotes
Acknowledgment
The authors like to thank our Institute, Thiagarajar College of Engineering, Madurai for the support to carry out this research work.
