Communicating with another board

I'm building a robot, and need to have some basic communication between two boards. The only free pins are 2 on each board (digital without PWM).

Basically, the 1st board controls one set of motors for the wheels, and the base of a robotic arm. The 2nd board controls another set of motors for a different system, and the end of the robotic arm.

How can I somehow send a number or anything from D2 on board 1 to D2 on board 2? (So I can coordinate the movement of the arm and the motors etc.)

Thanks!

What "boards" are you using, and do the boards have serial ports?

Is data being sent to the first board?
If so from where and in what format?

Could you use SoftwareSerial on the two free pins (plus GND).

How much information needs to pass from board1 to board2 - for example would a simple HIGH or LOW be sufficient? With 2 pins you can have 4 different combinations of HIGH and LOW.

You could use one of the free pins for a clock signal to synchronize a stream of data bits on the other pin.

What about using a Mega (which has a lot more I/O capacity) rather than two separate Arduinos?

...R

It's not uncommon for one Arduino to act as a I2C slave. In this case you could probably share the I2C bus with other devices.

Thanks for the help! (Especially @Robin2)

I'm using two Arduino Unos, and the first one needs to send a range of about 5 different commands to the second one, but I don't need any data to come back the other way. The first board is receiving data from my phone via bluetooth (I'm using a JY-MCU hooked up to pins 0 and 1) - this is how I'm controlling the robot.

I only have space to use pin 2 on each board (all the other pins are being used). A mega would have been a good idea, but it's a bit too late now :/. If I'm not mistaken, softwareSerial needs two pins on each board, and in any case, I don't need to send information in that kind of detail.

I think I may have found a rudimentary solution: I am setting board 1 pin 2 HIGH for a certain length of time, then setting it LOW again. On board two, I have a bunch of "if"s within each other, which basically set a variable to a certain value, depending on how long pin 2 was HIGH for. I know that sounds like a roundabout way of doing it, but that's just to give you an idea of what sort of thing I am looking at. If that's a bit confusing I've included the code below.

If anyone knows a better or more efficient way, please let me know! Thanks again

Board 1 Code:

void setup() {
  pinMode(2,OUTPUT);

}

void loop() {
  digitalWrite(2, HIGH);
  delay(10);
  digitalWrite(2, LOW);
  
  delay(2000);
  
  digitalWrite(2, HIGH);
  delay(20);
  digitalWrite(2, LOW);
  
  delay(2000);

  digitalWrite(2, HIGH);
  delay(30);
  digitalWrite(2, LOW);

  delay(2000);
  
}

Board 2 Code:

int val;

void setup() {
  pinMode(2,INPUT);
  Serial.begin(9600);
  val = 0;
}

void loop() {
  if(digitalRead(2) == 1){
    val = (val+1);
    delay(15);
    if(digitalRead(2) == 1){
      val = (val+1);
      delay(10);
      if(digitalRead(2) == 1){
        val = (val+1);
        delay(10);
      
      }
    }
  }

  if (val>0){
    Serial.println(val);
    val = 0;
  }
}

Basically prints 1, then 2, then 3, every 2 seconds. Feedback?

There is a hacked version of SoftwareSerial that allows you to transmit only, with no receive pin. (Usually the problem is the other way around.) It's a good idea to add some error detection, like a checksum byte, so that the receiver has some way of checking that the message is valid.

Feedback on your code: Use [ code ] tags (the </> symbol in the full editor) and then we'll read it.

I only have space to use pin 2 on each board (all the other pins are being used).

So what are the UART pins 0 and 1 being used for? They are typically used for serial communication.

If you only want one-way communication you just need 1 pin (plus GND for SoftwareSerial). (For some reason I though you said you had 2 free pins on each board).

If you want your Arduino to do anything else you CANNOT use delay().

Software serial (and HardwareSerial) does something similar to what you are trying to do but is a mite more sophisticated. :slight_smile:

I think you will quickly run into a problem with your code because the two systems will get out of sync. That is what Serial uses its start and stop bits for.

Also, your system would be very slow by Arduino standards.

I suggest you try using SoftwareSerial to send short messages. The system in the 3rd example in Serial Input Basics would be the most reliable. Use that example in your receiving Arduino and make the sending Arduino compatible.

...R

Thanks for the advice everyone, I've been testing it and my method works fine for the basic communications I need. Lots of helpful ideas though :slight_smile:

//especially thanks for the 'insert code' tip ;p

FYI I'm using pins 0 and 1 for bluetooth, and as far as I've used it the syncing has been fine (I'm not just running it in a loop like my example code, I'm sending different numbers at different times, so any offset gets cancelled I think)