01 Measuring Resistance with IO Ports
1. Introduction
In some previous applications of microcontrollers, there were times when there were insufficient AD port resources, but it was still necessary to measure the resistance value, such as that of a thermistor. An old method has been proposed to achieve precise resistance measurement using two IO ports. Below, we will test this ancient method of measuring resistance using IO ports through an experiment.
▲ Figure 1.1.1 Microcontroller Experiment Circuit Board
2. Measurement Principle
The measurement principle is quite simple. Using two microcontroller IO ports, connect two resistors to charge the same capacitor. Set one IO port as the output port and the other as the input port. The output port charges the capacitor through the connected resistor. The voltage across the capacitor rises, and when it exceeds a certain threshold, the logic level of the input port will change to 1. This charging time is related to the termination voltage, threshold voltage, and the time constant corresponding to RC. The specific values are determined by this formula. This process is measured again. The corresponding time is proportional to R2. Therefore, the ratio of the two times is equal to the ratio of the resistances. If the value of one resistor is known, the value of the other resistor can be calculated based on the time ratio. This is the basic principle of measuring resistance with IO ports.

3. Experimental Results
1. Port Threshold Voltage
This is the STM32F030K6 microcontroller, applying a triangular wave to its port PF0. The program loops to query the input logic level and outputs the inverted logic on PF1. It can be seen that the microcontroller has discretized the input signal. The rise and fall have certain hysteresis characteristics. The hysteresis voltage is approximately 200mV.
▲ Figure 1.3.1 Threshold Voltage of Microcontroller Port
2. Measuring Components
Next, we will use the PF0 and PF1 pins of the F030 microcontroller to measure the resistance and test the accuracy of this method.
In the experiment, a capacitor and two resistors are needed. The capacitance value is 313.8nF, the resistance of R1 is 19.545kΩ; the resistance of R2 is 4.718kΩ. They are installed on a breadboard for testing.

● Circuit Component Parameters: Capacitance C
: 313.8nF Resistance R1
: 19.545k Resistance R2
: 4.718k
The capacitor is grounded on one end, and the other end is connected to the two resistors. The two resistors are connected to the PF0 and PF1 ports of the microcontroller, respectively. Next, we will program the microcontroller to use a timer as a time reference for delay measurement.

PF0 pin is connected to R2, and PF1 is connected to R1. Set PF0 as the output port and PF1 as the input port. The cycle changes the high and low levels of PF0. Measure the voltage signals on PF0 and the capacitor. It can be seen that the voltage across the capacitor shows a charging curve. The time constant is approximately 1.5ms. Based on the known component values, the measured results are consistent with expectations.


This is the charging and discharging curve of resistor 2 on the capacitor. Next, we will measure the charging and discharging process of resistor 1. Since the resistance of resistor 1 is 20k ohms, the corresponding charging and discharging process is relatively slow, with a time constant approximately 4 times that of R2, which is 6.3ms. During the measurement process, both ports discharge the capacitor simultaneously. The discharge time is taken as 20ms.

3. Measuring Microcontroller Software
The measurement software first sets PF0 and PF1 to output low level to discharge the capacitor. Then, one is set as the input port, and the other is set to high level to charge the capacitor. At the same time, Timer 1 is started for timing. During this process, monitor whether the logic level of the input port is 1. When the input port changes to 1, stop the timer and read the time. Then discharge again and change the other port to the input port for testing the charging time. This measures the voltage signal on PF0 and the capacitor. The charging and discharging processes can be observed. The yellow curve is the voltage signal of PF0, and the cyan is the voltage signal of the capacitor during charging and discharging. This is the measurement process when PF0 is the output port and PF1 is the input port. These two charging times are proportional to the resistances.
▲ Figure 1.3.2 Voltage Waveform Measurement
This shows the measurement results; the first is the charging time corresponding to resistor 1. The second is the charging time corresponding to resistor 2. Their ratio is around 4.1. Based on the previously measured values of R1 and R2, the corresponding ratio is approximately 4.143. Thus, it can be seen that the ratio of measurement times is close to the ratio of resistances.

Testing 298 data points for statistics. The average value of the data is 4.119, and the standard deviation is 0.043. The average measured value is about 0.6% lower than the actual resistance ratio of 4.143.
● Data Statistical Results: Measurement Count
: 298 Average Value
: 4.119 Standard Deviation
: 0.043
※ Conclusion ※
This article tested the method of measuring resistance using microcontroller IO ports. The microcontroller platform used was the STM32F030K6. The measured resistance charging and discharging ratio was approximately 0.6% smaller than the resistance value ratio.