How to Use a Web Cam with Raspberry Pi

The Raspberry Pi comes with a camera connector on board. But if you don't want to buy the official camera and use a web camera instead, you can! I'll show you how to display a video from the webcam on a web server hosted by a Raspberry Pi

For this tutorial, I used Logitech C270 web camera. This camera is directly supported by Raspberry Pi. You can use any other webcam that is listed here.

Software Requirements

First, we download mjpg-streamer:

git clone

We need libjpeg and cmake as well:

sudo apt-get install cmake libjpeg8-dev

You should now have the mjpg-streamer folder. Go to mjpg-streamer-experimental folder and run make and install:

cd mjpg-streamer/mjpg-streamer/mjpg-streamer-experimental
sudo make install

That installs the mjpg-streamer to the Raspberry Pi.

Embedding Video to Web Page

First of all, we need to know the name of the web camera. Go to dev and look for videox where x is any number. For example, mine's video1

Next, we created a script that would run the video stream continuously in the background. On root, create a file named

sudo nano

Then paste the following:


ID="Raspberry Pi Web Cam"

export LD_LIBRARY_PATH=/usr/local/lib
mjpg_streamer -i " -f $FRAMERATE -r $SIZE -d /dev/video1 -y" -o " -w /var/www/html -p $PORT"

This is assuming that your web camera name is video1.

Run the script using the following commands

sudo chmod +x

Now it's time to embed the video to a web page. If you haven't installed Apache, do this:

sudo apt-get install apache2 -y

After installation of Apache server, CD to /var/www/html. In there, you'll see a file named index.html. Delete this file:

sudo rm index.html

Then create a new index.html file

sudo nano index.html

Paste the following:

<h1>Raspberry Pi Web Camera!</h1>
<img src="" />

Here I used because that's my Raspberry Pi's IP address. You should replace that with your own board's address or use raspberrypi.local.

Save the index.html file and open the IP address of the Raspberry Pi using your preferred browser. Here's what mine looks like:

Raspberry Pi Web Camera

That's it! You can now create cool camera applications like image processing or camera robots.

Leave a Reply

Your email address will not be published. Required fields are marked *