Spycam with Arduino / Digispark

This is my first real project with the Digispark.

My son made a bird feeder at school and every now and then we could spot a bird eating on our balcony. I thought that would be cool to make photos of the birds stopping by.

Somehow we would have to detect when something comes/flies by. This is done using the PIR motion sensor. But apart from that, Digispark should also trigger after a defined interval, like making a foto every 5 minutes for some nice time lapse movies. I actually added a 1k potentiometer with which we can adjust the delay between two photos between 30 secs and 10 minutes. Setting the poti value in the middle Digispark would trigger a photo every 5 mins.

I bought myself a spycam on ebay:

spycam

Features:
Image Resolution: 1280 x 960 pixel
Color Video Resolution: 640 x 480 pixel
FPS: 29 frames per second
Image file format: JPEG
Video file format: AVI
Color Video and Audio
Rechargeable Li-ion battery: 280mA 3.7V
Dimension: 5.0 x 3.0 x 1.2 cm

I then opened the thing and soldered two wires each to bridge the triggers using an optocoupler.

Modified cam

Modified cam with soldered cables to it to be able to trigger photos using Arduino/Digispark

A photo can be triggered by setting the “On/Off” button to HIGH for 2 secs then LOW again, wait a few millisecs then put the “REC” button to HIGH and LOW after 250 milliseconds, wait and then set “On/Off” HIGH again for 2 secs then back to LOW.

The camera is charged via USB. So I thought I use a female USB plug and connect its 5V and GND connection. This worked – but what I found out was that once it was charging, I couldn’t trigger photos. So there’s like a “Photo Mode” and a “Charging Mode”. I had to control the 5V to the USB – meaning that I would have to disconnect the USB plug from the 5V to take photos.

I then added a LDR to the circuit. This helps determining the ambient light. The idea is that at night I charge the camera and won’t trigger any photos. But during the day – with day light – I will be able to trigger photos.

I had a 2GB micro SD card around and made some calculations: A photo taken by the cam is around 160kB. This means that around 13000 photos would fit on the card. Triggering a photo every 5 minutes this would be 12 photos per hour and assuming 12 hours of daylight a day would mean the cam would make around 150 photos. In other words I can leave the camera over 80 days untouched… just having it plugged into a power plug. Because I wanted to use an old cell phone charger. A 7805 voltage regulator would then deliver the 5V needed to power the circuit.

After all this I set up this circuit on CircuitLab:

Spycam circuit

Circuit to trigger photos using optocouplers and charge the camera via USB.

This is the prototype setup:

Spycam prototype

Spycam prototype

Spycam prototype

Spycam prototype

I’ll solder that to a proper pcb and then trigger some photos and will upload them to this blog. Hopefully with some birds on it ūüôā

This is the code for the Digispark. I had to rearrange some pin assignment after the program didn’t work the first time. The Digispark has some “differences” as described here: Digispark Wiki.

// consts for attiny85/digispark
int powerPin = 0;
int triggerPin = 1;
int chargingPin = 3;

// duration constants in secs
const long durationMin = 30;           // 30sec
const long durationMax = 600;          // 10min
long previousMillis = 0;
long interval = 1000;                  // interval at which to trigger (milliseconds)

void setup() {
  // put your setup code here, to run once:

  pinMode(powerPin, OUTPUT);
  pinMode(triggerPin, OUTPUT);
  pinMode(chargingPin, OUTPUT);
  pinMode(2, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);

  digitalWrite(powerPin, LOW);   
  digitalWrite(triggerPin, LOW);   
  digitalWrite(chargingPin, LOW);   

  delay(500);

}

void loop() {

  int poti = analogRead(0);   // delay between photos
  int ldr = analogRead(2);    // ldr value, charge the cam at night
  long duration = map(poti, 0, 1023, durationMin, durationMax);
  int pir = analogRead(1);    // motion detector

  // charge it at night
  if (ldr < 20) {
    charge(60000);  
    return;
  }

  // take photo if motion detected 
  if (pir > 500) {
    takePhoto(); 
  }

  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > duration * interval) {
    // save the last time you triggered 
    previousMillis = currentMillis;     

    // take the photo at defined intervals. this depends on the poti.
    // it takes phots between 0.5 and 10 minutes
    takePhoto(); 
  }  
}

// charges the spycam
void charge(int duration) {
   digitalWrite(chargingPin, HIGH);
   delay(duration);
   digitalWrite(chargingPin, LOW);   
}
// shoots 1 photo, needs 5.5 secs approx
void takePhoto() {
   digitalWrite(powerPin, HIGH);
   delay(2000);
   digitalWrite(powerPin, LOW);   

   delay(1000);

   digitalWrite(triggerPin, HIGH);
   delay(250);
   digitalWrite(triggerPin, LOW);

   delay(500);

   digitalWrite(powerPin, HIGH);   
   delay(2000);
   digitalWrite(powerPin, LOW);   
}

void makeVideo(int time) {
   digitalWrite(powerPin, HIGH);
   delay(2000);
   digitalWrite(powerPin, LOW);   

   digitalWrite(triggerPin, HIGH);
   delay(5000);
   digitalWrite(triggerPin, LOW);
   delay(1000);

   digitalWrite(triggerPin, HIGH);
   delay(500);
   digitalWrite(triggerPin, LOW);

   // wait the given time
   delay(time);

   digitalWrite(triggerPin, HIGH);
   delay(500);
   digitalWrite(triggerPin, LOW);

   digitalWrite(powerPin, HIGH);   
   delay(2000);
   digitalWrite(powerPin, LOW);   

}

[Update]

I had the prototype powered on next to me when writing this blog post. I then realized that it got triggered by my reflection in the window. So here at least the proof that it is working ūüėČ I’ll try to upload some nicer videos soon… btw I created a movie out of the single jpgs a tool called MakeAVI.

One thought on “Spycam with Arduino / Digispark

Leave a Reply

Your email address will not be published. Required fields are marked *