qwertylabs
QK / LESSONS · PROJECT

Lucky Cam.

Build the motion sensor and camera separately, prove each one works, combine them, then make the latest image visible on the local network.

Studio deck
01 · project map

What Lucky Cam will eventually do

Lucky walks past → a PIR notices motion → Python tells the camera to take a photo → the new photo replaces the old one → a web page displays the newest image.

inputPIR
logicPython
captureCamera
publishlatest.jpg
PrerequisitesComplete Raspberry Pi Basics and the Electronics + GPIO LED/button builds first. Cat Cam assumes you know File Manager, Terminal, Thonny, safe shutdown and the difference between a GPIO number and physical pin number.
02 · camera rule

Lucky only

A camera in a studio needs a purpose and a boundary.

Technology is the toolbox. The project is a Lucky camera, not a general studio surveillance camera.
03 · project folder

Make a Cat Cam folder

  1. Start the Pi and wait for the desktop.
  2. Open File Manager.
  3. Open Home → pi-projects.
  4. Right-click an empty area → New Folder.
  5. Name it cat-cam.
  6. Press Enter, then double-click the new folder.
This folder will eventually containpir-test.py, camera-test.py, cat-cam.py, latest.jpg and index.html.
04 · sensor first

Identify your PIR before wiring it

A common PIR module has a white faceted dome. We need to identify its three electrical connections.

  1. Pick up the PIR by its edges. Do not pull on the dome.
  2. Find the three connection labels printed on the board: VCC, OUT, GND.
  3. Point to each label before you connect a wire.
  4. If the labels are hidden by a plug or case, ask your tutor to help identify them.
MeaningVCC = power into the sensor. OUT = the sensor's signal to the Pi. GND = ground.
Do not assume left/middle/right from a picture. Different PIR boards can arrange the pins differently.
05 · PIR wiring

Wire the common HC-SR501

Wiring diagram for a common HC-SR501 PIR sensor: VCC to 5V, OUT to GPIO4 and GND to ground
Use this exact map only ifYour sensor is an HC-SR501, or your tutor has confirmed its VCC/OUT/GND and supply match this circuit.
  1. Shut the Pi down and disconnect power.
  2. Connect PIR VCC to Pi physical pin 2 (5 V).
  3. Connect PIR OUT to Pi physical pin 7 (GPIO4).
  4. Connect PIR GND to Pi physical pin 6 (GND).
  5. Trace all three wires from sensor label to Pi pin.
  6. Have the wiring checked, then reconnect power and boot.
06 · PIR mini build

Create pir-test.py

  1. Open Thonny → File → New.
  2. Type:
from gpiozero import MotionSensor

pir = MotionSensor(4)

print("Waiting for movement...")
pir.wait_for_motion()
print("Something moved")
Read the three important linesMotionSensor is GPIO Zero's PIR tool. MotionSensor(4) means the signal wire is connected to GPIO4. wait_for_motion() pauses the program until the sensor reports movement.
  1. Choose File → Save As….
  2. Go to Home → pi-projects → cat-cam.
  3. Save as pir-test.py.
  4. Click Run.
At firstThe Shell should say Waiting for movement....
07 · PIR test

Give the sensor time, then move

Many PIR sensors need a short settling period after power-up.

  1. After the Pi boots, leave the sensor still for about 30–60 seconds.
  2. Run pir-test.py again if needed.
  3. Move a warm hand or your body across the sensor's view, not directly toward it from far away.
  4. Watch the Thonny Shell.
SuccessThe Shell adds Something moved.
If nothing happensCheck that the sensor had time to settle; check VCC/OUT/GND labels; check OUT really goes to GPIO4 / physical pin 7; check the Shell for Python errors. Do not change multiple things at once.
What have you proved?The Pi can receive a physical-world input from the PIR. That is mini build #1 complete.
08 · camera hardware

Choose the right camera cable

Raspberry Pi camera connector diagram for Pi 4 and earlier versus Pi 5
Pi 4 and earlier flagship boardsUse the standard 15-pin CAMERA connector and a standard-to-standard camera cable.
Pi 5Use CAM/DISP0 or CAM/DISP1 and a standard-to-mini camera cable.
Before touching the camera connectorShut down, disconnect power, and touch an earthed metal object such as a metal sink tap to discharge static from yourself.
09 · camera connection

Attach the ribbon cable carefully

  1. With power disconnected, find the Pi's camera connector for your model.
  2. Gently pull the small connector flap outward until it stops, then tilt it slightly open. Do not remove the flap.
  3. Hold the ribbon cable straight. On the Pi end, insert it with the metallic contacts facing away from the connector flap.
  4. Push the cable in evenly so it is not crooked.
  5. Tilt the flap back and press it closed to clamp the cable.
  6. At the camera board, hold the camera with the lens facing down/away. Open its flap.
  7. Insert the cable with the metallic contacts facing toward the camera board and away from that flap.
  8. Close the camera connector flap.
  9. Check both ends are straight and firmly seated.
  10. Reconnect power and boot.
Do not force the ribbon cable. If it seems too wide or narrow for the connector, you may have the wrong cable.
10 · camera mini build

Open Terminal and preview the camera

  1. Open Terminal from the taskbar, or Raspberry Pi menu → Accessories → Terminal.
  2. Type rpicam-hello.
  3. Press Enter.
What should happen?A live camera preview should appear for about five seconds, then close.

Want more time to frame it?

  1. Type rpicam-hello --timeout 0 and press Enter.
  2. The preview now keeps running.
  3. To stop it, hold the Ctrl key and tap C. This keyboard combination is written Ctrl+C.
Mini build #2If you can see a live preview, the camera hardware and camera software can talk to each other.
11 · camera troubleshooting

If there is no preview

  1. Read any message Terminal shows. Do not close it immediately.
  2. Type rpicam-hello --list and press Enter.
  3. If a camera is listed, the Pi can detect it. Try rpicam-hello again.
  4. If no camera is listed, shut down and disconnect power.
  5. Check both ends of the ribbon cable are fully inserted, straight, and the contacts face the correct direction.
  6. Check you used a camera connector, not a display connector on an older board.
  7. Reconnect power and test again.
If the command itself is “not found”Stop and tell your tutor. The camera software setup needs checking; do not start copying installation commands from search results.
12 · save a still

Take one photo from Terminal

  1. Open Terminal.
  2. Type cd ~/pi-projects/cat-cam and press Enter.
  3. Type pwd. Check the answer ends in /pi-projects/cat-cam.
  4. Type rpicam-jpeg -o test.jpg and press Enter.
  5. Wait for the preview/capture to finish.
  6. Open File Manager → Home → pi-projects → cat-cam.
  7. Double-click test.jpg.
SuccessYou should see the photo you just took.
If test.jpg is not thereReturn to Terminal and read the message above the prompt. Also run pwd again: the file is saved in whichever folder Terminal was working in.
13 · camera + python

Create camera-test.py

  1. Open Thonny → File → New.
  2. Before running anything, save the empty file in Home → pi-projects → cat-cam as camera-test.py.
  3. Type:
from picamera2 import Picamera2
from time import sleep

camera = Picamera2()
camera.start()
sleep(2)
camera.capture_file("latest.jpg")
camera.stop()

print("Photo saved")
Read the camera codePicamera2() creates the camera controller. camera.start() starts it. sleep(2) gives the camera two seconds to settle. capture_file("latest.jpg") takes one picture and saves it with that exact name. camera.stop() releases the camera when this one-photo test is finished.
  1. Choose File → Save.
  2. Click Run.
  3. Look in the Shell for Photo saved.
  4. Open the Cat Cam folder in File Manager and double-click latest.jpg.
Then prove replacement worksChange something in front of the camera, Run camera-test.py again, and reopen latest.jpg. It should now contain the new photo, not create latest2.jpg.
14 · camera python troubleshooting

If camera-test.py fails

If Thonny shows “No module named picamera2”Stop and tell your tutor. Picamera2 is not available in the Python environment Thonny is using. Do not install a similarly named package from the internet.
If Picamera2 opens but cannot use the cameraClose any other camera preview first. Only one program may be using the camera. Test rpicam-hello again in Terminal to separate a hardware problem from a Python problem.
If “Photo saved” appears but you cannot find latest.jpgCheck where camera-test.py was saved. A relative filename such as latest.jpg is normally created in the program's working folder.
15 · combine the boxes

Now make the real Cat Cam program

At this point you have already tested both difficult parts separately:

PIR works

pir-test.py detects movement.

Camera works

camera-test.py creates latest.jpg.

Now we connect those two proven ideas.

  1. Open Thonny → File → New.
  2. Save the new file first as Home → pi-projects → cat-cam → cat-cam.py.
  3. Then type the program on the next card.
16 · main program

Type, save, then run cat-cam.py

from gpiozero import MotionSensor
from picamera2 import Picamera2
from time import sleep

pir = MotionSensor(4)
camera = Picamera2()

camera.start()
sleep(2)

print("Lucky Cam is watching...")

while True:
    pir.wait_for_motion()
    camera.capture_file("latest.jpg")
    print("New photo saved")
    pir.wait_for_no_motion()
Read the loopwhile True: means “keep repeating”. The program waits for motion, saves a photo to the same latest.jpg, prints a message, then waits until the motion stops before it is allowed to trigger again. Reusing the same filename is what makes the new picture replace the old one.
  1. Choose File → Save.
  2. Place the camera and PIR so your test movement is inside both fields of view.
  3. Click Run.
  4. Wait for the Shell to say Lucky Cam is watching....
  5. Move across the PIR's view once.
  6. Move away and stay still so the PIR can return to “no motion”.
SuccessThe Shell says New photo saved and the Cat Cam folder contains a new latest.jpg.
17 · prove overwrite

Trigger it twice

  1. With Cat Cam running, trigger the PIR once.
  2. Open latest.jpg and look at the picture.
  3. Change the scene: move an object, or move yourself out of frame and back into the sensor area.
  4. Trigger the PIR again.
  5. Reopen or refresh latest.jpg.
What matters?There should still be one file named latest.jpg, but its picture should now be the newer one.
If it takes many photosMake sure pir.wait_for_no_motion() is inside the loop and indented at the same level as camera.capture_file. Also move the sensor so it is not continuously seeing warm movement.
18 · local web page

Make index.html

Before putting anything on the public internet, make a tiny web page on the Pi itself.

You are not expected to know HTML or JavaScript yetThis mini page is here to connect the pieces of the project. The important idea is: the browser displays latest.jpg, and a tiny script asks for a fresh copy every five seconds.
  1. Stop cat-cam.py for a moment using Thonny's red Stop button.
  2. In Thonny choose File → New.
  3. Choose File → Save As… and save the new file in the Cat Cam folder as index.html.
  4. Type the HTML below. This file is not Python; the browser will read it.
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Lucky Cam</title>
</head>
<body>
  <h1>Lucky Cam</h1>
  <img id="lucky" src="latest.jpg"
       alt="Latest Lucky Cam photo" width="640">

  <script>
    const luckyImage = document.getElementById("lucky");
    setInterval(() => {
      luckyImage.src = "latest.jpg?t=" + Date.now();
    }, 5000);
  </script>
</body>
</html>
  1. Choose File → Save.
  2. Do not click Run in Thonny. HTML is opened by a browser, not run as a Python script.
19 · local server

Let the browser see the Cat Cam folder

  1. Open Terminal.
  2. Type cd ~/pi-projects/cat-cam and press Enter.
  3. Type ls. Check you can see index.html and latest.jpg.
  4. Type python3 -m http.server 8000 and press Enter. You do not need to memorise this: python3 starts Python, -m http.server asks Python to use its built-in simple web-server module, and 8000 is the local network “door number” (port) we are using.
  5. Leave that Terminal window open. It is now acting as a tiny local web server.
  6. Open the Pi's web browser.
  7. In the address bar type http://localhost:8000 and press Enter.
SuccessThe browser shows the Lucky Cam page and the current latest.jpg. If Cat Cam updates that file, the page checks for a fresh copy every five seconds.
New word: localhostlocalhost means “this same computer”.
If the browser says it cannot connectCheck the server Terminal is still open and has not returned to its normal prompt. If you stopped it, run the server command again. To deliberately stop the server, use Ctrl+C.
20 · same Wi-Fi

View it from another device

This is still local network only. It is not on the public internet.

  1. Keep the web-server Terminal running.
  2. Open a second Terminal window.
  3. Type hostname -I and press Enter. This asks the Pi to show its current network addresses.
  4. Look for an address made of four number groups, often beginning 192.168. or 10.. Example: 192.168.1.42.
  5. Make sure the second device is on the same Wi-Fi/network as the Pi.
  6. On the second device, type http:// + the Pi address + :8000. Example: http://192.168.1.42:8000.
SuccessThe second device shows the same Lucky Cam page.
If it does not loadCheck both devices are on the same network, the server Terminal is still running, and you copied the address exactly. Some networks deliberately block devices from talking to one another; if so, ask your tutor.
21 · public publishing

What is left for the real online Lucky Cam?

The important hardware and learning are complete. Public publishing is a separate system choice, and it should not be improvised inside a beginner hardware lesson.

Capture

cat-cam.py replaces latest.jpg.

Upload

A later lesson will securely send that file to a chosen hosting service.

Public page

The public page requests the newest image without exposing the Pi or studio network.

Do not use router port-forwarding to put this beginner web server directly on the internet. We will choose a safe upload/hosting method and add it as the next Cat Cam lesson.
For nowYou have built a working motion-triggered camera and a live page on the local network. That is a complete, testable system before public publishing is added.