Serial communication question

Hello,

i've got this situation.

On ATtiny85 i am measuring voltage (up to 24V) with voltage divider and sending it through TX pin (PB3) to RX port of Arduino Nano every second. I saw idea about Tiny Debug Serial here -> Communication with Tiny's

I am sending received information about voltage to LCD screen, which is connected to Nano. I am using Serial.parseInt() on the RX side because i want to see real voltage value on LCD.

Everything works but from time to time (every few seconds but not constantly) i am seeing wrong readings on LCD screen. For example, i am measuring fixed voltage of 12V. Most of the time i see 12 on the LCD but every few seconds i am getting strange voltage readings like 1, 2, 9, etc, and than 12V again. I also tried with Serial.parseFloat(), to get more accurate numbers but the problem is even worse.

Probably, i've got problem in code, since this is my first serial communication project between two devices. I am running TX side (ATtiny) at 8 MHz and Nano with 16 MHz but this shouldn't be a problem if baudrate on both sides is set to 9600, right?

TX Attiny side

int voltage = 0;

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

void loop() {
  int raw = analogRead(A2);
  voltage = (raw / 1024.0) * 29;
 
  Serial.println(voltage);
  delay(1000); 
}

RX Nano side

int val;

void setup() {
  Serial.begin(9600);
  lcd.begin(16,2);
  lcd.clear();
}

void loop() {
 
  if (Serial.available() > 0)
  {
    val = Serial.parseInt();
   
    lcd.setCursor(0,0);
    lcd.print("RX voltage:");
    lcd.setCursor(0,1);
    lcd.print(val);
    if (val < 10) lcd.print(" "); 
  }
  else
  { 
    lcd.setCursor(0,0);
    lcd.print("RX disconnected");
  }
}

Either that or SoftwareSerial is failing, again....

Add a checksum to the data (or some way of seeing if it's valid). Find out if it's data corruption or not before you go looking for bugs.

fungus:
Either that or SoftwareSerial is failing, again....

Is that a frequent problem with SoftwareSerial?

On the first look, the code (on both sides, RX and TX) is ok or i made any obvious mistake? I will try to add checksum but i don't know how to do it yet.

luxy:
Is that a frequent problem with SoftwareSerial?

Yes.

So for example, if code is ok and this SoftwareSerial is causing problems (on which i have no impact), and since that's the only way i can communicate with ATtiny over Serial communication.. I have got serious problem.

How many pins do you have free on the chips?

Sending a separate clock signal (SPI or I2C protocol) will fix it.

Actually i am using only one port for analog read. All others are free so i am able to change protocol.

But for example, what if i want to upgrade this "cable" connection with bluetooth later?

luxy:
But for example, what if i want to upgrade this "cable" connection with bluetooth later?

In theory you can tune the internal oscillator. All you do is send a message over the serial line and put values into the OSCCAL register until it works (see "System Clock and Clock Options" in the datasheet).

People have varying success rates though. For an accurate clock speed you really need an external crystal.

If so,...

I think i will use ATmega8 with optiboot. Price for uC is almost the same, i can include external crystal and use supported I2C ports.

I use Pro Minis for nearly everything these days.

They're $3 each for a Mega328 on a PCB with 16MHz crystal. You get hardware interfaces for everything and most Arduino libraries are designed for Unos so they work with no modification.

At that price it's really not worth using anything else or even trying to build your own. They even come with header pins so you can fix them to a PCB easily.

Thank you for advice.. I think i will buy this because looks like a really good deal

I used to built standalone versions with ATmega8 because i have.. well, really a lot of this chips and mainly i don't need so much space for code.. For me 8kb is enough for most apps.

I will try serial RX/TX (not software serial) on pins 0/1 and with 16 MHz crystal. If this won't go, i will use I2C.

fungus:
For an accurate clock speed you really need an external crystal.

I tried Serial communication with ATmega8 optiboot (16 MHz external osc) and Arduino Uno.. It works like a charm, also with Serial.parseFloat() :slight_smile:

Try sending the data slower, lower baud rate , or leave a pause between the characters being sent.
Software Serial doesnt have a receive buffer, so data can get corrupted if data is coming in , but the CPU is doing something else
like writing to a display.

mauried:
Try sending the data slower, lower baud rate , or leave a pause between the characters being sent.

Ok, i will try with lower baudrate (300 for example) but where do you think to put pause on TX side? I have got 1 second pause after every sending, if that's it?

  Serial.println(voltage);
  delay(1000);