- Serial Communication Arduino Program
- Serial Communication Programs Arduino Code
- Serial Communication Programs Arduino Download
- Serial Communication Programs Arduino Free
- Serial Communication Programs Arduino Free
- Serial Communication Programs Arduino Software
First up is serial communication. This is pretty simple. I expected there to be more hitches, but it turned out to be pretty straight forward. Well let's get to it. First things first, HERE is the Arduino playground page for serial communication. You might want to read up on the different commands you have there. Ah, Arduino, I remember when you were just crawling around and blinking LEDs. Now you're ready to learn how to speak! In this lesson we'll learn how to use the Serial Library to communicate from the Arduino board back to the computer over the USB port.
Choosing a communication protocol for communication between microcontrollers and peripheral devices is an important part of embedded system. It is important because the overall performance of any embedded application depends on communication means as it is related to cost reduction, faster data transfer, long distance coverage etc.
In the previous tutorials we have learned about I2C communication protocol and SPI Communication protocols in Arduino. Now there is another serial communication protocol called RS-485.This Protocol uses an asynchronous serial communication. The main advantage of RS-485 is the long distance data transfer between two devices. And they are most commonly used in electrically noisy industrial environment.
In this tutorial, we will learn about RS-485 Serial communication between two Arduinos and then demonstrate it by controlling the brightness of the LED connected to a Slave Arduino from Master Arduino by sending ADC values through RS-485 Module. A 10k potentiometer is used to vary the ADC values at Master Arduino.
Let’s start by understanding the working of RS-485 Serial communication.
RS-485 Serial Communication Protocol
RS-485 is an asynchronous serial communication protocol which doesn’t require clock pulse. It uses a technique called differential signal to transfer binary data from one device to another.
So what is this differential signal transfer method??
Differential signal method works by creating a differential voltage by using a positive and negative 5V. It provides a Half-Duplex communication when using two wires and Full-Duplex requires 4 fourswires.
By using this method
- RS-485 supports higher data transfer rate of 30Mbps maximum.
- It also provides maximum data transfer distance compared to RS-232 protocol. It transfers data up to 1200 meters maximum.
- The main advantage of RS-485 over RS-232 is the multiple slave with single Master while RS-232 supports only single slave.
- It can have a maximum of 32 devices connected to RS-485 protocol.
- Another advantage of the RS-485 is that it is immune to the noise as they use differential signal method to transfer.
- RS-485 is faster compared to I2C protocol.
RS-485 in Arduino
For using RS-485 in Arduino, a module called 5V MAX485 TTL to RS485 which is based on Maxim MAX485 IC is needed as it allows serial communication over long distance of 1200 meters and it is bidirectional. In half duplex mode it has a data transfer rate of 2. 5Mbps.
5V MAX485 TTL to RS485 module requires a voltage of 5V and uses 5V logic levels so that it can be interfaced with hardware serial ports of microcontrollers like Arduino.
It has following features:
- Operating voltage: 5V
- On-board MAX485 chip
- A low power consumption for the RS485 communication
- Slew-rate limited transceiver
- 5.08mm pitch 2P terminal
- Convenient RS-485 communication wiring
- All pins of chip have been lead to can be controlled through the microcontroller
- Board size: 44 x 14mm
Pin-Out of RS-485:
Pin Name | Use |
VCC | 5V |
A | Non-inverting Receiver Input Non-Inverting Driver Output |
B | Inverting Receiver Input Inverting Driver Output |
GND | GND (0V) |
R0 | Receiver Out (RX pin) |
RE | Receiver Output (LOW-Enable) |
DE | Driver Output (HIGH-Enable) |
DI | Driver Input (TX pin) |
This RS-485 module can be easily interfaced with Arduino. Let’s use the hardware serial ports of Arduino 0 (RX) and 1(TX) (In UNO, NANO). Programming is also simple just use the Serial.print() to write to RS-485 and Serial.Read() to read from RS-485.
Programming part is explained later in detail but first lets check the required components and circuit diagram.
Components Required
- Arduino UNO or Arduino NANO (2)
- MAX485 TTL to RS485 Converter Module - (2)
- 10K Potentiometer
- 16x2 LCD Display
- LED
- Breadboard
- Connecting Wires
In this tutorial Arduino Uno is used as Master and Arduino Nano is used as Slave. Two Arduino Boards are used here so two RS-485 Modules are required.
Circuit Diagram
Circuit Connection between first RS-485 and Arduino UNO (Master):
RS-485 | Arduino UNO |
DI | 1 (TX) |
DE RE | 8 |
R0 | 0 (RX) |
VCC | 5V |
GND | GND |
A | To A of Slave RS-485 |
B | To B of Slave RS-485 |
Connection between second RS-485 and Arduino Nano (Slave):
RS-485 | Arduino UNO |
DI | D1 (TX) |
DE RE | D8 |
R0 | D0 (RX) |
VCC | 5V |
GND | GND |
A | To A of Master RS-485 |
B | To B of Master RS-485 |
Circuit Connection between a 16x2 LCD and Arduino Nano:
16x2 LCD | Arduino Nano |
VSS | GND |
VDD | +5V |
V0 | To potentiometer centre pin for contrast control of LCD |
RS | D2 |
RW | GND |
E | D3 |
D4 | D4 |
D5 | D5 |
D6 | D6 |
D7 | D7 |
A | +5V |
K | GND |
A 10K potentiometer is connected to the Analog Pin A0 of the Arduino UNO for providing Analog input and a LED is connected to pin D10 of Arduino Nano.
Programming Arduino UNO & Arduino Nano for RS485 Serial Communication
For programming both boards Arduino IDE is used. But make sure you have selected the corresponding PORT from Tools->Port and Board from Tools->Board.
Complete code with a Demo Video is given at the end of this tutorial. Here we are explaining important part of the code. There are two programs in this tutorial, one for Arduino UNO (Master) and other for Arduino Nano (Slave).
Code Explanation for Master: Arduino UNO
In the Master side, just simply take Analog input at pin A0 by varying the potentiometer and then SerialWrite those values to the RS-485 bus through the Hardware Serial Ports (0,1) of Arduino UNO.
To Begin Serial Communication at Hardware Serial Pins (0,1) use:
To read Analog value at the pin A0 of Arduino UNO and store them in a variable potval use:
Before writing the potval value to serial port, pins DE & RE of RS-485 should be HIGH that is connected to the pin 8 of Arduino UNO so to Make pin 8 HIGH:
Next to put those values in the Serial Port connected with the RS-485 module, use the following statement
Code Explanation for Slave: Arduino NANO
In the Slave side an integer value is received from the Master RS-485 that is available at the Hardware Serial port of the Arduino Nano (Pins -0,1). Simply read those value and store in a variable. The values are in form of (0 -1023). So it is converted into (0-255) as PWM technique is used to control LED brightness.
Then AnalogWrite those converted value to the LED pin D10 (It’s a PWM pin). So depending upon the PWM value the brightness of the LED changes and also display those values in 16x2 LCD display.
In order for the Slave Arduino‘s RS-485 to receive the values from the Master, just make the pins DE & RE of RS-485 LOW. So the pin D8 (enablePin) of Arduino NANO is made LOW.
Serial Communication Arduino Program
And to read the integer data available at Serial Port and store them in a variable use
Next convert value from (0-1023 to 0-255) and store them in a variable:
Next write the analog value (PWM) to pin D10 where LED anode is connected:
To print those PWM value in 16x2 LCD display use
Controlling LED Brightness with Serial Communication RS485
When the PWM value is set at 0 using potentiometer, the LED is turned OFF.
And when PWM value is set at 251 using potentiometer: The LED is turned ON with full brightness as shown in below picture:
So this is how RS485 can be used for serial communication in Arduino.
Master (Arduino UNO):
//Master code (Arduino UNO)
//Serial Communication Between Arduino using RS-485
int enablePin = 8;
int pushval = A0;
int potval =0 ;
void setup()
{
Serial.begin(9600); // initialize serial at baudrate 9600:
pinMode(enablePin, OUTPUT);
pinMode(pushval,INPUT);
delay(10);
digitalWrite(enablePin, HIGH); // (always high as Master Writes data to Slave)
}
void loop()
{
int potval = analogRead(pushval);
Serial.println(potval); //Serial Write POTval to RS-485 Bus
delay(100);
}
SLAVE CODE: (Arduino NANO):
//Slave code (Arduino NANO)
//Serial Communication Between Two Arduinos using RS-485
//Circuit Digest
#include <LiquidCrystal.h> //Include LCD library for using LCD display functions
int enablePin = 8;
int ledpin = 10;
LiquidCrystal lcd(2,3,4,5,6,7); // Define LCD display pins RS,E,D4,D5,D6,D7
void setup()
{
lcd.begin(16,2);
lcd.print('CIRCUIT DIGEST');
lcd.setCursor(0,1);
lcd.print('RS485 ARDUINO');
delay(3000);
lcd.clear();
Serial.begin(9600); // initialize serial at baudrate 9600:
pinMode(ledpin,OUTPUT);
pinMode(enablePin, OUTPUT);
delay(10);
digitalWrite(enablePin, LOW); // (Pin 8 always LOW to receive value from Master)
}
void loop()
{
while (Serial.available()) //While have data at Serial port this loop executes
{
lcd.clear();
int pwmval = Serial.parseInt(); //Receive INTEGER value from Master throught RS-485
int convert = map(pwmval,0,1023,0,255); //Map those value from (0-1023) to (0-255)
analogWrite(ledpin,convert); //PWM write to LED
lcd.setCursor(0,0);
lcd.print('PWM FROM MASTER');
lcd.setCursor(0,1);
lcd.print(convert); //Displays the PWM value
delay(100);
}
}
In our previous tutorial learned about SPI communication in Arduino. Today we will learn about another Serial Communication Protocol: I2C (Inter Integrated Circuits). Comparing I2C with SPI, I2C has only two wires while SPI uses four and I2C can have Multiple Master and Slave, while SPI can have only one master and multiple slaves. So there are more than one microcontroller in a project that need to be masters then I2C is used. I2C communication is generally used to communicate with Gyroscope, accelerometer, barometric pressure sensors, LED displays etc.
In this Arduino I2C tutorial we will use I2C communication between two arduino boards and send (0 to 127) values to each other by using potentiometer. Values will be displayed on the 16x2 LCD connected to each of the Arduino. Here one Arduino will act as Master and another one will act as Slave. So let’s start with the introduction about I2C communication.
What is I2C Communication Protocol?
The term IIC stands for “Inter Integrated Circuits”. It is normally denoted as I2C or I squared C or even as 2-wire interface protocol (TWI) at some places but it all means the same. I2C is a synchronous communication protocol meaning, both the devices that are sharing the information must share a common clock signal. It has only two wires to share information out of which one is used for the cock signal and the other is used for sending and receiving data.
How I2C Communication works?
I2C communication was first introduced by Phillips. As said earlier it has two wires, these two wires will be connected across two devices. Here one device is called a master and the other device is called as slave. Communication should and will always occur between two a Master and a Slave. The advantage of I2C communication is that more than one slave can be connected to a Master.
The complete communication takes place through these two wires namely, Serial Clock (SCL) and Serial Data (SDA).
Serial Clock (SCL): Shares the clock signal generated by the master with the slave
Serial Data (SDA): Sends the data to and from between the Master and slave.
At any given time only the master will be able to initiate the communication. Since there is more than one slave in the bus, the master has to refer to each slave using a different address. When addressed only the slave with that particular address will reply back with the information while the others keep quit. This way we can use the same bus to communicate with multiple devices.
The voltage levels of I2C are not predefined. I2C communication is flexible, means the device which is powered by 5v volt, can use 5v for I2C and the 3.3v devices can use 3v for I2C communication. But what if two devices which are running on different voltages, need to communicate using I2C? A 5V I2C bus can’t be connected with 3.3V device. In this case voltage shifters are used to match the voltage levels between two I2C buses.
There are some set of conditions which frame a transaction. Initialization of transmission begins with a falling edge of SDA, which is defined as ‘START’ condition in below diagram where master leaves SCL high while setting SDA low.
As shown in the above diagram below,
The falling edge of SDA is the hardware trigger for the START condition. After this all devices on the same bus go into listening mode.
In the same manner, rising edge of SDA stops the transmission which is shown as ‘STOP’ condition in above diagram, where the master leaves SCL high and also releases SDA to go HIGH. So rising edge of SDA stops the transmission.
R/W bit indicates the direction of transmission of following bytes, if it is HIGH means the slave will transmit and if it is low means the master will transmit.
Each bit is transmitted on each clock cycle, so it takes 8 clock cycles to transmit a byte. After each byte either sent or received, ninth clock cycle is held for the ACK/NACK (acknowledged/not acknowledged). This ACK bit is generated by either slave or master depending upon the situation. For ACK bit, SDA is set to low by master or slave at 9th clock cycle. So it is low it considered as ACK otherwise NACK.
Where to use I2C communication?
I2C communication is used only for short distance communication. It is certainly reliable to an extent since it has a synchronised clock pulse to make it smart. This protocol is mainly used to communicate with sensor or other devices which has to send information to a master. It is very handy when a microcontroller has to communicate with many other slave modules using a minimum of only wires. If you are looking for a long range communication you should try RS232 and if you are looking for more reliable communication you should try the SPI protocol.
I2C in Arduino
The image below shows the I2C pins present in Arduino UNO.
I2C Line | Pin in Arduino |
SDA | A4 |
SCL | A5 |
Serial Communication Programs Arduino Code
Before we get start into programming I2C using two Arduino. We need to learn about the Wire library used in Arduino IDE.
The library <Wire.h> is included in the program for using the following functions for I2C communication.
1. Wire.begin(address):
Use: This library is used for making communication with I2C devices. This Initiate the Wire library and join the I2C bus as a master or slave.
Address: The 7-bit slave address is optional and if the address is not specified, it joins the bus as a master like this [Wire.begin()].
2. Wire.read():
Use: This function is used to read a byte that was received from master or slave device, either that was transmitted from a slave device to a master device after a call to requestFrom() or was transmitted from a master to a slave.
3. Wire.write():
Use: This function is used to write data to a slave or master device.
Slave to Master: Slave writes data to a master when Wire.RequestFrom() is used in master.
Master to Slave: For transmission from a master to slave device Wire.write() is used in-between calls to Wire.beginTransmission() and Wire.endTransmission().
Wire.write() can be written as:
- Wire.write(value)
value: a value to send as a single byte.
- Wire.write(string) :
string: a string to send as a series of bytes.
- Wire.write(data, length):
data: an array of data to send as bytes
length: the number of bytes to transmit.
4. Wire.beginTransmission(address):
Use: This function is used to begin a transmission to the I2C device with the given slave address. Subsequently, build queue of bytes for transmission with the write() function and then transmit them by calling endTransmission() function. 7-bit address of the device is transmitted.
5. Wire.endTransmission();
Use: This function is used to end a transmission to a slave device that was begun by beginTransmission() and transmits the bytes that were queued by Wire.write().
6. Wire.onRequest();
Use: This function gets called when a master requests data using Wire.requestFrom() from the slave device. Here we can include Wire.write() function to send data to the master.
Use: This function gets called when a slave device receives a data from a master. Here we can include Wire.read(); function to read the data sent from master.
8. Wire.requestFrom(address,quantity);
Use: This function is used in the master to request bytes from a slave device. The function Wire.read() is used to read the data sent from the slave device.
address: the 7-bit address of the device to request bytes from
quantity: the number of bytes to request
Components Required
- Arduino Uno (2-Nos)
- 16X2 LCD display module
- 10K Potentiometer (4-Nos)
- Breadboard
- Connecting Wires
Circuit Diagram
Working Explanation
Here for demonstrating I2C communication in Arduino, we use Two Arduino UNO with Two 16X2 LCD display attached to each other and use two potentiometers at both arduino to determine the sending values (0 to 127) from master to slave and slave to master by varying the potentiometer.
We take input analog value at arduino pin A0 from (0 to 5V) by using potentiometer and convert them into Analog to Digital value (0 to 1023). Then these ADC values are further converted into (0 to 127) as we can send only 7-bit data through I2C communication. The I2C communication takes place through two wires at pin A4 & A5 of both arduino.
Values at Slave Arduino’s LCD will be changed by varying the POT at master side and vice versa.
I2C Programming in Arduino
This tutorial has two programs one for master Arduino and other for slave Arduino. Complete programs for both the sides are given at the end of this project with a demonstration Video.
Master Arduino Programming Explanation
1. First of all we need to include the Wire library for using I2C communication functions and LCD library for using LCD functions. Also define LCD pins for 16x2 LCD. Learn more about interfacing LCD with Arduino here.
2. In void setup()
- We Start Serial Communication at Baud Rate 9600.
- Next we start the I2C communication at pin (A4,A5)
- Next we initialize LCD display module in 16X2 mode and display the welcome message and clear after five seconds.
3. In void loop()
- First we need to get data from the Slave so we use requestFrom() with the slave address 8 and we request one byte
The received value is read using Wire.read()
- Next we need to read the analog value from the master arduino POT attached to pin A0
We convert that value in terms of one byte as 0 to 127.
- Next we need to send those converted values so we begin the transmission with slave arduino with 8 address
- Next we display those received values from the slave arduino with a delay of 500 microseconds and we continuously receive and display those value.
Slave Arduino Programming Explanation
1. Same as master, first of all we need to include the Wire library for using I2C communication functions and LCD library for using LCD functions. Also define LCD pins for 16x2 LCD.
2. In void setup()
- We Start Serial Communication at Baud Rate 9600.
- Next we start the I2C communication at pin (A4, A5) with slave address as 8. Here it is important to specify the slave address.
Next we need to call the function when Slave receives value from master and when Master request value from Slave
- Next we initialize LCD display module in 16X2 mode and display the welcome message and clear after five seconds.
3. Next we have two functions one for request event and one for receive event
For request Event
When Master request value from slave this function will be executed. This function does take input value from the Slave POT and convert it in terms of 7-bit and send that value to master.
For Receive Event
When Master sends data to slave with slave address (8) this function will be executed. This function reads the received value from master and store in a variable of type byte.
4. In Void loop():
We display the received value from master continuously in the LCD display module.
By rotating the Potentiometer at one side, you can see the varying values on LCD on another side:
So this is how I2C communication takes place in Arduino, here we have use two Arduinos to demonstrate not only sending of data but also receiving the data using I2C communication. So now you can interface any I2C sensor to Arduino.
The complete coding for Master and Slave Arduino is given below with a demonstration video
Master Arduino Programming
//I2C MASTER CODE
//I2C Communication between Two Arduino
//Circuit Digest
//Pramoth.T
Serial Communication Programs Arduino Download
#include<Wire.h> //Library for I2C Communication functions
#include<LiquidCrystal.h> //Library for LCD display function
LiquidCrystal lcd(2, 7, 8, 9, 10, 11); //Define LCD Module Pins (RS,EN,D4,D5,D6,D7)
void setup()
{
lcd.begin(16,2); //Initilize LCD display
lcd.setCursor(0,0); //Sets Cursor at first line of Display
lcd.print('Circuit Digest'); //Prints CIRCUIT DIGEST in LCD
lcd.setCursor(0,1); //Sets Cursor at second line of Display
lcd.print('I2C 2 ARDUINO'); //Prints I2C ARDUINO in LCD
delay(5000); //Delay for 5 seconds
lcd.clear(); //Clears LCD display
Serial.begin(9600); //Begins Serial Communication at 9600 baud rate
Wire.begin(); //Begins I2C communication at pin (A4,A5)
}
void loop()
{
Wire.requestFrom(8,1); // request 1 byte from slave arduino (8)
byte MasterReceive = Wire.read(); // receive a byte from the slave arduino and store in MasterReceive
int potvalue = analogRead(A0); // Reads analog value from POT (0-5V)
byte MasterSend = map(potvalue,0,1023,0,127); //Convert digital value (0 to 1023) to (0 to 127)
Wire.beginTransmission(8); // start transmit to slave arduino (8)
Wire.write(MasterSend); // sends one byte converted POT value to slave
Wire.endTransmission(); // stop transmitting
lcd.setCursor(0,0); //Sets Currsor at line one of LCD
lcd.print('>> Master <<'); //Prints >> Master << at LCD
lcd.setCursor(0,1); //Sets Cursor at line two of LCD
lcd.print('SlaveVal:'); //Prints SlaveVal: in LCD
lcd.print(MasterReceive); //Prints MasterReceive in LCD received from Slave
Serial.println('Master Received From Slave'); //Prints in Serial Monitor
Serial.println(MasterReceive);
delay(500);
lcd.clear();
}
Slave Arduino Programming
//I2C SLAVE CODE
//I2C Communication between Two Arduino
//CircuitDigest
//Pramoth.T
#include<Wire.h> //Library for I2C Communication functions
#include<LiquidCrystal.h> //Library for LCD display function
LiquidCrystal lcd(2, 7, 8, 9, 10, 11); //Define LCD Module Pins (RS,EN,D4,D5,D6,D7)
Serial Communication Programs Arduino Free
byte SlaveReceived = 0;
void setup()
{
lcd.begin(16,2); //Initilize LCD display
lcd.setCursor(0,0); //Sets Cursor at first line of Display
lcd.print('Circuit Digest'); //Prints CIRCUIT DIGEST in LCD
lcd.setCursor(0,1); //Sets Cursor at second line of Display
lcd.print('I2C 2 ARDUINO'); //Prints I2C ARDUINO in LCD
delay(5000); //Delay for 5 seconds
lcd.clear(); //Clears LCD display
Serial.begin(9600); //Begins Serial Communication at 9600 baud rate
Wire.begin(8); //Begins I2C communication with Slave Address as 8 at pin (A4,A5)
Wire.onReceive(receiveEvent); //Function call when Slave receives value from master
Wire.onRequest(requestEvent); //Function call when Master request value from Slave
}
Serial Communication Programs Arduino Free
void loop(void)
{
lcd.setCursor(0,0); //Sets Currsor at line one of LCD
lcd.print('>> Slave <<'); //Prints >> Slave << at LCD
lcd.setCursor(0,1); //Sets Cursor at line two of LCD
lcd.print('MasterVal:'); //Prints MasterVal: in LCD
lcd.print(SlaveReceived); //Prints SlaveReceived value in LCD received from Master
Serial.println('Slave Received From Master:'); //Prints in Serial Monitor
Serial.println(SlaveReceived);
delay(500);
lcd.clear();
}
void receiveEvent (int howMany) //This Function is called when Slave receives value from master
{
SlaveReceived = Wire.read(); //Used to read value received from master and store in variable SlaveReceived
}
Serial Communication Programs Arduino Software
void requestEvent() //This Function is called when Master wants value from slave
{
int potvalue = analogRead(A0); // Reads analog value from POT (0-5V)
byte SlaveSend = map(potvalue,0,1023,0,127); // Convert potvalue digital value (0 to 1023) to (0 to 127)
Wire.write(SlaveSend); // sends one byte converted POT value to master
}