Sending Serial data to PC from Arduino Uno UART port

Good Day All,

I am rather new to this new exciting world but now after a week of searching I am in need of advice,i have done a lot of reading maybe I just haven’t found the correct post to assist me with my trouble.

My project is running on a Uno, with a 16x2 LCD, USB serial converter for receiving data on the PC.

My aim of this project is to get a better understanding of the serial (UART) communication from the Arduino, I ultimately want to run a RS485 line with 485 chip and communicate “Modbus” like protocol to a bus-line of devices.

I am generating a number and increment the number every second, the number is displayed on the lcd and the number is from my understanding sent to the UART port, I have connected a serial-USB port on my laptop for me to read the data sent by a terminal session my problem is the data is all wrong and randomly wrong I cant seem to get the data I send.

When I read it from the on board serial monitor it displays what I expect.

I can change the output that is sent from a DEC value to HEX it remains inconsistent data I receive even sending a string makes no sense.

Please assist in clarifying my mistake, I have made sure my serial port on my pc is set to the same setting I declare in the sketch. (Baud 9600, 8 bits, Parity - None, SB -1)

I have attached my sketch and the serial data I receive from the Arduino, I receive two bytes first one being the random and unexpected byte the second is just 00 on every transaction sent.

Regards Jay

LCD_Print_Number_and_Send_to_PC_Serial.ino (885 Bytes)

Data Received on PC Serial.txt (518 Bytes)

your files are small, post them inline with the code tag... can't read easily those files from smartphone

PS/ read what Serial.write() does (depending on what type of param you give) - might want to consider Serial.print() if you want ASCII or give the address of your counter and the size of an int to the write function if you want to send your number in binary form (2 bytes little endian)

Serial.write(number);

The write function sends one binary byte. Serial monitor expects an ASCII character. If you write(1) the write function sends 0x01. So the serial monitor receives a byte = to 0x01, which is an unprintable ASCII character. To remedy that, use the print() function instead. If you print(1) the print function sends 0x32 which is the ASCII character code for '1'.

Thank you,

I have updated the post with my code and my serial input on my PC

Regards

#include <SoftwareSerial.h>

#include <LiquidCrystal_I2C.h>
#include <Wire.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);
int a = 0x31;
int b = 0x32;
int c = 0x33;
int d = 0x34;
int number = 1;
char alpha = 'A';


void setup()
{
Serial.begin(9600, SERIAL_8N1); // send and receive at 9600 baud
lcdInit ();
}


void lcdInit()
{
// Initilize LCD with welcome Message
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("STARTING SYSTEM");
delay(500);
lcd.clear();
lcd.setCursor(2,1);
lcd.print("SYSTEM READY");
delay(500);
lcd.clear();
}


void loop()
{
  //Display Number Sequence on LCD
  lcd.setCursor(0,0);
  lcd.print("Number is ");
  lcd.setCursor(11,0);
  lcd.print(number);
  //Send "Number" via serial port to PC
  Serial.write(number);
  // Wait 1s between number increment
  delay(1000);
  //Increment number
  number++;
  }

Please read the how to use this forum-please read sticky to see how to post code in code tags.

Apologies for that,

Thank you for the advise.

Hi All,

After reading more I came across a article that seems to indicate the data from the Arduino is transmitted in TTL and it has to be converted to 232 for my PC to display correctly. I will look in to a TTL - 232 interface to see if this resolves my problem.

This simple Python - Arduino demo should give you the general idea about the PC-Arduino communication. You should be able to replace the Python code with equivalent functionality in any other programming language if you prefer not to use Python.

…R

a complicated simple task

#include <LiquidCrystal.h> // includes the LiquidCrystal Library 
LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7) 
int number = 1;
void setup()
{
Serial.begin(9600);
  lcd.begin(16, 2);
}
void lcdInit()
{

lcd.setCursor(0,0);
lcd.print("STARTING SYSTEM");
delay(500);
lcd.clear();
lcd.setCursor(2,1);
lcd.print("SYSTEM READY");
delay(500);
lcd.clear();
}
void loop()
{
  //Display Number Sequence on LCD
  lcd.setCursor(0,0);
  lcd.print("Number is ");
  lcd.setCursor(11,0);
  lcd.print(number);
  //Send "Number" via serial port to PC
 // Serial.write(number);
    Serial.print(number);
  
  delay(1000);
  //Increment number
  number++;
  }

Vik321:
a complicated simple task

I don't understand.

What has this got to do with the OP's question?

...R

GainsfordJ:
Hi All,

After reading more I came across a article that seems to indicate the data from the Arduino is transmitted in TTL and it has to be converted to 232 for my PC to display correctly. I will look in to a TTL - 232 interface to see if this resolves my problem.

Not sure this is what you need since you were receiving stuff... what physical port did you use on your PC? Just USB cable/port? ?

What I wrote in#1 still stands:

//Send "Number" via serial port to PC
  Serial.write(number);

doesn’t notsend your number in full to your PC, just the least significant byte.

Try

//Send "Number" via serial port to PC
  Serial.write(&number, sizeof(number));

to send the data in full binary form or

//Send "Number" via serial port to PC
  Serial.print(number);

to send your number in ASCII