SerialCommunication Report
SerialCommunication Report
College of Engineering
Department of Computer Engineering
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.
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:
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();
}
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:
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.