Projects
Serial Comm

Overview

This project shows you how to send data to and receive data from a microcontroller board connected to your computer over the most commonly used serial protocol.

The first project for any board and the objective is to get familiar with the board you are using and to ensure the connection between the IDE on the computer and the board is established and you are able to upload and run a basic code on the board.

Components

ComponentPurpose
Arduino NanoThe microcontroller board.
USB CableTo connect the board to the microcontroller.

The microcontroller needs to be connected to a computer via a USB cable for power and to upload code.There are multiple types of USB cables (with combinations of connector types A, B, and C). We will not be specifying the cable to be used since each board comes with different options. Please check your board and use the appropriate cable.

Circuit Diagram

There is no circuit diagram for this project, it only requires the board connected to the computer USB port using the appropriate cable.

Assembly

Connect the board to the computer using the appropriate cable.

Code

/*
  Project: Serial In - Serial Out
  Project Description:
  This project shows you how to send data and receive data to and from a board connected to your computer.
 
  Author: STEMVentor Educonsulting
  
  This code is copyrighted. Please do not reuse or share for any purpose other than for learning with subscribed STEMVentor programs.
  This code is for educational purposes only and is not for production use.
*/
 
// LIBRARIES
 
// PIN DEFINTIONS
 
// CONSTANT DEFINITIONS
 
// GLOBAL VARIABLES
// Used for reading by character.
// Note that the 64 byte size of the Arduino serial input buffer does not limit the number of characters that you can receive 
// because the code can empty the buffer faster than new data arrives.
const byte num_chars = 32; // Limit to 32 bytes (1 byte = 1 character, including the newline or string terminator)
char receivedChars[num_chars]; // An array of characters, the size has to be pre-defined.
boolean new_data = false;
 
// Used for reading a string.
String receivedString;
 
// LOCAL FUNCTIONS
 
// Option 1: Read serial input by character and assemble into a string (array with line terminator).
void readSerialData() {
  static byte index = 0;
  char eol_char = '\n'; // Set the serial monitor to send a New Line as the end character.
  char in_char;
  
  while (Serial.available() > 0 && new_data == false) {
    in_char = Serial.read();
 
    if (in_char != eol_char) {
        receivedChars[index] = in_char;
        index++;
        if (index >= num_chars) {
            index = num_chars - 1;
        }
    }
    else {
        receivedChars[index] = '\0'; // terminate the string
        index = 0;
        new_data = true;
    }
  }
}
 
 
// Option2: Read an entire string (this is a blocking function and waits until timeout).
void readString(){
  Serial.println("Enter a data string:");
  while (Serial.available() == 0) {}     //wait for data available
  receivedString = Serial.readString();  //read until timeout
  receivedString.trim();                        // remove any \r \n whitespace at the end of the String
}
 
// THE setup FUNCTION RUNS ONCE WHEN YOU PRESS RESET OR POWER THE BOARD.
void setup() {
  Serial.begin(9600);
  Serial.println("The board is ready!");
}
 
// THE loop FUNCTION RUNS OVER AND OVER AGAIN FOREVER UNTIL THE BOARD IS POWERED OFF.
// Run only one option at a time (comment out all code for the other option).
void loop() {
  // Option 1: Read by character
  // readSerialData();
  // if (new_data == true) {
         Serial.print("Entered char: ");
  //     Serial.println(receivedChars);
  //     new_data = false;
  // }
 
  // Option 2: Read a string
  readString();
  Serial.print("Entered String: ");
  Serial.println(receivedString);
}