# I2C arduino communication

**URL:** <https://forum.arduino.cc/t/i2c-arduino-communication/370911>\
**Category:** Programming\
**Created:** [March 8, 2016, 3:32pm UTC](https://forum.arduino.cc/t/i2c-arduino-communication/370911 "2016-03-08T15:32:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![windu34](https://avatars.discourse-cdn.com/v4/letter/w/7c8e57/32.png) [@windu34](https://forum.arduino.cc/u/windu34)\
**Post date:** [March 8, 2016, 3:32pm UTC](https://forum.arduino.cc/t/i2c-arduino-communication/370911/1 "2016-03-08T15:32:31Z")

</div>

I am trying to send an integer between two arduinos using the Master Reader-Slave Writer configuration using the Wire.h library, but the example available is meant for strings. Can anyone tell me how to use the same config to send an integer?

```cpp
Code for Master Reader - Program for Arduino 1 

// Wire Master Reader
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Reads data from an I2C/TWI slave device
// Refer to the "Wire Slave Sender" example for use with this

// Created 29 March 2006

// This example code is in the public domain.

#include <Wire.h>

void setup() {
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop() {
  Wire.requestFrom(8, 6);    // request 6 bytes from slave device #8

  while (Wire.available()) { // slave may send less than requested
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);        // print the character
  }

  delay(500);
}

[Get Code]

Code for Slave Sender - Program for Arduino 2 

// Wire Slave Sender
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Sends data as an I2C/TWI slave device
// Refer to the "Wire Master Reader" example for use with this

// Created 29 March 2006

// This example code is in the public domain.

#include <Wire.h>

void setup() {
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onRequest(requestEvent); // register event
}

void loop() {
  delay(100);
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
  Wire.write("hello "); // respond with message of 6 bytes
  // as expected by master
}

```

---

<div class="post-metadata">

**Author:** ![ieee488](https://avatars.discourse-cdn.com/v4/letter/i/d2c977/32.png) [@ieee488](https://forum.arduino.cc/u/ieee488)\
**Post date:** [March 8, 2016, 3:34pm UTC](https://forum.arduino.cc/t/i2c-arduino-communication/370911/2 "2016-03-08T15:34:00Z")

</div>

Convert your integer to a string.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [March 8, 2016, 3:50pm UTC](https://forum.arduino.cc/t/i2c-arduino-communication/370911/3 "2016-03-08T15:50:10Z")

</div>

> but the example available is meant for strings.

No, it is meant for a collection of bytes. Hint: an integer is a collection of (2) bytes.

---

<div class="post-metadata">

**Author:** ![windu34](https://avatars.discourse-cdn.com/v4/letter/w/7c8e57/32.png) [@windu34](https://forum.arduino.cc/u/windu34)\
**Post date:** [March 8, 2016, 3:54pm UTC](https://forum.arduino.cc/t/i2c-arduino-communication/370911/4 "2016-03-08T15:54:19Z")

</div>

> PaulS:  
> No, it is meant for a collection of bytes. Hint: an integer is a collection of (2) bytes.

So can I just send an integer from the Slave and read it from the master with the following?:  
requestFrom(8, 2);  
while (Wire.available()) { // slave may send less than requested  
int c = Wire.read(); // receive a byte as character  
Serial.print(c); // print the character  
}

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [March 8, 2016, 3:56pm UTC](https://forum.arduino.cc/t/i2c-arduino-communication/370911/5 "2016-03-08T15:56:47Z")

</div>

> So can I just send an integer from the Slave and read it from the master with the following?:

No!

That reads ONE of the bytes.

You need to send the two bytes in some order (highByte() then lowByte() or the reverse, and then reassemble the int from the bytes (using the stupidly named word() macro).

---

<div class="post-metadata">

**Author:** ![windu34](https://avatars.discourse-cdn.com/v4/letter/w/7c8e57/32.png) [@windu34](https://forum.arduino.cc/u/windu34)\
**Post date:** [March 8, 2016, 4:17pm UTC](https://forum.arduino.cc/t/i2c-arduino-communication/370911/6 "2016-03-08T16:17:58Z")

</div>

> PaulS:  
> No!
> 
> That reads ONE of the bytes.
> 
> You need to send the two bytes in some order (highByte() then lowByte() or the reverse, and then reassemble the int from the bytes (using the stupidly named word() macro).

I don't understand how I would implement that.  
So say the integer I am trying to send from the slave is 13,789 (the range of what I will be trying to send will be between 10,000 and 14,000), how would I implement that?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [March 8, 2016, 4:29pm UTC](https://forum.arduino.cc/t/i2c-arduino-communication/370911/7 "2016-03-08T16:29:02Z")

</div>

> So say the integer I am trying to send from the slave is 13,789

On the slave:

```arduino
int slaveValue = 13789;

byte b[2] = { highByte(slaveValue),
                    lowByte(slaveValue) };

void requestEvent()
{
  Wire.write(b, 2); // respond with an array of two bytes
}

```

Then, on the master:

```arduino
  int slavesValue = 0;

  byte b[2];
  while(Wire.available() < 2) {;} // Wait until there are 2 bytes
  b[0] = Wire.read();
  b[1] = Wire.read(); 

  slavesValue = (unsigned int)b[0] << 8 + b[1];

```

---

<div class="post-metadata">

**Author:** ![windu34](https://avatars.discourse-cdn.com/v4/letter/w/7c8e57/32.png) [@windu34](https://forum.arduino.cc/u/windu34)\
**Post date:** [March 8, 2016, 4:39pm UTC](https://forum.arduino.cc/t/i2c-arduino-communication/370911/8 "2016-03-08T16:39:25Z")

</div>

Thanks! Ill try it out as soon as I can and get back to you.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 9:46pm UTC](https://forum.arduino.cc/t/i2c-arduino-communication/370911/9 "2021-05-05T21:46:23Z")

</div>

Closed on May 5, 2021, 9:46pm UTC


