When we hear the word Arduino, the first thing that comes to mind is a microprocessor chip assembled on a single board to help us to create an IOT friendly environment and IOT projects using Arduino communication.
This chip helps us create various projects and electronic devices, and moreover, facilitates physical interaction with the world of technology. Although it’s not only Arduino which is used in IOT Testing, other devices like raspberry pi are also used but Arduino is preferred first due to its cost and ease of handling.
However, Arduino is not just limited to a microprocessor chip. It is also a company that designs, manufactures, and supports electronic hardware and software and help to develop Arduino communication for IOT Application.
Arduino, moreover, makes the interaction between advanced technologies and the physical world easy and fun.
Additionally, Arduino is open-source, allowing anyone to contribute to and expand its capabilities.
Today in this blog we will cover Arduino and its different types of communication.
How Does Arduino communication using for IOT Application:
The main key to creating a project using any microprocessor is, therefore, to have an IoT-enabled environment.
To have an IOT enabled environment using any microprocessor is to be able to communicate or to be able to program and manage it accordingly, so is important in Arduino communication for IOT App.
Arduino communication is classified into two parts: wired and wireless, each with further subdivisions.
1. Wired Communication
Wired communication is, therefore, the most reliable and straightforward way to establish a connection. However, we won’t limit Arduino to simple wired communication. Instead, we’ll expand this wired communication into three distinct categories to develop IOT project/application using Arduino.
These 3 categories are explain below for Arduino Communication for IOT App:
Serial Communication:
UART communication is another name for serial communication. UART stands for universal Asynchronous receiver and transmitter.
It is mostly used for communication between Arduino and computers, but it can be applied in various other ways depending on our requirements and develop IOT application using Arduino. To establish the connection easily, we typically use the ‘Serial library’ in Arduino.
Examples for Serial Communication:
For an example let’s try to have UART communication between 2 Arduino.
- Hardware Set-up
- Connect the ground pin of both the Arduino.
- Connect the RX pin of first Arduino with TX pin of second Arduino and TX pin of first Arduino to second Arduino.
- Code Example :
Master Arduino Code:
void setup() {
Serial.begin(9600); // Initialize serial communication at 9600 bits per second
}
void loop() {
Serial.println("Hello from Master!"); // Send a message
delay(1000); // Wait for a second
}
Slave Arduino Code:
void setup() {
Serial.begin(9600); // Initialize serial communication at 9600 bits per second
}
void loop() {
if (Serial.available() > 0) { // Check if data is available to read
String message = Serial.readString(); // Read the incoming data
Serial.println("Received: " + message); // Print the received message
}
}
Here we go with a successful Arduino communication using UART
SPI Communication
SPI communication stands for Serial Peripheral Interface, which is usually used for short-distance communication and IOT projects.
The difference between serial communication and SPI communication is that, in contrast, SPI communication is synchronous and uses a clock cycle to communicate. Additionally, SPI is full-duplex communication.
In SPI you will find that there is a master slave relation where SPI has 4 wires to communicate which is MOSI (Master out Slave in) MISO (Master in Slave out) SCL (Serial clock) SS (slave select). Here, the first master sends the signal to all slaves with the identity number; consequently, the slave who has the same identity number reverts back to the master. After hearing back from the slave master, then send data to the particular slave. After the master sends the data, it then sends a clock signal to indicate that the data transfer is complete.
Examples for SPI Communication:
For example, let’s try to have SPI communication between two Arduino boards.
- Hardware Set-up
- First define the Pins and include SPI.H library in the code
- Here let’s consider 10, 11, 12, 13 digital pins SS, MOSI, MISO, SCK on Arduino UNO.
- Now let’s initialize the pins in the code:
void setup() {
SPI.begin(115200);
// Set pin modes for SS, MOSI, MISO, and SCK
pinMode(SS, OUTPUT);
pinMode(MOSI, OUTPUT);
pinMode(MISO, INPUT);
pinMode(SCK, OUTPUT);
// Set slave select (SS) pin high to disable the slave device
digitalWrite(SS, HIGH);
}
Code for the master board:
#include <SPI.h>
const int slaveSelectPin = 10;
void setup() {
SPI.begin(115200);
pinMode(slaveSelectPin, OUTPUT);
}
void loop() {
digitalWrite(slaveSelectPin, LOW);
SPI.transfer('H');
digitalWrite(slaveSelectPin, HIGH);
delay(1000);
}
Code for the Slave board:
#include <SPI.h>
const int slaveSelectPin = 10;
void setup() {
SPI.begin(115200);
pinMode(slaveSelectPin, OUTPUT);
}
void loop() {
if (digitalRead(slaveSelectPin) == LOW) {
char receivedData = SPI.transfer('L');
Serial.println(receivedData);
}
}
PFB image of Connections:
I2C Communication
I2C Communication stands for Inter Integrated Circuit, and it is a half-duplex communication that works on a 2-wire protocol. Additionally, it has an extra overhead with start and stop bits.
I2C has an acknowledgment bit after every byte of transfer. It has a pullup resistor requirement. I2c is slower but comes with clock stretch functionality.
Examples for I2C Communication:
For an example let’s try to have I2C communication between 2 Arduino
- Hardware Set-up
I2C pins on the Arduino Uno are SDA (A4) and SCL (A5).
- Connect the Ground pin of Both Arduinos.
- Connect the SDA pin of Master to the SDA pin of slave.
- Connect the SCL pin of Master to the SCL pin of Master.
- Master Arduino Code:
#include <Wire.h>
void setup() {
Wire.begin(); // Join I2C bus as master
}
void loop() {
Wire.beginTransmission(8); // Begin transmission to device with address 8
Wire.write("Hello from Master!"); // Send a message
Wire.endTransmission(); // End transmission
delay(1000); // Wait for a second
}
Slave Arduino Code:
#include <Wire.h>
void setup() {
Wire.begin(8); // Join I2C bus with address 8
Wire.onReceive(receiveEvent); // Register a function to be called when data is received
}
void loop() {
// Do nothing, everything is handled in receiveEvent
}
void receiveEvent(int howMany) {
while (Wire.available()) { // Loop through all received bytes
char c = Wire.read(); // Read each byte
Serial.print(c); // Print the received message
}
Serial.println();
}
PFB Image of Connection
2. Wireless Communication
Sometime we want to develop the projects which need to have the flexibility due which we need to have wireless connection. Don’t worry Arduino covered you there too Arduino do have 4 types wireless communication for IOT project using Arduino.
- Bluetooth: As the name is very familiar to us, let’s directly move to the components which help us to attain Bluetooth connectivity. By using HC-05 and HC-06 we can attain Bluetooth connectivity using Arduino. However Bluetooth provides connectivity to a short distance.
- Wifi: Sometimes we don’t need short distance coverage then we can switch to the wifi or in cases where we want the internet accessibility for some of our projects we can move to wifi connection. You can use the ESP32 and ESP8266 modules for the same purpose.
- RF (Radio Frequency): Uses radio waves for medium-distance communication, with modules like nRF24L01 and RF433.
- LoRa (Long Range): Great for long-distance, low-power communication, ideal for remote monitoring systems.
Conclusion
In conclusion, Arduino offers a versatile platform for various types of communication, allowing users to create innovative and interactive projects in IOT. Through serial, SPI, and I2C communications, Arduino facilitates reliable and efficient data exchange, each method catering to different needs and scenarios for exploring Arduino communication for IOT App.
Whether you are connecting multiple Arduinos or interfacing with other devices, understanding these communication protocols enhances your ability to leverage Arduino’s full potential in the IOT world.
https://github.com/spurqlabs/Arduino-Communication-Methods-for-IoT-Applications.
Click here to read more blogs like this.
3Deepti Rana is a passionate profession keen to learn new technologies and exceling them with expertise. She thrives to bridge the gap between real time problems and technical world. With a background in IT she has taken steps in robotics too. Her background in IT doesn’t stop her to explore different fields, that can be seen through her projects where see has used GSM, GPRS, ESP32, Arduino, PS2, line sensor, ultrasonic sensors, and fingerprint sensor. She does love to share her knowledge, which she accomplishes by volunteering in Bhumi NGO.