PC AND CRODUINO’S SERIAL COMMUNICATION – SERIAL MONITOR

There are many ways to read and write on the Serial Port using Arduino, and here we will demonstrate the simplest one. Make sure to use it in order to avoid frustrating ways! ūüôā While we have just briefly turned to Serial Monitor in previous tutorials, in this one, we will explain in detail how to work with it, whether with Arduino IDE or Atmel AVR Studio. Generally, Serial Monitor can be used with any serial device connected to your computer.¬†

SERIAL MONITOR

Serial Monitor is an addition to the Arduino IDE software. You have most likely already come across it if you have ever done some Arduino project, e.g. reading the temperature off the DHT sensor. Besides reading the current code condition, it allows sending and receiving text messages and as such it is very practical in tracing errors in the code (debugging, controlling Croduino using a keyboard or for viewing the data printout). But, one by one..

Note:  before moving on, connect the Croduino board and select Board and Port under Tools tab  of the Arduino IDE software 

Serial monitor is initiated by clicking on the magnification icon or using the Ctrl+Shift+M key combination.

Basic properties:

COM Port¬†–¬†virtual port through which we want to create serial communication between the computer and the Croduino board¬†
BAUD¬†–¬†a unit of pulse per second for the UART communication
LINE ENDING: (detailed)
No line editing Рsends what is written

Newline¬†– to the written text, it adds ASCII symbol for new line¬†(equivalent to “\n”)

Carriage return¬†– to the written text, it adds ASCII symbol for return (equivalent to “\r”)

Both NL & CR Рto the written text, it adds both Newline and Carriage return

 

CRODUINO TO SERIAL MONITOR

We begin the serial communication with¬†Serial.begin();¬†which is set in the¬†setup¬†part of the code. The parameter assigned to the function is¬†BAUD rate.¬†It has to coincide with the one from the Serial Monitor so that we do not read “hieroglyphs” that may occur because of incompatibility in the speed of communication.

void setup() {
  // beginning serial communication
  Serial.begin(9600); // the same BAUD Rate is set in the Serial Monitor
}
void loop() {
}

Writing commands:

Serial.print() Рprints readable data supported by ASCII, e,g,

Serial.print(“Pozz”); – prints¬†Pozz
Serial.print(22); Рprints 22
Serial.print(1.4142); Рprints only two digits after the decimal point 1.41

Serial.println()¬†– after what is entered, it prints a new line, i.e. adds “\n” in the end of the sent string
Serial.write() Рprints out just one byte

Additional parameter:

e.g.:
Serial.print(22,BIN);
 Рturns 22 to binary number, prints out 10110
Serial.print(22,HEX); Рprints 16
Serial.print(1.4142,5); Рnumber 5 indicates the number of decimal spots being printed out, prints 1.41420

SERIAL MONITOR TO CRODUINO

Reading commands:

Serial.read() Рreads the first byte of the default variable

int sm_int = 0;
void setup() {
  // beginning serial communication
  Serial.begin(9600); // the same BAUD Rate is set in the Serial Monitor
}
void loop() {
  if( Serial.available() ) {
    sm_int = Serial.read();
    Serial.println(sm_int, DEC);
  }
}

Serial.readString() Рreads the default variable and saves it to String

String sm_string = "";
void setup() {
  // beginning serial communication
  Serial.begin(9600); // the same BAUD Rate is set in the Serial Monitor
}
void loop() {
  if( Serial.available() ) {
    sm_string = Serial.readString();
    Serial.println(sm_string);
  }
}