Pages: [1]   Go Down
Author Topic: Pulsing ir emitter at my own frequency  (Read 1353 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-cry. 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.
Logged

0
Offline Offline
Shannon Member
****
Karma: 201
Posts: 11744
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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.
Logged

[ I won't respond to messages, use the forum please ]

Dubuque, Iowa, USA
Offline Offline
Edison Member
*
Karma: 45
Posts: 2474
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

The code uses a timer interrupt to modulate the beam.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. smiley-wink
Logged

Pages: [1]   Go Up
Jump to: