Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 2 of 2

Thread: tone() vs. analogWrite() for Lasertag IR

  1. #1

    tone() vs. analogWrite() for Lasertag IR

    I am currently working on a Lasertag project, using two Teensies (one for IR/Game management, and one for sound)
    Project Video:
    Populated PCB:

    Currently, I am using a HEXFET on the Teensy 3.1's pin, to drive an IR LED (TSAL6100) @ 56Khz in SIRC encoding (600us pulses)
    The IR is too strong for indoors, so I'd like to have variable control over the power of the LED, without having to resort to a potentiometer.

    In code, I'm presently using the tone & noTone functions where IROUTPIN = 20, FREQUENCY = 56000, IRpulse = 600, and pulselength = 1 or 2.
    From what I understand, this gives me a square wave with no control over the duty cycle:

    void sendPulse(int pulseLength)
        // Send Bit 
        delayMicroseconds(IRpulse * pulseLength); // (1200us = 2, 600us = 1)
        // Send 600us Break
        delayMicroseconds(IRpulse * pulseLength);

    Could I have control of the duty cycle and thus the IR power, if I use analogWrite(), after it is setup with:

    void setup() 
        analogWriteFrequency(IROUTPIN , FREQUENCY );
    Are there any "gotchas" when doing it this way?
    Last edited by Blitter; 10-02-2015 at 06:40 AM.

  2. #2
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Colmar, France
    Using PWM will allow you to set the analogWriteFrequency (56kHz), the resolution analogWriteResolution (10bit) and the duty cycle with analogWrite (1 to 1023) independently. To stop the pulsing, it is enough to do an analogWrite of 0 to the pin.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts