Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Table of Contents

Serial Communication

Method Serial communication is used to transfer data between two devices.

Image Removed

Serial Monitor & analogRead()

Image Removed

Sending a Message

void setup()

{

  // initialize serial:


...

Data passes between the computer and Arduino through the USB cable. Data is transmitted as zeros (‘0’) and ones (‘1’) sequentially.

Arduino dedicates Digital I/O pin # 0 to receiving and Digital I/O pin #1 to transmit.

For this reason, we typically do not use Digital I/O 0 or 1 for anything in our designs.

Using Serial

Arduino IDE

Tinkercad

Image Added

Image Added

Sending a Message

C++

Blocks

Code Block

void setup()
{
  // initialize serial to a baud rate of 9600.
  // Ensure that the same baud rate is set in the serial monitor.
  Serial.begin(9600);

...


}

...


 
void loop ()

...

{

...


{
  Serial.print(

...

"Hands on ") ;
  // does not insert a newline at the end;
  // next message continues on the same line
  Serial.print(

...

"Learning ") ;
  // does not insert a newline at the end;
  // next message continues on the same line
  Serial.println(

...

"is Fun!!!

...

}

Homework Exercise

...

  • When the switch is off
    • The message OFF is repeatedly printed via Serial, and the system will not do anything else
  • When the switch is on
    • The LED will gradually turn on or off at a speed which is related to the amount of light failing on the LDR
    • The message ON is repeatedly printed via Serial

...

") ;
  // inserts a newline at the end;
  // next message is printed on a new line
}

image2020-1-21_21-43-17.pngImage Added

Note: Blocks allow only 9600 as baud rate; there is no baud rate setting.

Try the example above for the following two cases

(i) Serial.println for all three messages

(ii) Serial.print for all three messages

Try the example above for the following two cases

(i) with newline for all three messages

(ii) without newline for all three messages

If you see strange-looking characters in the serial monitor, your bit / baud rate set in the Serial Monitor most likely does not match the one used in your Arduino sketch.

analogRead() Serial Example

Printing to Serial console is a very effective mechanism in debugging your code. You can print out the values of variables at different points in your code, which can then be compared with the expected values. This might be of help in identifying logical issues with your code.

It is also useful in monitoring the readings from various sensors so that you can decide on the thresholds etc, i.e., to fine-tune your design / code. The following example prints out the reading obtained from the analog sensor connected to pin A0 (for example, an LDR connected to pin A0 as is the case in the Analog Input page).

C++

Blocks

Code Block
void setup()
{
  Serial.begin(9600);
}
 
void loop()
{
  Serial.println(analogRead(A0));
  delay(500);
  // Read and print the value 2 times in a second.
}

image2020-1-21_23-6-43.pngImage Added