If you are out of Analog Inputs (AIs) on your MCU, or do not have any AIs at all, it’s possible to rig together a way to read analog signals that use varying resistance levels, even if you only have digital inputs (DIs). As an example, let’s assume that the varying resistance device is a thermistor, which is a temperature sensor that varies it’s resistance in response to the temperature of the surrounding environment.
Using a resistor and a capacitor and a little engineering magic (math) you can use the fact that a capacitor has a predictable amount of time to charge and discharge to create a pseudo-AI. The resistor and capacitor in series make an RC circuit, which you would connect to a DI pin on your MCU. In a nutshell, you would be able to determine the varying resistance of the thermistor by measuring its RC time constant, and from there calculate the ambient temperature. You would need to know a few things first.
But first, how does this work? If you apply voltage to an RC circuit (a resistor in series with a capacitor), the voltage across the capacitor increases to a peak voltage within a knowable time frame. The capacitor will charge up over a set period of time with increasing voltage, acting like a small battery that stores and releases energy. When the voltage is reduced, the capacitor will discharge in the other direction. A long time ago, someone worked out that tau (T) is the product of resistance times capacitance in the RC circuit (T=RC). We also know that it takes 5T to fully charge or discharge a capacitor. A fully charged capacitor will be very close to the supply voltage, or capacitor charging voltage. So, a fully charged capacitor takes 5T to get to a fully charged state. We also know that 1T is equal to 63% of 5T since the charging voltage of the capacitor (VC) is an exponential curve. Therefore, the time (T) that it takes for the voltage (VC) to reach 63% of the maximum is the resistance times the capacitance in the RC circuit.
Having explained the details above, starting with an uncharged capacitor, you will need to see how long it takes the RC circuit to register as a high value on the DI. The amount of time it takes is related to the time constant discussed above. Mashing the theoretical points above with reality in your circuit, the time it takes for the RC circuit to make the DI register from low to high is the time constant. One time constant is equal to 63% (theoretically) of your maximum input voltage (VC). Input voltages for MCU boards are typically 5V or 3.3V these days.
Once you know the time constant by timing how long it takes for the DI to flip high, since you know the capacitor values, the resistance value is easy to calculate: R=t/C. You will want to set the I/O pin to low to discharge the capacitor first, making sure to keep it low for at least 5RC (five times R times C.) As an example, an R value of 100Kohms and a C value of 1µF yields t= .1 second, so keep the output low for at least half a second. Set the digital pin to be an input, start charging the RC circuit, and count the time it takes for the DI to flip high. Record the time that it took for the DI to flip high and calculate the resistance value based on t=RC. Several values need to be captured and averaged to get a decent result, since the theory is never as neat in real practice.
The change from the DI as high to low is also relevant, and the above theory can be applied to the discharge cycle, as well.
The post MCUs: Using a digital input to read an analog signal without an ADC appeared first on Microcontroller Tips.