For the time being, I wanted to work with pygame to access and display the cameras. Pygame makes it easy to add a USB camera, but a little more challenging to add the second High definition Raspberry Pi camera v2. Below is a short program to display the cameras attached to the RPI that are detected by pygame. I gave it the original name of “check_for_cameras”. The USB camera is plugged into a USB port and the Raspberry pi v2 camera plugged into the camera port.
Now run the program.
The program shows the problem. With the USB cameras plugged in and the Raspberry
pi camera v2 plugged into the camera port, the program only displays the USB
camera. To be able to access the Raspberry PI camera with pygame, we need a
driver. Open the terminal and enter the command “sudo modprobe bcm2835-v4l2” like
the below picture and run the program again.
***I edited this section on Radomir Dopieralski advice and placed bcm2835-v4l2 in /etc/modules instead of /etc/rc.local. Thanks Radomir it works GREAT! ***
It worked! We can see both cameras. The next problem is it will only work until we reboot the Raspberry Pi. To get around this, we will need to run “sudo modprobe bcm2835-v4l2” at start up. To run the command at start up, open terminal and enter “sudo nano /etc/ modules” then add “bcm2835-v4l2” in the following window. Like the two pictures below and save the change.
Finally reboot the Raspberry PI and run the program, and both cameras should be detected.