# I2C protocol Programming

#### Tinashe228

Joined May 2, 2021
17
Activity 3: Serial communication protocols
2. Build the electronic shown on page 2;
3. Considering the Arduino connected to the LED as the master and that the one connected to the push button as the slave, write 2 text codes (one on the master side and one on the slave side) that enables the master and the slave to communicate using the I2C protocol. Please see below the communication steps that need to be implemented using the I2C protocol:
1. Master requests the status of digital pin 4 (S_Pin4) of the slave device;
2. Slave reads the status of its digital pin 4 (S_Pin4), displays it on the serial monitor and communicates it to the Master;
3. The master compares the status of S_Pin4 of the slave to “0” and implements the logic below:
If S_Pin4 >0; LED is switched “On”;
If S_Pin4 <0; LED is switched “Off”;

#### KeithWalker

Joined Jul 10, 2017
2,456
This looks very much like a college project. We do not write software for you.How do you propose to tackle it? We will try to help if you run into problems that you can not solve.

#### Tinashe228

Joined May 2, 2021
17
thanks for the response, i dnt have a clue when it comes to coding im totally lost..

#### Ya’akov

Joined Jan 27, 2019
6,289
thanks for the response, i dnt have a clue when it comes to coding im totally lost..
What resources have you been provided concerning the I2C protocol and how to use it?

#### Tinashe228

Joined May 2, 2021
17

#### Tinashe228

Joined May 2, 2021
17
from the examples tried this but it says my code has eerrors

#### ericgibbs

Joined Jan 29, 2010
16,033
hi T,
No problem, post your code and any info that could help us debug your project.
E

The screen shot is difficult to read

#### Tinashe228

Joined May 2, 2021
17
C-like:
#include <Wire.h>
int pushButton = A0;
int x = 0;
void setup()
{
Wire.begin();
pinMode(pushButoon, INPUT);
}

void loop()
{
Wire.beginTransmission(1);
Wire.write(x);
Wire.endTransmission();
delay(500);
}

Last edited by a moderator:

#### Tinashe228

Joined May 2, 2021
17
For the slave:
#include <Wire.h>

int pushButton = 4;

int x = 0;

void setup()

{

Wire.begin();

pinMode(pushButoon, INPUT);

}

void loop()

{

Wire.beginTransmission(1);

Wire.write(x);

Wire.endTransmission();

delay(500);

}

FOR THE SLAVE

Last edited by a moderator:

#### Tinashe228

Joined May 2, 2021
17
C-like:
#include <Wire.h>
int pinLed=8:
int x =0;

void setup()
{
Wire.begin(1);
pinMode(pinLed, OUTPUT);
}

void loop()
{
delay(100);
}

if (x == 1){

digitalWrite(pinLed,HIGH):
}
else{
digitalWrite(pinLed,LOW);
}
}
Mod edit - code tags - JohnInTX

Last edited by a moderator:

#### ericgibbs

Joined Jan 29, 2010
16,033
hi T.
The Master compiles OK , when you spell pushbutton correctly!
pinMode(pushButoon, INPUT);

#### Attachments

• 19.7 KB Views: 7

#### ericgibbs

Joined Jan 29, 2010
16,033
hi T,
Please don't keep flooding the thread with posts, lets do this one step at a time.
E

Lets focus on getting the Master code to compile. OK

BTW: The Slave has the same spelling error.
pinMode(pushButoon, INPUT);

#### Tinashe228

Joined May 2, 2021
17
Okay thanks , sorry for flooding the thread

#### Tinashe228

Joined May 2, 2021
17

#### Tinashe228

Joined May 2, 2021
17
so this is the main error i get i do not know were to put the coma or the 5:13: the X was not declared and i dnt understand the 26:34

#### ericgibbs

Joined Jan 29, 2010
16,033
hi T,
The Slave code.

The commands are Case sensitive.

Then it compiles OK.

Now you do some tests.

E
OT: ZA is my second home in PE

#### Tinashe228

Joined May 2, 2021
17
hi T,
The Slave code.

The commands are Case sensitive.

Then it compiles OK.

Now you do some tests.

E
OT: ZA is my second home in PE
So the digital read is fine is fixed the R is a capital letter the cases are fine its the commas and that x was not declared on the scope that im not sure of... Oh that awesome glad to have a fellow South African helping out

#### ericgibbs

Joined Jan 29, 2010
16,033
hi T,
Your Master Sketch, added two lines for testing with the Serial Monitor.
print 0 when A0 is Low and 1 when A0 is High.
E
C++:
// T Master 03/05/21 EG57A

#include <Wire.h>
int pushButton = A0;
int x = 0;
void setup()
{
Wire.begin();
pinMode(pushButton, INPUT);
Serial.begin(9600);// for testing program ONLY

}

void loop()
{
Wire.beginTransmission(1);
Wire.write(x);
Serial.println(x);// for testing program ONLY

Wire.endTransmission();
delay(500);
}

#### Attachments

• 1.8 KB Views: 5

#### ericgibbs

Joined Jan 29, 2010
16,033
hi T,
The Slave with the two lines added.
It works, the rest is up to you, if you get stuck ask.
E
C++:
// T_Slave 03/05/21 EG57A

#include <Wire.h>
int pushButton = 4;
int x = 0;

void setup()
{
Wire.begin();
pinMode(pushButton, INPUT);
Serial.begin(9600);// for testing program ONLY
}

void loop()
{
Wire.beginTransmission(1);
Wire.write(x);
Serial.println(x);// for testing program ONLY

Wire.endTransmission();
delay(500);
}

#### Attachments

• 1.8 KB Views: 4