Go Down

Topic: Pulsing ir emitter at my own frequency (Read 1 time) previous topic - next topic

cyberteen

Hi guys,
I made an ir sensor manually by taking an ir  photodiode, a resistor and an ir receiver. I connect it to a2 analog pin of my UNO r3. I can detect changes in the values as I obstruct the path. I have no part no. Data sheet for this thing. All I want is to modulate the pulse of the ir emitter at say 40khz. Can some one give me a code to achieve that? I saw some posts in Internet, talking about the 3 timers of at mega 328p, I can't make a head or tail =(. Just pls help me out guys.

Note:
I don't have that sharp ir emitter thing which is already sending pulses at 38khz. I want to achieve that thing manually.

MarkT

Timers are a bit complex. yes (that's why they are so flexible though).  You can pulse the output directly in code using delayMicroseconds() like this:

Code: [Select]

void loop ()
{
  digitalWrite (.., HIGH) ;
  delayMicroseconds (12) ;
  digitalWrite (..., LOW) ;
  delayMicroseconds (13) ; // 12us + 13us = 25us, one cycle for 40kHz.
}


Except that at these speeds the time spent in the calls to digitalWrite will mean it will toggle somewhat slower than 40kHz...  You can try tuning the delay values, values of 6 and 7 seem to be best.

Or adopt a more proper approach like this:
Code: [Select]

unsigned long last_time = micros () ;

void loop ()
{
  while (micros() - last_time < 12)
  {}
  digitalWrite (3, HIGH) ;
  last_time += 12 ;
  while (micros() - last_time < 13)
  {}
  digitalWrite (3, LOW) ;
  last_time += 13 ;
}


However this produces a jittery output as micros() takes some time to execute.
[ I won't respond to messages, use the forum please ]

Chagrin

http://www.arcfn.com/2010/03/detecting-ir-beam-break-with-arduino-ir.html

The code uses a timer interrupt to modulate the beam.

cyberteen

Thank you very much, chagrin. This was exactly what I was looking for. :smiley-mr-green:
Actually I saw the same Piece of code in an instructable page. But there wasn't sufficient info about the code. This link explains it all.

And MarkT, I was actually running a code similar to yours using digitalWrite.Thanks still, for giving me a more efficient version. ;)

Go Up