DIY mosquito detection & laser spotter - OpenCV + RPi + servo/galvo aiming
Python
5
6 commits
updated Mar 18, 2026
DIY mosquito detection and laser spotter system. Detects mosquitoes using computer vision (OpenCV background subtraction + tracking), then aims a visible-light laser pointer at the insect so you can find and eliminate it.
Non-lethal -- this is a spotter, not a killer.
┌─────────────────────────────────────────┐
│ SYSTEM OVERVIEW │
│ │
│ Camera ──► Detection ──► Tracking │
│ │ │
│ IR LEDs ▼ │
│ (850nm) Aim Controller │
│ (servo/galvo) │
│ │ │
│ Laser Pointer │
│ (5mW red) │
│ │
│ Web Dashboard ◄── Status │
│ (live MJPEG + controls) │
└─────────────────────────────────────────┘
Good for tracking landed mosquitoes. Servos are too slow for flight tracking.
Can track mosquitoes in flight. Sub-millisecond mirror repositioning.
| Part | Price | Links |
|---|---|---|
| Raspberry Pi 4 (4GB) | $55-75 | Amazon |
| Pi Camera V2 NoIR (no IR filter!) | $25-30 | Amazon |
| OR: USB webcam (Logitech C920) | $50-60 | Amazon |
| 5mW 650nm red laser module (10-pack) | $7-9 | Amazon |
| 850nm IR illuminator (Tendelux BI8) | $16-20 | Amazon |
| OR: High-power 850nm IR LEDs (DIY circuit) | $5-10 | Amazon / AliExpress |
| NE555 timer ICs (25-pack) | $5-6 | Amazon |
| IRF520 MOSFET driver modules (5-pack) | $7-9 | Amazon |
| 10K potentiometers (10-pack) | $6-8 | Amazon |
| MicroSD card (32GB+) | $8-10 | Amazon |
| 5V 3A USB-C power supply | $10-12 | Amazon |
| Breadboard + jumper wires | $8-10 | Amazon |
| Part | Price | Links |
|---|---|---|
| 20K/30K galvo scanner set (XY + drivers) | $25-80 | AliExpress / Amazon |
| MCP4922 dual 12-bit SPI DAC | $2-5 | AliExpress / Amazon |
| +/-12V dual-rail power supply (for galvo amps) | $10-15 | Amazon |
Detailed wiring diagrams with ASCII schematics are in the circuits/ folder:
circuits/servo_wiring.txt -- Full servo build wiring (RPi + camera + servos + laser + IR)circuits/galvo_wiring.txt -- Full galvo build wiring (RPi + camera + galvos + DAC + laser + IR)circuits/ir_led_circuit.txt -- IR LED driver circuit with 555 timer (adjustable freq/duty)| Function | GPIO (BCM) | Physical Pin |
|---|---|---|
| Pan servo PWM | GPIO12 | Pin 32 |
| Tilt servo PWM | GPIO13 | Pin 33 |
| Laser ON/OFF | GPIO18 | Pin 12 |
| IR LED enable | GPIO24 | Pin 18 |
| SPI MOSI (galvo) | GPIO10 | Pin 19 |
| SPI SCLK (galvo) | GPIO11 | Pin 23 |
| SPI CE0 (galvo) | GPIO8 | Pin 24 |
Flash Raspberry Pi OS (Bookworm, 64-bit) to your SD card.
Enable camera, SPI, and SSH via raspi-config.
sudo apt update
sudo apt install -y python3-opencv python3-flask python3-numpy python3-pip
pip3 install spidev # for galvo mode
git clone <this-repo>
cd mosquito-laser
# Servo mode (default)
python3 src/main.py --aim servo
# Galvo mode
python3 src/main.py --aim galvo
# Detection only (no aiming hardware needed)
python3 src/main.py --aim none --headless
# Test with a video file
python3 src/main.py --camera file --video test_mosquito.mp4 --aim none
# Custom port
python3 src/main.py --port 9090
Navigate to http://<pi-ip>:8080 in your browser.
The web UI shows:
The system needs to know how pixel offsets map to aim angles:
SERVO_DEG_PER_PX_X/Y (servo) or GALVO_VOLTS_PER_PX_X/Y (galvo)
in src/config.py until the laser tracks your finger accuratelySERVO_PAN_INVERT, etc.)Use the dashboard sliders to tune for your environment:
The IR illumination uses a 555 timer in astable mode driving IR LEDs via MOSFET:
+12V ─────┬──────────────────────┐
│ │
[R1 1K] [IR LEDs]
│ 4x 850nm
├──── Pin 7 (DIS) in 2S2P
│ │
[R2 POT] [MOSFET]
(0-100K) IRLZ44N
│ │
├──── Pin 2/6 GND
│ (THR/TRIG)
[C1 470nF]
│
GND
circuits/ir_led_circuit.txtsrc/
├── main.py # Main orchestrator and entry point
├── config.py # All tunable parameters
├── camera.py # Camera abstraction (Pi/USB/file)
├── detector.py # Detection + tracking pipeline
├── aim_servo.py # Servo pan-tilt controller
├── aim_galvo.py # Galvo mirror controller (MCP4922 DAC)
├── laser.py # Laser + IR LED GPIO control
└── dashboard.py # Flask web UI + MJPEG streaming
templates/
└── index.html # Dashboard frontend
circuits/
├── servo_wiring.txt # Servo build wiring diagram
├── galvo_wiring.txt # Galvo build wiring diagram
└── ir_led_circuit.txt # IR LED driver circuit
⚠️ LASER SAFETY: Even 5mW lasers can cause eye damage. Never look directly into the beam. The system includes auto-off timers and a software kill switch, but these are NOT substitutes for physical safety measures.
⚠️ IR LEDS: 850nm IR is invisible but can still damage eyes at close range. Don't stare into IR illuminators.
⚠️ ELECTRICAL: The 555 timer + MOSFET circuit handles 12V. Use proper current-limiting resistors. Don't exceed LED current ratings.
Best detection setup: Dark room + IR illumination + NoIR camera. Mosquitoes appear as bright/dark moving blobs against IR-lit background.
Start with --aim none to tune detection before adding hardware.
Test with video first: Record mosquitoes with your phone, then test
with --camera file --video your_recording.mp4.
Servo limitations: SG90 servos reposition in ~100-200ms. This is fine for mosquitoes on walls but too slow for flight tracking. Galvos are sub-millisecond.
Reduce false positives: Good, consistent lighting helps. Avoid the camera seeing ceiling fans, curtains, or other moving objects.
Pi 4 performance: Expect ~25-30 FPS at 640x480 with the full detection pipeline. The Pi 5 is significantly faster.
MIT -- Build it, hack it, track those mosquitoes. 🦟🔫
DIY mosquito detection & laser spotter - OpenCV + RPi + servo/galvo aiming
Python
5
6 commits
updated Mar 18, 2026
DIY mosquito detection and laser spotter system. Detects mosquitoes using computer vision (OpenCV background subtraction + tracking), then aims a visible-light laser pointer at the insect so you can find and eliminate it.
Non-lethal -- this is a spotter, not a killer.
┌─────────────────────────────────────────┐
│ SYSTEM OVERVIEW │
│ │
│ Camera ──► Detection ──► Tracking │
│ │ │
│ IR LEDs ▼ │
│ (850nm) Aim Controller │
│ (servo/galvo) │
│ │ │
│ Laser Pointer │
│ (5mW red) │
│ │
│ Web Dashboard ◄── Status │
│ (live MJPEG + controls) │
└─────────────────────────────────────────┘
Good for tracking landed mosquitoes. Servos are too slow for flight tracking.
Can track mosquitoes in flight. Sub-millisecond mirror repositioning.
| Part | Price | Links |
|---|---|---|
| Raspberry Pi 4 (4GB) | $55-75 | Amazon |
| Pi Camera V2 NoIR (no IR filter!) | $25-30 | Amazon |
| OR: USB webcam (Logitech C920) | $50-60 | Amazon |
| 5mW 650nm red laser module (10-pack) | $7-9 | Amazon |
| 850nm IR illuminator (Tendelux BI8) | $16-20 | Amazon |
| OR: High-power 850nm IR LEDs (DIY circuit) | $5-10 | Amazon / AliExpress |
| NE555 timer ICs (25-pack) | $5-6 | Amazon |
| IRF520 MOSFET driver modules (5-pack) | $7-9 | Amazon |
| 10K potentiometers (10-pack) | $6-8 | Amazon |
| MicroSD card (32GB+) | $8-10 | Amazon |
| 5V 3A USB-C power supply | $10-12 | Amazon |
| Breadboard + jumper wires | $8-10 | Amazon |
| Part | Price | Links |
|---|---|---|
| 20K/30K galvo scanner set (XY + drivers) | $25-80 | AliExpress / Amazon |
| MCP4922 dual 12-bit SPI DAC | $2-5 | AliExpress / Amazon |
| +/-12V dual-rail power supply (for galvo amps) | $10-15 | Amazon |
Detailed wiring diagrams with ASCII schematics are in the circuits/ folder:
circuits/servo_wiring.txt -- Full servo build wiring (RPi + camera + servos + laser + IR)circuits/galvo_wiring.txt -- Full galvo build wiring (RPi + camera + galvos + DAC + laser + IR)circuits/ir_led_circuit.txt -- IR LED driver circuit with 555 timer (adjustable freq/duty)| Function | GPIO (BCM) | Physical Pin |
|---|---|---|
| Pan servo PWM | GPIO12 | Pin 32 |
| Tilt servo PWM | GPIO13 | Pin 33 |
| Laser ON/OFF | GPIO18 | Pin 12 |
| IR LED enable | GPIO24 | Pin 18 |
| SPI MOSI (galvo) | GPIO10 | Pin 19 |
| SPI SCLK (galvo) | GPIO11 | Pin 23 |
| SPI CE0 (galvo) | GPIO8 | Pin 24 |
Flash Raspberry Pi OS (Bookworm, 64-bit) to your SD card.
Enable camera, SPI, and SSH via raspi-config.
sudo apt update
sudo apt install -y python3-opencv python3-flask python3-numpy python3-pip
pip3 install spidev # for galvo mode
git clone <this-repo>
cd mosquito-laser
# Servo mode (default)
python3 src/main.py --aim servo
# Galvo mode
python3 src/main.py --aim galvo
# Detection only (no aiming hardware needed)
python3 src/main.py --aim none --headless
# Test with a video file
python3 src/main.py --camera file --video test_mosquito.mp4 --aim none
# Custom port
python3 src/main.py --port 9090
Navigate to http://<pi-ip>:8080 in your browser.
The web UI shows:
The system needs to know how pixel offsets map to aim angles:
SERVO_DEG_PER_PX_X/Y (servo) or GALVO_VOLTS_PER_PX_X/Y (galvo)
in src/config.py until the laser tracks your finger accuratelySERVO_PAN_INVERT, etc.)Use the dashboard sliders to tune for your environment:
The IR illumination uses a 555 timer in astable mode driving IR LEDs via MOSFET:
+12V ─────┬──────────────────────┐
│ │
[R1 1K] [IR LEDs]
│ 4x 850nm
├──── Pin 7 (DIS) in 2S2P
│ │
[R2 POT] [MOSFET]
(0-100K) IRLZ44N
│ │
├──── Pin 2/6 GND
│ (THR/TRIG)
[C1 470nF]
│
GND
circuits/ir_led_circuit.txtsrc/
├── main.py # Main orchestrator and entry point
├── config.py # All tunable parameters
├── camera.py # Camera abstraction (Pi/USB/file)
├── detector.py # Detection + tracking pipeline
├── aim_servo.py # Servo pan-tilt controller
├── aim_galvo.py # Galvo mirror controller (MCP4922 DAC)
├── laser.py # Laser + IR LED GPIO control
└── dashboard.py # Flask web UI + MJPEG streaming
templates/
└── index.html # Dashboard frontend
circuits/
├── servo_wiring.txt # Servo build wiring diagram
├── galvo_wiring.txt # Galvo build wiring diagram
└── ir_led_circuit.txt # IR LED driver circuit
⚠️ LASER SAFETY: Even 5mW lasers can cause eye damage. Never look directly into the beam. The system includes auto-off timers and a software kill switch, but these are NOT substitutes for physical safety measures.
⚠️ IR LEDS: 850nm IR is invisible but can still damage eyes at close range. Don't stare into IR illuminators.
⚠️ ELECTRICAL: The 555 timer + MOSFET circuit handles 12V. Use proper current-limiting resistors. Don't exceed LED current ratings.
Best detection setup: Dark room + IR illumination + NoIR camera. Mosquitoes appear as bright/dark moving blobs against IR-lit background.
Start with --aim none to tune detection before adding hardware.
Test with video first: Record mosquitoes with your phone, then test
with --camera file --video your_recording.mp4.
Servo limitations: SG90 servos reposition in ~100-200ms. This is fine for mosquitoes on walls but too slow for flight tracking. Galvos are sub-millisecond.
Reduce false positives: Good, consistent lighting helps. Avoid the camera seeing ceiling fans, curtains, or other moving objects.
Pi 4 performance: Expect ~25-30 FPS at 640x480 with the full detection pipeline. The Pi 5 is significantly faster.
MIT -- Build it, hack it, track those mosquitoes. 🦟🔫