cancel
Showing results for 
Search instead for 
Did you mean: 

Tip: A super-simple circuit for auto-detecting hardware variants

EThom.3
Senior

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:

  • A high-level input signal must be above 0.7 × Vdd.
  • A low-level input signal must be below 0.3 × Vdd.
  • The weak pull-up and pull-down resistors are between 25 and 55 kΩ.

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:

  1. Initial signal is low. With pull-up enabled, still low.
  2. Initial signal is low. Goes high when pull-up is enabled.
  3. Initial signal is high. Goes low when pull-down is enabled.
  4. Initial signal is hign. With pull-down enabled, still high.

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

  • GPIO signal for a logic-level FET gate, driving a status LED
  • the Tx signal for an RS-485 transceiver (which isn't transmitting anything yet)
  • the direction signal for a motor controller (which hasn't been enabled yet)

This way, you can add hardware variant detection without even spending extra pins. And only use a single resistor.

 

The detection method:

  • Configure the pin as a high-impedance input.
  • If the pin was configured differently before, wait a few µs for the voltage to settle.
  • Read the logic input level.
  • If the input level is low, enable the weak pull-up resistor; otherwise enable the weak pull-down resistor.
  • Wait a few µs for the voltage to settle.
  • Read the new logic input level.
  • If relevant, configure the pin as it should be.

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.

0 REPLIES 0