Analog data Serial communication

I am receiving 3 analog data throuth 2 nano 33 IoT module.

data sending code is:

byte pin[3];

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

}

void loop() {

int a = analogRead(A0);

int b = analogRead(A1);

int c = analogRead(A2);

byte pin[] = {a, b, c};

for(int i = 0; i<3; i++){
  Serial.println(pin[i], DEC);
}
Serial1.write(pin, 3);
delay(1000);
}

data receive code is:

byte SerialData[3];
byte SensorData;
int i = 0;

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


}

void loop() {
  if (Serial1.available()) {
    SensorData = Serial1.read();
//   for (i = 0; i < 3; i++) {
      SerialData[i] = SensorData;
 //   }
    int a = int(SerialData[0]);
    int b = int(SerialData[1]);
    int c = int(SerialData[2]);

    Serial.println(a);
    Serial.println(b);
    Serial.println(c);
  }
}

A problem arises when receiving data.

image

"0" output regardless of analog data.

I want to receive only the analog data I transmit(without "0").

Use Serial.print() instead of Serial.println() so that you don't send Carriage Return and Linefeed after the data, but how will you differentiate between the end of one value and the start of the next at the receiving end ?

See Serial input basics - updated for ideas

I want to transmit 3 or more sensor data through Serial communication between Arduino

I've tried several ways, but it doesn't work.

How to transmit and receive 3 or more analog data?

have a look at this tutorial (also available in the Introductory Tutorials section)

Serial Input Basics

hope that helps...

Maybe if you posted your attempts, we could help...

3 data send code;

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

}

void loop() {

int a = analogRead(A0);
int b = analogRead(A1);
int c = analogRead(A2);

byte pin[] = {a, b, c};

for(int i = 0; i<3; i++){
  Serial.println(pin[i], DEC);
  Serial1.println(pin[i], DEC);

}
delay(1000);
}

data receive code;

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

}

void loop() {
  if (Serial1.available()) {
    int a = Serial1.read();
    Serial.println(a);
  }

}

I am not familiar with programming, so I wrote it simply.

In it's simplest form, use

Serial.println(a);
Serial.println(b);
Serial.println(c);

As you're already going the way of arrays, place the pins (not your pins, the pin numbers of the analog inputs in an array.

// array with the pins that you want to read
const byte inputPins[] = {A0, A1, A2};

And add a matching array to store the readings after that

// array with the pins that you want to read
const byte inputPins[] = {A0, A1, A2};
// array to store the readings
int readings[sizeof(inputPins) / sizeof(inputPins[0])];

sizeof(inputPins) / sizeof(inputPins[0]) is a safe way to determine the the number of elements in any type of array; it will always give the number ofelements regardless of the type of the variable.

You can now iterate through the inputPins array as demonstrated below; the code follows your approach.

// array with the pins that you want to read
const byte inputPins[] = {A0, A1, A2};
// array to store the readings
int readings[sizeof(inputPins) / sizeof(inputPins[0])];


void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop()
{
  // do analog readings and store
  for (byte i = 0; i < sizeof(inputPins) / sizeof(inputPins[0]); i++)
  {
    readings[i] = analogRead(inputPins[i]);
  }

  // display the readings
  for (byte i = 0; i < sizeof(readings) / sizeof(readings[0]); i++)
  {
    Serial.println(readings[i]);
    Serial1.println(readings[i]);
  }

  delay(1000);
}

sizeof(inputPins) / sizeof(inputPins[0]) is a lot of typing; therefore one usually puts it in a macro.

#define NUMELEMENTS(x) (sizeof(x) / sizeof(x))

When using the macro, you specify the array as demonstarted below.

#define NUMELEMENTS(x) (sizeof(x) / sizeof(x))


// array with the pins that you want to read
const byte inputPins[] = {A0, A1, A2};
// array to store the readings
int readings[sizeof(inputPins) / sizeof(inputPins[0])];


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

  Serial.print("number of elements in inputPins array: ");
  Serial.println(NUMELEMENTS(inputPins));
  Serial.print("number of elements in readings array: ");
  Serial.println(NUMELEMENTS(readings));
}

void loop()
{
  // do analog readings and store
  for (byte i = 0; i < NUMELEMENTS(inputPins); i++)
  {
    readings[i] = analogRead(inputPins[i]);
  }

  // display the readings
  for (byte i = 0; i < NUMELEMENTS(readings); i++)
  {
    Serial.println(readings[i]);
    Serial1.println(readings[i]);
  }

  delay(1000);
}

Now you have one problem to solve; how do you now for sure which reading belongs to which pin :wink:

One comment on the receiver; Serial1.read returns a single byte, not a text like "345". So your result for a single reading will e.g. look like

3
4
5

I suggest to study Robin's Serial Input Basics - updated (already mentioned) and write the receiver code based on that. Next you can write the matching sender code.

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

}

void loop() {

int a = analogRead(A0);
int b = analogRead(A1);
int c = analogRead(A2);

int pin[] = {a, b, c};

for(int i = 0; i<3; i++){
  Serial.println(pin[i], DEC);
  Serial1.println(pin[i], DEC);

}
delay(1000);
}
int pin[4];

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

}

void loop() {
  if (Serial1.available()) {
    int pin = Serial1.parseInt();
    Serial.println(pin);
    Serial.println(sizeof(pin));
    
  }

}```

I can receive 3 pieces of data using the above code.

Can I store 3 different data in different variables?

Yes. Read the link provided by @sherzaad and by me.

Note: the last three backticks in your post #7 should be on their own line; that way they will indicate the end of the code block.

What examples can I refer to?

Read it, understand it and make your choice. The receive with start and end marker gives the most solid communication. There is a part about parsing; you can e.g. send the 3 values separated by a comma.

Topics on the same subject merged

I will send and receive 3 analog data between Arduino.
The three data must be transmitted and received simultaneously.
The three received data must be declared as different variables.
Could you write this code for me?
I have spent enough time and have no more time.

Hello
Post your current sketch and schematic of the hardware in use.

The device that transmits the data is the nano 33 IoT and the device that receives the data is the MKR WiFi 1010.

It is connected with RX and TX pins.

@anjongsu
I have stopped you from posting for 24 hours as you keep starting new topics with the same question. Please read the forum rules before you return.

Do you want someone to write your code? There is a forum section for that: Jobs and Paid Consultancy - Arduino Forum

If you want someone to write code for you ask me to move this topic there, expect to pay.

Thank you.