PIR sensor + Arduino + Processing + Skype…

This post is about a simple and interesting combination of software and hardware.

Last week, I picked up a couple of PIR sensors from Ebay.

pir2_w500

PIR sensors are (usually) easy to interface with an arduino board.
Red: 5V, Black: Ground, Yellow: Analog In. Could it be easier than this?

PIR_w500

After a bit of experimentation, I found that the board sends a pulse as soon as it detects some sort of movement. Also, it sends a pulse at the very beginning, when it sort of “boots”.

My ultimate aim was to start Skype as soon as movement was detected.

I loaded the standard firmata sketch on my arduino, see: http://arduino.cc/en/Reference/Firmata. Basically, the arduino simply sends all the analog port readings to Processing.

The following Processing code is simply a proof of concept. The sketch starts, looks for an arduino at the second serial port of my PC, waits for 5 secs for the PIR to send its first pulse and then, if movement is detected calls a number, using Skype.

At any moment, in the middle of the screen, you see the value of the PIR sensor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import cc.arduino.*;
import java.io.*;
import processing.serial.*;
 
int analogPin = 5;
int myV; // PIR sensor value
 
PFont font;
Arduino arduino;
 
//////////////////////////////////////////////////////////////////////////////
void setup(){
  size(400, 300);
  font = createFont("ArialUnicodeMS-12.vlw",22, false); 
 
  //my board is the 2nd serial device, hence Arduino.list()[1]
  println(arduino.list());
  arduino = new Arduino(this, Arduino.list()[1], 57600);
}
 
//////////////////////////////////////////////////////////////////////////////
void draw(){
    background(0);
    fill(255);
 
  // the PIR sensor is sending out HIGH signal in the first 5 secs
  // so we will ignore all the early signals
  if(millis() < 5000){
 
  }
 
  else{
    // myV is the scaled value of the PIR sensor
    myV = arduino.analogRead(analogPin);
 
    textFont(font, 22); 
    textAlign(CENTER,CENTER);
    text("PIR sensor value: " + myV,width/2,height/2);
    text("Active for " + round(millis()/1000) + " seconds",width/2,height*3/4);
 
    callme();
  }
}
 
//////////////////////////////////////////////////////////////////////////////
void callme(){
 
  // you should be logged in on Skype and have some credit
  // if your are dialling an external line
  String mySkype = "C:\\Program Files\\Skype\\Phone\\skype.exe /nosplash /callto:+44798000000";
 
  if(myV>100){
 
    print(" MyV: " + myV + " ");
 
    try {
      String line;
      Process p = Runtime.getRuntime().exec(mySkype);
      p.waitFor();
      System.out.println("EXIT: " + p.exitValue());
    }
 
    catch (Exception err) {
      err.printStackTrace();
    }
 
      delay(3000); 
  }
 
 
 
}
//////////////////////////////////////////////////////////////////////////////

Here is a very short video of this test in action:

PIR Sensor – Test from Arkadian.Eu on Vimeo.

Simple Analog Signal Meter

I bought a couple of these signal meters from Maplin a few months ago and today I found some time to try them on an an arduino.

Maplin sell these for £4 each, which is probably quite expensive for what they really are, given the cheap plastic material and the overall quality: just to give you an idea: the transparent front cover is attached to the rest of the unit with a piece of tape…

Still, if you want to add a simple meter on your arduino, without messing around with lcds and their libraries, this is by far the easiest way.

In my test, I simply used a potentiometer and I attached the meter like this:

Simple Analog Signal Meter
Simple Analog Signal Meter

(btw, the battery underneath the meter has just a “supporting” role)

Here is a simplified version of Tom Igoe’s “AnalogueWriteMega.pde” example, that comes with Arduino 0017:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const int thisPin = 5;
 
void setup() {
  pinMode(thisPin, OUTPUT); 
}
 
void loop() {
  for (int x = 0; x < 255; x++) {
    analogWrite(thisPin, x);
    delay(50);
  } 
 
  for (int x = 255; x >= 0; x--) {
    analogWrite(thisPin, x);
    delay(50);
  } 
 
  delay(100);
}

… and here is a video of how it works (apologies for the poor quality):

Simple Signal Meter from Arkadian.Eu on Vimeo.

Arduino-based keyboard for Windows

Today, on the arduino.cc forums, a user had the following problem: he wanted to start video in quicktime by pressing a custom button on an arduino. Basically, the arduino board should act as a simple keyboard for the computer.

I found this problem interesting and I ended up solving it. Here is the link to the forum.

Basically this problem has three parts.
1. An arduino sketch which, when the button is pressed, sends a character on the serial.
2. A serial-to-keystrokes program, which takes the character from the serial connection and converts it to a real keystroke
3. A program on the windows machine that translates the keystroke to whatever we want to do.

Sounds tricky? It’s actually easier than it sounds.

1. The Arduino Sketch
This is quite straight forward. The simple arduino keyboard circuit looks like this:

arduino-keyboard

Here is a modified version of the Button.pde example that comes with the latest Arduino release. I have only added the lines required for the serial connection and a very long (2 sec) delay after the key is pressed; I used this 2 sec delay to keep things simple, but you may want to adapt the longer and more appropriate “Debounce.pde” sketch (also available as an example in the latest Arduino release) which takes care of the debouncing more efficiently.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const int buttonPin = 2;     
const int ledPin =  13;      
int buttonState = 0;         
 
void setup() {
  Serial.begin(9600); 
  pinMode(ledPin, OUTPUT);      
  pinMode(buttonPin, INPUT);     
}
 
void loop(){
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {     
    digitalWrite(ledPin, HIGH);  
    Serial.print("a");
    delay(2000);
  }  
  else {
    digitalWrite(ledPin, LOW); 
  }
}

2. From Serial to Keystrokes
I found a very nice program, AAC Keys, which converts any characters the arduino sends to the serial port to actual keystrokes. This is available for both windows and Mac and it’s free to download; the download is a single executable file, which, when activated, stays in your system tray and monitors the serial port. The only thing you need to configure here is the port and the Baud rate:
aac_keys

Note that if AAC Keys is activated and configured, the arduino environment won’t be able to communicate with the arduino board, as the port cannot be shared.

At this stage, with the AAC Keys activated and configured, pressing the button on the arduino will create the character “a” on the computer.

3. Windows Shortcuts
What we need to do now is to somehow start playing a video with Quicktime. To play a video with QuickTime, all we need to do is to run the following command in a DOS window:

"C:\Program Files\QuickTime\QuickTimePlayer.exe" "C:\your video path\Video_A.avi"

If the video doesn’t start directly, then go to QuickTime >> Preferences >> Player Preferences… and select “automatically play movies when opened”.

My favourite windows program for these tasks is AutoHotKey. When AutoHotKey is installed, textfiles with the .AHK extension are executed by AutoHotKey.

The following AHK script, when executed, will stay in the system tray waiting for the “a” key (either from the keyboard or from the arduino board) to be pressed.

1
2
3
a:: 
Run "C:\Program Files\QuickTime\QuickTimePlayer.exe" "C:\your video path\Video_A.avi"
return

…and that’s it! A simple (1-key) Arduino keyboard, which, when pressed, launches the video we want in quicktime.

There are many ways to extend this idea. More keys can be added (many can be added on a single analog port with resistors), and, with a bluetooth adaptor on the arduino board and the computer, we could even design a bluetooth keyboard for the computer! 🙂

Arduino/Ikea Lamp – 4,000 views…

40 days ago, I added here a post about a “hacked” IKEA lamp; a simple lamp, fitted with 20 RGB leds, an arduino board and a push-to-make switch which I built as a gift to the 2 year old daughter of a good friend of mine.
combination

Now, the lamp has appeared on the front page of the blog of the :MAKE magazine, the video had over 4,000 views recorded on Vimeo and I’m still getting many hits every day.

The most important thing: my friend’s daughter loves this lamp! 🙂

Arduino Project: Intervalometer for Fuji cameras

This post is about an arduino-based intervalometer I built for my camera, a Fuji S9600 dSLR.

I decided to go for a very simple interface: a rotary switch in the middle of a plastic box which would allow me to select 12 pre-defined intervals. I went for a switch and not a potentiometer because I wanted to be certain about the option I select: the reassuring “click” of the rotary switch does exactly that!

Also, I wanted to make the device easy to use, so the labelling of the different options around the rotary switch was very important too.

The Fuji S9600 (a.k.a. Fuji S9100 in the States) has a mini-B USB connector on the side which is used, amongst other things, for a remote shutter release cable. Bob Mahar has “dissected” an original Fuji RR-80 remote release cable and has revealed that the device is actually very simple inside; Bob’s great page on the Fuji RR-80 is here: http://www.trafficshaper.com/RR-80/RR-80.html.

Based on Bob’s page, I came up with the following diagram for the RR-80:

circuit_rr80_5_w500

The circuit of the RR80 is simple to build, but I couldn’t find a suitable mini-B USB plug: remember, the camera uses the 5th pin, which is not always available on the plugs you can buy online or at Maplin’s.
http://en.wikipedia.org/wiki/Universal_Serial_Bus
http://en.wikipedia.org/wiki/File:Types-usb_new.svg

After a couple of failed attempts to source a 5-pin USB mini-B connector, I ended up buying a cheap Chinese RR80 clone:

dscf0890_w500

I was actually lucky, because this model has a 2.5″ jack on the side, which allows two cameras to be sync’d (not all the clones have this, I think the JJC clones marked “MA” have it). This meant that I could use a 2.5″ stereo connector in order to connect the arduino with the remote. All the arduino has to do in order to take a photo is to short 2 of the 3 parts of the stereo connector.

dscf0893_w500

I used an old Sparkfun Skinny arduino (it is called “Arduino Pro” now and it’s not red anymore):

dscf1065_w500

This is a 12-pole rotary switch from Maplin and 12 resistors:

dscf1050_w500

I found a simple plastic box at Muji:

Muji Box

First things first: I converted the rotary switch into a “potentiometer”:

dscf1097_w500

In case it’s not clear from the image above, I connected all the pins in the periphery with resistors. The yellow cable coming out from the middle pin of the switch goes to the analog In pin (pin 5 here). The black cable coming out from the 12th pin of the switch goes to the ground and the red cable coming out from the 1st pin of the switch goes to the 3.3V arduino output.

At the opposite side of the arduino board, I used a blue LED on pin 13 (and ground) and on pin 12 I connected a transistor, 2N222A: http://en.wikipedia.org/wiki/File:2n2222A_and_schema.jpg) and the cable (twisted red-black) from the 2.5″ connector.

dscf1078_w500

Here is a better diagram of the wiring of the transistor (to keep things simple, only the transistor is shown here):
transistor1

For more information on the use of transistors as switches: http://en.wikipedia.org/wiki/Bipolar_junction_transistor

After a few minor tweaks, the prototype was up and running:
dscf1077_w500

The code loaded on the arduino is pretty simple really:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
///////////////////////////////////////////////////
int swPin  =  5;  // 12-step switch
int ledPin = 13;  // Blue & pcb leds
int camPin = 12;  // Camera pin
 
int sw    = 0;  // 12-step resistance value
int stage = 1;  // 12-step stage (i.e. 1 - 12)
 
// this delay is specified by the rotary switch
int  delaySec;   
 
// this is the current second count
long currentSec = 0;   
 
// this is how long the shutter switch will be on
long shutterSec  = 100; 
 
///////////////////////////////////////////////////
void setup() {
 
  // declare the swPin & ledPin as an OUTPUTs
  pinMode(ledPin, OUTPUT);  
  pinMode(swPin,  OUTPUT);
 
// the serial is only used for debugging and for
// the calibration of the rotary switch  
//  Serial.begin(9600); 
 
}
 
///////////////////////////////////////////////////
// I 
void loop() {
  readSwitch();
  timeCheck();
 
// printLine is only used for debugging and for
// the calibration of the rotary switch
//  printLine(); 
}
 
///////////////////////////////////////////////////
///////////////////////////////////////////////////
void timeCheck(){
  if(delaySec<=currentSec){
 
    digitalWrite(ledPin, HIGH);  // turn the ledPin on
    digitalWrite(camPin, HIGH);  // turn the camPin on
 
    delay(shutterSec); // keep the led and the switch on
 
    digitalWrite(ledPin, LOW);   // turn the ledPin off
    digitalWrite(camPin, LOW);   // turn the camPin off
 
    delay(1000-shutterSec);
    currentSec = 0;
  }
 
  else{
    delay(1000);
    currentSec = currentSec + 1;
  }
}
///////////////////////////////////////////////////
void readSwitch(){
 
  // read the value from the sensor
  sw = analogRead(swPin);  
 
// the serial is only used for debugging and for
// the calibration of the rotary switch 
//  Serial.print(sw);
 
  if(sw>=0  && sw< 20) {
    stage =  1; 
    delaySec =   10;
  } // 10 secs
  if(sw>=20  && sw< 110) {
    stage =  2; 
    delaySec =   20;
  } // 20 secs
  if(sw>=110  && sw< 200) {
    stage =  3; 
    delaySec =   30;
  } // 30 secs
  if(sw>=200  && sw< 290) {
    stage =  4; 
    delaySec =   60;
  } // 1 min
  if(sw>=290  && sw< 380) {
    stage =  5; 
    delaySec =   90;
  } // 1.5 mins
  if(sw>=380  && sw< 480) {
    stage =  6; 
    delaySec =  120;
  } // 2 mins
  if(sw>=480  && sw< 570) {
    stage =  7; 
    delaySec =  180;
  } // 3 mins
  if(sw>=570  && sw< 660) {
    stage =  8; 
    delaySec =  300;
  } // 5 mins
  if(sw>=660  && sw<750) {
    stage =  9; 
    delaySec =  600;
  } // 10 mins
  if(sw>=750 && sw<850) {
    stage = 10; 
    delaySec =  900;
  } // 15 mins
  if(sw>=850 && sw<950) {
    stage = 11; 
    delaySec = 1200;
  } // 20 mins 
  if(sw>=950 && sw<1024) {
    stage = 12; 
    delaySec = 1800;
  } // 30 mins
}
///////////////////////////////////////////////////
void printLine(){
  Serial.print(" - Stage: ");
  Serial.print(stage);  
  Serial.print(" (");
  Serial.print(delaySec);    
  Serial.print(") - Next shot in: ");
  Serial.print(delaySec - currentSec);    
  Serial.println();
}
///////////////////////////////////////////////////

In order to calibrate the rotary switch, I had enabled the printLine() function (which is commented out in the loop() above and I got the following results on the terminal window (the first number on each line is the pin5 analog in reading):

serial_settings

Now it’s time to put everything in the box. I used my soldering iron to make the necessary holes on the box (I have a tip which I only use to make holes on plastic). Here is everything placed in the box for the first time (to make sure that everything fits the way it should).

dscf1099_w500

dscf1090_w500

The black surfaces you see on bottom of the box are foam boards; you can find them at art supplies shops. The foam boards are easy to cut and shape and, in this case, prevent the battery and the remote control moving around the box.

Bare box #1

My next task was to come up with a way of indicating the time interval of each one of the 12 stages of the rotary switch. I didn’t think twice – this was a nice and easy job for processing. I created this image, which I got printed as a 5″ x 7″ in matt paper at photobox.com (10 s = 10 seconds, 1 m = 1 minute):

frame_w500

The Processing code below created the above image:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
PFont myFont;
PFont myFont2;
 
float thisStage;
float nextStage;
 
float tp24 = TWO_PI/24;
 
void setup() {
  size(750, 1070);
  background(255);
 
  smooth();
  noStroke();
 
  float adj = -17 * tp24; 
  float space = TWO_PI/120;
  int diameter = 400;
 
  String[] stage = {    
    "10 s", "20 s", "30 s" , "1 m", 
    "1.5 m", "2 m", "3 m", "5 m", 
    "10 m", "15 m", "20 m", "30 m"  };
 
 
//remember to create the Arial-BoldMT-48 font
//otherwise this is not going to work...
  myFont = createFont("Arial-BoldMT-48", 18,true);
  myFont2 = createFont("Arial-BoldMT-48", 38,true);
 
  for (int i = 0; i < stage.length; i++){
 
    thisStage = (TWO_PI * i /12  - adj);
    nextStage = (TWO_PI *(i+1)/12 - adj);
 
    fill(0,50,100);
    noStroke();
    arc(width/2, 225, diameter, diameter, thisStage, nextStage);
    fill(255);
    arc(width/2, 225, diameter, diameter, nextStage  - space, nextStage);
    arc(width/2, 225, diameter, diameter, thisStage, thisStage + space);
 
    fill(150);
    stroke(150);
    textFont(myFont); 
 
    textAlign(CENTER,CENTER); 
    text(stage[i],(width/2) + diameter * 0.43 * cos( thisStage + tp24 ), 
225 + diameter * 0.43 *sin(thisStage + tp24 ));   
 
  }
 
  //this is the middle white circle
  fill(255);
  ellipse(width/2, 225, diameter * 0.6, diameter * 0.6);
 
  fill(0,50,100);
  noStroke();
  rect(0,0,150,150);
  rect(width-150,0,150,150);
  rect(0,450,width,150);
  rect(0,height-150,width,150);
 
  fill(0);
  stroke(0);
 
  textFont(myFont2); 
  textAlign(CENTER,CENTER); 
  text("Arduino-based Intervalometer 
\n for Fuji Digital SLRs", width/2,height -150 - (450/2) );   
 
  textFont(myFont); 
  textAlign(CENTER,CENTER); 
  text("www.arkadian.eu", width/2,height -200);   
 
  fill(0,50,100,100);
  noStroke();
  for(int x = 1; x < 30; x++){
    rect(int(random(0,width)),int(random(450,height-150)),20,20);
  }
  saveFrame("frame.png"); 
}

The 7″ x 5″ prints in matt paper (the glossy photo paper would get fingerprints):

dscf1121_w500

I had to make two more holes at the top part of the box to secure the cover.

Here is my intervalometer in its final form.

dscf1136_w500

dscf1125_w500

As you can see from the photo below, the intervals are clearly visible:

dscf1131_w500

That’s it, the end… In the next few weeks I will post a couple of videos put together using frames taken with this brand new intervalometer.

This must be my longest post ever…