File Exchange

image thumbnail

Analyzing Traffic Using a Webcam, a Raspberry Pi and ThingSpeak

Develop a traffic monitoring algorithm and analyze traffic data sent to ThingSpeak


Updated 05 Jan 2018

View License

In this IoT project, we use a Raspberry Pi, a web cam and ThingSpeak to analyze traffic on a busy highway. With Simulink we design and deploy a traffic monitoring algorithm to the Raspberry Pi, and we analyze and visualize the traffic patterns using ThingSpeak, an IoT analytics platform, and MATLAB. This article features channel 38629 on ThingSpeak.
For more information about ThingSpeak, see

Cite As

MathWorks Internet of Things Team (2020). Analyzing Traffic Using a Webcam, a Raspberry Pi and ThingSpeak (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (26)


phil phil

raspeberry pi sensehat

Hello, I am using a Raspberry Pi model B and a LifeCam Studio (Microsoft) as the webcam. By increasing the resolution, I can identify more image errors (parts of the image are mixed up and not in the right position) and the counting doesn't work properly. Does anyone please know why this happens and how to solve this problem?

When I run the m file getting following error but graph appears for Traffic Density for the week starting at a particular day.

Error using reshape
Product of known dimensions, 5655, not divisible into total number of elements, 39586.

Error in traffic_analysis_final_updated_old (line 176)
dailymeaneast = mean(reshape(eastTraffic, floor(length(alltrafficData)/7),[]));

Thanks a lot M.Khan.Its working.


SDL Video Display and FinalOutput are for debugging purpose only. To view these two displays, you need to enable the subsystem 'raspberrypi_viptrafficfd3_Density/Car Density/Enabled Subsystem'. To enable this subsystem, double click on the block 'raspberrypi_viptrafficfd3_Density/Car Density/Debug Flag' and set the Constant Value to 1. Redeploy the generated code and it should display the outputs.

Thanks for including model. when I run the model on Raspberrypi I am not getting the SDL video display output and the final output..but get the camera output.Please help.

Thanks for including the model!

has anyone re-created this model or received the original to run the experiment??

hello sir, can you ad simulink model please?

how to get the total no of vehicles

You forgot to include the model



Where is the simulink model?


please add the model


+1 on looking for the missing model

the model missing. please add a new file with model for downloading.

where can I find this model ,I'm very intereted in this project!


It seems that you've forgotten to share the Simulink model used for car detection.


Updated traffic monitoring algorithm and added Simulink model to the submission.

Updated description.

Updated license

Initial release.

MATLAB Release Compatibility
Created with R2015a
Compatible with any release
Platform Compatibility
Windows macOS Linux