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.
— Pietro Saccardi 2017/11/26 22:35
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:
VSMY2850G
, 850 nm LED, suitable for PWM, quantity: 10.RK73H-5715
, 15 Ohm resistor, quantity: 5.DMG1012UW
, n-channel MOSFET, quantity: 1.DS1031-03
, 3 pin connector, quantity: 1. too smallA (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.
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:
cad.py
data model and Kicad object abstraction (limited to what is needed for this project only)pcb.py
conversion from and to Kicadpolar.py
geometric primitivesradial_illuminator.py
configurable placer and router for a Ratcam-like illuminator circuit.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:
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.
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.
While assembling the illuminator I realized there are several limitations:
Among the changes to follow, I'll rearrange the components.
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.
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:
sudo apt-get install wiringpi build-essential g++ -lwiringPi oscillate.cpp -o oscillate sudo ./oscillate
#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.