File Exchange

image thumbnail

Analyzing Traffic Using a Webcam, a Raspberry Pi and ThingSpeak

version (919 KB) by MathWorks Internet of Things Team
Develop a traffic monitoring algorithm and analyze traffic data sent to ThingSpeak


Updated 05 Jan 2018

View Version History

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 (2021). Analyzing Traffic Using a Webcam, a Raspberry Pi and ThingSpeak (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (28)

Sreela Paul

Sanjib Narzary

Jacobo Sánchez Coba

Onur Dora ÖZTÜRK


sa babuş

Tarik Dandin


popat jadhav

phil phil

raspeberry pi sensehat

Vishnu P Kumar

Shreif Ahmed

Oliver Mörth

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?

avadhesh verma


charushila raskar

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),[]));

charushila raskar

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.

charushila raskar

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.

Hans Scharler

Thanks for including the model!

Matthew Zwetolitz

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

Muhammad Kalifardi

hello sir, can you ad simulink model please?

charushila raskar

how to get the total no of vehicles

Neven Lazarov

You forgot to include the model



Where is the simulink model?


Omar Madrigal

please add the model


+1 on looking for the missing model

saeed amindin

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

Liu Hongxiang

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.

MATLAB Release Compatibility
Created with R2015a
Compatible with any release
Platform Compatibility
Windows macOS Linux
More Files in the  ThingSpeak Community

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!