# Wire.h write data

**URL:** <https://forum.arduino.cc/t/wire-h-write-data/651028>\
**Category:** Programming\
**Created:** [April 25, 2020, 11:11am UTC](https://forum.arduino.cc/t/wire-h-write-data/651028 "2020-04-25T11:11:17Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![ammygo](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@ammygo](https://forum.arduino.cc/u/ammygo)\
**Post date:** [April 25, 2020, 11:11am UTC](https://forum.arduino.cc/t/wire-h-write-data/651028/1 "2020-04-25T11:11:17Z")

</div>

hi!

Just start to play with I2C communication and stuck with data transfer. Can slave send data like this?  
**SLAVE**

```cpp
void requestEvent() {
  for (int i_1 = 0; i_1 < 16; i_1 ++) {
    a[i_1] = readMux_1(i_1);
  }
  Wire.write(a[]);
}

```

Can **Wire.write** send all array?  
If YES how MASTER should read this array?

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [April 25, 2020, 11:18am UTC](https://forum.arduino.cc/t/wire-h-write-data/651028/2 "2020-04-25T11:18:12Z")

</div>

it needs to know the number of bytes in the array

```cpp
Wire.write (a, 16);

```

look are Wire.h to see what functions are available

---

<div class="post-metadata">

**Author:** ![ammygo](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@ammygo](https://forum.arduino.cc/u/ammygo)\
**Post date:** [April 25, 2020, 11:28am UTC](https://forum.arduino.cc/t/wire-h-write-data/651028/3 "2020-04-25T11:28:12Z")

</div>

I find this: [Wire - Arduino Reference](https://www.arduino.cc/en/Reference/WireWrite)  
It is same as you say **Wire.write(data, length)**  
In my case **Wire.write(a, 16)**  
But I get error: **no matching function for call to 'TwoWire::write(int [16], int)'**  
Using array is my weak spot...

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [April 25, 2020, 11:32am UTC](https://forum.arduino.cc/t/wire-h-write-data/651028/4 "2020-04-25T11:32:34Z")

</div>

the argument to write is a uint8\_t (i.e char, byte). your arrays are ints (maybe they can be bytes) so you need to provide a pointer to a byte. sizeof() returns the size of a variable in bytes.

```cpp
Wire.write ((uint8_t*)a, sizeof(a));

```

---

<div class="post-metadata">

**Author:** ![ammygo](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@ammygo](https://forum.arduino.cc/u/ammygo)\
**Post date:** [April 25, 2020, 11:53am UTC](https://forum.arduino.cc/t/wire-h-write-data/651028/5 "2020-04-25T11:53:27Z")

</div>

ok, then I declare **a** as byte: **byte a[16];**  
I get no more error using this line: **Wire.write(a, 16);**  
So right now I told to send byte a and all its data?

In MASTER code I use code like this:

```cpp
Wire.requestFrom(1, 16);
  while (Wire.available()) {
    byte c = Wire.read();
    Serial.println(c);
  }

```

and I get 16 different meanings, but it is not the same as SLAVE send me

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [April 25, 2020, 11:57am UTC](https://forum.arduino.cc/t/wire-h-write-data/651028/6 "2020-04-25T11:57:58Z")

</div>

> ammygo:  
> If YES how MASTER should read this array?

**1.** Connect UNO (Master) and NANO (Slave) as per Fig-1 using I2C Bus.

 ![i2cUnoNano.png](https://europe1.discourse-cdn.com/arduino/original/3X/b/1/b13941da78306b5d2a34159031799a2c900538f0.png)  
Figure-1:

**2.** Upload the following sketch in NANO (yours one slightly modified).

```arduino
#include<Wire.h>
#define slaveAddress 0x08  //0001000 (7-bit)
byte readMux_1[2] = {0x12, 0x34};
byte arrayA[2];

void setup()
{
  Serial.begin(9600);
  Wire.begin(slaveAddress);
  Wire.onRequest(requestEvent);
}

void loop()
{

}

void requestEvent()
{
  for (int i_1 = 0; i_1 < 2; i_1 ++)
  {
    arrayA[i_1] = readMux_1[i_1];
  }
  Wire.write(arrayA, sizeof(arrayA));
}

```

**3.** Upload the following sketch in UNO.

```arduino
#include <Wire.h>
#define slaveAddress 0x08
byte arrayA[2];

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

void loop() 
{
  Wire.requestFrom(slaveAddress, 2);
  arrayA[0] = Wire.read();
  arrayA[1] = Wire.read();

  Serial.println(arrayA[0], HEX);   //
  Serial.println(arrayA[1], HEX);  //
  Serial.println("=======================");

  delay(2500);
}

```

**4.** Check that 12 and 34 appear on Serial Monitor of Master at 2.5 sec interval. The data is being sent by Slave as per request of Master.  
 ![smk1.png](https://europe1.discourse-cdn.com/arduino/original/3X/6/1/61a8c6cc3c028b2a0ca8204d6f8542043b047a67.png)

**5.** You may read the [attached file](https://forum.arduino.cc/index.php?action=dlattach;topic=680073.0;attach=359660) for more knowledge on I2C Bus Operation and Programming.

 ![i2cUnoNano.png](https://europe1.discourse-cdn.com/arduino/original/3X/b/1/b13941da78306b5d2a34159031799a2c900538f0.png)

![smk1.png](https://europe1.discourse-cdn.com/arduino/original/3X/6/1/61a8c6cc3c028b2a0ca8204d6f8542043b047a67.png)

[Ch-6(I2C)doc.doc](https://forum.arduino.cc/uploads/short-url/oIpHpO7EN63gsJwqlXNm6C41l7i.doc) (1.43 MB)

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [April 25, 2020, 12:24pm UTC](https://forum.arduino.cc/t/wire-h-write-data/651028/7 "2020-04-25T12:24:20Z")

</div>

> and I get 16 different meanings, but it is not the same as SLAVE send me

what do you mean by "meanings"?

you might try specifying that you want the value displayed in HEX (or DEC). it may display the byte as an [ASCII character](https://en.wikipedia.org/wiki/ASCII#/media/File:USASCII_code_chart.png)

```arduino
    Serial.println(c, HEX);

```

---

<div class="post-metadata">

**Author:** ![ammygo](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@ammygo](https://forum.arduino.cc/u/ammygo)\
**Post date:** [April 25, 2020, 6:46pm UTC](https://forum.arduino.cc/t/wire-h-write-data/651028/8 "2020-04-25T18:46:48Z")

</div>

now everything is fine, I get all correct data.

SLAVE:

```cpp
void requestEvent() {
  for (int i_1 = 0; i_1 < 16; i_1 ++) {
    a[i_1] = readMux_1(i_1);
  }
  Wire.write(a, 16);
}

```

MASTER:

```cpp
Wire.requestFrom(1, 16); 
  while (Wire.available()) {
    byte c = Wire.read();
    Serial.println(c);

```

Thanks for help **gcjr** and **GolamMostafa**

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [April 25, 2020, 9:50pm UTC](https://forum.arduino.cc/t/wire-h-write-data/651028/9 "2020-04-25T21:50:34Z")

</div>

> ammygo:  
> now everything is fine, I get all correct data.

Please, post complete sketches for both Master and Slave.

```arduino
Wire.requestFrom(1, 16);
  while (Wire.available()) {
    byte c = Wire.read();
    Serial.println(c);

```

You are receiving 16 bytes data from Slave; but, you saving saving only the last byte as is known from the above snippet. That.s why I am asking for the full sketch to see what you are doing with the received data.

```arduino
void requestEvent() {
  for (int i_1 = 0; i_1 < 16; i_1 ++) {
    a[i_1] = readMux_1(i_1);
  }
  Wire.write(a, 16);
}

```

In the above snippet, you are saving data in an array returned by a function. We would like to see the function definition.

---

<div class="post-metadata">

**Author:** ![ammygo](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@ammygo](https://forum.arduino.cc/u/ammygo)\
**Post date:** [April 26, 2020, 5:50pm UTC](https://forum.arduino.cc/t/wire-h-write-data/651028/10 "2020-04-26T17:50:50Z")

</div>

it's only in testing mode but it works  
SLAVE

```cpp
#include <Wire.h>

//Mux control pins
int s0_1 = 8;
int s1_1 = 9;
int s2_1 = 10;
int s3_1 = 11;

int s0_2 = 8;
int s1_2 = 9;
int s2_2 = 10;
int s3_2 = 11;

//Mux in "SIG" pin
int SIG_pin_1 = 0;
int SIG_pin_2 = 1;

byte a[16];

void setup() {
  Wire.begin(1);                /* join i2c bus with address 8 */
  //Wire.onReceive(receiveEvent); /* register receive event */
  Wire.onRequest(requestEvent); /* register request event */
  Serial.begin(9600);          /* start serial comm. */
  Serial.println("-= Slave =-");

  pinMode(s0_1, OUTPUT);
  pinMode(s1_1, OUTPUT);
  pinMode(s2_1, OUTPUT);
  pinMode(s3_1, OUTPUT);

  pinMode(s0_2, OUTPUT);
  pinMode(s1_2, OUTPUT);
  pinMode(s2_2, OUTPUT);
  pinMode(s3_2, OUTPUT);

  digitalWrite(s0_1, LOW);
  digitalWrite(s1_1, LOW);
  digitalWrite(s2_1, LOW);
  digitalWrite(s3_1, LOW);

  digitalWrite(s0_2, LOW);
  digitalWrite(s1_2, LOW);
  digitalWrite(s2_2, LOW);
  digitalWrite(s3_2, LOW);
}

void loop() {

  //Loop through and read all 16 values
  //Reports back Value at channel 6 is: 346
  //for (int i_1 = 0; i_1 < 16; i_1 ++) {
  //a[i_1] = readMux_1(i_1);
  //delay(10);
  //}

  //for (int i_2 = 0; i_2 < 16; i_2 ++) {
  //Serial.println(readMux_2(i_2));
  //delay(10);
  //}

}

int readMux_1(int channel_1) {
  int controlPin_1[] = {s0_1, s1_1, s2_1, s3_1};

  int muxChannel_1[16][4] = {
    {0, 0, 0, 0}, //channel 0
    {1, 0, 0, 0}, //channel 1
    {0, 1, 0, 0}, //channel 2
    {1, 1, 0, 0}, //channel 3
    {0, 0, 1, 0}, //channel 4
    {1, 0, 1, 0}, //channel 5
    {0, 1, 1, 0}, //channel 6
    {1, 1, 1, 0}, //channel 7
    {0, 0, 0, 1}, //channel 8
    {1, 0, 0, 1}, //channel 9
    {0, 1, 0, 1}, //channel 10
    {1, 1, 0, 1}, //channel 11
    {0, 0, 1, 1}, //channel 12
    {1, 0, 1, 1}, //channel 13
    {0, 1, 1, 1}, //channel 14
    {1, 1, 1, 1} //channel 15
  };

  //loop through the 4 sig
  for (int i_1 = 0; i_1 < 2; i_1 ++) {
    digitalWrite(controlPin_1[i_1], muxChannel_1[channel_1][i_1]);
  }

  //read the value at the SIG pin
  int val_1 = analogRead(SIG_pin_1);

  //return the value
  return val_1;
}

int readMux_2(int channel_2) {
  int controlPin_2[] = {s0_2, s1_2, s2_2, s3_2};

  int muxChannel_2[16][4] = {
    {0, 0, 0, 0}, //channel 0
    {1, 0, 0, 0}, //channel 1
    {0, 1, 0, 0}, //channel 2
    {1, 1, 0, 0}, //channel 3
    {0, 0, 1, 0}, //channel 4
    {1, 0, 1, 0}, //channel 5
    {0, 1, 1, 0}, //channel 6
    {1, 1, 1, 0}, //channel 7
    {0, 0, 0, 1}, //channel 8
    {1, 0, 0, 1}, //channel 9
    {0, 1, 0, 1}, //channel 10
    {1, 1, 0, 1}, //channel 11
    {0, 0, 1, 1}, //channel 12
    {1, 0, 1, 1}, //channel 13
    {0, 1, 1, 1}, //channel 14
    {1, 1, 1, 1}  //channel 15
  };

  //loop through the 4 sig
  for (int i_2 = 0; i_2 < 2; i_2 ++) {
    digitalWrite(controlPin_2[i_2], muxChannel_2[channel_2][i_2]);
  }

  //read the value at the SIG pin
  int val_2 = analogRead(SIG_pin_2);

  //return the value
  return val_2;
}

void requestEvent() {
  for (int i_1 = 0; i_1 < 16; i_1 ++) {
    a[i_1] = readMux_1(i_1);
    Serial.print("a[");
    Serial.print(i_1);
    Serial.print("] = ");
    Serial.println(a[i_1]);
    delay(10000);
  }
  Wire.write(a, 16);  /*send string on request */
  Serial.println("---------------------");
}

```

MASTER

```cpp
#include <Wire.h>

void setup() {
  Serial.begin(9600); /* begin serial comm. */
  Wire.begin(); /* join i2c bus as master */
  Serial.println("-= Master =-");
}

void loop() {
  Wire.beginTransmission(1); /* begin with device address 8 */
  //Wire.write("Hello Slave");  /* sends hello string */
  //Wire.endTransmission();    /* stop transmitting */

  Wire.requestFrom(1, 16); /* request & read data of size 9 from slave */
  while (Wire.available()) {
    byte c = Wire.read();/* read data received from slave */
    Serial.println(c);
  }
  Serial.println("----------------------------");
  delay(1000);
}

```

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [April 27, 2020, 12:10am UTC](https://forum.arduino.cc/t/wire-h-write-data/651028/11 "2020-04-27T00:10:06Z")

</div>

Thank you for posting the sketches; however, there are many anomalies between your codes and the corresponding comments. When posting works for others to read, it is necessary that we try to spend sometime to make the write up friendly, which may not be possible due to some other constrains.

---

<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 6, 2021, 12:28am UTC](https://forum.arduino.cc/t/wire-h-write-data/651028/12 "2021-05-06T00:28:50Z")

</div>

Closed on May 6, 2021, 12:28am UTC


