Lab 6 - Mes - Group 4
Lab 6 - Mes - Group 4
Faculty of Engineering
Lab Report
Experiment # 06
Experiment Title: Communication between two Arduino Boards using SPI.
Date of Perform: 11 DEC 2024 Date of Submission: 18 DEC 2024
Course Title: Microprocessor and Embedded Systems Lab
Course Code: EE4103 Section: E
Semester: Fall 2024-25 Degree Program: BSc in CSE/EEE
Course Teacher: Prof. Dr. Engr. Muhibul Haque Bhuyan
Declaration and Statement of Authorship:
1. I/we hold a copy of this Assignment/Case Study, which can be produced if the original is lost/damaged.
2. This Assignment/Case Study is my/our original work; no part has been copied from any other student’s work or any other source except where
due acknowledgment is made.
3. No part of this Assignment/Case Study has been written for me/us by any other person except where such collaboration has been authorized.
by the concerned teacher and is acknowledged in the assignment.
4. I/we have not previously submitted or am submitting this work for any other course/unit.
5. This work may be reproduced, communicated, compared, and archived to detect plagiarism.
6. I/we permit a copy of my/our marked work to be retained by the Faculty Member for review by any internal/external examiners.
7. I/we understand that Plagiarism is the presentation of the work, idea, or creation of another person as though it is your own. It is a form of cheating and is a
very serious academic offense that may lead to expulsion from the University. Plagiarized material can be drawn from, and presented in, written, graphic, and
visual forms, including electronic data, and oral presentations. Plagiarism occurs when the origin of the source is not appropriately cited.
8. I/we also understand that enabling plagiarism is the act of assisting or allowing another person to plagiarize or copy my/our work.
* Student(s) must complete all details except the faculty use part.
** Please submit all assignments to your course teacher or the office of the concerned teacher.
Group # 04
Total Marks
1
Table of Contents
Objectives 3
Equipment List 3
Circuit Diagram 3
Experimental Output Results (Color Photographs) 4
Simulation Output Results (Color Photographs) 8
Discussion 9
References 9
2
Objectives:
The objectives of this experiment are to-
1. Study the SPI protocol used in Arduino.
2. Write assembly language programming code for SPI communication with Arduinos.
3. Use SPI protocol for communication between two Arduinos.
4. Build a circuit to control the master side LED by the push button at the slave side and vice versa using
the SPI Serial communication protocol.
5. Know the working principles of the SPI used in Arduino.
Equipment List:
1) Arduino IDE (2.0.1 or any recent version)
2) Arduino UNO (2)
3) LED (2)
4) Push Button (2)
5) Resistors 10k, 2.2k (2+2)
6) Connecting Wires
7) Breadboard.
Circuit Diagram:
The Arduino platform is made up of the following components.
Fig. 1 Two Arduino board’s pin connections for SPI communications (schematic diagram)
3
Experimental Output Results:
Here is the hardware implementation of an LED light control using a switch from Master and slave and
the necessary explanation of the implementation:
Fig.2 Hardware implementation of LED light control using a switch from Master
and slave
Fig.3 Peripheral LED was ON in the SPI communication circuit when pressing the button of Controller.
4
Fig.4 The Controller LED is ON in the SPI communication circuit when pressing the button of Peripheral
Code:
PART 1: Master Code:
#include<SPI.h>
#define LED 7
#define ipbutton 2
int buttonvalue;
int x;
void setup (void){
Serial.begin(115200);
pinMode(ipbutton,INPUT);
digitalWrite(SS,HIGH);
}
void loop(void){
byte Mastersend, Mastereceive;
buttonvalue = digitalRead(ipbutton);
if(buttonvalue == HIGH){
digitalWrite(SS, LOW);
Mastersend = x;
Mastereceive = SPI.transfer(Mastersend);
}
if(Mastereceive == 1){
digitalWrite(LED,HIGH);
Serial.println("Master LED is ON");
5
}
else{
digitalWrite(LED,LOW);
Serial.println("Master LED is OFF");
}
delay(1000);
}
#include<SPI.h>
#define LEDpin 7
#define buttonpin 2
volatile boolean received;
volatile byte Slavereceived, Slavesend;
int buttonvalue;
int x;
void setup(){
Serial.begin(115200);
pinMode(buttonpin,INPUT);
pinMode(MISO,OUTPUT);
SPCR |= _BV(SPE);
SPI.attachInterrupt();
}
ISR(SPI_STC_vect){
Slavereceived = SPDR;
received = true;
}
void loop(){
if(received)
Master{
if (Slavereceived == 1)
{
digitalWrite(LEDpin, HIGH);
Serial.println("Slave LED is ON");
}
else{
digitalWrite(LEDpin,LOW);
Serial.println("Slave LED is OFF");
}
buttonvalue = digitalRead(buttonpin);
}
if (buttonvalue == HIGH){
Master{
x = 1;
6
}
else{
x=0;
}
}
Slavesend = x;
SPDR = Slavesend;
}
Explanation:
When the code is implemented and the loop () function runs on the Master Arduino, the digitalRead()
function continuously monitors the status of the button connected to pin 2. If the button is pressed,
value 1 is stored in a variable x, indicating the intent to turn on the LED on the Slave Arduino. If the
button is not pressed, the value 0 is stored in x. This value (x) is then transmitted to the Slave Arduino
using the SPI.transfer() function.
On the Slave Arduino, an interrupt service routine (ISR) is triggered whenever data is received from
the Master via the SPI bus. The received value is stored in the variable Slavereceived. If the received
value is 1, a digitalWrite() operation sets pin 7 of the Slave to HIGH, turning the Slave LED ON. If the
received value is 0, a digitalWrite() operation sets pin 7 to LOW, turning the Slave LED OFF.
Simultaneously, the Slave monitors the status of its own button connected to pin 2. When pressed, the
Slave sets a value x to 1 and sends it to the Master using the SPDR register. If the button is not pressed,
the Slave sets x to 0 and sends this value to the Master.
Back on the Master, the value received from the Slave is stored in the variable Mastereceive. If this
value is 1, a digitalWrite() operation sets pin 7 of the Master to HIGH, turning the Master LED ON. If
the value is 0, a digitalWrite() operation sets pin 7 to LOW, turning the Master LED OFF.
This bidirectional communication via SPI ensures synchronized LED control on both microcontrollers,
continuously repeating until the microcontroller is turned off or disconnected from power.
7
Simulation Output Results:
Here is the simulation implementation of an LED light control using a switch from Master and slave and
the necessary explanation of the implementation
Fig. 5 Peripheral LED was ON in the SPI communication circuit simulation when pressing the button of
Controller.
Fig. 6 Controller LED was ON in the SPI communication circuit simulation when pressing the button of
Peripheral.
8
Explanation: connections were configured to replicate the hardware setup. The program for both the
Master and Slave was developed and verified in the Arduino IDE before being implemented in the
Proteus software. The SPI communication between the Master and Slave was simulated, where button
presses on either board controlled the LEDs on both boards as per the programmed logic.
The simulation successfully demonstrated the bidirectional data transfer over the SPI protocol, turning
the LEDs on or off based on the button states. The results obtained from the simulation were captured
and compared with the hardware implementation, verifying consistency in the operation.
In this simulation, two Arduino Uno boards, resistors, LEDs, push buttons, and SPI
Discussion:
The experiment aimed to establish and understand SPI communication between two Arduino boards
configured as Master and Slave. Initially, the foundational concepts of SPI, a full-duplex serial
communication protocol, were reviewed. This included understanding the roles of key connections like
SS, SCK, MISO, and MOSI.
During the experiment, the Master and Slave were programmed to exchange data based on input from
push buttons, with LEDs indicating the communication status. The experiment successfully demonstrated
bidirectional data transfer, validating the SPI protocol's functionality. The role of SS in initiating
communication and the synchronization of data transmission using SCK were effectively implemented.
Results obtained from the hardware setup were consistent with those observed in the Proteus simulation,
confirming the accuracy of the implementation. This experiment enhanced understanding of SPI
operations and its practical applications, achieving the objectives set for the study.
References:
[1] https://www.arduino.cc/.
[2] ATMega328 manual
[3] https://www.avrfreaks.net/forum/tut-c-newbies-guide-avr-timers
[4] http://maxembedded.com/2011/06/avr-timers-timer0/