Simple 38khz signal with attiny85


I am new to Arduino and microcontrollers and all I am looking for right now is a simple 38khz output signal from an attiny85. Now that I have got the USBasp programmer up and running I have tried a few sketches from around the web but none seem to work.

I have tried a 555 timer, which has been largely a waste of time as getting the exact resistors to make it work is a problem, using a trimpot is not reliable either as I have to keep re-tuning it from regular use. So I want to do it via software to make it more reliable and compact.

The example code in Arduino IDE for the blinking LED is a possibility if I can change the timing. Can that be done easily using the blink example or do I have to use entirely different code? I have uploaded and used the example successfully just by changing the output pins. Any help appreciated. Thanks

The example code in Arduino IDE for the blinking LED is a possibility if I can change the timing. Can that be done easily using the blink example or do I have to use entirely different code?

38kHz is a period of 26us, so something like:

while (1) {
   digitalWrite(pin, 0);
   digitalWrite(pin, 1);

should generate "approximately" 38kHz. (it's 10 rather than 13, because digitalWrite() takes a couple microseconds. It might need to be tuned.) Also look into timer and tone libraries...


It's not quite working yet though I am getting a dim, flickering signal on the receiver. The code you gave turned up errors:

Blink:27: error: expected unqualified-id before 'while'

 while (1) {


exit status 1
expected unqualified-id before 'while'

Still trying to work it all out...

This is the output:

38.26kHz at 50.2% duty cycle. (Clock at 1mHz, attiny85)

My final code is:

void setup() {

  pinMode(0, OUTPUT);

// the loop function runs over and over again forever
void loop() {
  digitalWrite(0, HIGH); 
  digitalWrite(0, LOW);    

Thanks for the help again. Now to boost the range of this thing...

Are you doing this for an IR led? There are ready-made libraries that deal with 38kHz modulated signals that might be easier to utilize than starting from scratch.


I have got it transmitting at the right frequency with only a little bit of code, I have found however that the TSOP 1738 receiver quickly shuts down when the transmitter is turned on, which means I will have to look at modulating the 38khz signal in pulses to stop the TSOP shutting off. I might have a slightly long winded solution.

It’s for a break-beam sensor type camera trap rig I am building. I need both break-beam and “make beam” modes. I have read how others have done it, but I would like to go a different way for more versatility…

Hi arduino_x,

I read somewhere that the TSOP will adjust to constant signals and reduce sensitivity to powerful signals. Some people have had the 38khz signal delivered in pulses to apparently avoid this. When the transmitter is either turned on or the LED (or TSOP) is covered then uncovered, the output LED I put in as a way to check status will glow then fade. A constant reaction for a constant signal would be the desired outcome

I have measured the cycles at 38.26khz, so it is constant for sure. I don't understand why it is being so fussy. I have also tried a TSOP 1738 ready made module for the same result, and a different TSOP 1738 on a different circuit for the same results.

Also, I am new to microcontrollers, and have searched and searched for easy answers to the 38khz problem but haven't found anything that seems to suit. How would I use the tone()? Any resources you know of?

Thanks again


Many TSOP receivers are made to work with intermittent signals (bursts) like used with infrared remote controls. If they get a continuous signal they will "shut down" after some seconds witch will make some trouble if you want to use it with a light barrier (not sure, if this is what you want to do). You should search for a TSOP receiver that does support "continuous mode" like this one (just one example)

arduino_x: But what does it do when it "shuts down"?

Normal operation (with bursts): If a burst of (let's say) 10 to 100 infrared impulses is received the receiver's output will go HIGH otherwise it will be LOW (or the other way round).

If a receiver that does not support "continuous mode" is getting continuous 38 kHz for several seconds it will go LOW for some time, even if the 38 kHz signal does not stop. So it will indicate "there is no 38 kHz signal", this may be confusing. :)

Hi again,

I’ve tried with a 555, and as much as I don’t like it, the TSOP has no issues with the constant signal. Despite the attiny putting out a much more accurate signal, the tsop does not like it… Any ideas? I tried the tone() idea with limited results. Distance and a constant functioning tsop with the attiny would be ideal…

The attiny stays on, if I block the LED for a moment and expose it again, the TSOP will start and shut down all over again. Really weird

In case anyone is still reading this thread, I figured out the problem, I used a TSSAL IR receiver which solved the shutting down issue and the circuit works perfectly, in fact a little too well as the signal bounces off walls. Easily fixed with a trimpot at the emitter end.