How to Use Raspberry Pi as a PC Webcam
(Image credit: Tom’s Hardware)
As I write this, there’s a shortage of USB webcams and industry-leader Logitech has said that it will be four to six weeks before there’s adequate supply. However, it’s still easy to get a Raspberry Pi camera module, if you don’t already have one, and use your Raspberry Pi as a webcam for your PC.
While there are a few potential ways to get video from your Raspberry Pi to appear on your PC in Skype, Meet, Zoom or other conferencing apps, the only method I’ve found that works is to stream the Pi’s video using mjpg_streamer and then receive it on the Windows (or Linux or macOS) side where another app makes the OS think that the stream is a webcam. After combing through a lot of resources, including this very helpful thread, I see that there was at one time a way to have a Pi Zero become a literal USB webcam in Windows, but it doesn’t appear to work with recent builds of Raspbian.
Mục lục
What You’ll Need
- A camera module: Raspberry Pi Camera modules use a ribbon cable to connect to the Pi’s proprietary CSI camera port. They cost as little as $10, but we recommend using one of the better-quality models such as the official Raspberry Pi Camera Module V2 which goes for around $25 (we used cheap models for our tests and the low-light performance was not good). Most of these modules don’t come with a mounting bracket so you’ll need to get creative (perhaps with tape) and / or buy a case in order to position one on top of your monitor or in another optimal position.
- A Raspberry Pi: You can use a Raspberry Pi Camera Module with any recent-era Raspberry Pi, but the Raspberry Pi Zero / Zero W might be the best choice. While more powerful models such as the Raspberry Pi 4 B may be able to handle higher frame rates with aplomb, the most important thing is the strength of the connection between the Pi and your PC. With a Zero / Zero W, you can do a direct USB connection to your PC, but with other models, you’ll need to use Ethernet or Wi-Fi. The faster and more reliable the connection, the better chance you’ll have to limit lag and freezing pictures.
- A microSD Card with Raspbian: It almost goes without saying that you’ll need the official Raspberry Pi OS. If you don’t already have one set up, see our guide on how to do a headless install on Raspberry Pi so you can control it from your computer.
Installing the Raspberry Pi Camera Module
If you already have a Raspberry Pi camera module installed or know how to use one, then you can skip down to the next section and step 6.
Raspberry Pi 3 and 4 have two different serial interface (ribbon cable) ports, one for the camera and another that can be used for some displays. On the 3 and 4, the CSI (camera serial interface) port is the one located between the 3.5mm audio jack and the HDMI port(s).
(Image credit: Tom’s Hardware)
On Pi Zero / Zero W, the camera port is the only ribbon-cable port and it’s located on the opposite side of the board from the microSD card slot.
(Image credit: Tom’s Hardware)
The standard-size ribbon cable that comes with most camera modules is too wide to fit into the narrower port on the Pi Zero / Zero W. So, if you plan to use one of those, you’ll need a camera cable that’s designed specifically for Pi Zero (some cameras come with them and others require you to buy them separately).
Inserting the cable into the port is a very delicate operation.
1. Lift and loosen the plastic cuff on top of the port, without pulling it out. If you pull the cuff out, you should be able to pop it back in, but on one Pi Zero where I did this, the plastic broke.
2. Slide the ribbon cable into the port with the metal teeth facing the HDMI ports on Pi 3 / 4 or facing down on a Pi Zero.
3. Snap the cuff down to hold the cable in place. You should keep pushing the cable into the port while you push the cuff in. If the cable is even a little loose, it will slide out or it may just not work.
(Image credit: Tom’s Hardware)
4. Activate the camera module in Raspbian. To do that, launch raspi-config at the command line, navigate to the Interfaces menu and toggle Camera to on. You will be asked to reboot.
(Image credit: Tom’s Hardware)
5. Take a picture to make sure the camera works. Do this by typing this at the command line.
raspistill -o mytest.jpg
If you are in the windowed environment, you’ll see an image pop up on screen for a couple of seconds. If not, you can always try viewing the JPG file you created.
(Image credit: Tom’s Hardware)
Getting Your Best Connection between Pi and PC
6. Connect your PC to your Pi. There are four main ways to connect your Raspberry Pi to your PC:
- Direct USB Connection (Pi Zero / Zero W only): This is probably the best connection. To make this work, add the text dtoverlay=dwc2 to the bottom of /boot/config.txt and add modules-load=dwc2,g_ether after the word rootwait in /boot/cmdline.text. Install Bonjour Printer Services
(opens in new tab)
if you’re using Windows. Your Pi Zero must be connected to a USB port on your PC via the micro USB port that’s labeled “USB” on the Pi itself. Make sure to enable Internet sharing on your PC (see below).
- Wi-Fi: Make sure that both the PC and the Raspberry Pi are on the same exact wireless network. Using a Raspberry Pi that has 802.11ac (the Pi 4 B or Pi 3 B+) helps.
- Ethernet: Plug your Raspberry Pi into your router.
- Direct Ethernet Connection: You can run an Ethernet cable directly from your Raspberry Pi to your PC. If your PC doesn’t have an Ethernet port or its port is already in use, you can use a USB-to-Ethernet adapter. Though this method seems like it would be the best, it actually was the slowest in my tests. Also, for some reason, I had to enter my Raspberry Pi’s IP address rather than its host name (ex: raspberrypi) in order to access the stream.
If you’re using a direct USB or Ethernet connection to your desktop or laptop, you will need to share your PC’s Internet connection with the Raspberry Pi. To do this in Windows, navigate the network connections menu in control panel, first right click on your PC’s Ethernet connection, select properties.
(Image credit: Tom’s Hardware)
Then enable sharing in the Share tab and select the Ethernet connection your Pi is using (even the Pi Zero’s USB connection is labeled as an Ethernet connection here).
(Image credit: Tom’s Hardware)
Setting Up Streaming From mjpg-streamer
7. Enter the code below at the command prompt to install a required library.
sudo apt-get install cmake libjpeg8-dev -y
8. Enter the following code to install more prerequisite software. There’s a good chance it is already installed, particularly if you have the latest version of Raspbian.
sudo apt-get install gcc g++ -y
9. Clone, Compile and install mjpg-streamer by typing the following commands
git clone https://github.com/jacksonliam/mjpg-streamer
cd mjpg-streamer/mjpg-streamer-experimental && make && sudo make install
Testing mjpg_streamer Settings
10. Test different mjgp_streamer settings to see what works. At this point, the mjpg_streamer is installed but you need to figure out what camera resolution and framerate work best with your Raspberry Pi model and level of network connectivity.
Start with the lowest-level settings, which you can invoke by entering the following:
mjpg_streamer -i “input_raspicam.so” -o output_http
You should get a message that shows it is active.
(Image credit: Tom’s Hardware)
To test the quality of your broadcast, go to your PC, open a browser and navigate to http://[raspberrypihostname]:8080?action=stream. If you haven’t changed the default hostname on your Pi, it will be http://raspberrypi:8080/?action=stream.
(Image credit: Tom’s Hardware)
Now, it’s time to try turning up the resolution and frame rate. You can stop the mjpg_stream by hitting CTRL + C in the terminal window. However, if you test a resolution and you get an error message, you’ll need to reboot the Raspberry Pi before you try again. Even if you don’t get an error, your Pi may not exit the app and you’ll need to restart.
You can set the resolution by adding the parameters -x [horiz res] ] -y [vertical res] -fps [frames per second]. To get a 720p, 30 fps stream, enter:
mjpg_streamer -i "input_raspicam.so -x 1280 -y 720 -fps 30" -o output_http.so
I found that 1920 x 1080 (full HD) worked on my camera on a Raspberry Pi Zero W, but it was sluggish, even at 30 fps. I also was able to do 60 fps at 720p, but the image was dark. So 720p at 30 fps might be the best bet for most people.
Create a Bash Script to Start mjpg_streamer
Once you’ve settled on the settings you like, you should create a bash script that makes it easy to launch with a simple command.
11. Create a new file called startcam.sh by entering:
sudo nano startcam.sh
12. Enter your code. It’s #!/bin/bash in the top line, followed by your mjp_streamer launch line. For example:
#!/bin/bash
mjpg_streamer -i "input_raspicam.so -x 1280 -y 720 -fps 30" -o output_http.so
(Image credit: Tom’s Hardware)
Hit CTRL+X to exit and hit Y to save
13. Make startcam.sh executable by typing:
sudo chmod a+x startcam.sh
You can now launch it anytime you want by typing the following at the command prompt when you’re in your home directory.
./smartcam.sh
14. Add startcam.sh to the bottom of your /etc/rc.local file, above the exit 0 command, if you want it to run every time you boot the Raspberry Pi. Remember to use the full path name (ex: /home/pi/startcam.sh).
(Image credit: Tom’s Hardware)
Setting Up Your Windows PC
15. Download and Install IP Camera Adapter.
16. Launch Configure IP Camera Adapter.
17. Enter the camera feed URL, most likely http://raspberrypi:8080?action=stream and click the Autodetect button to test it and grab the resolution.
(Image credit: Tom’s Hardware)
18. Select MJPEG Camera in the settings for your favorite video conferencing apps.
(Image credit: Tom’s Hardware)
MJPEG Camera should appear as an option in most conferencing apps, including Skype, Google Meet and Zoom. Note that, for Skype, I found that MJPEG Camera was available in version 8.58 of Skype Desktop, but not in Skype for Windows 10.
You may find that, when you’re chatting, the video isn’t perfectly synchronized with your audio. The more you turn down the resolution and fps, the smoother the video is likely to be. Also, a better connection between your PC and your Raspberry Pi will improve the quality.
More Raspberry Pi Tutorials