Basics

  1. Arduino Serial Input Basics Keyboard
  2. Arduino Serial Input Basics Download
  3. Arduino Serial Input Basics

The easiest way to configure the Arduino’s UART is by using the function Serial.begin (speed). The speed parameter is the baud rate that we want the UART to run. Using this function will set the remaining UART parameters to default values (Data length =8, Parity bit =1, Number of Stop Bits=None). Things should be looking pretty familiar by now. The new stuff has to do with the serial monitor: In setup, look for Serial.begin(9600); which tells the Arduino to start communicating via text, at 9600 baud. The baud rate controls how much information is sent per second.

Don’t connect these pins directly to an RS232 serial port; they operate at +/- 12V and can damage your Arduino board. To use these extra serial ports to communicate with your personal computer, you will need an additional USB-to-serial adaptor, as they are not connected to the Mega’s USB-to-serial adaptor. These Arduino projects are designed to display the value of inputs using the serial monitor. Serial is a method of communication between a peripheral and a computer. In this case, it is serial communication over Universal Serial Bus (USB). When reading sensors with an Arduino, the values are sent over this connection and can be. Serial monitor of Arduino is a very useful feature.Serial monitor is used to see receive data, send data,print data and so on.Serial monitor is connected to the Arduino through serial communication. This serial communication occurs using RX (pin 0) and TX (pin 1) terminal of Arduino. Any kind of data can send through this serial monitor.

How to connect a serial port Arduino Mega 2560 R3 to an Arduino UNO R3 compatible and to an Arduino Leonardo clone.

  • 14,941 views
  • 0 comments
  • 1 respect

Components and supplies

Arduino Mega 2560
×1
Adafruit Metro 328
×1
Arduino Leonardo With Headers
×1
Arduino UNO
×1

Apps and online services

About this project

This tutorial we are covering Serial Port Basics on the Arduino Mega 2560 R3.

If you ever wonder how to use the other Serial ports on the Arduino Mega 2560 R3, but don't know where to start? This short tutorial we'll go over the basics, we'll connect to an Adafruit Metro 328 = its an Arduino compatible; works just like an Arduino UNO R3, and we will connect to an Arduino Leonardo clone...

The connections will be UART to UART, SoftwareSerial to SoftwareSerial, UART to SoftwareSerial, and ALL 3 connected together!! :)

Before you make the physical wire connections.... LOAD the code to your Arduinos first!

Please follow this step because your Arduinos use the 'DEFAULT' SerialPort to upload your code from the Computer, once you make the physical wire connections the 'Default' SerialPort will be used on the Arduino UNO (Adafruit Metro 328) to communicate with the Arduino Mega 2560 R3's SerialPort #1 which is pin19 RX1 and pin18 TX1 on the Mega. This will not allow the Arduino UNO(Adafruit Metro 328) to communicate with the outside world; AKA your computer. The Leonardo will use the SoftwareSerial example but if using the UART; same will apply as the Arduino UNO's....

MEGA to Adafruit 328(Arduino UNO) UART to UART connection.

  • Upload the the codes to Arduino Mega and Arduino UNO(Adafruit 328)
  • Connect one end of a wire to Mega's pin19 RX1 and the other end to Arduino UNO's(Adafruit 328) pin1 TX...
  • Connect one end of a wire to Mega's pin18 TX1 and the other end to Arduino UNO's(Adafruit 328) pin0 RX
  • Connect the ground pin on the Mega to the ground pin on the Arduino UNO(Adafruit 328).
  • Connect Arduino UNO to a Stable/Reliable power source.
  • Connect Arduino Mega to the Computer from the USB cable.
  • On the Arduino Mega UART1 program window of the Arduino IDE, make sure your COM is selected to the Arduino Mega before opening the Serial Console, Open the Serial Console set to 9600 baud COM Speed.
  • On the Top window in the text box enter the letter x … not the capital X … the LED on the Arduino UNO(Adafruit 328) goes ON.
  • On the Top window in the text box enter the letter a … not the capital A … the LED on the Arduino UNO(Adafruit 328) goes OFF.
Arduino

What we are doing in this code is calling on the Arduino Mega's default UART Serial port by calling Serial.begin at a 9600 baud speed rate. This is going to listen to the Computer for instructions through the USB cable. We are also calling upon the 2nd UART Serial port by calling Serial1.being at a 9600 baud rate. This is going to communicate with the Arduino UNO(Adafruit Metro 328).

What happens.... when you type the letter x or a on the text window on the Serial Console it sends it to the Arduino Mega, then the Arduino Mega sends it to the Arduino UNO(Adafruit Metro 328) through the 2nd UART Serial port with the Serial1.write(Serial.read()); command.

In this code we are using the Arduino UNO (Adafruit Metro 328) default Serial UART to communicate with the Mega, The Arduino waits for the Serial UART to receive a message, if the data is a refence what is in the code, in this case is a letter x or a the code will turn ON or OFF the LED on the Arduino UNO (Adafruit Metro 328).

Arduino Mega to Arduino Leonardo …. SoftSerial to SoftSerial connection.

  • Load code to Arduinos
  • One end of one wire goes to pin9 of Mega, the other side of wire goes to pin8 of Arduino Leonardo.
  • One end of one wire goes to pin9 of Mega, the other side of wire goes to pin9 of Arduino Leonardo.
  • One end of one wire goes to Ground pin of Mega, the other side of the wire goes to the Ground pin of Arduino Leonardo.
  • Connect a reliable power supply to your Leonardo
  • Connect the computer USB cable to your Mega
  • On the Arduino Mega SoftSerial window in the Arduino IDE, make sure your comm port is set for the Arduino Mega.
  • Open your Serial Console, set it to 9600 baud speed rate if its not there yet, type the letter a on the text box on the top, the light on the Leonardo should go ON, type x on the text box and the light on the Leonardo should go OFF.

In this code we use software serial instead of the hardware UART serial, to communicate with the Leonardo, Pin8 will be RX and pin9 will be TX. Hardware Serial UART is used to communicate with the computer. The same process hapens here; type something in the serial console text window and the computer sends it to the Mega, then the Mega sends it to the Leonardo.

Here we use software serial instead of the hardware serial; pin8 is RX, pin9 is TX, and the same thing happens as the other example; if the letter a is received the LED on the Leonardo is turned ON if the letter x is received the LED is turned OFF.

Arduino Mega Hardware UART to Arduino Leonardo Software Serial

  • Keep the same program on the Arduino Leonardo
  • Load Arduino Mega UART3 code on the Mega
  • connect pin14 TX3 on the Mega to pin8 RX on the Leonardo
  • connect pin15 RX3 on the Mega to pin9 TX on the Leonardo
  • connect the Leonardo to a reliable power source
  • connect the Mega to the computer USB cable
  • On the Arduino Mega UART3 code in the Arduino IDE make sure your using the Mega port, Open the serial console when you type a the LED turns ON the Leonardo, when you type an x the LED on the Leonardo turns OFF.

In this code we use UART3 on the Mega to communicate with the Leonardo. Same thing happens as the other example we only used UART #3 on the Mega. The code on the Leonardo remains the same as the previous example.

This is the LAST Example!!!

We will connect ALL 3 together!!!

You may be wondering why would you want to do that?!??!

Because you may find yourself in a situation where your using your default hardware UART for a device and then you need another UART connection; this is where you use the software serial on the same board!!!... you may say.... well I will just use a Mega... Well, … my little programmer, not ALL shields or sensors are compatible with the Mega; that's why were are going to connect the Arduino UNO (Adafruit Metro 328) as an In-between microcontroller just for this reason.... You'll thank me later when the issue arrives..

  • To save time and energy, the Mega keeps the same program and the Leonardo keeps the same program from the last example.
  • We just need to upload the new program to the Arduino UNO (Adafruit Metro 328)
  • Upload Arduino UNO UART___SoftSerial code to the Arduino UNO(Adafruti Metro 328)
  • connect pin9 TX on the Leonardo to pin8 RX on the Arduino UNO(Adafruit Metro 328) this is for Software Serial
  • connect pin8 RX on the Leonardo to pin9 TX on the Arduino UNO(Adafruti Metro 328) this is for Software Serial
  • connect pin14 TX3 on the Mega to pin0 RX on the Arduino UNO(Adafruit Metro 328) this is for Hardware Serial UART
  • connect pin15 RX3 on the Mega to pin1 TX on the Arduino UNO (Adafruit Metro 328)
  • connect ALL 3 Micros ground pins together
  • connect Leonardo and UNO (Adafruit Metro 328) to a reliable power source
  • connect Mega to computers USB cable
  • On the Arduino Mega UART3 code IDE make sure your Mega comm port is selected.
  • Open the Serial Monitor use 9600 baud speed, when you type the letter v on the text window the LED on the UNO (Adafruit Metro 328) goes ON. Whe you type the letter b the LED goes OFF.
  • When you type the letter a on the text window the computer sends it to the Mega then the Mega sends it to the UNO (Adafruti Metro 328) then the UNO (Adafruit Mero 328) sends it to the Leonardo to turn ON the LED on the Leonardo. Type the letter x and the LED on the Leonardo goes OFF

Here you can see how softSerial is declared and used, when the letter x is recieved, softSerial will send a letter x to the Leonardo, same for the letter a.

Code

Arduino UNO (Adafruit Metro 328) SLAVE UARTArduino

Schematics

uploads2ftmp2f0d50ef77-6185-4601-942d-4247ff8283162fproject_2_arduinos_uart_to_uart_Fz1loFMYvo.fzz
How to connect Arduino Mega to Arduino Leonardo... Fritzing file..
uploads2ftmp2ff24e9ff1-b8a2-4e5d-bf6e-3145c47c040b2fproject_2_arduinos_softwareserial_to_softwareserial_fAMQ55FFEd.fzz
uploads2ftmp2f6ad8f826-fb59-49dd-a156-a81436d9f7162fproject_all_3_XamcrhTrwn.fzz

Author

Alex Fraga
  • 1 project
  • 0 followers

Arduino Serial Input Basics Keyboard

Published on

September 16, 2019
Write a comment

Members who respect this project

See similar projects
you might like

Table of contents

Write a comment

This tutorial explains how to take analog input to Arduino. One of the basic tutorials for Arduino. This input is shown through LED and Serial monitor.

1. Introduction:

A step by step illustrated basic tutorial for Arduino. Here we are taking analog input form a potentiometer. And this input is shown on LED as PWM and analog values on Serial monitor.

Arduino gives analog output in range of 0 to 255. Technically the output is digital but in the form of PWM, but it seems to be analog.

Arduino Boards have 6 PWM(Analog Pins) these are PIN No. 3,5,6,9,10,11.

Arduino Boards have 6 Analog Input pins these are PIN A0, A1, A2, A3, A4 and A5.

1.2 Hardware required

S.No.ItemQuantity
1Arduino UNO 1
2Breadboard 1
3LED 1
4Resistor 1k 1
5Jumper Male to male 7
6Preset 10k 1

2. Building Circuit

Schematic Diagram:

Circuit Layout:

Arduino Serial Input Basics Download

3. Programming:

Once we are done with circuit part, here is our programme to this circuit.

Arduino Serial Input Basics

A few points to understand:

1. Arduino has got 10 bit ADC so input we are taking from potentiometer will give us 10 bit data i.e. 0 to 1023.

2. Arduino’s PWM (output for LED) has got a range of 0 to 255.

3. Thus in order to take analog input to PWM we have divided the input value by 4. Please go through the coding you will be clear about these points.

4. This programme runs for ever, every time it takes reads value from input, if it is near about the same to last value it will not do anything, if we dont apply this algo of comparing last value to new value we will receive every value on serial monitor and it will be a difficult thing to even read the serial monitor.

You may download this code (Arduino Sketch) from here.

4. output:

Arduino Serial Input Basics

Here is the output of this tutorial-

When we rotate potentiometer, The output voltage changes. This change is detected by Arduino Analog Input code, This value is sent to serial monitor. And the same value after dividing by 4 is sent to LED’s PWM. So the brightness of LED changes with the change of potentiometer.

Here is an still image from this video so that you can be clear about circuit.

If you have any query please write us at support@roboindia.com

Thanks and Regards
Content Development Team
Robo India
https://roboindia.com