The new Raspberry Pi Camera Module 3 offers exceptional image quality and a choice between standard (75°) and wide (120°) lenses. Best of all, we now have autofocus. Take pictures with Picamera2 Easy, but sometimes we just want to press a button, take a picture, and be in the shot!
In this project, we will be using Blue Dot, a Python module, and an Android app to create a Bluetooth-controlled camera trigger. Thanks to the easy-to-use Blue Dot library and Picamera2’s verbose structure, we’ll capture 1080p images with just a small amount of code.
For this project you will need
- Raspberry Pi 3 or 4
- Raspberry Pi camera
- Android device
Install the Raspberry Pi Camera Module
1. Open the camera port Gently lift the plastic lock up.
2. Insert the ribbon connector with the blue tab facing the USB/Ethernet ports. Raspberry Pi Zero users will need to use an adapter and connect the camera to the port on the right side of the board.
3. Close the lock on the connector Pull it gently to make sure it is in place.
4. Turn on the Raspberry Pi on your desktop. Open Terminal and install the latest Picamera updates.
sudo apt update && sudo apt upgrade -y
5. From the terminal, verify that the camera is working properly. The libcamera command is useful for quickly checking that our camera is connected and working as expected.
libcamera-hello
Blue Dot is being installed
Blue Dot is the brainchild of Martin O’Hanlon and provides a really simple way to remotely control your Raspberry Pi. The name “Blue Dot” is the large blue dot that dominates the screen of your Android device. We’ll use the blue dot as a big blue button to launch the camera to take a picture.
1. On your Android device, open the Google Play Store and search for Blue Dot. instead Follow this link.
2. Install Blue Dot on your Android device.
3. On your Raspberry Pi, Open Terminal and install the Python library for Blue Dot.
sudo pip3 install bluedot
4. Go to the Bluetooth menu, right-click and select “Make Discoverable”.
5. On your Android device, go to Settings >> Connected devices and select Pair new device.
6. Select “raspberrypi” and follow the pairing instructions. If your Raspberry Pi has a different hostname then “raspberrypi” does not appear, find your hostname.
With our Android device and our Raspberry Pi now connected, we’ll write a quick Python script to verify that Blue Dot can communicate between the two devices.
1. Open Thonnylocated in the main menu under Programming.
2. Create a new file and import the Blue Dot Python library.
from bluedot import BlueDot
3. create object, bd, which we will use to work with the library.
sudo pip3 install bluedot
4. Wait for the user to press the blue button. This line of Python is a blocker. It will wait for the user to interact. When that happens, the code moves to the next line.
bd.wait_for_press()
5. Print a message to the Python shell.
print("You pressed the blue dot!")
6. Save the code as bd-test.py and click Run. The code will wait for connection from our Android device.
7. On your Android device, open Blue Dot.
8. Select your Raspberry Pi hostname. Usually this is “raspberrypi”.
9. Click the blue dot to run the Python code. You should see a message in the Python shell.
Complete test code list
from bluedot import BlueDot
bd = BlueDot()
bd.wait_for_press()
print("You pressed the blue dot!")
Create a blue dot camera trigger
The goal of this project is to create a camera trigger using Blue Dot. When the button is pressed, a function on the Raspberry Pi that deals with taking a photo is launched.
1. Create a new file and import the Blue Dot Python library.
from bluedot import BlueDot
2. Import Picamera2 and libcamera. The Preview class is used to create preview windows, which are useful for framing a shot. The Controls class from libcamera allows us to use autofocus with the new camera module 3.
from picamera2 import Picamera2, Preview
from libcamera import controls
3. Import the pause function from the signal and then the time library. The pause will be used to stop the code from exiting. The time will delay the code after the preview window has been created, giving us time to frame the shot.
from signal import pause
import time
4. create object, bd, which we will use to work with the library.
bd = BlueDot()
5. create object, picam2, It will enable us to use the Picamera2 library easily.
picam2 = Picamera2()
6. Define a function, take_picture() that will be used to take a picture. Functions work by calling their names, and this causes the function to run through all the steps within it.
7. Create a configuration for the camera to capture still images. This sets the image size to 1080p, while the preview windows will be 720p.
camera_config = picam2.create_still_configuration(main={"size": (1920, 1080)}, lores={"size": (1280, 720)}, display="lores")
8. Set Picamera2 to use the new configuration.
picam2.configure(camera_config)
9. Start a 720p preview window. We set the position using the X and Y coordinates, otherwise it defaults to 0,0. Modify this to meet your needs.
picam2.start_preview(Preview.QTGL, x=100, y=200, width=1280, height=720)
10. Show the preview window.
picam2.start(show_preview=True)
11. Set the camera to use continuous auto focus. Note that this only works with the Camera 3 module.
picam2.set_controls({"AfMode": controls.AfModeEnum.Continuous})
12. Pause for 2 seconds before capturing the image to a file called picam1.jpg.
time.sleep(2)
picam2.capture_file("picam1.jpg")
13. Close the preview window and turn off Picamera2.
picam2.stop_preview()
picam2.stop()
14. Off the job, Use Blue Dot’s “when_pressed” function to interact with user input by running the take_picture function.
bd.when_pressed = take_picture
15. Use the pause to prevent the code from exiting.
pause()
16. Save the code as bluedot_camera.py and click Run to start the code. You will see that the icon is waiting for your Android device to connect.
17. On your Android device, Open the blue dot.
18. Select your Raspberry Pi hostname. Usually this is “raspberrypi”.
19. Click on the blue dot to turn on the camera. You will see the preview window appear and then, after a couple of seconds, the image will be saved to the micro SD card. Repeated presses will create a new image, but since the file name is the same, it will be overwritten each time.
Complete code list
from bluedot import BlueDot
from picamera2 import Picamera2, Preview
from libcamera import controls
from signal import pause
import time
bd = BlueDot()
picam2 = Picamera2()
def take_picture():
camera_config = picam2.create_still_configuration(main={"size": (1920, 1080)}, lores={"size": (1280, 720)}, display="lores")
picam2.configure(camera_config)
picam2.start_preview(Preview.QTGL, x=100, y=200, width=1280, height=720)
picam2.start(show_preview=True)
picam2.set_controls({"AfMode": controls.AfModeEnum.Continuous})
time.sleep(2)
picam2.capture_file("picam1.jpg")
picam2.stop_preview()
picam2.stop()
bd.when_pressed = take_picture
pause()