3.1.1 Photograph Module

Introduction

In this kit, equipped with a camera module, let’s try to take a picture with Raspberry Pi.

Components

../_images/photo1.png

For more information on how to connect the camera module and its configuration, please refer to Camera Module.

Experimental Procedures

Step 1: Go into the Raspberry Pi Desktop. You may need a screen for a better experience, refer to: Connect your Raspberry Pi. Or access the Raspberry Pi desktop remotely, for a detailed tutorial please refer to Remote Desktop.

Step 2: Open a Terminal and get into the folder of the code.

cd /home/pi/raphael-kit/nodejs/

Step 3: Run the code.

sudo node camera.js

After the code runs, the camera will take a photo. Now you can see the photo named test.jpg in current directory.

Code

const exec = require('child_process').exec;

exec('libcamera-still -o test.jpg');

Code Explanation

const exec = require('child_process').exec;

Import the child_process module, which allows nodejs to perform various operations on child processes, including creating child processes to directly execute system commands.

Note

About this module, please refer to: https://nodejs.org/api/child_process.html

exec('libcamera-still -o test.jpg');

After enabling the Camera function, you can directly use the command libcamera-still -o test.jpg to capture photos in the terminal. We can also use the method provided by the child_process module child_process.exec(cmd, [options] , callback) to create child processes to run system commands.

By adding loop and delay functions, we can also achieve the effect of timing photos or time-lapse video.