0% found this document useful (0 votes)
7 views

SerialCommunication Report

Uploaded by

alialbaghdadi3h
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

SerialCommunication Report

Uploaded by

alialbaghdadi3h
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

University of Basrah

College of Engineering
Department of Computer Engineering

Embedded System Lab

Serial Communication (ISP, UART, and I2C)

Name :
Ahmed Hussein Taher (2)
Abass Jafar Ali (20)
Mohammed Abass Hatem (28)
Introduction:

I2C
(Inter-Integrated Circuit) is a serial communication protocol that allows multiple devices
to communicate with each other on the same bus. It uses two bidirectional signal lines -
SDA (data line) and SCL (clock line) - and supports multiple devices by assigning
unique addresses to each device. I2C is commonly used for communication between
various components in embedded systems, such as sensors, memory devices, and
displays.

UART
(Universal Asynchronous Receiver/Transmitter) is another serial communication
protocol that enables point-to-point communication between two devices. It consists of
two unidirectional signal lines - TX (transmit) and RX (receive) - and does not require a
clock signal like I2C. UART is often used for data transfer between a microcontroller
and external peripherals, such as GPS modules, Bluetooth modules, and serial
communication devices.

In summary, I2C is suitable for communication between multiple devices on the same
bus, while UART is ideal for simple point-to-point communication between two devices.
Each protocol has its own advantages and use cases depending on the specific
requirements of the communication system.

ISP (In-System Programming):


ISP is a protocol used for programming microcontrollers in-circuit. It enables the
firmware or program to be uploaded directly to the microcontroller's flash memory. ISP
typically uses multiple pins for communication, including a clock line (SCK), a data line
(MISO), a data line (MOSI), and a reset line (RESET). It is commonly used when you
want to program or update the firmware of an Arduino board or other compatible
microcontrollers.
Protocol Bus Type Number of wires Number of Wiring
Devices
I2C Shared bus (SDA,SCL) Multiple Two-wie
devices( up bus
to 127)
UART Point –to-pint (TX,RX) Point to Two
point signal line
(Tx & Rx)
ISP Dedicated pins Multiple(SCK,MISOmMOSI,RESET) Master Pins
Practical Implementation of I2C:
1. Connecting an I2C Sensor: Many sensors, such as temperature sensors (e.g.,
TMP102) or motion sensors (e.g., MPU-6050), use the I2C protocol for communication.
You can connect these sensors to an Arduino using the I2C interface. The Arduino acts
as the I2C master and communicates with the sensor to read data, such as temperature
or motion information.
2. Interfacing with I2C LCD Display: I2C-based LCD displays, such as the popular 16x2
or 20x4 character LCDs with an I2C backpack, simplify the wiring and reduce the
number of pins required to drive the display. By connecting the I2C LCD display to the
Arduino, you can easily display text or custom messages on the screen using the I2C
protocol.

Practical Implementation of UART:


1. Serial Communication with a Computer: UART is commonly used for serial
communication between an Arduino and a computer. You can connect the Arduino's
UART (Tx and Rx pins) to the corresponding UART pins of a USB-to-Serial converter or
a microcontroller with a built-in UART. This allows you to send and receive data
between the Arduino and the computer, enabling tasks such as debugging, data
logging, or controlling the Arduino remotely.
2. Wireless Communication: UART can be used for wireless communication by
connecting a UART-based wireless module, such as Bluetooth modules (e.g., HC-
05/HC-06) or Wi-Fi modules (e.g., ESP8266/ESP32), to an Arduino. By sending and
receiving UART data between the Arduino and the wireless module, you can establish
wireless communication and control or exchange data with other devices or systems.
In both cases, the practical implementation involves connecting the respective protocol's
signal lines (SDA and SCL for I2C, Tx and Rx for UART) between the devices,
configuring the communication settings (e.g., baud rate, address), and implementing the
appropriate code on the Arduino to send and receive data using the protocol's specific
functions or libraries.
Note: The practical implementation may vary depending on the specific Arduino board,
sensors, modules, or displays you are using. It's recommended to refer to the
datasheets, pinout diagrams, and example codes provided by the manufacturers for
proper wiring and programming guidance.
I2C:
SLAVE1
MASTER
SLAVE2

SLAVE1
MASTER
SLAVE2
Master code
// Include Arduino Wire library for I2C
#include <Wire.h>
int 4=green;
Int 5=yelwo;
Int 6=red;
#define SLAVE_ADDR 9
#define ANSWERSIZE 2
#define SLAVE2_ADDR 8
#define ANSWERSIZES2 2
void setup() {
// Initialize I2C communications as Master
Wire.begin();// Setup serial monitor
Serial.begin(9600);
Serial.println("I2C Master Demonstration");
}
void loop() {
delay(50);// Read response from Slave
Wire.requestFrom(SLAVE_ADDR,ANSWERSIZE);
// Add characters to string
String response1 = "";
while (Wire.available()) {
char b = Wire.read();
response1 += b;
} // Print to Serial Monitor
Serial.println(response1);
Wire.requestFrom(SLAVE_ADDR2,ANSWERSIZE2);
// Add characters to string
String response2 = "";
while (Wire.available()) {
char c = Wire.read();
response2 += c;
}
Serial.println("Write data to slave");
int Push=digitalRead(3);
// Write a charatre to the Slave
Wire.beginTransmission(SLAVE_ADDR);
Wire.write(Push);
Wire.write(response2);
Wire.endTransmission();
Wire.beginTransmission(SLAVE2_ADDR);
Wire.write(Push);
Wire.write(response1);
Wire.endTransmission();
Serial.println("Receive data");
// Print to Serial Monitor
Serial.println(response2);
If(Push==1){
digitalWrite(green,HIGH);
}
Else{
digitalWrite(green,LOW);
digitalWrite(yelwo,HIGH);
}
If(response1=='1'||response2=='1'){
digitalWrite(green,LOW);
digitalWrite(yelwo,LOW);
digitalWrite(red,HIGH);
}
}
SLAVE_1 CODE:
// Include Arduino Wire library for I2C
#include <Wire.h>
int Push=3;
int 4=green;
Int 5=yelwo;
Int 6=red;
// Define Slave I2C Address
#define SLAVE_ADDR 9
// Define Slave answer size
#define ANSWERSIZE 2
// Define string with response to Master
String answer = "";
void setup() {
// Initialize I2C communications as Slave
Wire.begin(SLAVE_ADDR);
// Function to run when data requested from master
Wire.onRequest(requestEvent);
// Function to run when data received from master
Wire.onReceive(receiveEvent);
// Setup Serial Monitor
Serial.begin(9600);
Serial.println("I2C Slave Demonstration");
}
void receiveEvent() {
// Read while data received
while (0 < Wire.available()) {
byte x = Wire.read();
If(x[0]=='1'||x[1]==1){
digitalWrite(green,LOW);
digitalWrite(yelwo,LOW);
digitalWrite(red,HIGH);
}
}
// Print to Serial Monitor
Serial.println("Receive event");
}
void requestEvent() {

response=digitalRead(Push);
}
// Send response back to Master
Wire.write(response,sizeof(response)); // Print to Serial Monitor
Serial.println("Request event");
}
void loop() {
if(Push==1){
digitalWrite(green,HIGH);
digitalWrite(yelwo,LOW);
digitalWrite(red,LOW;
}
delay(50);
}
SLAVE_2 CODE:
// Include Arduino Wire library for I2C
#include <Wire.h>
int Push=3;
int 4=green;
Int 5=yelwo;
Int 6=red;
// Define Slave I2C Address
#define SLAVE_ADDR 8
// Define Slave answer size
#define ANSWERSIZE 2
// Define string with response to Master
String answer2 = "";
void setup() {
// Initialize I2C communications as Slave
Wire.begin(SLAVE2_ADDR);
// Function to run when data requested from master
Wire.onRequest(requestEvent);
// Function to run when data received from master
Wire.onReceive(receiveEvent);
// Setup Serial Monitor
Serial.begin(9600);
Serial.println("I2C Slave Demonstration");
}
void receiveEvent() {
// Read while data received
while (0 < Wire.available()) {
byte x = Wire.read();
If(x[0]=='1'||x[1]==1){
digitalWrite(green,LOW);
digitalWrite(yelwo,LOW);
digitalWrite(red,HIGH);
}
}
// Print to Serial Monitor
Serial.println("Receive event");
}
void requestEvent() {
response=digitalRead(Push);
}
// Send response back to Master
Wire.write(response,sizeof(response)); // Print to Serial Monitor
Serial.println("Request event");
}
void loop() {
if(Push==1){
digitalWrite(green,HIGH);
digitalWrite(yelwo,LOW);
digitalWrite(red,LOW;
} // Time delay in loop
delay(50);
}
UART:

FIRST ARDUINO DEVICE CODE:

int push=3;
int green=4;
int yelow=5;
int red=6;

void setup() {
Serial.begin(9600);
}

void loop() {

if (Serial.available()) {
String message = "000"
message = Serial.readString();

Serial.println(receivedMessage); // Print the received data


}
}
message[0]=String(push);
If(push==1){
digitalWrite(green,HIGH);
digitalWrite(yelwo,LOW);
digitalWrite(red,LOW);

}
else if(message[1]=="1"||message[2]=="1"){
digitalWrite(green,LOW);
digitalWrite(yelow,LOW);
digitalWrite(red,HIGH);

}
else {
digitalWrite(green,LOW);
digitalWrite(yelow,HIGH);
digitalWrite(red,LOW);

}
Serial.println(message); // Send text via UART
delay(1000);
}
SECOND ARDUINO DEVICE

int push=3;
int green=4;
int yelow=5;
int red=6;

void setup() {
Serial.begin(9600);}

void loop() {
if (Serial.available()) {
String message = Serial.readString();

}
message[1]=String(push);
If(push==1){
digitalWrite(green,HIGH);
digitalWrite(yelwo,LOW);
digitalWrite(red,LOW);

}
else if(message[0]=="1"||message[2]=="1"){
digitalWrite(green,LOW);
digitalWrite(yelow,LOW);
digitalWrite(red,HIGH);

}
else {
digitalWrite(green,LOW);
digitalWrite(yelow,HIGH);
digitalWrite(red,LOW);

}
Serial.println(message);
delay(1000);

}
THIRD ARDUINO DEVICE:

int push=3;
int green=4;
int yelow=5;
int red=6;

void setup() {
Serial.begin(9600);}

void loop() {
if (Serial.available()) {
String message = Serial.readString();

}
message[2]=String(push);
If(push==1){
digitalWrite(green,HIGH);
digitalWrite(yelwo,LOW);
digitalWrite(red,LOW);

}
else if(message[0]=="1"||message[1]=="1"){
digitalWrite(green,LOW);
digitalWrite(yelow,LOW);
digitalWrite(red,HIGH);

}
else {
digitalWrite(green,LOW);
digitalWrite(yelow,HIGH);
digitalWrite(red,LOW);

}
Serial.println(message);
delay(1000);

}
4-
The statement is accurate. Arduino's analog pins can indeed be used for both reading analog
sensors and performing general-purpose input/output (GPIO) operations. The analog pins on
Arduino boards, such as the Arduino Uno, Arduino Mega, or Arduino Nano, serve a dual purpose.

Let's consider an example to verify this statement. Suppose you have an Arduino Uno board with six
analog input pins labeled A0 to A5. These pins can be used to read values from analog sensors,
such as a potentiometer or a light sensor. However, they can also be used as digital pins for
general-purpose I/O operations.

Here's an example code snippet that demonstrates the dual functionality of analog pins on an
Arduino Uno:

// Digital I/O using analog pins


const int analogPin = A0; // Using analog pin A0 as a digital pin

void setup() {
pinMode(analogPin, OUTPUT); // Configure analog pin A0 as an output
}

void loop() {
digitalWrite(analogPin, HIGH); // Set A0 pin to HIGH (5V)
delay(1000); // Wait for 1 second
digitalWrite(analogPin, LOW); // Set A0 pin to LOW (0V)
delay(1000); // Wait for 1 second
}

In this example, we use the pinMode function to configure analog pin A0 as an output. Then, we use
digitalWrite to set the voltage level of that pin to either HIGH or LOW, effectively turning it on or off.
This demonstrates the usage of an analog pin as a digital output.

Therefore, the example confirms that analog pins on an Arduino board can perform both analog and
digital operations, providing the functionality of general-purpose input/output (GPIO) pins, as stated
in the original statement.

You might also like