Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
    • Design Guides
      • WiFi & the IOT Design Guide
      • Microcontrollers Design Guide
      • State of the Art Inductors Design Guide
  • Women in Engineering

How To Send Serial Data From Arduino- (Part 10/49)

By Ajish Alfred

The serial communication is the most important communication port which all microcontrollers should have. This enables the user to communicate with the microcontroller using text commands. The microcontroller can also send back data in text format or simply as data bytes. Thus the serial communication provides a text user interface (TUI) for the microcontroller. Apart from simply communicating with the user the serial communication is a very useful debugging tool in the code development process. It can send back the run time status of the internal registers, value of the variables etc. When a given with a microcontroller a developer will first try to make the serial port up and working. The initialization process of the serial port is not such an easy task as far as the common microcontrollers are concerned. One have to go through the data sheet, do some calculation for the precise baud rate which may vary with the crystal frequency etc.

The arduino has built-in functions for accessing the serial port and hence there is no need to go to the register details. One can simply call the functions and get the job done. It makes the coding process fast and the debugging far more efficient. The arduino IDE itself has a serial monitor tool which can display all the data going from and coming into the serial port. This project demonstrates a simple program which can be used to send a string to the serial port of the PC. The data can be displayed in the hyperterminal of the PC or in the arduino’s serial monitor itself.


 

The arduino board is built around an AVR microcontroller providing all the necessary circuitry for the microcontroller to operate. The arduino board used in this project is the arduino pro-mini board and the IDE version of the arduino is 1.0.3 for windows. The image of the arduino pro-mini board and the arduino IDE are shown below;

Arduino

Fig. 2: Typical Arduino Pro-Mini Board

 

Arduino

Fig. 3: Arduino IDE Software Window

 

Since the arduino pro-mini board has no circuitary for interfacing it with the serial port or the USB port of the PC, an external USB to TTL converter board is required to connect it with the PC. This hardware helps in programming the arduino board and also helps in the serial communication with the USB port of the PC.

Arduino

Fig. 4: External USB to TTL converter board for programming Arduino and serial communication

It is assumed that the reader has gone through the project Getting started with Arduino and tried out all the things discussed there.There are some built-in functions in the arduino IDE which helps in the serial communication process. There is a function which helps to initialize the serial communication port with a particular baud rate and there are functions to send data to the serial port. The functions used in this projects are namely Serial.begin(), Serial.print() and Serial.println() whose details are discussed in the following section.

Serial.begin()  

This function helps to initialize the serial port of the arduino with the baud rate mentioned in its argument. For example to initialize the serial port with a baud rate 9600, the Serial.begin() function can be used as shown below;

Serial.begin(9600);

The above statement will initialize the serial port with the baud rate 9600 which is given as the argument of the function and also with 8 bits of data frame, one start bit, one stop bit and no parity bits.

Serial.print()

The Serial.print() function helps in sending serial data to the devices connected at the serial port of the arduino board. This function can send both ASCII characters and strings to the serial port. For example the Serial.print() function can be used to send a character as shown in the following;

Serial.print(‘A’);

The above statement is able to send the character ‘A’ to the serial port. The function can operate with all ASCII characters; For example the new line and carriage return characters can be send using this function as given in the following statements.

Serial.print(‘n’);

Serial.print(‘r’);

The same function Serial.print() can be used to send a string to the serial port as shown in the following statement.

Serial.print(“hello”);

If two strings are printed one after the other using the Serial.print() function the output will appears as shown below;

Serial.println(“hello”);

Serial.println(“world”);

Output:

helloworld

Serial.println()

The Serial.println() functions operation is almost similar to the previously discussed Serial.print() function. The only difference is that the function Serial.println() prints a character or string each time into a new line where as the Serial.print() function cannot do this. One have to use Serial.print(‘n’);

Serial.print(‘r’); statements together to get the next character or string to get printed on the new line.

For example if two strings are printed one after the other using the Serial.println() function the output will appears as shown below;

Serial.println(“hello”);

Serial.println(“world”);

Output:

hello

world

THE CODE

In the following code the function call ‘Serial.begin(9600)’ will initialize the serial port with a baud rate of 9600. The function Serial.print() is used to send both the characters and string to the serial port. The function which is called inside the infinite loop as ‘Serial.println(“hello world”)’ will send the string “hello world” continuously to the serial port each time with a new line and carriage return characters before the string. The code written for this project is given below;

int led = 5;

void setup()

{

  // initialize the led pin as an output.

  pinMode(led, OUTPUT); 

  // start serial port at 9600 bps

  Serial.begin(9600);

  // wait for a while till the serial port is ready

  delay(100);

// send the initial data once //   

  Serial.print(‘n’);

  Serial.print(”    EG LABS   “);

  Serial.print(‘n’);

  Serial.print(‘r’);

  Serial.print(“Serial Tx Demo”);

  Serial.print(‘n’);

  Serial.print(‘r’);

  Serial.print(‘n’);

}

void loop()

{

  // send this string continuously //

    Serial.println(“hello world”);

    // blink the LED once //   

    digitalWrite(led, HIGH);      

    delay(1000);                 

    digitalWrite(led, LOW);      

    delay(1000);               

}

The details of the function setup(), loop() are already explained in the project Get started with arduino. The code also blinks an LED which is connected to the pin number 5 of the arduino board using the functions pinMode (), digitalWrite () and delay(). The details of those functions are already discussed in the previous project on How to use digital input and digital output of the arduino board. Once the coding is finished one can verify and upload the code to the arduino board as explained in the project how to get started with the arduino.

 

Project Source Code

###



/*================================= EG LABS =======================================

 Send a string contnuopusly to the serial port along with Turning on and off a LED

 connected to digital pin 5

 The circuit:

 * LED attached from pin 5 to ground through a 1K resistor

//================================= EG LABS =======================================*/

// give the pin a name:

int led = 5;

void setup()

{

  // initialize the led pin as an output.

  pinMode(led, OUTPUT);  

  // start serial port at 9600 bps

  Serial.begin(9600);

  // wait for a while till the serial port is ready

  delay(100);

// send the initial data once //    

  Serial.print('n');

  Serial.print("    EG LABS   ");

  Serial.print('n');

  Serial.print('r');

  Serial.print("Serial Tx Demo");

  Serial.print('n');

  Serial.print('r');

  Serial.print('n');

}

void loop()

{

  // send this string continuously //

    Serial.println("hello world"); 

    // blink the LED once //    

    digitalWrite(led, HIGH);       

    delay(1000);                  

    digitalWrite(led, LOW);       

    delay(1000);                

}

###

 


Circuit Diagrams

CIRCUIT_4

Project Components

  • Arduino Pro Mini
  • LED
  • Resistor

Project Video


Filed Under: Arduino
Tagged With: Arduino, serial data
 

Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


Featured Tutorials

  • PS2 Keyboard To Store Text In SD Card Using Arduino Circuit Setup On Breadboard
    How To Use PS2 Keyboard To Store Text In SD Card Using Arduino- (Part 42/49)
  • Wireless Path Tracking System Using Mouse, XBee And Arduino Circuit Setup On Breadboard
    How To Make A Wireless Path Tracking System Using Mouse, XBee And Arduino- (Part 43/49)
  • How to Make a Wireless Keyboard Using Xbee with Arduino- (Part 44/49)
  • Making Phone Call From GSM Module Using Arduino Circuit Setup On Breadboard
    How to Make Phonecall From GSM Module Using Arduino- (Part 45/49)
  • How to Make a Call using Keyboard, GSM Module and Arduino
    How To Make A Call Using Keyboard, GSM Module And Arduino- (Part 46/49)
  • Receiving SMS Using GSM Module With Arduino Prototype
    How to Receive SMS Using GSM Module with Arduino- (Part 47/49)

Stay Up To Date

Newsletter Signup

Sign up and receive our weekly newsletter for latest Tech articles, Electronics Projects, Tutorial series and other insightful tech content.

EE Training Center Classrooms

EE Classrooms

Recent Articles

  • Renesas delivers intelligent sensor solutions for IoT applications
  • Microchip Technology releases AVR-IoT Cellular Mini Development Board
  • Qualcomm acquires Cellwize to accelerate 5G adoption and spur infrastructure innovation
  • MediaTek’s chipset offers high-performance option for 5G smartphones
  • Nexperia’s new level translators support legacy and future mobile SIM cards

Most Popular

5G 555 timer circuit 8051 ai Arduino atmega16 automotive avr bluetooth dc motor display Electronic Part Electronic Parts Fujitsu ic infineontechnologies integratedcircuit Intel IoT ir lcd led maximintegratedproducts microchip microchiptechnology Microchip Technology microcontroller microcontrollers mosfet motor powermanagement Raspberry Pi remote renesaselectronics renesaselectronicscorporation Research samsung semiconductor sensor software STMicroelectronics switch Technology vishayintertechnology wireless

RSS EDABOARD.com Discussions

  • Netlist physical name update
  • nt1065_USB3 gnss receiver
  • LLC HB with synchronous rectifiers can be very dodgy?
  • VHDL using a stimulus for one clock cycle
  • PSS fail to converge when internal clock was using

RSS Electro-Tech-Online.com Discussions

  • Capacitor to eliminate speaker hum
  • Decapped Chip On Board
  • undefined reference header file in proteus
  • Sony KV-A2913E (chassis AE1C) auto shuts off after one minute
  • CRT TV repair
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2022 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
    • Design Guides
      • WiFi & the IOT Design Guide
      • Microcontrollers Design Guide
      • State of the Art Inductors Design Guide
  • Women in Engineering