# Using same pins number in different arduino?

I'm new beginner in programming arduino, and I want ask a question about arduino code, this is the problem " i used two arduino to build a robot, but i don't know how to set the code that for two arduino uno that pins in first arduino i used them all and now i wanna use the pins in second arduino, and how write code for the second arduino, which it is the same pin number i used in first arduino, how to creat a code? thanks a lot guys for answer my question... (sorry for my bad english)...

• Do both Arduinos run the same sketch, but with different pins? If the sketches are different there is no problem. If the sketches are the same, `#define ARDUINONUMBER 1` and `#if ARDUINONUMBER == 1`, `#else`, and `#endif` can make the pin numbers change by changing just the `#define` statement. If that isn't the problem, please explain more. – JRobert Feb 10 '17 at 14:16
• I also do not fully understand the question. If the 2 arduinos run independently, there should be no problem? If you are saying you want 1 Arduino to be the MAIN ONE.. but you have run out of pins.. and want to add another Arduino (to have more I/O pins).. but have the MAIN ARDUINO still 'control' the second Arduino.. then you'd need to set up a way for the MAIN Arduino to communicate with the SECONDARY Arduino. I suggest using Serial Comm (Software Serial) to do so. – whispers Feb 10 '17 at 21:36
• this is simple problem, I have two arduinos, 1st arduino all pins is connected to their functions, now i have the second arduino, in 2nd arduino i wanna use the pins for other function, both arduinos is in same sketch, how to write the code for the both arduinos, how do i connect between them? (thanks for respons my question before, sir) – Aliebi Leah Feb 11 '17 at 4:46
• You want two Arduinos, running the same sketch, to use the same pins for different purposes? How will that work? Why do you want to do that? Is this an X-Y problem? – Nick Gammon Feb 11 '17 at 9:03
• You could look at GPIO expanders like the MCP23017, cheaper and easier than a second Arduino. – Code Gorilla Feb 13 '17 at 12:54

The notion that “both Arduinos are in the same sketch” is slightly off. Each of the Arduinos will contain a program; their programs might be identical if you do some fancy programming, but it is simpler to write one sketch per Arduino.

Thus, your first Arduino will run a sketch designed to control the devices it is connected to, and your second Arduino will run a sketch designed to control the devices that it's connected to. Conceivably, you might have some devices that both Arduinos connect to.

Calling the two sketches S1 and S2, the device on pin number n as referred to in S1 will typically be a different device than the one on pin number n as referred to in S2. Most but not all of the pin usage assignments for each pin number will be different in one sketch versus the other.

It's likely the two Arduinos will need to communicate with each other. For a range of methods, see for example How to have an Arduino communicate with 3 other Arduinos. For additional detail see Arduino-to-Arduino serial communication and How to network many Arduinos with sensors using I2C.

If you decide to use I2C, then also see MasterWriter Tutorial and Wire Library webpages at arduino.cc.

To expand on my comment, if you begin your sketch with conditional definitions, you can compile it for different sets of pin assignments just by changing the `#define ARDUINONUMBER xxx` statement:

``````#define ARDUINONUMBER 1

#if ARDUINONUMBER == 1
#define FRONTMOTORPIN 7
#define REARMOTORPIN  8

#elif ARDUINONUMBER == 2
#define FRONTMOTORPIN 12
#define REARMOTORPIN  13

#else
#error Board number must be 1 or 2.
#endif

void setup(void)
{
pinMode(FRONTMOTORPIN, OUTPUT);
pinMode(REARMOTORPIN, OUTPUT);
; // etc
}

void loop(void)
{
;
}
``````