Configuring Arduino from input pin values

Hi there,

I've done a search on this but it's bringing up too much irrelevant stuff to find if it's been answered before, so apologies if it has!!

I'd like to set an I2C address using jumpers (I have multiple identical boards that I'd like to use identical code for). It seems like it ought to be possible, except that the I2C address is set before the Arduino has a chance to look at pins...

Any thoughts?

On which boards you plan to set the address ? Can you provide us a link ? -Malhar

They're Pro Minis, being used for servo control. I was planning to use pins A0 -> A4 to set the slave address (the only 4 pins in a row still available after PWM pins have been used). I've not written any code yet, as I wasn't sure how to achieve this.

Can the I2C address assignment (or anything else one might wish to configure) be set within the program itself, and therefore be done after input pins are checked?

You can set the address in software. why do you want to go with hardware method ?

Servos don’t require PWM pins. Servo library allows up to 12 servos on any digital pins.

If you hard wire the Ax pins to Vcc or gnd for the address you can do something like:

byte address;
void setup()
{
address = digitalRead(A0) + (2 * digitalRead(A1)) + (4 * digitalRead(A3));  // * 8, * 16 etc for the rest
Wire.begin(address);
}

Ok, thanks for that - I'll remember that I don't need to use PWM pins for the next project - 6 servo boards are already made for this one!

I'll give that code a go - I hadn't realised that variable assignments and reading pins could happen at setup.

byte address;
void setup()
{
address = digitalRead(A0) + (2 * digitalRead(A1)) + (4 * digitalRead(A3));  // * 8, * 16 etc for the rest
Wire.begin(address);
}

Will that work with the address variable being declared as a byte ?

Did you consider storing the I2C-address in an EEPROM cell?

UKHeliBob: byte address; void setup() { address = digitalRead(A0) + (2 * digitalRead(A1)) + (4 * digitalRead(A3));  // * 8, * 16 etc for the rest Wire.begin(address); }

Will that work with the address variable being declared as a byte ?

There's no reason why not.

The OP said, "A0 -> A4", so it would be a 5-bit value.

byte address;
address = digitalRead(A0) + (2 * digitalRead(A1)) + (4 * digitalRead(A2)) + (8 * digitalRead(A3)) + (16 * digitalRead(A4));

Worst case:- address = 1 + 2 + 4 + 8 + 16 = 31

Anything up to 7 bits should be fine.