Posts Tagged ‘Infrared’

Arduino – Redefining the TV Remote

TVRemotes

We use them every day, but has no one got bored of pressing buttons on a stick, it’s far too much effort pressing buttons! Surely there are better ways to control a device? After doing some work with my Nikon camera using IR to control it, I wanted to do the same with other devices. Check out the video at the bottom of this post…

However, unlike the Nikon remote, my Samsung TV remote has many many buttons so each IR sequence sent from the remote will be different. This can be a problem when you want to decode the signals, which while not impossible I am lazy, so thankfully Ken Shirriff has built a library to do just that and while its built for TV remotes you can decode an IR signal to its raw pulses using it. Essentially the library senses IR and notes each pulse and gap between pulses, Kens library saves a lot of time and its well coded – I’ll cover the basics of it in a bit.

My idea is to capture the IR sequences and then using the Arduino send them by using different inputs other than buttons. My first idea is to use my SRF05 distance sensor (You can use any distance sensor) and the premise being that different distances from the sensor send different signals to the TV. So rather than pressing a button you just wave your hand above the sensor. Of course this is slightly limited but since I only have 5 channels (yep – only 5!) so it turned out to be quite feasible.

There are drawbacks to this of course – the main one being that you can only define so many actions in the sensors dectection range. But there is plenty of range to do the basics, power, sound and channel and by constantly measuring distances we can even say the direction of movement, up to down and vice versa, can have an effect on what signal to send. For example moving your hand closer to the sensor will change the channel down.

So first of all you may want to read some of my other tutorials/projects concerning IR and the SRF-05 and Sharp IR (it should also work well).
Arduino Nikon IR Intervalometer Camera Remote
SRF-05
– contains handy wiring diagram!
Arduino and Sharp GP2Y0A02 Infrared distance sensor

(Other Arduino projects and tutorials)

OK, next take a look at Ken Shirrifs IR library and guide here:
http://www.arcfn.com/2009/08/multi-protocol-infrared-remote-library.html

Arduino TV Remote Components

Arduino
Breadboard
IR Diode
3pin (NPN) Phototransistor/ IR receiver (
Radio Shack 276-640 IR receiver, Panasonic PNA4602, Vishay TSOP4838 – or just get one out an old mouse)
SRF-05 (or any distance measuring device e.g. Sharp IR GP2Y0A02)
Jumper wires

Oh and stating the obvious but you’ll also need a T.V with working remote to steal the signals from – course you can use other remotes (stereos etc..)

The circuits themselves are very very easy to build, an IR LED to pin 3, a IR receiver to pin 11 and the SRF-05 I’ve plugged into pins 2 and 4. I have all of them in one breadboard and it works very well (see below).

Using Kens Arduino TV Remote Library

If you download the library and then unzip it to your Arduino/Libaries directory (older versions, I think its Arduino/hardware/libaries). The library assumes that your phototransistor/ IR receiver is on digital pin 11 and your IR diode is on digital pin 3. Typically you want a IR receiver with a 38Khz range – they seem to work best for me.

How to get our TV infrared/ remote codes

First of all use Ken’s IRrecvDump example (should be in your examples menu) load this into your Arduino and begin to capture your remotes codes. My Samsung wasn’t recognised so I used the Raw codes – there’s plenty of documentation on Ken’s site for this – it’s really simple, even I could figure it out. You need to note how many pulses etc.. it decodes in the raw signal which helpfully is outputted e.g. Raw (68):

Now we process the codes slightly and put them in an array for each one now that we have our codes and the information we need to use them – since mine are in the raw format I need to clean them up slightly ready to be put in my code – just adding commas etc…

Now we can test the remote codes to make sure you can control your TV

Now using the IRsendDemo example, altering it my case to send the raw signal, we can test the codes to make sure that we can control the T.V – just use the basic sketch to send the codes which I edited slightly just to use an array for the raw code. You can check out the library files themselves to see the functions.

/*
 * IRremote: IRsendDemo - demonstrates sending IR codes with IRsend
 * An IR LED must be connected to Arduino PWM pin 3.
 * Version 0.1 July, 2009
 * Copyright 2009 Ken Shirriff
 * http://arcfn.com
 */

#include 

IRsend irsend;

// just added my own array for the raw signal
unsigned int powerOn[68] = {4450,4500,550,1700,500,1750,500,1750,500,600,550,600,500,600,550,600,500,600,550,1700,550,1700,550,1700,500,600,550,600,500,600,550,600,500,650,500,600,550,1700,500,650,500,600,550,600,500,600,550,600,500,600,550,1700,550,600,500,1700,550,1700,550,1700,550,1700,500,1750,500,1750,500};

void setup()
{
  Serial.begin(9600);
}

void loop() {

      // altered the code just to send/test my raw code
      irsend.sendRaw(powerOn,68,38);
      delay(100);

}

Add the distance sensor

This is actually the hardest bit and it’s not that hard really I just used my previous work and adapted it and wrote a few statements concerning the detected distance. You just have to spend some time debugging and getting your values right to ensure that your commands are only sent at the right time and that it doesn’t get confused. My code is still a little buggy if you’re not used to how to move your hand but it does work well once you’re used to it.

[ad#Google Ad in content]

/*
    http://luckylarry.co.uk
    Larrys alternative TV remote - oops no buttons!
    Sends signals to TV based upon sensor readings

    Makes use of Kens Shirriffs IRremote library
    An IR LED must be connected to Arduino PWM pin 3.
    Version 0.1 July, 2009
    Copyright 2009 Ken Shirriff
    http://arcfn.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see .
*/

#include
IRsend irsend;

const int numReadings = 5;   // set a variable for the number of readings to take
int index = 0;                // the index of the current reading
int total = 0;                // the total of all readings
int average = 0;              // the average
int oldAverage = 0;           // the old average
int echoPin = 2;              // the SRF05's echo pin
int initPin = 4;              // the SRF05's init pin
unsigned long pulseTime = 0;  // variable for reading the pulse
unsigned long distance = 0;   // variable for storing distance

// setup my arrays for each signal I want to send
unsigned int powerOn[68] = {4450,4500,550,1700,500,1750,500,1750,500,600,550,600,500,600,550,600,500,600,550,1700,550,1700,550,1700,500,600,550,600,500,600,550,600,500,650,500,600,550,1700,500,650,500,600,550,600,500,600,550,600,500,600,550,1700,550,600,500,1700,550,1700,550,1700,550,1700,500,1750,500,1750,500};
unsigned int soundUp[68] = {4450,4500,550,1700,550,1700,500,1750,500,600,550,600,500,600,550,600,500,600,550,1700,550,1700,550,1700,500,650,500,600,550,600,500,600,550,600,500,1750,500,1700,550,1700,550,600,500,600,550,600,500,600,550,600,500,600,550,600,550,600,500,1700,550,1700,550,1700,550,1700,500,1750,500};
unsigned int soundDown[68] = {4400,4550,500,1750,500,1700,550,1700,550,600,500,600,550,600,500,600,550,600,500,1750,500,1750,500,1700,550,600,500,650,500,600,550,600,500,600,550,1700,550,1700,500,600,550,1700,550,600,500,600,550,600,500,600,550,600,500,600,550,1700,550,600,500,1750,500,1750,500,1700,550,1700,550};
unsigned int channelUp[68] = {4400,4550,500,1700,550,1700,550,1700,550,600,500,600,550,600,500,600,550,600,500,1750,500,1700,550,1700,550,600,500,600,550,600,500,650,500,600,550,600,500,1700,550,600,550,600,500,1700,550,600,500,650,500,600,550,1700,500,600,550,1700,550,1700,550,600,500,1700,550,1700,550,1700,550};
unsigned int channelDown[68] = {4450,4500,500,1750,500,1750,500,1700,550,600,500,650,500,600,550,600,500,600,550,1700,500,1750,500,1750,500,600,550,600,500,600,550,600,500,600,550,600,500,650,500,600,550,600,500,1700,550,600,500,650,500,600,500,1750,500,1750,500,1750,500,1700,550,600,500,1750,500,1750,500,1700,550};

void setup() {
  // make the init pin an output:
  pinMode(initPin, OUTPUT);
  // make the echo pin an input:
  pinMode(echoPin, INPUT);
  // initialize the serial port:
  Serial.begin(9600);
}

 void loop() {

    // loop for a number of readings on the SRF-05 to get an average to smooth the results. Much like all my other examples
    for (index = 0; index<=numReadings;index++) {
      digitalWrite(initPin, LOW);
      delayMicroseconds(50);
      digitalWrite(initPin, HIGH);
      delayMicroseconds(50);
      digitalWrite(initPin, LOW);
      pulseTime = pulseIn(echoPin, HIGH);
      distance = pulseTime/58;
      total = total + distance;
      delay(10);
   }
    // store the previous reading
    oldAverage = average;
    // store the current reading
    average = total/numReadings;
    // debug to check for spikes in the sensor etc..
    Serial.println(average);

    // now the fun part...
    // if my distance is less than 5...
    if (average <= 5) {
      Serial.println("Power Off");
      // use Kens IR library to send my signal (array, number of items in array, Khz)
      irsend.sendRaw(powerOn,68,38);
      // these delays depend on how long it take my device to recognise the signal sent and to act - I don't want to send signals that aren't getting read etc..
      delay(5000);
      // otherwise if my hand is higher
    } else {
      // check to see if my hand is in the registered space above the sensor
      if (average <=20 && average >=10 && oldAverage >=10) {
        // the below statement is our sensitive the readings are so if the current and previous readings are different with a tolerance of +/- 1 we can look at the direction of movement
        if ((average != oldAverage)
        && (average+1 != oldAverage)
        && (average-1 != oldAverage)) {
          // if the current reading is higher than the previous, then my hand is moving upwards
          if (average > oldAverage) {
            Serial.println("Channel Up");
            irsend.sendRaw(channelUp,68,38);
            delay(2000);
          } else {
            // otherwise if it is below then my hand is moving downwards
            if (average < oldAverage && oldAverage <=20) {               Serial.println("Channel Down");               irsend.sendRaw(channelDown,68,38);               delay(2000);                        }                  }          // otherwise my hand must be stationary so check where it is.         } else {           // if my hand is stationary between 10 and 15 cms away from the sensor           if (average >= 10 && average <=15) {             Serial.println("Sound down");             irsend.sendRaw(soundDown,68,38);           } else {             // if my hand is a bit higher up...             if (average >= 16 && average <=20) {               Serial.println("Sound up");               irsend.sendRaw(soundUp,68,38);             }           }         }         }     }        // clear our index and total for the next reading just in case     if (index >= numReadings)  {
      index = 0;
      total = 0;
    }
}

[ad#Google Ad in content]

Arduino – (Very) Basic motion tracking with 2 PIR sensors

Arduino 2PIR motion tracker

Took me a little while to get started but I’ve managed to wire 2 PIR infrared sensors with an Arduino to sense motion either on the left or on the right side. The result will trigger an LED to represent each PIR sensor then I also added in a servo to be controlled – so it turns left when triggered by the left sensor and so on.

First have a look at my previous tutorial – Arduino PIR motion detector circuit. Now we’re going to use 2 of them and this isn’t that different from just using one of them. However this is only a first attempt so I’ve not calibrated anything properly, for instance we can’t ascertain velocity or true direction of movement because these sensors aren’t that precise and have a wide range of detection. But hey, it’s a start!

Arduino PIR Motion Sensor Circuit Parts

2x 220 Ohm resistor (Red, Red, Brown, Gold)
2x 10K Ohm resistor (Brown, Black, Orange, Gold)
2x PIR sensor
1x Servo (has to need no more than 5v supply)
2x LED
Arduino Deumilanove w/ ATMEGA328
Breadboard / Prototyping board
Jumper/ Connector wires
Optional 9V DC power supply or use the USB power for the Arduino
You will also need a soldering iron and solder if you use the same PIR as myself.
Some sort of  temporary adhesive to hold the sensors in place.

Arduino Infrared Motion Detector Circuit

So you’ll see that its really just a lot of wires. The PIRs I’ve soldered on the wires and on their output pin there’s a 10K Ohm resistor for each going between them and the positive rail on the breadboard. The LED’s are the same layout as the basic blink tutorials, 220 Ohm resistor between the positive pin (the longer one) and the Arduino pin.

Arduino-2PIR-motion-tracker

PIR Motion Sensor Arduino Code

This isn’t too bad. Basically we set 2 sensors and have a few if statements to do shit based on if they turn on or off, so essentially they’re treated just like a couple of switches.
[ad#Google Ad in content]

/*
LUCKYLARRY.CO.UK - 2 3pin PIR sensors to track basic motion.

We have 1 sensor for left, 1 for right.

The left sensor is triggered, the LED for the left comes on and the servo moves until no motion is detected.
The same happens if the right sensor is triggered.

If both sensors detect motion then its likely the object may be between the 2 but given the field of detection
its not going to be precice. Enjoy!

*/

#include                                                 // Include servo library, you can get it from http://www.arduino.cc/playground/ComponentLib/Servo
Servo myservo;                                                    // Create a servo object
int pos = 0;                                                      // Variable to store the servo position in degrees
int pinPIRleft = 4;                                               // left infrared sensor, digital pin 4
int pinLEDleft = 8;	                                          // left LED, digital pin 8
int pinPIRright = 2;                                              // right sensor, digital pin 2
int pinLEDright = 10;                                             // right LED, digital pin 10

void setup() {
  pinMode(pinLEDleft, OUTPUT);                                    // set LEDs as outputs
  pinMode(pinLEDright, OUTPUT);
  pinMode(pinPIRleft, INPUT);                                     // set sensors as inputs
  pinMode(pinPIRright, INPUT);
  myservo.attach(9);                                              // set the servo to digital pin 9
}

void loop() {

  if (digitalRead(pinPIRleft) == LOW) {                           // if left detects motion
    digitalWrite(pinLEDleft, HIGH);                               // turn on LED
    if ((pos < 180) && (digitalRead(pinPIRright) == HIGH)) {      // if less than 180 degrees and the right sensor is off then move servo
      pos += 1;                                                   // increment servo degrees by +1
      myservo.write(pos);                                         // write the position to the servo
      delay(15);
    }
  } else {
    digitalWrite(pinLEDleft, LOW);                                // otherwise turn off LED and no servo movement
  }

  if (digitalRead(pinPIRright) == LOW) {
    digitalWrite(pinLEDright, HIGH);
    if ((pos >= 1) && (digitalRead(pinPIRleft) == HIGH)) {
      pos -= 1;
      myservo.write(pos);
      delay(15);
    }
  } else {
    digitalWrite(pinLEDright, LOW);
  }

  if ((digitalRead(pinPIRleft) == LOW) && (digitalRead(pinPIRright) == LOW)) {
    // do something here if both sensors detect movement.
  }
}

[ad#Google Ad in content]

And here it is in action:

Well, you could add a 3rd sensor to validate that an object is in front of both sensors, you’ll see that I’ve angled my sensors to try and work this with just 2 sensors. Perhaps limiting the field of detection for each sensor might also be a good thing to make it more precise.

Arduino – motion triggered camera

Nikon D80 Motion sensor trigger

So having worked out that I can make a remote for my Nikon D80 to do some timelapse photography. I started thinking of things I can do to trigger the remote, below video shows it working but you’ll need sound to hear the shutter going.

First of all check out my tutorial for making the remote as this builds on that tutorial – also saves me having to recap and explain that. Arduino Nikon intervalometer infrared remote for timelapse photography.

Ok so now you know how I’m going to trigger the camera to take pictures. So now we need the sensor, for this I’m going to use a PIR (Pyroelectric InfraRed sensor) IC that works like a switch turning on when motion is detected, the one I’m using is basically a couple of IR emitters and sensors under a Fresnel lens – word of warning make sure you get the power supply right as they tend to smoke otherwise!. It works by measuring any difference to the background temperature and infrared radiation. I’ve also added an LED to signal when the PIR is on or off.

The biggest challenge for this was making sure the camera didn’t keep triggering for the length of time the PIR was active – sometimes it would be active for 5 or seconds after initial movement, so I wanted to capture say 2 photos for each time it was triggered.

For lazyness I’ve left my camera in auto setting so that it *should* autofocus along with everything else – e.g. exposure times and apertures.

Arduino Motion Tracker Parts

220 Ohm resistor (Red, Red, Brown, Gold)
10K Ohm resistor (Brown, Black, Orange, Gold)
PIR sensor
Infrared emitting diode
LED
Arduino Deumilanove w/ ATMEGA328
Breadboard / Prototyping board
Jumper/ Connector wires
Optional 9V power supply (here) or use the USB power for the Arduino
You will also need a soldering iron and solder if you use the same PIR as myself.

The Arduino Motion Detector Circuit

You may not be able to see but I’ve used the power supply from the Arduino to power the breadboard. The PIR gets its supply from this and there’s a 10k Ohm resistor between its output pin and pin 4 on the Arduino. The LED has a 220 Ohm resistor between that and the digital pin 9. The IR LED I’ve left in pin 13 and grounded – for a better system I would solder on a couple of wires to allow more flexibility with transmitting the signal. Also for the PIR, the one I have I had to solder a few jumper wires into the back of it just so you know.

arduino-camera-motion-trigger

Arduino Motion Sketch / Code

Based off of my remote tutorial I’ve just added in a few extra things really. An output for the LED, an input for the PIR and a chunk of code to limit the taking of photos.
[ad#Google Ad in content]

/*

LUCKYLARRY.CO.UK - IR Remote control for Nikon using Arduino with motion trigger

Uses a PIR sensor to trigger the camera remote/ IR led if a change in background
temperature is detected.

*/

int currentState = 0;                                   // set a variable to store a count.
int pinPIR = 4;                                         // digital pin 4 for PIR
int pinLED = 9;                                         // digital pin 9 for LED
int pinIRLED = 13;                                      // assign the Infrared emitter/ diode to pin 13

void setup() {
  pinMode(pinIRLED, OUTPUT);                            // set the pin as an output
  pinMode(pinLED, OUTPUT);                              // set the LED pin as ouput
  pinMode(pinPIR, INPUT);                               // set the PIR pin as an input
}

// sets the pulse of the IR signal.
void pulseON(int pulseTime) {
  unsigned long endPulse = micros() + pulseTime;        // create the microseconds to pulse for
  while( micros() < endPulse) {
    digitalWrite(pinIRLED, HIGH);                       // turn IR on
    delayMicroseconds(13);                              // half the clock cycle for 38Khz (26.32×10-6s) - e.g. the 'on' part of our wave
    digitalWrite(pinIRLED, LOW);                        // turn IR off
    delayMicroseconds(13);                              // delay for the other half of the cycle to generate wave/ oscillation
  }

}

void pulseOFF(unsigned long startDelay) {
  unsigned long endDelay = micros() + startDelay;       // create the microseconds to delay for
  while(micros() < endDelay);
}

void takePicture() {
  for (int i=0; i < 2; i++) {
    pulseON(2000);                                      // pulse for 2000 uS (Microseconds)
    pulseOFF(27850);                                    // turn pulse off for 27850 us
    pulseON(390);                                       // and so on
    pulseOFF(1580);
    pulseON(410);
    pulseOFF(3580);
    pulseON(400);
    pulseOFF(63200);
  }                                                     // loop the signal twice.
}

void loop() {

  if ((digitalRead(pinPIR) == LOW) && (currentState <= 2)) { // count to limit the taking of photos
    takePicture();                                      // take the picture
    digitalWrite(pinLED, HIGH);                         // turn LED on
  } else {
    digitalWrite(pinLED, LOW);
    currentState = 0;                                   //reset the count when the PIR is off.
  }

  currentState++;
  delay(2000);                                          // delay for 2 seconds - 2 seconds between taking photos if the PIR is active for more than 2 seconds.
}

[ad#Google Ad in content]

I think adding in another PIR would help me make some kind of turret system. Also you could use the ultrasonic range finder jobby, the SRF-05, to detect the distance and if less than say 3 metres take the photo etc.. I'm also going to try and setup a trip-wire system to trigger the camera :)

Arduino – IR remote/ intervalometer for Nikon D80 DSLR (that means timelapse photography yarrr!)

Nikon D80 + Arduino remote trigger/ intervalometer

I’m cheap and skint, yet I want to do timelapse photography with my Nikon D80 DSLR. Unfortnately that requires spending some cash on an intervalometer for time lapse photography which will set me back a sizeable chunk of cash. Or I could get a remote or get the trigger system then create a delay mechanism to do the timelapse. But again it’d cost a few quid to even get a remote…

Thankfully I already have an Arduino board and a bag of Infrared emitter diodes which I was wondering what I could use them for. So I had a quick scout round the interweb and saw various projects where people had written programs to allow Arduino to work as a TV remote etc.. and I stumbled up on this site: http://www.bigmike.it/ircontrol/ which listed the very IR timing sequence and frequency I would need to trigger my camera. I’m guessing you can find other sequences/ frequencies for other bits of hardware too.

There is no point in me writing up a circuit diagram or parts list for this as you just need an IR diode and an Arduino board. Oh and check that your camera has an Infrared remote port on it or else this is pointless!

Arduino Nikon Intervalometer Remote Code

You will see that basically we blink an IR LED for a set time, wait and repeat to create our signal. The only complicated bits are working out the delays to create the pulse cycle/ wave. Turns out Arduino isn’t so hot at measuring delays in Microseconds so we need to give it a hand keeping track using the micros() function – so we just create a counter to do this and specify an end time for it to count up to.
[ad#Google Ad in content]

/*

LUCKYLARRY.CO.UK - IR Remote control for Nikon using Arduino

Mimics the infrared signal to trigger the remote for any Nikon camera
which can use the ML-L1 and ML-L3 remotes. Can be used as an intervalometer
for time lapse photography.

The IR sequence I used is originally taken from: http://www.bigmike.it/ircontrol/

You should be able to use my pulse methods to alter to suit other cameras/ hardware.

micros() is an Arduino function that calls the time in Microseconds since your program
first ran. Arduino doesn't reliably work with microseconds so we work our timings by
taking the current reading and then adding our delay on to the end of it rather than rely
on the in built timer.

*/

int pinIRLED = 13;                                      // assign the Infrared emitter/ diode to pin 13

void setup() {
  pinMode(pinIRLED, OUTPUT);                            // set the pin as an output
}

// sets the pulse of the IR signal.
void pulseON(int pulseTime) {
  unsigned long endPulse = micros() + pulseTime;        // create the microseconds to pulse for
  while( micros() < endPulse) {
    digitalWrite(pinIRLED, HIGH);                       // turn IR on
    delayMicroseconds(13);                              // half the clock cycle for 38Khz (26.32×10-6s) - e.g. the 'on' part of our wave
    digitalWrite(pinIRLED, LOW);                        // turn IR off
    delayMicroseconds(13);                              // delay for the other half of the cycle to generate wave/ oscillation
  }

}

void pulseOFF(unsigned long startDelay) {
  unsigned long endDelay = micros() + startDelay;       // create the microseconds to delay for
  while(micros() < endDelay);
}

void takePicture() {
  for (int i=0; i < 2; i++) {
    pulseON(2000);                                      // pulse for 2000 uS (Microseconds)
    pulseOFF(27850);                                    // turn pulse off for 27850 us
    pulseON(390);                                       // and so on
    pulseOFF(1580);
    pulseON(410);
    pulseOFF(3580);
    pulseON(400);
    pulseOFF(63200);
  }                                                     // loop the signal twice.
}

void loop() {
  takePicture();                                        // take the picture
  delay(5000);                                          // delay in milliseconds which allows us to do timelapse photography - 1 second = 1000 milliseconds
}

[ad#Google Ad in content]

Ok, so one other thing when using your camera and thats a quick modification to the remote timer, if like mine your infrared camera remote port is set to be active for less than a minute then you'll need to edit the settings accordingly - just check your owner manual. For me it's in the menu screen, custom setting menu, then option 30: Remote on duration.

Now I just got to take some cool timelapse stuff like my friends here:

Which also reminds me to look into CHDK and my Canon Powershot A530 and see what I can do there. :)