Go Down

Topic: ATtiny85 SoftSerial, takes a while to stablise? (Read 854 times) previous topic - next topic

davivid

I'm experimenting with Serial communication with an ATtiny85 and the SoftwareSerial Library, this simple example kind of works but only after a few seconds. To begin with the ATtiny only seems to respond correctly around 50% of the time, but after a couple of seconds it  functions correctly. Take this example code, which simply sends the character "a" to the Attiny, which echoes it back to the Mega, which then echos it to the PC:

Arduino Mega:
Code: [Select]
void setup()
{
Serial.begin(4800);
Serial1.begin(4800);
}

void loop()
{
if (Serial1.available() > 0)
{
char c = Serial1.read();
Serial.println(c);
}

Serial1.print("a");
delay(100);
}


ATtiny85
Code: [Select]

#include <SoftwareSerial.h>
const int rx=2;
const int tx=3;
const int led=0;

SoftwareSerial mySerial(rx, tx);

void setup()
{
  pinMode(rx,INPUT);
  pinMode(tx,OUTPUT);
  pinMode(led,OUTPUT);
  mySerial.begin(4800);
}

void loop()
{
  if (mySerial.available()>0)
  {
     digitalWrite(led, HIGH);
     char c = mySerial.read();
     mySerial.println(c);
   }
   
   digitalWrite(led, LOW);
}


Then on  the serial monitor I might see something like this:
Code: [Select]
a



a



a


?


a



a



a



a



a



a
a
a
a
a
a
a


Where as I would expect just:
Code: [Select]
a
a
a
a
a
a
a
a


Any thoughts as to why this happens?

PaulS

You send an "a". The 85 sends back "a<cr><lf>". Why?

davivid


You send an "a". The 85 sends back "a<cr><lf>". Why?


Well spotted thanks.

So I guess my question changes to: Why does the 85 start by sending "a<cr><lf>", but then changes to just "a<cr>" ?

PaulS

Does the same behavior occur if you add delay(100); to setup(), at the end? Perhaps you are trying to read data before the 85 is quite ready.

davivid

Yeah same behavior with that delay. Another example showing weird behavior:

Mega:
Code: [Select]
const int led=13;

void setup()
{
pinMode(led, OUTPUT);
Serial.begin(4800);
Serial1.begin(4800);
delay(100);
}

void loop()
{
if (Serial1.available() > 0)
{
char c = Serial1.read();
Serial.println(c);
}
digitalWrite(led, HIGH);
Serial1.print("hello");
delay(50);
digitalWrite(led, LOW);
delay(50);
}


85:
Code: [Select]
#include <SoftwareSerial.h>
const int rx=2;
const int tx=3;
const int led=0;

SoftwareSerial mySerial(rx, tx);

void setup()
{
  pinMode(rx,INPUT);
  pinMode(tx,OUTPUT);
  pinMode(led,OUTPUT);
  mySerial.begin(4800);
  delay(100);
}

void loop()
{
  if (mySerial.available()>0)
  {
     digitalWrite(led, HIGH);
     char c = mySerial.read();
     mySerial.print(c);
   }
   
   digitalWrite(led, LOW);
}


Arduino Serial Monitor
Code: [Select]
h
Ø
±
½
ÿ
h
Ø
±
½
ÿ
h
Ø
±
½
ÿ
h
¸
±
½
ÿ
h
Ø
±
½
ÿ
h
Ø
±
½
ÿ
h
Ø
±
½
ÿ
h
Ø
±
½
ÿ
h
Ø
±
½
ÿ
h
Ø
±
½
ÿ
h
¸
±
½
ÿ
h
Ø
±
½
ÿ
h
?
±
½
ÿ
h
Ø
±
½
ÿ
h
Ø
±
½
ÿ
h
Ø
±
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
h
e
l
l
o
h
e
l
l
o
h
e
l
l
o
h
e
l
l
o
h
e
l
l
o
h
e
l
l
o
h
e
l
l
o
h
e
l
l
o
h
e
l
l
o
h
e
l
l
o


Erni

What clock speed are you using, try 8MHZ

The internal oscillator is not  particularly precise which can affect the serial communication, but you can tune it

http://arduino.cc/forum/index.php/topic,8553.0.html

davivid

Yeah I'm selecting ATtiny85 (internal 8Mhz Clock) from the boards menu. Thanks for the link, although I would like a solution that will not involve so much calibration since I plan to use a lot of ATiny85's! (or similar)

Erni

If you only need one way communication (from the Tiny to PC) you could use TinyDebugSerial or TinyISP and the knockbang library:

http://www.ernstc.dk/arduino/tinycom.html




Erni

Quote
Yeah I'm selecting ATtiny85 (internal 8Mhz Clock) from the boards menu.


And select "Burn Bootloader"  ?

Go Up