i2c hex commands

hi there,

I am new to arduino and circuits. It would be grateful if someone could help me with my doubt.

I have an arduino mega (master) and arduino uno (slave). I wanted to give hex commands to my slave from my master; the master is connected to the pc and the commands go through the serial monitor to the slave. For example if i give a command 0x0A the slave returns 1A to the master which can be seen in the serial monitor.

please help me with the coding. thanks in advance

For informed help, please read and follow the directions in the "How to use this forum" post.

Note that "hex" is simply one of several human readable representations of binary data.

I fail to see where I2C is involved in this. Seems like all serial communication. And I am confused as to what is connected to what and how. Maybe a wiring diagram would help.

master is connected to pc via serial link. the slave uno is connected to master via i2c. the commands to the slave are given through the master which is connected to the pc via serial link.

thank you.

Google "arduino as i2c slave" for several project descriptions.

And please do read the "How to use this forum" post before posting again.

Have you tried the examples that come with the Wire library to get an understanding of the way that I2C works?

Tell about which one of the following two connections that you are using between MEGA and UNO.

1. UART Port Connection
SerialPortF.png

2. I2C Bus Connection
i2cF.png

SerialPortF.png

i2cF.png

the second one

criton07:
the second one

1. MEGA-Master Code

#include<Wire.h>
bool flag1 = false;
byte x;

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

  Wire.beginTransmission(0x68); //roll calling for the Slave
  byte busStatus = Wire.endTransmission();
  if (busStatus != 0x00)
  {
    Serial.print("I2C Bus communication problem...!");
    while (1);  //wait for ever
  }
  Serial.println("Slave found!");
}

void loop()
{
  Wire.beginTransmission(0x68);
  Wire.write(0x0A);
  Wire.endTransmission();

  Wire.requestFrom(0x68, 1);  //requesting slave to send data
  x = Wire.read();
  if (x == 0x1A)
  {
    Serial.print("Received from UNO is: ");
    Serial.println(x, HEX);
  }
  delay(1000);    //test interval
}

2. UNO-Slave Code

#include<Wire.h>
bool flag1 = false;
byte x;

void setup()
{
  Serial.begin(9600);
  Wire.begin(0x68);  ////UNO-Slave Address
  Wire.onReceive(receiveEvent);
  Wire.onRequest(sendEvent);
}

void loop()
{
  if (flag1 == true)
  {
    Serial.print("Received from MEGA is: ");
    Serial.print('0');  //leading 0 of 0A
    Serial.println(x, HEX);
    flag1 = false;
  }
}

void receiveEvent(int hoeMny)
{
  x = Wire.read();  //read the received byte
  if (x == 0x0A)    //be sure that 0x0A is coming from MEGA
  {
    flag1  = true;
  }
}

void sendEvent()
{
  Wire.write(0x1A);
}

3. MEGA Screenshot
smmega-1.png

4. UNO Screenshot
smuno-1.png

smmega-1.png

smuno-1.png

thank you for your help let me modify my code and see thanks a lot

thank you verymuch for your help
i have one more doubt i want to send the hex data through keyboard ie to type it to the serial port of the master how do i do this? i have other commands like 0x0B 0x0C so i use the keyboard of the pc to send commands which then is written to the slave

thanks in advance

criton07:
i have one more doubt i want to send the hex data through keyboard ie to type it to the serial port of the master how do i do this? i have other commands like 0x0B 0x0C so i use the keyboard of the pc to send commands which then is written to the slave

Let us send your original 0x0A from the InputBox of the Serial Monitor of MEGA.
MEGA-Master Code:

#include<Wire.h>
bool flag1 = false;
byte x;
char myData[20] = "";
byte m;

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

  Wire.beginTransmission(0x68); //roll calling for the Slave
  byte busStatus = Wire.endTransmission();
  if (busStatus != 0x00)
  {
    Serial.print("I2C Bus communication problem...!");
    while (1);  //wait for ever
  }
  Serial.println("Slave found!");
}

void loop()
{
  byte n = Serial.available();
  if (n != 0)
  {
    Serial.readBytesUntil('\n', myData, 20);
    m = (myData[0]-0x30) + (myData[1]-0x37);  //0A
    char myData[20] = "";
    i2cXmit();
  }
  delay(1000);
}

void i2cXmit()
{
  Wire.beginTransmission(0x68);
  Wire.write(m); //m (0x0A) comes from Keyboard via Serial Monitor
  Wire.endTransmission();

  Wire.requestFrom(0x68, 1);  //requesting slave to send data
  x = Wire.read();
  if (x == 0x1A)
  {
    Serial.print("Received from UNO is: ");
    Serial.println(x, HEX);
  }
}

UNO-Slave Code:

#include<Wire.h>
bool flag1 = false;
bool flag2 = false;
byte x;

void setup()
{
  Serial.begin(9600);
  Wire.begin(0x68);  ////UNO-Slave Address
  Wire.onReceive(receiveEvent);
  Wire.onRequest(sendEvent);
}

void loop()
{
  if (flag2 == true)
  {
    Serial.print("Received from MEGA is: ");
    Serial.print('0');  //leading 0 of 0A
    Serial.println(x, HEX);
    flag2 = false;
  }
}

void receiveEvent(int howMany)
{
  x = Wire.read();  //read the received byte
  if (x == 0x0A)    //be sure that 0x0A is coming from MEGA
  {
    flag1  = true;
  }
}

void sendEvent()
{
  if (flag1 == true)
  {
    Wire.write(0x1A);
    flag1 = false;
    flag2 = true;
  }
}

Screenshots:

BTW: Line ending tab option of the Serial Monitor should be at Newline option.
SerialMonitor.png

SerialMonitor.png

thank you..
can you please explain the loop code of the master?

criton07:
thank you..
can you please explain the loop code of the master?

Upload the sketches in your UNOs; observe the results and then study the sketches line by line. Try to get the meanings yourself. Tell what line you have not understood; answers will be provided if needed.

i didnt understand this part of the code
m = (myData[0]-0x30) + (myData[1]-0x37); //0A
char myData[20] = "";

thank you

criton07:
i didnt understand this part of the code
m = (myData[0]-0x30) + (myData[1]-0x37); //0A

User has entered 0A (zero and A) in the InputBox of Serial Monitor, and then he has clicked on the Send button. As a result, these two ASCII codes/frames 0x30 against 0 and 0x41 against A have arrived at the UNO. Now, the task is to retrieve the original value (0A) from the received frames: 0x30 and 0x41. The above quoted line has done it. Please, do the simple arithmetic as follows:

myData[0] contains 0x30   //the ASCII code of 0 (zero)
myData[1] contains 0x41  //the ASCII code of A
==> (0x30 - 0x30) + (0x41 - 0x37)
==> 0x00 + 0x0A
==> 0x0A
==> 0A          //0x tells to the software/MCU that the number that follows 0x is in hex format

but it dosent work

It most certainly does exactly what you told it to do. That it does not do what you expect means that your expectations are wrong. Adjust them.

That statement is just too lame. At a minimum, you need to tell us what the code actually does, with what data, and what you expect it to do.

the slave has got some array of numbers when i give this command the slave should send data to the master serially which can be seen on the serial monitor.

imhavingtoomuchtroubleparsingthatcantyouusepunctuationandcapicalletters

sorry i dint understand what you said.

You should post your codes using code tags (</>):

#include<Wire.h>
bool flag1 = false;
byte x;

void setup()
{
 Serial.begin(115200);
 Wire.begin();

 Wire.beginTransmission(0x08); 
 byte busStatus = Wire.endTransmission();
 if (busStatus != 0x00)
 {
   Serial.print("I2C communication problem...!");
   while (1);
 }
 Serial.println("Slave found!!!!");
}

void loop()
{
 char data[20]="";
 Wire.beginTransmission(0x08);
 Wire.write(0x0D);
 Wire.endTransmission();

 Wire.requestFrom(0x08, 1);  //requesting slave to send data
 x = Wire.read();
 if (x == 0x1D)
 {
   Serial.print("slave response is: ");
   Serial.println(x, HEX);
   Serial.readBytes(data,20); //Read the serial data and store in var
   Serial.println(data);
 }
 delay(500);    //test interval

}

Do you know how does the following command work? If not, you should first exercise basic lesson on UART communication between UNO and serial Monitor of the Arduino IDE.

byte n = Serial.readBytes(myData, 20);