Attiny85 + 38 kHzcarrier + SoftwareSerial

Hi,

i'm trying to make an Attiny85 talk with an Arduino Yùn using IR and SoftwareSerial library. I've just solved the problem on the Yùn side, now it's time for the little tiny85. I have already done this using a Yùn and an Arduino Uno, and it works. I'm having some problems with the tiny85 because of (in my opinion) timers. I've never used them!

I burned the bootloader @ 8 MHz (internal).

First of all i followed this guide to check the SoftwareSerial: it works!

Then i followed this link to generate the 38 kHz carrier, and i realized that the SoftwareSerial doesn't work anymore. I also tried to make an LED blink, but the delay() function doesn't work after the carrier generation.

I'd like to know if the Attiny can use different timers to handle the 38 kHz carrier and the delay/SoftwareSerial part... If i must tell the truth i don't know if this is the right question. This is the first time i meet timers, even if i have been using Arduino for years :confused:

Any suggestion would be appreciated!

SoftwareSerial doesn't use a hardware timer though, as far as I can tell...

The link for generating the 38khz signal will take out delay on some cores, because only some cores use timer1 for timekeeping (which that code assumes) - the core you have uses timer0, so when you use timer0 to get 38khz output, that takes out timekeeping.

If you're using my attiny core (which uses timer0), I think you can do tone() on the OC1A pin (I think that's pin 3 or 4 - it's the third PWM pin, which not all cores support, because some people just threw their hands up in the air and didn't bother working with the more complicated Timer1) to get 38khz using the hardware timer.

Hi DrAzzy,

thank you for you answer! I installed your Attiny core. Here’s the configuration i used :

  • Attiny x5 series
  • Timer 1 clock : “CPU”
  • B.O.D. disabled
  • Attiny85
  • Clock 8 MHz (internal)
  • Programmer: Arduino as ISP

With this configuration the serial IR communication still doesn’t work. You mentioned the Timer 0, how can i use it?

The code i’m trying to use is:

//                           +-\/-+
//  Ain0       (D  5)  PB5  1|    |8   VCC
//  Ain3       (D  3)  PB3  2|    |7   PB2  (D  2)  INT0  Ain1
//  Ain2       (D  4)  PB4  3|    |6   PB1  (D  1)        pwm1
//                     GND  4|    |5   PB0  (D  0)        pwm0
//                           +----+

#include <SoftwareSerial.h>

#define pwmPin 0
#define ledPin 3
#define rx 5
#define tx 4

SoftwareSerial mySerial(rx, tx); 

void setup() {

  mySerial.begin(1200);
  pinMode(ledPin, OUTPUT);
  pinMode(pwmPin, OUTPUT);

  //38 kHz carrier using tone()
  tone(pwmPin, 38000);

}

void loop() {
  
  mySerial.println("Hello, world!");
  digitalWrite(ledPin, HIGH);
  delay(1000);
  digitalWrite(ledPin, LOW);
  delay(1000);

}

The IR led is connected between pin 0 and 4. The blink works well, but the arduino Yùn can’t receive the “Hello, world!” message. The speed (1200 baud) is the same.

PS. The softwareserial is working, i tried to connect directly the Attiny and the UNO by wire and the message is correctly received.

Just a thought, but is the IR LED polarity right? Since the hardware connection works...

I got curious about this topic, and tried it out. And it worked amazingly weel.
Even with a smal IR-diode I got a range of a couple of meters, and a steady reading on my UNO that I used as a receiver.

Below my sketch on the t85. I used timer1 to generate the 38 kHz.
The IR diode is connected to pin 1 and 4.

#include <SoftwareSerial.h>
#define pwmPin 1
#define ledPin 3
#define rx 5
#define tx 4
SoftwareSerial mySerial(rx, tx);

void setup() {
  mySerial.begin(1200);
  pinMode(ledPin, OUTPUT);
  pinMode(pwmPin, OUTPUT);
  OSCCAL = 0xAA;
  TCNT1 = 0;
  TCCR1 = 0;
  GTCCR |= (1 << PSR1); //section 13.3.2 reset the prescaler
  TCCR1 |= (1 << CTC1); // section 12.3.1 CTC mode
  TCCR1 |= (1 << COM1A0); //togle pin PB1 table 12-4
  TCCR1 |= (1 << CS10); //prescaler 1 table 12-5
  OCR1C = 104;
  OCR1A = 104;
}

void loop() {
  mySerial.println("Hello, world!");
  digitalWrite(ledPin, HIGH);
  delay(1000);
  digitalWrite(ledPin, LOW);
  delay(1000);

}