How To Create a Bluetooth Raspberry Pi Camera Trigger

Bluetooth Raspberry Pi Camera Trigger
(Image credit: Tom's Hardware)

The new Raspberry Pi Camera Module 3 offers exceptional image quality and a choice between a standard (75 degrees) and wide (120 degrees) lenses. Best of all, we now have autofocus. Taking pictures with the Picamera2 is easy, but sometimes we just want to press a button and take a picture, and appear in the shot!

In this project we will use Blue Dot, a Python module and Android app to create a Bluetooth controlled camera trigger. Thanks to Blue Dot’s easy to use library and Picamera2’s verbose structure we will be capturing 1080p photos via a small amount of code.

For This Project You Will Need

  • A Raspberry Pi 3 or 4
  • A Raspberry Pi Camera
  • An Android device

Installing the Raspberry Pi Camera Module

 1. Open the camera port by gently lifting the plastic lock upwards.

(Image credit: Tom's Hardware)

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.

(Image credit: Tom's Hardware)

3. Close the lock on the connector and give it a very gentle pull to make sure it is in place.

4. Power up the Raspberry Pi to the desktop. Open a terminal and install the latest Picamera updates.

sudo apt update && sudo apt upgrade -y

5. From the terminal, check that your camera is working properly. The libcamera command is handy for quickly checking that our camera is connected and working as expected.

libcamera-hello

Installing Blue Dot

Blue Dot is the creation of Martin O’Hanlon and it provides a truly simple means to remotely control a Raspberry Pi. The name “Blue Dot” represents the large, blue dot that dominates the Android device’s screen. We’re going to use Blue Dot as a large, blue button to trigger the camera to take a picture.

1. On your Android device open the Google Play Store and search for Blue Dot. Alternatively follow this link.

2. Install Blue Dot on your Android device.

3. On your Raspberry Pi, open a terminal and install Blue Dot’s Python library.

sudo pip3 install bluedot

4. Go to the Bluetooth menu, right click and select “Make Discoverable”.

(Image credit: Tom's Hardware)

5. On your Android device go to the 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” will not appear, look for your hostname.

(Image credit: Tom's Hardware)

With our Android device and Raspberry Pi now connected we will write a quick Python script to check that Blue Dot can communicate between the two devices.

1. Open Thonny, found in the main menu under Programming.

2. Create a new file and import the Blue Dot Python library.

from bluedot import BlueDot

3. Create an object, bd, that 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 until the user interacts. 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 a connection from our Android device.

7. On your Android device, open Blue Dot.

8. Select the hostname of your Raspberry Pi. Typically this is “raspberrypi”. 

(Image credit: Tom's Hardware)

9. Click on the Blue Dot to trigger the Python code into action. You should see a message in the Python shell.

(Image credit: Tom's Hardware)

Complete Test Code Listing

from bluedot import BlueDot
bd = BlueDot()
bd.wait_for_press()
print("You pressed the blue dot!")

Creating a Camera Trigger with Blue Dot

(Image credit: Tom's Hardware)

The goal of this project is to create a camera trigger with Blue Dot. When the button is pressed a function is launched on the Raspberry Pi which handles taking a photograph.

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 generate preview windows, useful for framing a shot. The controls class from libcamera enables 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 signal and then the time library. Pause will be used to stop the code from exiting. Time will delay the code after a preview window is created, giving us time to frame a shot.

from signal import pause
import time

4. Create an object, bd, that we will use to work with the library.

bd = BlueDot()

5. Create an object, picam2, that will enable us to easily use the Picamera2 library.

picam2 = Picamera2()

6. Define a function, take_picture() that will be used to take a picture. Functions work by calling their name, this triggers the function to run through all of the steps within it.

7. Create a configuration for the camera to take still images. This sets the image size to 1080p, while 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 preview window with a 720p resolution. We set the position using X and Y coordinates, otherwise it defaults to 0,0. Tweak 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 autofocus. Note, this only works with the Camera Module 3.

   picam2.set_controls({"AfMode": controls.AfModeEnum.Continuous})

12. Pause for two 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 then stop Picamera2.

   picam2.stop_preview()
   picam2.stop()

14. Out of the function, use Blue Dot’s “when_pressed” function to react to user input by running the take_picture function.

bd.when_pressed = take_picture

15. Use 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 code waits for the Android device to connect.

17. On your Android device, open Blue Dot.

18. Select the hostname of your Raspberry Pi. Typically this is “raspberrypi”.

19. Click on the Blue Dot to trigger the camera. You will see the preview window appear and then, two seconds later, an image will be saved to the micro SD card. Repeated presses will create a new image, but as the filename is the same it will overwrite each time.

Complete Code Listing

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()
Les Pounder

Les Pounder is an associate editor at Tom's Hardware. He is a creative technologist and for seven years has created projects to educate and inspire minds both young and old. He has worked with the Raspberry Pi Foundation to write and deliver their teacher training program "Picademy".