# Sending 4 pot values with I2C from one arduino to another

**URL:** <https://forum.arduino.cc/t/sending-4-pot-values-with-i2c-from-one-arduino-to-another/685259>\
**Category:** Networking, Protocols, and Devices\
**Created:** [December 2, 2020, 9:03am UTC](https://forum.arduino.cc/t/sending-4-pot-values-with-i2c-from-one-arduino-to-another/685259 "2020-12-02T09:03:47Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![adrianskalni](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@adrianskalni](https://forum.arduino.cc/u/adrianskalni)\
**Post date:** [December 2, 2020, 9:03am UTC](https://forum.arduino.cc/t/sending-4-pot-values-with-i2c-from-one-arduino-to-another/685259/1 "2020-12-02T09:03:47Z")

</div>

Hello

I have modded a cheap drone controller to output its joystick values to an Arduino nano. I made it to output its 4 pot values using i2c to another Arduino don't ask why. So my problem is that I need to send 4 values but I can send only 1. help me please I don't have a time limit or something so I don't need this quick or now! I am also using a 16x2 LCD without i2c to show the live values and sometime I want to make a menu there or stuff like that

Master(a.k.a the nano in the controller):

```cpp
/*
  Set delay time on delayTime
  its like the refresh rate for the lcd
*/

int delayTime = 50;

int pot1 = A0;
int pot2 = A1;
int pot3 = A2;
int pot4 = A3;

int potValue1 = 0;
int potValue2 = 0;
int potValue3 = 0;
int potValue4 = 0;

int led = 10;
int button = 8;

int lastButtonState;    // the previous state of button
int currentButtonState; // the current state of button

int output1 = A4;
int output2 = A5;
int output3 = A6;
int output4 = A7;

#include<Wire.h>
#include <LiquidCrystal.h> // includes the LiquidCrystal Library 
LiquidCrystal lcd(2, 3, 4, 5, 6, 7); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)
#define SLAVE_ADDR 9

void setup() {
  pinMode(led, OUTPUT);
  pinMode(button, INPUT_PULLUP);

  // Open serial communications and wait for port to open:
  Wire.begin();

  lcd.begin(16, 2);
  lcd.print("  ArduinoTX");
  digitalWrite(led, HIGH);
  delay(600);
  digitalWrite(led, LOW);
  delay(600);
  digitalWrite(led, HIGH);
  delay(600);
  digitalWrite(led, LOW);
  delay(600);
  digitalWrite(led, HIGH);
  delay(600);
  delay(3000);
  lcd.clear();

}

void loop() {

  currentButtonState = digitalRead(button);
  potValue1 = analogRead(pot1);
  potValue2 = analogRead(pot2);
  potValue3 = analogRead(pot3);
  potValue4 = analogRead(pot4);

  int sendValue = map(potValue4, 1, 1023, 1, 255);  //converting to values 255 max 
  int sendValue2 = map(potValue3, 1, 1023, 1, 255);
  int sendValue3 = map(potValue2, 1, 1023, 1, 255);
  int sendValue4 = map(potValue1, 1, 1023, 1, 255);

  Wire.beginTransmission(SLAVE_ADDR); //sending only one pot value
  Wire.write(sendValue);
  Wire.endTransmission();
 

  lastButtonState  = currentButtonState;      // save the last state
  currentButtonState = digitalRead(button); // read new state

  if (lastButtonState == LOW && currentButtonState == LOW) { //dont worry about this

    lcd.print(potValue4);
    lcd.print("        ");
    lcd.print(potValue2);
    delay(delayTime);
    lcd.clear();
    lcd.setCursor(0, 2);
    lcd.print(potValue3);
    lcd.print("        ");
    lcd.print(potValue1);
    delay(delayTime);
    lcd.clear();

  }

}

```

Slave(or an uno who receives the values):

```cpp
int rd;
int br;
#include <Wire.h>

#define SLAVE_ADDR 9

void setup() {
  // put your setup code here, to run once:
  Wire.begin(SLAVE_ADDR);
   
  // Function to run when data received from master
  Wire.onReceive(receiveEvent);
  
  // Setup Serial Monitor 
  Serial.begin(9600);
}
void receiveEvent() {
  // read one character from the I2C
  rd = Wire.read();
  Serial.println(rd);
    
}

void loop() {
  delay(50);

}

```

Thanks!

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [December 2, 2020, 9:35am UTC](https://forum.arduino.cc/t/sending-4-pot-values-with-i2c-from-one-arduino-to-another/685259/2 "2020-12-02T09:35:40Z")

</div>

This [Arduino to Arduino I2C Tutorial](https://forum.arduino.cc/index.php?topic=683181) may help.

...R

---

<div class="post-metadata">

**Author:** ![adrianskalni](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@adrianskalni](https://forum.arduino.cc/u/adrianskalni)\
**Post date:** [December 2, 2020, 10:37am UTC](https://forum.arduino.cc/t/sending-4-pot-values-with-i2c-from-one-arduino-to-another/685259/3 "2020-12-02T10:37:59Z")

</div>

Thank you i will look into it

---

<div class="post-metadata">

**Author:** ![adrianskalni](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@adrianskalni](https://forum.arduino.cc/u/adrianskalni)\
**Post date:** [December 2, 2020, 11:05am UTC](https://forum.arduino.cc/t/sending-4-pot-values-with-i2c-from-one-arduino-to-another/685259/4 "2020-12-02T11:05:35Z")

</div>

> <https://stackoverflow.com/questions/21073085/how-to-send-4-pot-values-via-i2c-from-arduino-to-arduino-how-to-differentiate-t/21739149#21739149>

This is very close to my setup but I need to have the pots connected to the master and I don't understand that complicated of code so maybe you can modify it for me? I understand if you cant

---

<div class="post-metadata">

**Author:** ![AmphenolSensors](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@AmphenolSensors](https://forum.arduino.cc/u/AmphenolSensors)\
**Post date:** [December 2, 2020, 10:01pm UTC](https://forum.arduino.cc/t/sending-4-pot-values-with-i2c-from-one-arduino-to-another/685259/5 "2020-12-02T22:01:24Z")

</div>

Why does the pot measuring nano need be master? It can still run display as slave.

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [December 2, 2020, 10:29pm UTC](https://forum.arduino.cc/t/sending-4-pot-values-with-i2c-from-one-arduino-to-another/685259/6 "2020-12-02T22:29:54Z")

</div>

> adrianskalni:  
> [How to send 4 Pot values via i2c from Arduino to Arduino? How to differentiate these values while receiving them? - Stack Overflow](https://stackoverflow.com/questions/21073085/how-to-send-4-pot-values-via-i2c-from-arduino-to-arduino-how-to-differentiate-t/21739149)
> 
> This is very close to my setup but I need to have the pots connected to the master and I don't understand that complicated of code so maybe you can modify it for me? I understand if you cant

Sorry, but I don't like that code. At first glance I see more than ten issues with it. Don't be tempted to use it as example. Please ignore that sketch in the answer at stackoverflow.

Your sketch is going in the right direction. If you look at the Robin2 tutorial, you will notice that it is more complex with the struct, but the base principle is the same as what your sketch was heading to. If you don't understand something, just ask.

> adrianskalni:  
> I made it to output its 4 pot values using i2c to another Arduino don't ask why.

Why ?

---

<div class="post-metadata">

**Author:** ![adrianskalni](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@adrianskalni](https://forum.arduino.cc/u/adrianskalni)\
**Post date:** [December 3, 2020, 9:09am UTC](https://forum.arduino.cc/t/sending-4-pot-values-with-i2c-from-one-arduino-to-another/685259/7 "2020-12-03T09:09:29Z")

</div>

Well, I would be happy if someone could rewrite the code for me but in my opinion, it feels like I'm wasting someone's time? But if you can I will be thankful.

---

<div class="post-metadata">

**Author:** ![adrianskalni](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@adrianskalni](https://forum.arduino.cc/u/adrianskalni)\
**Post date:** [December 3, 2020, 9:26am UTC](https://forum.arduino.cc/t/sending-4-pot-values-with-i2c-from-one-arduino-to-another/685259/8 "2020-12-03T09:26:34Z")

</div>

> Koepel:  
> Why ?

Well I'm making it as an all in one controller for everything so that's why I'm outputting it to another Arduino

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [December 3, 2020, 11:00am UTC](https://forum.arduino.cc/t/sending-4-pot-values-with-i2c-from-one-arduino-to-another/685259/9 "2020-12-03T11:00:34Z")

</div>

> adrianskalni:  
> Well, I would be happy if someone could rewrite the code for me

Have you tried using the code from my Tutorial?

...R

---

<div class="post-metadata">

**Author:** ![adrianskalni](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@adrianskalni](https://forum.arduino.cc/u/adrianskalni)\
**Post date:** [December 3, 2020, 11:26am UTC](https://forum.arduino.cc/t/sending-4-pot-values-with-i2c-from-one-arduino-to-another/685259/10 "2020-12-03T11:26:03Z")

</div>

I don't understand that complicated of code

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [December 3, 2020, 1:10pm UTC](https://forum.arduino.cc/t/sending-4-pot-values-with-i2c-from-one-arduino-to-another/685259/11 "2020-12-03T13:10:32Z")

</div>

> adrianskalni:  
> I don't understand that complicated of code

If you explain what exactly you don't understand I will try to help.

...R

---

<div class="post-metadata">

**Author:** ![adrianskalni](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@adrianskalni](https://forum.arduino.cc/u/adrianskalni)\
**Post date:** [December 4, 2020, 9:46am UTC](https://forum.arduino.cc/t/sending-4-pot-values-with-i2c-from-one-arduino-to-another/685259/12 "2020-12-04T09:46:07Z")

</div>

I dont understand like the byte stuff like all i do is simple code so this is a challenge for me i just really wanted to make myself face a challenge not like write in the serial monitor on blink leds and other a little bit complicated stuff

I have not used tge string byte and other stuff like that when i learned to code they teached like the basic basic stuff

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [December 4, 2020, 10:01am UTC](https://forum.arduino.cc/t/sending-4-pot-values-with-i2c-from-one-arduino-to-another/685259/13 "2020-12-04T10:01:14Z")

</div>

> adrianskalni:  
> I dont understand like the byte stuff

That is not specific enough for me to help you. You can't really do any Arduino programming without understanding about bytes - they are the fundamental building block for everything.

Can you identify the lines of code in my Tutorial that you don't understand?

...R

---

<div class="post-metadata">

**Author:** ![adrianskalni](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@adrianskalni](https://forum.arduino.cc/u/adrianskalni)\
**Post date:** [December 4, 2020, 1:07pm UTC](https://forum.arduino.cc/t/sending-4-pot-values-with-i2c-from-one-arduino-to-another/685259/14 "2020-12-04T13:07:42Z")

</div>

Wire.write((byte\*) &sendData, sizeof(sendData))  
I dont understand senddata and the byte sizeof  
Thats what i dont understand

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [December 4, 2020, 1:31pm UTC](https://forum.arduino.cc/t/sending-4-pot-values-with-i2c-from-one-arduino-to-another/685259/15 "2020-12-04T13:31:35Z")

</div>

> adrianskalni:  
> Wire.write((byte\*) &sendData, sizeof(sendData))  
> I dont understand senddata and the byte sizeof  
> Thats what i dont understand

In the example Master program the equivalent line is

```arduino
Wire.write((byte*) &txData, sizeof(txData));

```

Perhaps I should have used the same name in both cases - you can consider sendData and txData to be different names for the same thing.

txData is the name of the struct that is to be sent  
sizeof(txData) figures out how many bytes there are in txData

Wire.write() sends a block of bytes and the code (byte\*) tells the compiler to treat the contents of txData as block of bytes. The &txData tells the compiler to use the data that starts at the address in memory where txData is stored.

...R

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [December 4, 2020, 5:46pm UTC](https://forum.arduino.cc/t/sending-4-pot-values-with-i2c-from-one-arduino-to-another/685259/16 "2020-12-04T17:46:36Z")

</div>

The sizeof() is not a real function, it is solved by the compiler and gives the size of that thing in bytes.  
Below I show a few different ways to return data in the onRequest handler. You can use it without fully understanding it 😉

The Wire.write() can be used in two ways: with a single byte or with a pointer. The name of an array is already a pointer and the name of a struct is not a pointer. To get the address of a struct the '&' is used. The address of a struct is a pointer.

```cpp
// send a single byte
byte myByte;
Wire.write( myByte);

// send an array of bytes
byte myArray[10]
Wire.write( myArray, 10);

// send an array of integers
int myIntegers[10];
Wire.write( (char *) myIntegers, 20);  // assuming an int is 2 byte

// send an array of integers in a smart way
int myIntegers[10];
Wire.write( (char *) myIntegers, sizeof( myIntegers));

// send a struct
Wire.write((byte*) &sendData, sizeof(sendData));

```

---

<div class="post-metadata">

**Author:** ![Power\_Broker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/power_broker/32/226104_2.png) [@Power\_Broker](https://forum.arduino.cc/u/Power_Broker)\
**Post date:** [December 5, 2020, 5:12am UTC](https://forum.arduino.cc/t/sending-4-pot-values-with-i2c-from-one-arduino-to-another/685259/17 "2020-12-05T05:12:50Z")

</div>

All of this can be greatly simplified by using [SerialTransfer.h](https://github.com/PowerBroker2/SerialTransfer). It comes with several I2C examples ([master](https://github.com/PowerBroker2/SerialTransfer/blob/master/examples/i2c_tx_data/i2c_tx_data.ino) and [slave](https://github.com/PowerBroker2/SerialTransfer/blob/master/examples/i2c_rx_data/i2c_rx_data.ino)). [To install](https://www.ardu-badge.com/SerialTransfer)

---

<div class="post-metadata">

**Author:** ![adrianskalni](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@adrianskalni](https://forum.arduino.cc/u/adrianskalni)\
**Post date:** [December 5, 2020, 4:16pm UTC](https://forum.arduino.cc/t/sending-4-pot-values-with-i2c-from-one-arduino-to-another/685259/18 "2020-12-05T16:16:31Z")

</div>

```arduino
// send an array of integers
int myIntegers[10];
Wire.write( (char *) myIntegers, 20);  // assuming an int is 2 byte

```

So i need to use this one?  
Because i have those 4 pot values i make them integers and send them in an array.  
If yes then how do i put this in my code?  
But isnt arduino sending 4byte integers?

> Power\_Broker:  
> All of this can be greatly simplified by using [SerialTransfer.h](https://github.com/PowerBroker2/SerialTransfer). It comes with several I2C examples ([master](https://github.com/PowerBroker2/SerialTransfer/blob/master/examples/i2c_tx_data/i2c_tx_data.ino) and [slave](https://github.com/PowerBroker2/SerialTransfer/blob/master/examples/i2c_rx_data/i2c_rx_data.ino)). [To install](https://www.ardu-badge.com/SerialTransfer)

And i cant find any good video tutorials maybe you can find a article or a video

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [December 5, 2020, 8:34pm UTC](https://forum.arduino.cc/t/sending-4-pot-values-with-i2c-from-one-arduino-to-another/685259/19 "2020-12-05T20:34:33Z")

</div>

If you prefer to use integers, then yes, you can use an array.  
With an Arduino Nano, an integer is two bytes, so if you want to send 10 integers, you have to send 20 bytes.  
Using the "sizeof()" is nicer, but not needed.

Instead of using the struct, you can use an array of 4 integers and send 8 bytes.

Master:

```cpp
int myData[4];

...

Wire.beginTransmission( otherAddress);
Wire.write( (byte*) myData, 8);
Wire.endTransmission();    // this is what actually sends the data

```

Slave:

```cpp
volatile int myData[4];

...

Wire.readBytes( (byte*) myData, 8);
newRxData = true;

```

---

<div class="post-metadata">

**Author:** ![adrianskalni](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@adrianskalni](https://forum.arduino.cc/u/adrianskalni)\
**Post date:** [December 7, 2020, 9:45am UTC](https://forum.arduino.cc/t/sending-4-pot-values-with-i2c-from-one-arduino-to-another/685259/20 "2020-12-07T09:45:18Z")

</div>

Now I understand but how do I put the 4 pot values in the

```cpp
int myData[4];

```

Like this?

```cpp
int myData[pot1, pot2, pot3, pot4];

```

[Next page](https://forum.arduino.cc/t/sending-4-pot-values-with-i2c-from-one-arduino-to-another/685259.md?page=2)
