Arduino Omega: A complete step by step tutorial
A complete step-by-step tutorial on How to use Arduino Serial Readarduino tutorial
Arduino Omega: For upon |Hello friends, I hope you all are fine and having fun with your lives. Today, I am going to share a very basic and introductory tutorial named How to use Arduino Serial Read. I am sharing this tutorial because I am getting a lot of emails in which users normally ask about basic Arduino tutorials as they are very new to them. So, I thought of sharing this very basic Arduino tutorial in which we are gonna have a look at how we can use the Arduino Serial Read command.
I selected this tutorial as my first tutorial in this list of Arduino basic tutorials because learning to use a Serial port is very necessary as it’s one of the best troubleshooting tools for your code. I know things are looking a bit complex here but I have explained everything below in detail so don’t worry. Just read it once incomplete so that you get all the tiny details of this Arduino Serial Read. I have also given a Proteus Simulation in which I received the incoming data from the serial port and displayed it on LCD. Before going into the details of this Arduino Serial Read, let me first discuss the Serial Port in General. Arduino Omega.
What is Serial Port?
- I have already written a detailed tutorial on this topic which you can read at What is Serial Port?
- Serial Port is used for data communication, it sends data from one place to another.
- Serial Port has 9 pins in total and all these 9 pins are used for different purposes.
- The two of these pins most commonly used are TX (transmitter) and RX (Receiver).
- So, using these two pins we send our data from one place to another.
- Now I hope that you have got a pretty basic idea of What is Serial Port but if not then you should read What is Serial Port?
- Now let’s have a look at Arduino Serial Port first, before having a look at Arduino Serial Read.
Serial Port in Arduino
- All Arduino boards have Serial Ports on them.
- If we talk about cc, then it has only one serial port on it and it is located at pin 0 and pin 1.
- If you look closely at the Arduino UNO board then you can see a little TX is written on its pin # 1 and a little RX is written on its pin # 0, as shown below figure:
- So, now we have got the Serial Port on Arduino UNO which we know is at pin # 0 and pin # 1, now in the next part, we are gonna have a look at How to use Arduino Serial Read and get data from this Serial Port. Arduino Omega.
How to use Arduino Serial Read?
- Arduino Serial read command is used for reading any data available at the Serial Port.
- I have also designed a Proteus simulation which you can download from the below button, and I have explained this simulation in the last step of this tutorial:
Download Simulation & Code
- For example, you have some module let’s say a GPS module (most of the GPS module works at a serial port).
- So, when you connect your GPS module with Arduino, you have to connect the TX pin of GPS with the RX pin of Arduino.
- Now the TX pin of GPS will be sending/transmitting the data and because this pin is connected to the RX pin of Arduino, so Arduino will keep receiving the data.
- So, that’s how Serial Port works.
- Now the data is coming to Arduino but you have to write some code to read this incoming serial data and then save it in some space.
- So, here the Arduino Serial Read command is used.
- Arduino Serial read command reads the incoming data from Serial Port and then saves it in some variable.
- Here’s the syntax of the Arduino Serial Read command:
char data = Serial.read();
- One important thing is, in order to make Arduino Serial Read command work, you have to first initialize the Serial Port in Arduino, as shown below:
Serial.begin(9600);
Note:
- Arduino USB Port which is plugged into the computer and is used for uploading the code also works on the same serial port.
- So, if you have anything plugged in pin # 0 of Arduino then you can’t upload the code in Arduino.
Now, let’s design a simple example in which we will be receiving data from Serial Port and then saving it in some variable.
- So, connect your Serial device with your Arduino board and now upload the below code to your Arduino board:
1
2
3
4
5
6
7
8
9
10
11
|
void setup() {
Serial.begin(9600); // Serial Port initialization
}
void loop() {
if(Serial.available()) // Chek for availablity of data at Serial Port
{
char data = Serial.read(); // Reading Serial Data and saving in data variable
Serial.print(data); // Printing the Serial data
}
}
|
- Now, you need to open the Serial Monitor of Arduino which is used for debugging purposes.
- So, whenever you write something on Serial Port then it got printed in the Serial monitor.
- So, whatever you will be receiving in the Serial Port you will get in the Serial Monitor.
- Here are some random data of the GSM module coming on the serial port and showing in the serial monitor:
How to use Arduino Serial Read in Proteus?
- So, now let’s design a small Proteus simulation in which we will see how to use Arduino Serial Read.
- Proteus doesn’t have Arduino by default in it, so you need to first download this Arduino Library for Proteus and then you will be able to simulate your Arduino board in Proteus. Arduino Omega.
- So, design a simple circuit as shown below figure:
- In the above figure, I have placed an LCD and I will get the data from the serial port and then I will print that data on LCD.
- So, in simple words, whatever I type in the Virtual terminal will be shown on LCD.
- You also need to download this New LCD Library for Proteus to get this amazing LCD in Proteus.
- So, now use the below code and Get your Hex File from Arduino Software:
Programming Arduino Tutorial
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
#include <LiquidPrograming Arduino Tutorial: A complete step-by-step tutorial crystal.h>
//Initialize the library with the numbers of the interface pins
LiquidCrystal Lcd(13, 12, 11, 10, 9, 8);
void setup() {
//Set up the LCD’s number of columns and rows:
LCD.begin(20, 4);
// Print a message to the LCD.
LCD.setCursor(1,0);
lcd.print(“www.TheEngineering”);
LCD.setCursor(4,1);
LCD.print(“Projects.com”);
LCD.setCursor(1,0);
Serial.begin(9600);
}
void loop() {
if(Serial.available()) // Chek for availablity of data at Serial Port
{
char data = Serial.read(); // Reading Serial Data and saving in data variable
Serial.print(data);
lcd.print(data); // Printing the Serial data
}
}
|
- Now when you start the Proteus simulation the first screen will look something like this:
- Now whatever you write in your Serial Port, will show on the LCD as shown below figure:
- That’s how the Arduino Serial Read works.
- You can download this Proteus simulation and the Arduino code by clicking the Download button given at the start of this post. Arduino Omega.
So, that’s how you can use the Arduino Serial Read command and can do your task. If, it’s still difficult for you then let me know in the comments and I will try my best to resolve your issues. Thanks.
The article was originally published here.
Comments are closed.