Indice

Ratcam v1

Ratcam allows you to control your RaspberryPi camera through a Telegram bot, and alerts you when movement is detected, by sending photos and videos.

The idea came to me when I found traces of mice in my house; rather than adopting drastic measures (and having to deal with the remains), I wanted to figure out from where they entered.

I developed a initial version, but I'd like to make some improvements to it.

Project status

  • ONGOING Printed circuit board illuminator that controls IR LEDs in PWM
    • OK Kicad script support for circular placement and routing
    • OK Component selection and PCB manifacturing
    • OK Test hardware PWM
    • ONGOING Re-aligning pin header and camera
  • ONGOING Software modularization to enable/disable features.
  • TO DO RGB LED support that displays the camera status.
  • TO DO 3D printed case.

— Pietro Saccardi 2017/11/26 22:35

Ratcam

  • Began: Oct 13, 2017
  • Ended: ongoing
  • Stato: integrating illuminator

PCB project

code

Version 0

Functionalities

Circuit development

The experience of g5pw, wifasoi and ziongate was essential to develop the PCB with Kicad . The circuit should just power some IR LEDs from the 5V pin of the Raspberry Pi. Moreover, I'd like to control the output from a 3v3 pin; in this way I can tune the emitted light using PWM.

All the project files are on Gitlab.

Choosing the components among those available online was like finding a needle in a haystack; anyway I picked: I componenti che ho trovato nella miriade dei semiconduttori disponibili online sono:

Note on component selection

A (tedious) work of datasheet reading on several supplier's websites until I found those that would fit together.

I first picked the IR LEDs, comparing datasheets on how much light they emit with respect to the specs of some IR LEDs I have here. 10 LED looked like the right amount to have (at least) twice as much (emitted) light with respect to version 0. With this information, I computed how many I can put in series (two at most) and power using the 5V pin. I then selected the mosfet basing on the amount of current the LEDs would draw (and the need of using PWM).

In choosing the components I made sure that they would not be too small, since I do not have experience in SMT soldering.

PCB Design

I wanted to arrange the LED in a circular fashion, but Kicad does not provide suitable tools to do so; it is not possible to draw curves with traces or pours. I took inspiration from this project to draw circular elements with Kicad.

At the beginning I had written the bare minimum code that was need to make circular tracks, but afterwards I realized that to align correctly the components and compute the fills I needed a more extensive support for polar coordinates. Hence, I wrote a few classes to manipulate chords and circular sectors, and an extra, more “pythonic” Kicad interface. The set of scripts is on Gitlab. It includes:

Kicad can be controlled in python, but documentation is scarce and often one has to inspect the source code of Kicad to understand what a certain method does. Objects are transported via Swig directly from C++ to python, and therefore they are not exactly trivial to use. Some very useful links for people that want to go down the same road:

Placement and routing

The outline of the placement and routing algorithm follows.

Front components: (LEDs and resistors)

Back components: (connector, mosfet)

I later added QR code and text.

Outline and manufacturing

In the Raspberry Pi's camera module documentation I found the measurements of the camera. I drew the same elements in Kicad, to get the camera lens at the very center. I added footprints for the holes and had the PCB manufactured by Elecrow. I add here the settings that wifasoi recommended, since I found different opinions on how to export for Elecrow.

Elecrow export settings.
PCB (front)
PCB (back)

Assembly and limitations

While assembling the illuminator I realized there are several limitations:

Among the changes to follow, I'll rearrange the components.

Wrong cable/flex alignment.
First blinking LED test
Assembly with M2 screws and helping hands

PWM LED control

It seems the mosfet I'm using works well in PWM mode. I did some tests with the WiringPi library]]. It seems RPi.GPIO uses software PWM, which consumes a bit extra CPU, but wiringPi and the gpio tool can exploit the hardware PWM on physical pin 12. I wrote a short C++ (C would've been enough) program which oscillates linearly the hardware PWM duty cycle, and recorded the outcome.

PWM wants sudo

Important: setting and writing hardware PWM mode requires superuser privileges. gpio is SUID, so no problem, but my snippet must be run as sudo.

Install the wiringpi library to compile:

setup.sh
sudo apt-get install wiringpi build-essential
g++ -lwiringPi oscillate.cpp -o oscillate
sudo ./oscillate
oscillate.cpp
#include <wiringPi.h>
#include <iostream>
 
int main() {
	wiringPiSetupPhys();
	pinMode(12, PWM_OUTPUT);
	std::div_t div;
	for (int i = 0; ; i = (i + 1) % 2048, div = std::div(i, 1024), delay(1)) {
		const int duty_cycle = div.rem * (1 - 2 * div.quot) + 1024 * div.quot;
		pwmWrite(12, duty_cycle);
		if (i % 64 == 0) {
			std::cout << int(100.f * duty_cycle / 1024.f) << std::endl;
		}
	}
	return 0;
}

The result follows. Note how the illumination is more than enough to record videos and photos with Ratcam.

Under naked eye already looks promising
Ratcam PWM test
Enough light