When designing gadgets with microcontrollers (MCU), I like to make use of among the analog-to-digital converter (ADC) inputs to measure onboard voltages together with all of the required sensors inputs. This implies I usually run out of ADC inputs. So introduced here’s a method so as to add extra ADCs with out including exterior chips, costing lower than 5 cents, and taking on negligible house on a PCB!
There are two issues within the MCU you might be utilizing: a pulse width modulator (PWM) output and an onboard analog comparator. Some MCU traces which have these are the PIC, AVR, and ATmega MCUs from Microchip. Additionally, TI’s Piccolo line and STMicroelectronics STM32L5 have each a PWM and comparator.
So, let’s have a look at how that is configured.
Determine 1 is a diagram exhibiting the addition of a resistor and capacitor to your MCU mission.
The resistor and capacitor type a single pole low-pass filter. So, the circuit idea takes the output of an onboard PWM, filters it to create a DC sign that’s set by the PWM’s responsibility cycle. The DC stage is then in comparison with the enter sign utilizing the on-board comparator. The circuit may be very easy so let’s discuss in regards to the code used to create an ADC from this association.
To get a pattern studying of the enter sign, we begin by setting the PWM to a 50% responsibility cycle. This square-wave PWM sign will probably be filtered by the RC low-pass filter to create a voltage that’s ½ of the MCU’s system voltage. The comparator output will go excessive (or output a digital 1) if the filtered DC stage is larger than the instantaneous enter sign voltage, in any other case the comparator output will go low (outputting a digital 0).
The code will now learn the comparator output and execute a search to discover a new stage that forces the comparator to an reverse output. In different phrases, if the comparator is a 0 the code will modify the PWM responsibility cycle up till the comparator outputs a 1. If the comparator is at the moment exhibiting a 1 the PWM responsibility cycle will probably be diminished till the comparator outputs a 0. If the PWM is able to one thing like 256 steps (or extra) in responsibility cycle, this search may take some important time. To mitigate this, we’ll do a binary search so if there are 256 steps accessible within the PWM, it should solely take log2(256), or 8, steps to check the degrees.
A fast description of the binary search is that after the primary 50% stage studying, the following take a look at will probably be at a 25% or 75% stage, relying on the state of the comparator output. The steps after this can once more take a look at the center of the remaining ranges.
Let’s present a fast instance and assume the next:
The primary take a look at will probably be executed with the PWM at about 50% responsibility cycle (a setting of 128), making a 2.50-V sign that’s utilized to the “+” enter of the comparator. This implies the comparator will output a excessive which suggests that the PWM responsibility cycle is simply too excessive. So, we’ll reduce the responsibility cycle in half giving a setting of 64, which creates 1.25 V on the “+” enter. The comparator once more will output a 1… to excessive so we drop the PWM responsibility cycle by half once more to 32. This provides a “+” stage of 0.625 V. Now the comparator will output a 0 so we all know we went too low, and we improve the PWM responsibility cycle. We all know 64 was too excessive and 32 was too low so we go to the middle, or (64+32)/2 = 48, giving 0.9375 V. We’re nonetheless too low so we break up the distinction of 64 and 48 leading to 56 or about 1.094 V…too excessive. This continues with (56+48)/2=52, giving 1.016 V…too excessive. Once more, with a PWM setting of (52+48)/2=50, giving 0.9766 V. One final step, (52+50)/2=51 giving 0.9961 V.
This was 8 steps and bought us as shut as we will to the reply. So, our ADC setup would return a solution that the instantaneous enter sign was 0.9961 V.
Let’s check out a real-world instance. This instance makes use of an Arduino Nano which makes use of an ATmega328P which has a lot of PWM outputs and one analog comparator. The PWM we’ll use could be clocked at numerous charges and we wish to clock it quick as this can make the filtering simpler. It would additionally velocity up the time for the filter output to settle to its ultimate stage. We’ll choose a PWM clocking price of about 31.4 kHz. Determine 2 reveals the schematic with a one pole RC low-pass filter.
Determine 2 Schematic of the pattern circuit utilizing an Arduino Nano and a one-pole RC low-pass filter.
On this schematic D11 is the PWM output, D6 is the comparator’s “+” enter, whereas D7 is the comparator’s “-” enter. The filter consists of a 20kΩ resistor and a 0.1 µF capacitor. I arrived at these values by taking part in round in an LTspice simulation to attempt to reduce the remnants of the PWM pulse (ripple) whereas additionally sustaining a reasonably quick settling time. A goal for the ripple was the decision of a 1-bit change within the PWM, or much less. Utilizing the 5 V of the system voltage and the knowledge that the PWM has 8-bit (256 settings) adjustability we get 5 V/256 = ~20 mV. Within the LTspice simulation I bought 18 mV of ripple whereas the dc stage settled in inside just a few millivolts of its ultimate worth at 15 ms. Due to this fact, when writing the code, I used 15 ms because the delay between samples (with a small twist you’ll see beneath). Because it takes 8 readings to get a ultimate usable pattern, it should take 8*15 ms = 120 ms, or 8.3 samples per second. As famous originally, you received’t be sampling at audio charges, however you possibly can actually monitor DC voltages on the board or slow-moving analog alerts.
[This may be a good place to note that the analog input does not have a sample-and-hold as most ADCs do, so readings are a moving target. Also, there is no anti-aliasing filter on the input signal. If needed, an anti-alias filter can remove noise and also act as a rough sample and hold.]
Under is the code itemizing to be used in an Arduino growth surroundings. You can even obtain it right here. It would learn the enter sign, do the binary search, convert it to a voltage, after which show the ultimate 8-bit DAC worth, corresponding voltage studying, and a slower shifting filtered worth.
The next offers a deeper description of the code:
Step one was to measure the system voltage on the +5-V pin or the Arduino Nano. This worth (4.766 V) was entered on line 3 of the code. I then ran the code on an Arduino Nano V3 and monitored the output on the Arduino serial monitor. To check the code and system, I first linked a 2.5-V reference voltage to the sign enter. This reference was first warmed up and a voltage studying was taken on a calibrated 5 ½ digit DMM. The reference learn 2.5001 V. The serial monitor confirmed an instantaneous voltage various from 2.5232 to 2.4858 V and the common voltage diversified from 2.5061 to 2.5074 V. That is round 0.9% error within the instantaneous voltage studying and about 0.3% on the averaged voltage studying. This reveals we’re getting a studying with about ±1 LSB error within the instantaneous voltage studying and a filtered studying of about ± 0.4 LSB. When inputting numerous different voltages I bought comparable accuracies.
I additionally examined with an enter of Vcc (4.766 V) and seen outcomes of 4.7473 V which suggests it may work up very near the higher rail. With the enter grounded the instantaneous and filtered voltages confirmed 0.000 V.
These appear to be an excellent consequence for an ADC created by including two cheap components.
So subsequent time you’re in need of ADCs give this a attempt. The fee is negligible, PCB house may be very minimal, and the code is small and straightforward to grasp.
Damian Bonicatto is a consulting engineer with a long time of expertise in embedded {hardware}, firmware, and system design. He holds over 30 patents.
Phoenix Bonicatto is a contract author.
Associated Content material
The put up Want an additional ADC? Add one for just a few cents appeared first on EDN.
👇Observe extra 👇
👉 bdphone.com
👉 ultraactivation.com
👉 trainingreferral.com
👉 shaplafood.com
👉 bangladeshi.assist
👉 www.forexdhaka.com
👉 uncommunication.com
👉 ultra-sim.com
👉 forexdhaka.com
👉 ultrafxfund.com
👉 ultractivation.com
👉 bdphoneonline.com
- Commercial - Lawrence Livermore Lab develops a silicone-based 3D-printed options for electrostatic discharge safety…
Collaboration goals to considerably improve value, vitality effectivity, driver expertise and automobile vary Firms signed…
Palestinian officers, witnesses and journalists are accusing Israel of stepping up a marketing campaign of…
Know-how brings large alternatives, if accomplished proper. Think about the instance of the digital twin.…
9to5Mac Safety Chew is solely dropped at you by Mosyle, the one Apple Unified Platform. Making…
Bharti Airtel's CEO, Gopal Vittal, has hinted at a brand new tariff construction that the…