Wednesday, August 6, 2014

Arduino + Raspberry Pi + servo + camera

Arduinos were one of my purchases this summer. With our free time before school starts here in Pakistan, I've ripped through the whole tutorial book and started my first project.

I've got the Raspberrypi set up to take time-lapse photos, something that I wrote about a few months ago. Now I have an Arduino kit that came with servo-motors so I can add motion to my time-lapse.

I have two separate scripts running. The Arduino language looks like Java and if there are any differences, I can't spot them. I re-wrote some of the tutorial code for the servo:

#include <Servo.h> // servo library

Servo servo1; // servo control object

void setup()
{
servo1.attach(9);
}

void loop()
{
int position;

for(position = 0; position < 180; position += 1)
{
servo1.write(position); // Move to next position
delay(10000); // Short pause to allow it to move
}
}

I don't know Java very well, so the tutorials were great. The electric circuit side of the Arduino was easy thanks to Mr. Rutherford's 7th grade class. I've only spent an hour or two with Java, but you certainly don't need to learn it too in depth for simple projects like this.

So now I have a servo that is turning 1 degree every 10 seconds.

I adjusted my Pi's camera script to snap a picture every 10 seconds too. It is written in Python:

import os
import time

i = 0

while i <= 200:
imageNum = str(i)
os.system(“raspistill -o image%s.jpg”%(imageNum))
i += 1
time.sleep(10)

I do need to adjust the code to dump the pictures onto a usb drive...it would save me one step later, but the script is simple and works as is.

Here is the set-up.
(yup, the camera is upside-down...but it is easier to fix in post)








Yes, I am running the Pi off of a portable USB quick charger. These are marketed as phone chargers, and that is why I bought it. This little experiment shows that it has the power to run the Pi and the camera, but is probably not a great long term solution. The USB charger is also being charged... it is like a lil UPS system, which is necessary in a city where the power goes off every few hours due to 'load sharing' and the generator (we are lucky to have one) kicks in 2 minutes later.

The box is just that, an empty box. I attached the pi camera with two little screws and hooked it to the servo motor arm with a stripped twist tie.

To keep it steady, my pocket knife held it down.

Not too fancy, but I didn't have to buy or fabricate anything for the set up.


After hitting the 'upload' button once, I was able to unplug the Arduino from my computer. Every time I re-attached the battery, it started the code again (back at 0 degrees, headed for 180 over 30 minutes), I just had to start the Pi code quickly thereafter. I did that through VNC and snapped pictures of our living room. Test pictures are boring, but I want to check how it looks and how the timing is between the two scripts.
I'm also not going to post a time-lapse of a panorama of the living room while Janet and I read on the couches.

I adjusted the servo to move once a minute, aimed the whole mess out our window and ran it again. In case you were curious, but didn't want to do the math:
Servo turns 1 degree every 60000 mS so 60 seconds I believe
picture every 10 seconds = 6 at each degree.
180 degrees = 180 minutes = 3 hours of camera time.
180 degrees *6 = 1080 pictures
30 frames a second in digital videos = 36 seconds of video.


That video will be added tomorrow.  You may have caught  my bug, i <= 200
so I only got 201 pictures today:


1 comment:

  1. You're no longer in Hanoi, so update your bio info from your blog-sphere. In related news, you still lost to me last year in Fantasy Football and lost even harder the year before. That is all, loser.

    ReplyDelete