2025-06-03 6:03 AM
Hello fellow community members.
I recently needed to find a method for the microcontroller on a board to detect which variant of a board it is installed on. In this particular case, the PCB comes in (for now) two different variants: One with all components mounted, and one with the bare minimum mounted. This is for cost saving on the less advanced devices.
To simplify firmware uploading and updates, the firmware for the two variants is going to be the same. The firmware is to figure out by itself which board it is installed on, in order to handle the differences properly.
I was running out of pins, and couldn't waste ADC inputs on this. Also, pulling one digital input pin high or low only gave me two possibilities, which might not be enough in the future. But then I thought of something else, which I wish to share with the community.
With one resistor on one general-purpose input pin, I can detect four different configurations. This requires the use of the built-in weak pull-up and pull-down resistors. If you have already figured it out, or know about this method, you don't need to keep reading.
Before explaining the method, I'll make a few assumptions, based on the devices I use:
The method is to configure the pin as a high-impedance input, read the input level, and then see if enabling the weak pull-up or pull-down resistor will change the logic level. This gives the four combinations:
For combinations 1 and 2, an external pull-down resistor is necessary. A pull-up resistor for combinations 3 and 4.
For combinations 1 and 4, the resistor should be 10 kΩ or lower. If a 10 kΩ pull-down resistor is used, worst-case weak pull-up (25 kΩ) results in a ≈ 0.29 × Vdd input voltage, which should register as low logic level.
For combinations 2 and 3, the resistor should be higher than 130 kΩ. If a 130 kΩ pull-down resistor is used, worst-case weak pull-up (55 kΩ) results in a ≈ 0.70 × Vdd input voltage, which should register as high logic level. However do keep leakage currents in mind, so they dont pull the voltage outside the safe zone. 150 kΩ is probably a good choice.
Now for the bonus: If one of the output pins on your microcontroller drives a high-impedance input, where the initial level is irrelevant, this pin can also be used for hardware variant detecting. Examples of such outputs could be
This way, you can add hardware variant detection without even spending extra pins. And only use a single resistor.
The detection method:
The combination of the first and second read determines the variant.
If you have improvements, or better/simpler methods, please feel free to post them here.