• Stars
    star
    1,322
  • Rank 35,559 (Top 0.8 %)
  • Language
  • License
    MIT License
  • Created about 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Automation to configure a Raspberry Pi as a USB OTG webcam

Raspberry Pi Webcam

CI

Raspberry Pi Zero W with HQ Camera and wide-angle lens on Tripod

Inspired by David Hunt's blog post showing how to use a Raspberry Pi Zero with a Pi Camera as a USB Webcam, as well as justinschuldt's gist, I wanted to make my Raspberry Pi do the same thing, but automated and with all the scripts wrapped in version control, since the blog post was a little bit vague in some areas.

This Ansible playbook can be run on most Raspberry Pis to set it up as a USB webcam.

What does that mean? Well, after running the playbook:

  • You plug the Pi into your computer
  • You wait until it boots
  • Bingo! Webcam usable in any software (Zoom, Teams, Meet, OBS, QuickTime, etc.)

It works on Mac, Windows, and all the flavors of Linux I've tested so far. It even works on another Raspberry Pi (I've tested it on a Pi 4 and Pi 400).

Video overview of this project

I published a video in November 2020 detailing how to set up your Pi Webcam using this project. Click the thumbnail below to view it:

Raspberry Pi Zero is a REAL USB webcam

Caveats

The playbook is meant to run on a brand new installation of Raspbian that has not had any configuration changes via raspi-config or any other tools, though it should work correctly with an existing installation.

Note: This modifies your boot config, and as such you should not run this playbook on a microSD or other boot volume you're not ready to reformat and re-flash!

Setting up the Hardware

It doesn't matter if you set up the software or hardware first, you just need to do both to have a functional webcam.

There are a number of different components you can purchase instead of these, but here's the list of what I'm using in my 'official' version that is well-tested and that I use frequently on my Mac and Windows laptops:

Once you have everything, it's a matter of putting it all together.

Check out my video on YouTube for a detailed assembly and setup guide: Raspberry Pi Zero is a PRO HQ webcam for less than $100!

Mounting the webcam

The HQ Camera includes a female tripod socket, so any tripod or mounting arm will do. I've used a variety of mounts depending on the purpose, for example:

Setting up the Software

There are two ways you can run this automated setup. You can either run everything on the Raspberry Pi itself (e.g. if you plug in a keyboard, mouse, and monitor), or you can run it from another computer.

Setup on the Raspberry Pi

Pro Tip: If you want to use a Pi Zero for the webcam, and don't want to wait half an hour for it to run all this automation, you can put the flashed microSD card into a newer Pi (like the Pi 4) and do all this work there, then when it's done pull the card and pop it into a Pi Zero.

  1. Flash the latest Raspberry Pi OS to a microSD card.
  2. Once Raspbian is loaded on the card, insert the card in your Pi, and plug in your Pi to boot it up.
  3. Follow the setup wizard, and if you want to easily be able to log into the Pi later, connect to a WiFi network, and also run the raspi-config tool and enable SSH.
  4. The Raspberry Pi should ask to be restarted. Go ahead and restart now, and wait for it to boot back up.
  5. Open the Terminal application (in the launcher or in Menu > Accessories > Terminal).
  6. Install Ansible and Git: sudo apt update && sudo apt install -y python3-dev python3-pip libyaml-dev libffi-dev git && sudo pip3 install --no-binary pyyaml ansible (Warning: this can take a while, especially on slower Pis!).
  7. Clone this repository to your Pi: git clone https://github.com/geerlingguy/pi-webcam.git
  8. Go into the repository directory: cd pi-webcam
  9. Use the local inventory file: cp inventory-local.example inventory
  10. Run the Ansible playbook: ansible-playbook main.yml
  11. You can shutdown the Pi at this point.

Setup from another Computer

  1. Make sure you have Ansible installed on your computer.

  2. Flash the latest Raspberry Pi OS a microSD card. Make sure you added an ssh file to the boot volume so SSH is enabled on first boot.

  3. Once Raspbian is loaded on the card and you have the ssh file in the boot volume, insert the card in your Pi, and plug in your Pi to boot it up.

  4. Make sure you can log into the Pi via SSH. Ideally, add your SSH key to the Pi using ssh-copy-id. If you don't, make sure to add the -k parameter to the ansible-playbook command later so you can enter the SSH password.

  5. Clone or download this repository to your computer.

  6. Use the ssh inventory file: cp inventory-ssh.example inventory

  7. Update the IP address in inventory to match the IP address or hostname of your Raspberry Pi.

  8. Edit the config.yml file to your liking (the defaults should be fine though).

  9. Run the Ansible playbook:

    ansible-playbook main.yml
    
  10. You can shutdown the Pi at this point (log in via SSH then sudo shutdown now).

Plugging in the Camera

You could've done this earlier, but if you haven't yet, plug the camera module into the Raspberry Pi using the appropriate camera connector.

Using the Pi as a webcam

At this point, it should (assuming everything worked) be set up as a USB webcam.

All you need to do is grab a USB cable and plug the Pi into any USB port on your computer. But which port do you plug into on the Pi? Well, I have a handy little table here:

Pi Model Which USB Port for device mode Webcam use?
Pi Zero / Pi Zero W 'USB' port (next to 'PWR IN')
Pi A/A+ 'USB' port (you need a non standard Type-A to Type-A USB cable)
Pi 4 USB-C 'Power' port

Once it's plugged in, you need to wait 30 seconds or so before the Pi will be booted and fully ready, then you can open any video recording/conferencing software, go to the camera selection, and BOOM! Select the 'Pi Webcam'.

What about the other Pi models? Unfortunately, other Pi models do not have full support for Device mode, so they cannot be used as a webcam.

Powering down the Pi

If you want, and you still have WiFi enabled, or the Pi is otherwise connected to your network, you can log into it via SSH and run sudo shutdown now to power it off cleanly.

But I like living on the edge... it's not like the thing is writing a ton of data to the microSD card. When I'm finished using it as a webcam, I just unplug it. Simple as that.

If, for some strange reason, it did end up getting corrupted, I could just re-run this automation to set it up again. No big deal! I haven't had that happen yet, though.

Known issues

Well... most of the known issues have to do with the other projects this project relies on:

  • If you're not using a Mac, you may need to adjust the brightness setting in the uvc-gadget.c file and re-compile it manually. I'm looking into a better way to allow this to be configured.
  • The upstream repository providing the uvc-gadget application currently defaults to 720p resolution, which is great for most use cases. You can stream at 1080p, though the Pi Zero and older Pis may drop frames at that resolution. Follow this issue for progress making resolution more easily configurable.
  • The Pi 4 model B currently locks up when you try to use USB Device mode and enable the webcam for some reason. You can run the 2020-02-07 Raspbian release to work around this bug for now. Follow this issue for progress resolving this bug.
  • This stuff is a little bit of a complicated ball of string, so future Raspberry Pi OS and kernel updates could cause issues. I would not run this on a Raspberry Pi that is controlling a breathing machine or something like that.

Yadda, yadda, standard "if something breaks don't blame me" disclaimer. The worst thing I've done to my Pi in testing this so far is accidentally breaking off the locking connector for the camera cable. Oops.

License

MIT.

Author

I'm Jeff Geerling, and I approve of this repository.

More Repositories

1

ansible-for-devops

Ansible for DevOps examples.
Python
7,475
star
2

mac-dev-playbook

Mac setup and configuration via Ansible.
Shell
5,906
star
3

internet-pi

Raspberry Pi config for all things Internet.
Jinja
3,690
star
4

macos-virtualbox-vm

Instructions and script to help you create a VirtualBox VM running macOS.
Shell
2,519
star
5

ansible-vagrant-examples

Ansible examples using Vagrant to deploy to local VMs.
2,034
star
6

raspberry-pi-dramble

DEPRECATED - Raspberry Pi Kubernetes cluster that runs HA/HP Drupal 8
Shell
1,656
star
7

ansible-role-docker

Ansible Role - Docker
1,515
star
8

drupal-vm

A VM for Drupal development
Jinja
1,370
star
9

internet-monitoring

Monitor your network and internet speed with Docker & Prometheus
1,237
star
10

raspberry-pi-pcie-devices

Raspberry Pi PCI Express device compatibility database
HTML
1,230
star
11

ansible-role-mysql

Ansible Role - MySQL
Jinja
988
star
12

ansible-role-jenkins

Ansible Role - Jenkins CI
Groovy
802
star
13

ansible-role-nginx

Ansible Role - Nginx
Jinja
774
star
14

ansible-role-security

Ansible Role - Security
Jinja
706
star
15

ansible-role-certbot

Ansible Role - Certbot (for Let's Encrypt)
Shell
698
star
16

ansible-role-gitlab

Ansible Role - GitLab
Jinja
653
star
17

packer-boxes

Jeff Geerling's Packer build configurations for Vagrant boxes.
Shell
635
star
18

ansible-for-kubernetes

Ansible and Kubernetes examples from Ansible for Kubernetes Book
Shell
612
star
19

my-backup-plan

How I back up all my data.
Shell
576
star
20

dotfiles

My configuration. Minimalist, but helps save a few thousand keystrokes a day.
Shell
540
star
21

ansible-role-firewall

Ansible Role - iptables Firewall configuration.
Shell
481
star
22

ansible-role-postgresql

Ansible Role - PostgreSQL
Jinja
478
star
23

kubernetes-101

Kubernetes 101 - by Jeff Geerling
HTML
477
star
24

ansible-role-php

Ansible Role - PHP
Jinja
468
star
25

ansible-role-kubernetes

Ansible Role - Kubernetes
Jinja
425
star
26

Ping

A PHP class to ping hosts.
PHP
415
star
27

ansible-role-apache

Ansible Role - Apache 2.x.
Jinja
392
star
28

ansible-role-nodejs

Ansible Role - Node.js
Jinja
386
star
29

turing-pi-cluster

DEPRECATED - Turing Pi cluster configuration for Raspberry Pi Compute Modules
Jinja
347
star
30

awx-container

Ansible Container project that manages the lifecycle of AWX on Docker.
297
star
31

ansible-role-java

Ansible Role - Java
Jinja
293
star
32

ansible-role-ntp

Ansible Role - NTP
Jinja
290
star
33

pi-timelapse

Time-lapse app for Raspberry Pi computers.
Python
278
star
34

temperature-monitor

Raspberry Pi-based home temperature monitoring network.
JavaScript
256
star
35

pi-cluster

Raspberry Pi Cluster automation
Shell
231
star
36

ansible-role-awx

DEPRECATED Ansible Role - AWX
230
star
37

ansible-role-homebrew

Ansible Role - Homebrew (MOVED to geerlingguy.mac collection)
Shell
229
star
38

ansible-role-redis

Ansible Role - Redis
Jinja
228
star
39

packer-centos-7

This build has been moved - see README.md
223
star
40

ansible-collection-mac

Collection of macOS automation tools for Ansible.
Shell
220
star
41

ansible-role-dotfiles

Ansible Role - Easy and flexible dotfile installation.
214
star
42

ansible-role-nfs

Ansible Role - NFS
Jinja
207
star
43

ansible-role-haproxy

Ansible Role - HAProxy
Jinja
194
star
44

ansible-role-git

Ansible Role - Git
188
star
45

ansible-role-repo-epel

Ansible Role - EPEL Repository for RHEL/CentOS
175
star
46

ansible-role-composer

Ansible Role - Composer PHP Dependency Manager
Jinja
175
star
47

ansible-role-pip

Ansible Role - Pip (for Python)
174
star
48

turing-pi-2-cluster

DEPRECATED - Turing Pi 2 Cluster
Jinja
174
star
49

ansible-role-logstash

Ansible Role - Logstash
Jinja
168
star
50

ansible-role-elasticsearch

Ansible Role - Elasticsearch
Jinja
167
star
51

pico-w-garage-door-sensor

Wireless garage door sensor for Home Assistant powered by Raspberry Pi Pico W
Python
166
star
52

drupal-for-kubernetes

Drupal Example Site for Kubernetes
PHP
149
star
53

ansible-role-filebeat

Ansible Role - Filebeat for ELK stack
Jinja
137
star
54

ansible-role-backup

Ansible Role - Backup for simple servers
Shell
135
star
55

ansible-role-ansible

Ansible Role - Ansible
130
star
56

airgradient-prometheus

AirGradient Prometheus exporter.
C++
116
star
57

ansible-role-kibana

Ansible Role - Kibana
Jinja
115
star
58

ansible-role-swap

Ansible Role - Swap
107
star
59

obs-task-list-overlay

An HTML and Node.js-based task list overlay for OBS.
JavaScript
105
star
60

drupal-pi

Drupal on Docker on a Raspberry Pi. Pi Dramble's little brother.
Jinja
105
star
61

ansible-role-glusterfs

Ansible Role - GlusterFS
104
star
62

ansible-role-raspberry-pi

Configures a Raspberry Pi (running Raspbian).
102
star
63

docker-centos7-ansible

CentOS 7 Docker container for Ansible playbook and role testing.
Dockerfile
101
star
64

packer-ubuntu-1804

This build has been moved - see README.md
101
star
65

ansible-role-solr

Ansible Role - Apache Solr
Shell
97
star
66

ansible-mastodon

Mastodon installation on a single server using Ansible.
Jinja
95
star
67

sbc-reviews

Jeff Geerling's SBC review data - Raspberry Pi, Radxa, Orange Pi, etc.
93
star
68

ansible-role-supervisor

Ansible Role - Supervisor
Shell
92
star
69

ansible-role-docker_arm

Ansible Role - Docker for ARM and Pi
91
star
70

ansible-requirements-updater

Update your requirements.yml with this grisly Ansible playbook.
90
star
71

ansible-role-php-versions

Ansible Role - PHP Versions
88
star
72

ansible-role-ruby

Ansible Role - Ruby
Shell
86
star
73

ansible-role-mas

Ansible Role - Mac App Store CLI (MOVED to geerlingguy.mac collection)
85
star
74

JJG-Ansible-Windows

[DEPRECATED] Windows shell provisioning script to bootstrap Ansible from within a Vagrant VM.
Shell
85
star
75

ansible-role-drupal

Ansible Role - Drupal
84
star
76

pi4gpu

Raspberry Pi GPU Carrier Board
OpenSCAD
83
star
77

ansible-role-postfix

Ansible Role - Postfix
82
star
78

tower-operator

DEPRECATED: This project was moved and renamed to: https://github.com/ansible/awx-operator
Dockerfile
82
star
79

ansible-role-varnish

Ansible Role - Varnish HTTP accelerator
Jinja
82
star
80

packer-ubuntu-1404

DEPRECATED - Packer Example - Ubuntu 14.04 Vagrant Box using Ansible provisioner
Shell
82
star
81

pi-camera

A Raspberry Pi Camera
Python
79
star
82

packer-centos-6

This build has been moved - see README.md
79
star
83

pi-router

Raspberry Pi-based OpenWRT router config for 4G/5G Waveshare Dual Ethernet board.
Dockerfile
79
star
84

docker-ubuntu1804-ansible

Ubuntu 18.04 LTS (Bionic) Docker container for Ansible playbook and role testing.
Dockerfile
78
star
85

top500-benchmark

Automated Top500 benchmark for clusters or single nodes.
Jinja
76
star
86

deskpi-super6c-cluster

DEPRECATED - DeskPi Super6c 6-node Raspberry Pi CM4 Cluster
76
star
87

docker-examples

There are many like it, but this one is mine.
Python
75
star
88

youtube-10k-pods

10,000 Kubernetes Pods for 10,000 Subscribers
74
star
89

docker-ubuntu2004-ansible

Ubuntu 20.04 LTS (Focal Fossa) Docker container for Ansible playbook and role testing.
Dockerfile
74
star
90

pi-bell-slapper

The King of Ding. Internet-connected Raspberry Pi-based notification bell.
OpenSCAD
74
star
91

baby-safe-temp

Safe temperature monitor for baby's room. Made for Raspberry Pi Pico.
Python
74
star
92

ansible-role-memcached

Ansible Role - Memcached
Jinja
68
star
93

ansible-role-node_exporter

Ansible role - Node exporter
Jinja
66
star
94

ansible-role-mailhog

Ansible Role - MailHog for catching and viewing emails
Shell
62
star
95

molecule-playbook-testing

This is an example from the Ansible 101 livestream
62
star
96

ansible-role-ssh-chroot-jail

Ansible Role - SSH chroot jail config
Shell
60
star
97

pi-nvr

Raspberry Pi NVR for home CCTV recording.
Dockerfile
58
star
98

Imap

Simple wrapper class for PHP's IMAP-related email functions.
PHP
57
star
99

Request

A simple PHP HTTP request class.
PHP
56
star
100

ansible-role-samba

Ansible Role - Samba
55
star