ATtiny and ATmega serial communication not working

Hi guys,
I’m getting trouble with this simple code. I’m trying to put on a communication pipe between an Attiny85 and an Atmega328P. I’m sending single characters.
This is my scheme:

Bluetooth(HC-06) -----------> Attiny85---------------------> Atmega328. ( I’m starting with just single way )

They seems to talk to each other but the data i sent is totally vanished when i try to display it on the Atmega serial console.
Here i report my sketches:

Attiny85:

#include <SoftwareSerial.h>

SoftwareSerial FromB(3,4); // Serial data from HC-06
SoftwareSerial ToMega(1,2); //Serial data to be sent to atmega

void setup()  
{
  ToMega.begin(9600);
  pinMode(0,OUTPUT); // put just a led to see if there was  life on attiny planet 
  FromB.begin(9600);
}

void loop() // run over and over
{
  if (FromB.available()){
     char c=FromB.read();
     while(FromB.available()){ // getting rid of doubled data
    int temp=FromB.read();
    delay(1);
     }
    ToMega.write(c);   //writing to atmega : tried both .write and .print
    ToMega.print(c);
    digitalWrite(0,HIGH);   //Houston , we have life on attiny
    delay(200);
    digitalWrite(0,LOW);
}
}

Atmega code:

void setup() {
  
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  if(Serial.available()){
    Serial.println(Serial.read());}
delay(100);
}

And here’s the result!:
SENT CHARCTER via Putty ------------------> CHARACTER RECEIVED ON ATMEGA SERIAL MONITOR
A ---------------------> 128
B ---------------------> 128
C ---------------------> 128
D ---------------------> 128
E ---------------------> 128
F ---------------------> 128
G ---------------------> 128
a ---------------------> 128
b ---------------------> 128

And so on…
Can someone help me?
I suppose that 128 is the NULL character…
Where is my fault?

#include <SoftwareSerial.h>

SoftwareSerial FromB(3,4); // Serial data from HC-06
SoftwareSerial ToMega(1,2); //Serial data to be sent to atmega

Are you absolutely positive that you can run two instances of SoftwareSerial on the ATTiny? Are you absolutely positive that the instances are using pins that can be used?

     while(FromB.available()){ // getting rid of doubled data
    int temp=FromB.read();
    delay(1);
     }

There is data to read and discard. What the f**k is the delay() for?

How are the ATMega and ATTiny85 connected? Do you want to use the hardware serial port to talk to the ATTiny OR to talk to the PC? Both is the wrong answer. So is yes.

Hi Paul,

Are you absolutely positive that you can run two instances of SoftwareSerial on the ATTiny? Are you absolutely positive that the instances are using pins that can be used?

Yes I am. I've googled large and wide and didn't find anything about SoftwareSerial restriction on attinys ( both pins and instances use) . There were some example with attiny + bluetooth, some of them were using pin 3,4 , some 1,2 , some 0,1 so i guess you can use all the pins with no difference.

There is data to read and discard. What the f**k is the delay() for?

The delay(1) line helps getting rid of problems while reading more than a single character ( like missing characters or doubled characters due to transmission problems) . Took it from a an old project where i had to use that line to get correct datas.

Here's my wiring (sorry for my bad drawing skills) :

My goal : Attiny will receive a message ( in particular a single character) from the bluetooth module and it will forward the message to the Arduino UNO board ( for now) / ATMEGA328 in my fnished project. Just a single way HC-06 ----> Attiny ----> Atmega communication . I'm using the UNO hardware serial just for debugging

I'm using the UNO hardware serial just for debugging

But it may be interfering with using it to talk to the bluetooth module.

This could be possibile. But i cannot see any other way to debug my code…
Moreover, in my final project, i won’t display data on serial monitor , but i’ll have a serial port that acquires data from attiny.
Here’s the matter!

If you only want to feed data into the Uno from the Attiny and then display the data on the Serial monitor it would be sufficient to connect the Attiny ONLY to the Rx pin on the Uno.

I have my doubts about SoftwareSerial working on an Attiny. I think one of the Forum contributors has an Attiny core called tinyDebug (??) which has serial debugging stuff. And I know I wrote my own serial code for a 1MHz Attiny.

...R

Actually there are many options for debugging code on the Attiny85

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

I use TinyISP and Tinyknockbang. It is very easy to use, it use on of the programming lines to communicate with the tiny.

@Erni & Robin2

Thanks guys!! i will certainly try the debug solution you suggested me. I didn't know them at all.
Finally i got something working. I established the "3 point" connection , and i can finally receive my data on the Uno/mega. But problems stills up. In fact, if i send a single character , i receive it correctly ( ascii codes where shifted up! so i had to make somthing int cmd = Serial.read(); cmd= cmd -128; to get the correct value.). If i send a string of characters, the data i receive is totally weird and uncorrect.

For example: Sent " A188255K " -----> received " R634t:Pé°*?=)($r2"&£çK+)&%%£$F£Gg;
That's not only uncorrect but even way longer than the code expected.
I must work on it.
But thank you all for the help :slight_smile:

nicostak:
For example: Sent " A188255K " -----> received " R634t:Pé°*?=)($r2"&£çK+)&%%£$F£Gg;

Try examples that have a simple sequence so that you might be able to recognize a pattern in the error. For example "AAAAA" and "ABCDE".

If subtracting 128 consistently solves part of the problem it looks like there may be a systematic error. But, sorry, it is too much trouble to program an Attiny to try your code (and I only have Attiny 45s).

By the way have you connected the HC06 directly to the Uno just to make sure that it is receiving correctly?

And another thought - is there any possibility the device generating the test data is producing Unicode - which, I think, require 2 bytes for every char. (It's a while since I read about Unicode).

And another - what sort of clock is driving the Attiny. If the clock is not stable and accurate SoftwareSerial may not work properly - perhaps no identifying stop bits properly. That might account for the rubbish with multiple characters. There is also a risk that the two instances of SoftwareSerial are interfering with each other. It may be worth stopping one while the other is working.

...R

Hi Robin2,

By the way have you connected the HC06 directly to the Uno just to make sure that it is receiving correctly?

Yeah, i tried that ( to be honest, i started with that and the tiny came later )

And another thought - is there any possibility the device generating the test data is producing Unicode - which, I think, require 2 bytes for every char. (It’s a while since I read about Unicode).

That’s a really big hint , i totally forgot that possibility. I’m going to verify soon.

And another - what sort of clock is driving the Attiny. If the clock is not stable and accurate SoftwareSerial may not work properly - perhaps no identifying stop bits properly. That might account for the rubbish with multiple characters. There is also a risk that the two instances of SoftwareSerial are interfering with each other. It may be worth stopping one while the other is working.

Attiny is running on internal 8MHz clock. i thought about let it work with an external 16 MHz clock to use the same clock of the UNO board, but i do need the serial pins for the communication.
Stopping the serial port is a great idea that i’m going to try right now!
Tomorrow i will post the results
Thank you again!

I know this is old, but I stumbled upon it and it gave me an idea of how to solve this same problem. At least it is very close to the same.

I am using an attiny85 with a Bluetooth board (HC-10) and SoftwareSerial. I am sending hex from a Bluetooth app to the attiny85 and echoing it back. Any character (hex) I would send would result in getting back 128 (or 0x80 to be precise).

The last part of this post gave me the hint of what the problem was. I too changed the build options to use 8Mhz internal clock. This was the problem. Once I switched it back to 1Mhz clock, it worked perfectly.

I'm guessing that most projects with attiny85 don't need to run at 8Mhz anyway so I'm perfectly happy with the result.

BonEvil:
I know this is old, but I stumbled upon it and it gave me an idea of how to solve this same problem. At least it is very close to the same.

I am using an attiny85 with a Bluetooth board (HC-10) and SoftwareSerial. I am sending hex from a Bluetooth app to the attiny85 and echoing it back. Any character (hex) I would send would result in getting back 128 (or 0x80 to be precise).

The last part of this post gave me the hint of what the problem was. I too changed the build options to use 8Mhz internal clock. This was the problem. Once I switched it back to 1Mhz clock, it worked perfectly.

I'm guessing that most projects with attiny85 don't need to run at 8Mhz anyway so I'm perfectly happy with the result.

That's because you didn't set the tiny to run at 8mhz, and so it's still running at 1.

Set it to 8mhz, do "burn bootloader" to set fuses to tell it to run at 8mhz, then upload your sketch.