4-bit bus address selection via analog input pin: Monte Carlo simulation shows overlapping address values
According to your simulations, your addressing scheme works fine as long as only uncertainties in resistor values are considered. It's the uncertainty on the voltage that makes it fail.
My advice is to cancel out the fluctuations on VCC by using it as a reference to the ADC. You can do so by programming the REFS
bits in the ADMUX
register, as explained in the datasheet:
Once you switch to VCC as a reference voltage for ADC, you will not be measuring voltage anymore, but a ratio between resistor values, and those are precise to 1%. No changes are required in the schematic (though you may want to increase the value of R7 to get your measurements in the middle of ADC's conversion range), only the firmware of Attiny has to be updated.
If you're still willing to rework the sensor board, you can replace your schematic by an R-2R ladder as Cano64 suggested:
simulate this circuit – Schematic created using CircuitLab
This will optimize the resolution by making all steps in the ladder equal. 1% resistors are precise enough to build monotonic ladders of up to 5 bits.
I had to solve a similar problem once. I needed to connect 4 buttons to one analog pin on arduino, however I needed to recognize simultaneous presses (all combinations). I came up with the same circuit as you, then I wrote a program to search all possible resistor value combinations so the final values when you press the buttons, are as much apart as possible. Guess what? We are screwed, it cannot be done. That's it, have a nice day.
I learned that the optimal resistor values for switches are close to R, 2R, 4R, 8R and the bottom part of the divider has a value of R. Your circuit is already close to the optimal one, you just need to replace R7 with 10k resistor.
But there is a good news for you. Since you just need to set an address (you won't operate the switch while the device is running) I recommend to use SPDT switches and R2R ladder instead.