Pages: [1]   Go Down
Author Topic: ATtiny85 SoftSerial, takes a while to stablise?  (Read 712 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 141
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
#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:
a



a



a


?


a



a



a



a



a



a
a
a
a
a
a
a

Where as I would expect just:
Code:
a
a
a
a
a
a
a
a

Any thoughts as to why this happens?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Full Member
***
Karma: 0
Posts: 141
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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>" ?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Full Member
***
Karma: 0
Posts: 141
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Mega:
Code:
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:
#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:
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
Logged

Denmark
Offline Offline
Edison Member
*
Karma: 35
Posts: 1051
Happy Hobbyist
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 141
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

Denmark
Offline Offline
Edison Member
*
Karma: 35
Posts: 1051
Happy Hobbyist
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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



Logged

Denmark
Offline Offline
Edison Member
*
Karma: 35
Posts: 1051
Happy Hobbyist
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

And select "Burn Bootloader"  ?
Logged

Pages: [1]   Go Up
Jump to: