How to turn your Raspberry Pi Zero into a Webcam

This discussion has a more recent version.
Minoso
Minoso ✭✭
Second Anniversary First Comment
edited June 2020 in Hobby Boards & Projects

We all know how hard it can be sourcing a webcam right now, and while there are some alternatives like the Wyze Cam V2 we did a guide on, here’s another fun one, especially for those of you who are DIY inclined! In this guide we will work through converting a Raspberry Pi Zero into a webcam that you can plug in as if it were made to come that way. There are two different methods: a simple one using a pre-made kit Micro Center created, and a do-it-yourself guide for those of you who want see exactly how it all works


The method using the pre-made image is the simpler of the two, because we have already created a custom Raspbian OS image with all the coding and setup done already for this purpose. Pretty much all you have to do is download it, write it to a microSD card, then put it all together and plug it in.

Click here to be taken to the guide using our pre-made image


The DIY guide is much more in depth, and will cover everything needed to do this from scratch. All the steps used in the DIY guide are what we used to make the custom image ourselves, so if you want to do all the coding and see how it works, you can take it step by step. 

Click here to be taken to the DIY guide

Comments

  • TSTonyV
    TSTonyV ✭✭✭✭✭
    1000 Comments Fourth Anniversary 250 Likes 25 Answers
    edited June 2020

    How to set up the Raspberry Pi Zero W Webcam using our premade Image

    If you're new to Raspberry Pi doing something like this may seem a little daunting at first. As such, Micro Center decided to create a custom Raspbian image with everything already configured for you. You just need to assemble it, install the software, and you'll be good to go!

    Hardware you will need:

    1. Any Pi Zero model
    2. Pi camera
    3. Raspberry Pi Zero Case
    4. USB Type-A to Micro USB Type-B cable
    5. Windows PC
    6. SD card adapter (If your computer does not have one)
    7. microSD card
    Software you will need:
    1. Raspberry Pi Imager (Download and install)
    2. Custom Raspbian image (Save for later)
    3. Bonjour Services (Download and install)
    4. IP Camera Adapter (Download and install. Ignore any errors saying a module failed to install, just press ignore and finish the installation.)
    Preparing our custom image for the Pi:
    Note: Micro Center did have some pre-made kits available for purchase in store, with all the hardware included, along with a microSD card that already has the Raspbian image on it. If you purchased that kit, skip down to Assembling the Pi
    1. Insert the microSD card into your computer. You may need a USB microSD card reader if your system doesn’t have an SD card slot built-in
    2. Open Raspberry Pi Imager and select Choose OS
    3. Scroll to the bottom and choose Use custom. Navigate to where you downloaded our custom Raspbian image, select picam.zip and click open.
    4. Select your microSD card (MAKE SURE TO SELECT THE RIGHT ONE – Selecting the wrong one could result in data loss)
    5. Select Write, this will flash the microSD card and verify it.
    6. Once this process is done, you'll get a prompt letting you know it can be removed from the computer.
    7. Remove your SD card and insert it into your Pi.

    Assembling the Pi

    First we need to assemble everything. Our Pi case comes with a few swappable top pieces, but we are going to be using the top with a hole in the middle.

    1. Take your Pi camera and detach the cable that comes with it. To do this, you must push up both sides as shown below.


    2. Lightly pull the cable out. Now we need to insert the cable that came with our case.

    3. Insert the larger side of the cable into the camera interface with the pins facing the same side as the camera.


    4. Push the piece back down so that it locks the cable in place.

    5. Now unlock the Pi’s camera interface and insert the smaller end of the cable. The pins should be facing the back side of the Pi.


    6. Once we have the cable pushed in, lock it down as we did with the camera interface. Put in the microSD card, then we can insert our Pi into our case. (It may be a tight fit, that is normal). Insert it so the ports on the PI match up with the holes in the case. The Pi should lock in place and fit snug in our case by pushing it down, there are a few pegs that line up with the holes on each corner of the Pi. The cable may need to be bent to fit in, but don’t worry about that, the cables are made to handle this. Now grab the top we mentioned earlier and fit the camera into it. It will only fit one way, so once we have that seated, we can pull the top onto the pi to finish assembling the case. The final product should look like this before closing the lid. 

     

    7. We need to download IP Camera Adapter 3.1. Go to https://ip-webcam.appspot.com/ and click download, then run the .msi file it downloads. If it pops an error saying a module failed to install just press ignore and finish the installation. Now we just need to open it up and point it to our Pi. Type camera adapter into your taskbar and open Configure IP Camera Adapter


    8. In the Camera feed URL, type or copy/paste raspberrypi.local:8080/?action=stream and click Apply and OK.  If this works, it’s time to test this out for real!


    Open up your app of choice.  If using Skype, make sure you download the standalone desktop client from https://www.skype.com/en/ (Microsoft Camera and other default Windows 10 apps cannot see our MJPEG camera device, so the built-in version of Skype in Windows 10 will not work). Make sure your camera is set to MJPEG Camera to show you the proper feed. Once set, you’ll see the Pi Camera and that’s it. Enjoy your webcam!

  • TSTonyV
    TSTonyV ✭✭✭✭✭
    1000 Comments Fourth Anniversary 250 Likes 25 Answers
    edited June 2020

    DIY Pi Zero Webcam Setup

    For those of you who want to learn how to do the setup yourself, this is a comprehensive guide that will walk you through all the steps to turn your Raspberry Pi Zero into a webcam. This is the same process we used to create the microSD cards in the pre-made bundle. We'll go over all the software and tools used, as well as how to configure the Raspbian OS properly to make it all work. 

    Hardware you will need:

    1. Any Pi Zero model
    2. Pi camera
    3. Raspberry Pi Zero Case
    4. USB Type-A to Micro USB Type-B cable
    5. Windows PC
    6. SD card adapter (If your computer does not have one)
    7. microSD card

    Software you will need:

    • From your PC (we are using a PowerSpec running Windows 10 Pro for ours) navigate to https://downloads.raspberrypi.org/raspbian_lite_latest  and download Raspian Lite.  This will download a zip file containing the operating system for the Raspberry Pi Zero.
    • We will be using Etcher to burn our Raspbian image to our microSD card. Etcher will work on Windows, Linux, and macOS and can be downloaded from here https://www.balena.io/etcher

    Preparing the microSD card for the Pi:

    1. Insert the microSD card into your computer. You may need a USB microSD card reader if your system doesn’t have an SD card slot built-in

    2. Open Etcher and press Select image


    3. Navigate to where you downloaded the Raspbian Lite image, select the .zip file and click open

    4. Select your microSD card (MAKE SURE TO SELECT THE RIGHT ONE – Selecting the wrong one could result in data loss)

    5. Click Flash! Then click OK on the User Account Control prompt. This may take a few minutes to complete depending on your hardware, maybe go grab a coffee while you wait.   


    6. Eject the microSD card/drive and re-insert it.

    7. A new drive should appear called Boot.  If it does not automatically open, navigate to File Explorer or My Computer and open the drive (it should be listed on the left). We need to modify two specific files: cmdline.txt and config.txt

    • Note:  If you are prompted to format the drive click cancel.  You may also be prompted telling you the drive is not accessible, if so hit OK. 

    8Right-click and open config.txt with Wordpad and add this to the bottom: dtoverlay=dwc2Make sure you save before you close it.

    Or…

    You can copy the snippet of code below and paste it in replacing what is in your file.
    # For more options and information see
    # http://rpf.io/configtxt
    # Some settings may impact device functionality. See link above for details

    # uncomment if you get no picture on HDMI for a default "safe" mode
    #hdmi_safe=1

    # uncomment this if your display has a black border of unused pixels visible
    # and your display can output without overscan
    #disable_overscan=1

    # uncomment the following to adjust overscan. Use positive numbers if console
    # goes off screen, and negative if there is too much border
    #overscan_left=16
    #overscan_right=16
    #overscan_top=16
    #overscan_bottom=16

    # uncomment to force a console size. By default it will be display's size minus
    # overscan.
    #framebuffer_width=1280
    #framebuffer_height=720

    # uncomment if hdmi display is not detected and composite is being output
    #hdmi_force_hotplug=1

    # uncomment to force a specific HDMI mode (this will force VGA)
    #hdmi_group=1
    #hdmi_mode=1

    # uncomment to force a HDMI mode rather than DVI. This can make audio work in
    # DMT (computer monitor) modes
    #hdmi_drive=2

    # uncomment to increase signal to HDMI, if you have interference, blanking, or
    # no display
    #config_hdmi_boost=4

    # uncomment for composite PAL
    #sdtv_mode=2

    #uncomment to overclock the arm. 700 MHz is the default.
    #arm_freq=800

    # Uncomment some or all of these to enable the optional hardware interfaces
    #dtparam=i2c_arm=on
    #dtparam=i2s=on
    #dtparam=spi=on

    # Uncomment this to enable infrared communication.
    #dtoverlay=gpio-ir,gpio_pin=17
    #dtoverlay=gpio-ir-tx,gpio_pin=18

    # Additional overlays and parameters are documented /boot/overlays/README

    # Enable audio (loads snd_bcm2835)
    dtparam=audio=on

    [pi4]
    # Enable DRM VC4 V3D driver on top of the dispmanx display stack dtoverlay=vc4-fkms-v3d 
    max_framebuffers=2 console=serial0,115200 console=tty1 root=PARTUUID=738a4d67-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait modules-load=dwc2,g_ether

    [all]
    #dtoverlay=vc4-fkms-v3d
    dtoverlay=dwc2

    9.   Next open up cmdline.txt and add this between rootwait and quiet: modules-load=dwc2,g_ether. Save and exit. 
    • Note: If you do not see "quiet" you likely have booted this SD card in the pi.  Just add the code to the end like we did previously

    10.   Make sure your folder view in File Explorer allows you to see file extensions. Go to View at the top and check “file name extensions.”

     

    11.   Next, we need to create a new file called SSH

    • With the Boot drive opened, right click anywhere within it, select new and select text document
    • Name the new .txt file SSH, then make sure you delete the .txt extension after it. You’ll get a prompt saying if you change the file name extension, it may be unusable. That’s fine, click Yes to confirm the change. The file should just show as SSH now, this will allow us to connect via SSH once our Pi fully boots. (more on that later)

     

    12.   Eject your microSD card and insert it into your Pi. 

    Setting up the Camera

    Now we need to assemble everything. Our Pi case comes with a few swappable top pieces, but we are going to be using the top with a hole in the middle.

    13.  Take your Pi camera and detach the cable that comes with it. To do this, you must push up both sides as shown below.


    14.   Lightly pull the cable out. Now we need to insert the cable that came with our case.

    15.   Insert the larger side of the cable into the camera interface with the pins facing the same side as the camera.


    16.   Push the piece back down so that it locks the cable in place.

    17.   Now unlock the Pi’s camera interface and insert the smaller end of the cable. The pins should be facing the back side of the Pi.


    18.   Once we have the cable pushed in, lock it down as we did with the camera interface.  Now we can insert our Pi into our case. (It may be a tight fit, that is normal). Insert it so the connectors match the ports on the case. The Pi should lock in place and fit snug in our case by pushing it down, there are a few pegs that line up with the holes on each corner of the Pi. The cable may need to be bent to fit in, but don’t worry about that, the cables are made to handle this. Now grab the top we mentioned earlier and fit the camera into it. It will only fit one way, so once we have that seated, we can pull the top onto the pi to finish assembling the case. The final product should look like this before closing the lid. 

     

    19.   Now it’s time to boot up our Pi for the first time. We need to plug our USB to microUSB cable into the computer and into our Pi. Since our Pi has two micro USB ports, we will be using the port closest to the middle so we can SSH into it from our computer. SSH is how we interface with the Pi over a data connection, think of it like a gateway to our terminal. Since Windows doesn’t have an SSH client, we will need to download one. The most common SSH client is known as Putty, and it can be downloaded here: https://the.earth.li/~sgtatham/putty/latest/w64/putty.exe

    •  Make sure you save it somewhere you can easily access it, as this is not an installer, it’s a standalone .exe file so you can run it from anywhere. Once its downloaded, run it and you will see a screen like this:

                                                                  

    20. Yours will be empty if this is your first time opening it. First, we need to create a new connection. To do this, type a name for your pi into the saved sessions box, then type raspberrypi.local into the hostname box and click save. If done correctly you will see your new saved session below the saved sessions box. 

    21. Double click on the name you gave it and you should now be presented with a warning of a potential security breach.  This is normal to see if this is your first time connecting to your Pi. Once you click yes, it will not pop up again. Now it’s time to sign into your Pi. Raspbian’s default user is pi and default password is raspberry. While typing your password, it won’t show you anything on the screen. This is done for security reasons, so enter raspberry and press enter and you’ll be logged into your Pi. Now we need a network to continue.

    22. With your Pi still powered on and connected to our computer, search in the taskbar for network status and open Network Status. You can also click your Start Button, go to Settings, then open Network and Internet and you’ll be in the status menu. Click Change adapter options and a new window will open up and show you all of your network adapters. One of them will have “USB Ethernet/RNDIS Gadget” under the name. Remember which connection show that because that is the connection to our Pi. My pi’s connection name is Ethernet 4 and my primary internet connection is named Ethernet 1.

    23. Right click on your primary internet adapter and click properties. At the top you will see sharing. Click this and check the top box to allow other network users to connect through it and select your Pi’s connection. Click ok to close the window. Now unplug the pi and plug it back in. This will reboot your Pi and share your connection. This will also terminate our SSH session. For now, we will close Putty.

    • Note: If you are familiar with Linux, you can bring the interface down and back up, but for the sake of simplicity, we will just unplug and plug it back in.

    24. It may take up to 90 seconds for our Pi to fully reload. Open Putty again and double click our sessions name to log back in. Same details as last time, pi for username and raspberry for password.  Now we can move onto installing the required software. Type the following command and hit enter:

    • sudo apt-get update && sudo apt-get install cmake git libjpeg8-dev -y

    25. After our above command finishes, we need to type and run sudo raspi-config and navigate to interfacing options. Select Camera and enable it. Navigate to finish and press enter to save everything. It will ask to reboot, select yes and let it reboot, then restart Putty.

     26. Now we need to get mjpg-streamer. We will be using this to stream our webcam via our network on the Pi. Run the following command:

    27. Once that finishes, run this command:

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

    28. Once that command finishes, we need to run a test to ensure it works. While in the ~/mjpg-streamer/mjpg-streamer/experimental directory, run this command:

    • mjpg_streamer -i input_uvc.so -o output_http.so

    29. If you see a red light on your camera, you’re live! It’s time to set it up so our camera starts automatically when we plug in the Pi. We need to first close out of our running stream. To do this, hold down ctrl and press C. Wait a few minutes and you should see our pi@raspberry:~ $ line again. If not, hold ctrl and press to get back to that line.

    30. Now we need to make a script to run at startup. Type and run nano ~/cam.sh, then paste in the following.

    #!/bin/bash
    cd /home/pi/mjpg-streamer/mjpg-streamer-experimental
    mjpg_streamer -i input_uvc.so -o output_http.so

    Make sure the lines match up exactly as they are listed here. Now hold ctrl and press X. It will ask if you wish to save, press y for yes and enter to save and close.

    31. Run the command chmod +x ~/cam.sh , then run the command crontab -e

    32. It will ask which editor you prefer, press 1 for nano and enter to confirm. Now you will see our Pi user’s crontab. At the top, press enter to make a new line, and add PATH=~/bin:/usr/bin/:/bin:/usr/local/bin/, then go to the bottom of the file and enter @reboot /home/pi/cam.sh, like so:

     PATH=~/bin:/usr/bin/:/bin:/usr/local/bin/
    # Edit this file to introduce tasks to be run by cron.
    #
    # Each task to run has to be defined through a single line
    # indicating with different fields when the task will be run
    # and what command to run for the task
    #
    # To define the time you can provide concrete values for
    # minute (m), hour (h), day of month (dom), month (mon),
    # and day of week (dow) or use '*' in these fields (for 'any').
    #
    # Notice that tasks will be started based on the cron's system
    # daemon's notion of time and timezones.
    #
    # Output of the crontab jobs (including errors) is sent through
    # email to the user the crontab file belongs to (unless redirected).
    #
    # For example, you can run a backup of all your user accounts
    # at 5 a.m every week with:
    # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
    #
    # For more information see the manual pages of crontab(5) and cron(8)
    #
    # m h  dom mon dow   command
    @reboot /home/pi/cam.sh

    33. Just like our cam file, hold ctrl and press X, then hit and enter to save and exit. Now we just need to reboot the pi to confirm it works. Run sudo reboot and wait for it to reboot. In about 30 seconds, your camera should light up red again. This means our script works and now we can work on getting our camera streaming to our PC.

    34. We need to download IP Camera Adapter 3.1. Go to https://ip-webcam.appspot.com/ and click download, then run the .msi file it downloads. If it pops an error saying a module failed to install just press ignore and finish the installation. Now we just need to open it up and point it to our Pi. Type Camera into your taskbar and open Configure IP Camera Adapter


    35. Now in the Camera feed URL, enter raspberrypi.local:8080/?action=stream and click Apply and OK.  If this works, it’s time to test this out for real!


    Open up your app of choice.  If using Skype, make sure you download the standalone desktop client from https://www.skype.com/en/ (Microsoft Camera and other default Windows 10 apps cannot see our MJPEG camera device, so the built-in version of Skype in Windows 10 will not work). Make sure your camera is set to MJPEG Camera to show you the proper feed. Once set, you’ll see the Pi Camera and that’s it. Enjoy your webcam!

     



  • We also have another Raspberry Pi article on our forum. Be sure to check it out!
    How to make a NAS with a Raspberry Pi using Linux (2020) 

We love seeing what our customers build

Submit photos and a description of your PC to our build showcase

Submit Now
Looking for a little inspiration?

See other custom PC builds and get some ideas for what can be done

View Build Showcase

SAME DAY CUSTOM BUILD SERVICE

If You Can Dream it, We Can Build it.

Services starting at $149.99