{"id":401,"date":"2020-03-14T18:09:32","date_gmt":"2020-03-14T18:09:32","guid":{"rendered":"https:\/\/addictek.com\/?p=401"},"modified":"2022-03-16T23:17:55","modified_gmt":"2022-03-16T23:17:55","slug":"low-power-current-sensor","status":"publish","type":"post","link":"https:\/\/addictek.com\/en\/2020\/03\/14\/low-power-current-sensor\/","title":{"rendered":"Low Power Current Sensor"},"content":{"rendered":"\n<p>\u00a0 <\/p>\n\n\n\n<p>In this article we will see how realize a current sensor circuit in a device using Arduino or more in general the ATmega328P CPU. The current sensor will leverage the built-in ADC of ATmega328P and few external components. The current sensor will have a good level of accuracy and low power consumption, making the solution applicable even to battery powered devices. Specifically, I realized this solution to measure the charging and discharging current flowing to and from the backup battery used in the remote sensor which is part of the <em>Solar Thermal Controller<\/em> project (see ref. [<a class=\"papercite_bibcite\" href=\"#paperkey_0\">1<\/a>]).  The backup battery is connected to an UPS module to provide continuos power to the the circuit. The UPS module receives input power from a solar cell and provides a 5v regulated power to the output. When the input power is above the load power the backup battery is kept under charge. Charging power is the difference between the available input power and the load power. When the input power is below the load power, the backup battery is used to source the missing power (see Figure 1). <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"280\" height=\"230\" src=\"http:\/\/addictek.com\/wp-content\/uploads\/2020\/03\/CurrentSensor0.jpg\" alt=\"\" class=\"wp-image-540\"\/><figcaption>Figure 1. UPS module and backup battery.<\/figcaption><\/figure><\/div>\n\n\n\n<p>The battery charge\/discharge current along with the battery voltage level are used to determine  the state of charge (SoC) of the battery.<\/p>\n\n\n\n<p>The technique used to measure the current is based on the Ohm&#8217;s law. A shunt resistor is connected in series to the battery between the anode and ground.<br>The current flowing into the battery and resistor are the same, the voltage drop on the shunt resistor is proportional to the intensity of the current.<\/p>\n\n\n\n<p>\\[<br>{V_{shunt} = I_{shunt} \\cdot R_{shunt}}<br>\\]<\/p>\n\n\n\n<p>For the application scenario, I wanted to sense charging current up to 600<em>mA  <\/em>and discharging current up to 150<em>mA<\/em>. With a 0.1\u03a9 shunt resistor, this corresponds to a maximum voltage drop across the resistor of 0.06<em>V<\/em> while charging and -0.015<em>V<\/em> while discharging.<\/p>\n\n\n\n<p>Wanting to sample this voltage drop directly with ATmega328P built-in ADC (see figure 2), we have two problems: 1) voltage drop is actually too low to be measured with acceptable accuracy. Even having a adequate voltage reference for the ADC, due to the low voltage range,  the reading would possibly be affected by eccessive noise, 2) more important the voltage drop changes polarity, being positive during battery charge and negative during battery discharge .<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"346\" height=\"346\" src=\"http:\/\/addictek.com\/wp-content\/uploads\/2020\/03\/CurrentSensor2-3.jpg\" alt=\"\" class=\"wp-image-542\" srcset=\"https:\/\/addictek.com\/wp-content\/uploads\/2020\/03\/CurrentSensor2-3.jpg 346w, https:\/\/addictek.com\/wp-content\/uploads\/2020\/03\/CurrentSensor2-3-300x300.jpg 300w, https:\/\/addictek.com\/wp-content\/uploads\/2020\/03\/CurrentSensor2-3-150x150.jpg 150w, https:\/\/addictek.com\/wp-content\/uploads\/2020\/03\/CurrentSensor2-3-100x100.jpg 100w\" sizes=\"(max-width: 346px) 100vw, 346px\" \/><figcaption>Figure 2. <em>ADC directly sampling the shunt resistor<\/em> drop voltage<\/figcaption><\/figure><\/div>\n\n\n\n<p>Here is where the use of an<strong> instrumentation operational amplifier<\/strong> comes in handy. An instrumentation amplifier is a kind of differential amplifier with additional input buffer stages to easily match the impedance of the amplifier with the preceding stage. Instrumentation amplifiers are commonly used in industrial test and measurement application and has some useful features like low offset voltage, high CMRR (Common mode rejection ratio), high input resistance and high gain.<\/p>\n\n\n\n<p>For the purpose of the application scenario described here, we need to select a model supporting single power supply and rail-to-rail output operation range. Second, we want it to sink very low current since we don&#8217;t want it to affect the measured current and reduce the battery backup time. There are several models that have similar capabilities, one of them is the AD627an by Analog Devices (see ref.  [<a class=\"papercite_bibcite\" href=\"#paperkey_1\">2<\/a>]). The AD627an supports<em> <\/em>85<em>\u03bcA<\/em> maximum supply current, single supply power starting from +2.2<em>V<\/em> and rail-to-rail output swing. As for other instrumentation amplifiers, the gain can be set with one external resistor. Figure 3 shows how the amplifier can be connected between the shunt resistor and ATmega328p for current sensing:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"479\" height=\"336\" src=\"http:\/\/addictek.com\/wp-content\/uploads\/2020\/03\/CurrentSensor1-4.jpg\" alt=\"\" class=\"wp-image-543\" srcset=\"https:\/\/addictek.com\/wp-content\/uploads\/2020\/03\/CurrentSensor1-4.jpg 479w, https:\/\/addictek.com\/wp-content\/uploads\/2020\/03\/CurrentSensor1-4-300x210.jpg 300w\" sizes=\"(max-width: 479px) 100vw, 479px\" \/><figcaption>Figure 3. AD627 used to amplify shunt voltage drop before sampling it with the ADC<\/figcaption><\/figure><\/div>\n\n\n\n<p>Note that the <em>V<sup>&#8211;<\/sup><\/em> negative input of the amplifier is connected to ground and <em>V<\/em><sup>+<\/sup> to the the other side of the shunt resistor. Having ground as reference, the voltage on <em>V<\/em><sup>+<\/sup> will be positive during battery charge (current flowing like the red arrow in the picture) and negative during battery discharge (current flowing like the blue arrow in the picture).<\/p>\n\n\n\n<p>From the datasheet we see that the <em>V<\/em><sup>+<\/sup> amplifier input terminal can go from <strong>(\u2212<em>VS<\/em>)\u22120.1<em>V<\/em><\/strong> to (+<em>VS<\/em>)\u22121<em>V<\/em>, while the output can go from (\u2212<em>VS<\/em>)+0.1<em>V<\/em> to (+<em>VS<\/em>)\u22120.15<em>V<\/em>. On the  <em>V<\/em><sup>+<\/sup> input side, the ability of this amplifier to go 0.1<em>V<\/em> below the negative voltage supply (the ground in our case and hence <em>V<\/em><sup>&#8211;<\/sup>) is very important because we need to sense both positive and negative voltage drop accross  the shunt resistor. Applying these specifications to the application scenario where \u2212<em>VS<\/em> is 0<em>V<\/em> (GND) and +<em>VS<\/em> is 5<em>V<\/em>, will result in following absolute limits for <em>V<\/em><sup>+<\/sup> input voltage and <em>V<sub>out<\/sub><\/em> output voltage:<\/p>\n\n\n\n<p>\\[<br>-0.1V < V^+ < 4V<br>\\]<\/p>\n\n\n\n<p>\\[<br>0.1V \\lt V_{out} \\lt 4.85V<br>\\]<\/p>\n\n\n\n<p>As stated above, the shunt resistor voltage drop will be from <em>\u2212<\/em>0.015<em>V<\/em> to 0.06<em>V<\/em> which is well inside the supported range. Now we need to select appropriate values for the gain (<em>G<\/em>) and reference voltage <em>V<sub>off<\/sub><\/em>, the latter being the offset applied to amplifier output. Ideally, the input\/output transfer function of the amplifier is:<\/p>\n\n\n\n<p>\\[<br>V_{out} = [(V^+)-(V^-)] \\cdot G + V_{off}<br>\\]<\/p>\n\n\n\n<p>Hence, being V<sup>&#8211; <\/sup>= 0:<\/p>\n\n\n\n<p>\\[<br>V_{out} = [(V^+)] \\cdot G + V_{off}<br>\\]<\/p>\n\n\n\n<p>However, since the reality is different from theory, in order to stay within the amplifier operating limits, we need to refer to &#8220;<em>INPUT RANGE LIMITATIONS IN SINGLE-SUPPLY APPLICATIONS<\/em>&#8221; section of the datasheet and select proper gain and offset values.<\/p>\n\n\n\n<p>The equation in the datasheet on page 16 is used to check the amplifier internal limits and is reported below:<\/p>\n\n\n\n<p>\\[<br>V_{A1} = 1.25 \\cdot (V^- + 0.5V) &#8211; 0.25 \\cdot V_{off}-\\frac{(V^+ &#8211; V^-) \\cdot 25k\\Omega}{R_{G}} <br>\\]<\/p>\n\n\n\n<p>In the above equation, <em>R<sub>G<\/sub><\/em> is the value of the external resistor used to set the amplifier gain and is calculated according to the following formula:<\/p>\n\n\n\n<p>\\[<br>G = 5 + \\frac {200k\\Omega}{R_{G}}<br>\\]<\/p>\n\n\n\n<p>\\[<br>R_{G} = \\frac {200k\\Omega}{G &#8211; 5}<br>\\]<\/p>\n\n\n\n<p>The datasheet states that <em>V<sub>A<\/sub><\/em><sub>1<\/sub> must be in the range from (\u2212<em>VS<\/em>+50<em>mV<\/em>)=50<em>mV <\/em>to (+<em>VS\u2212<\/em>200<em>mV<\/em>)=4.8<em>V<\/em>.<\/p>\n\n\n\n<p>Setting a gain of 20 (<em>R<sub>G<\/sub><\/em>=13.7 <em>k<\/em>\u03a9) and <em>V<sub>off<\/sub><\/em> = 1.235<em>V<\/em>, we have:<\/p>\n\n\n\n<p>In case of <em>V<\/em><sup>+<\/sup>=-0.015<em>V<\/em>:<\/p>\n\n\n\n<p>\\[<br>V_{A1}^{-} = 1.25 \\cdot (0.5V) &#8211; 0.25 \\cdot (1.235V) &#8211; \\frac{ (-0.015V) \\cdot 25k\\Omega}{13.7k\\Omega} = 0.343V<br>\\]<\/p>\n\n\n\n<p>In case of <em>V<\/em><sup>+<\/sup>=0.06<em>V<\/em>:<\/p>\n\n\n\n<p>\\[<br>V_{A1}^{+} = 1.25 \\cdot (0.5V) &#8211; 0.25 \\cdot (1.235V) &#8211; \\frac{ (0.06V) \\cdot 25k\\Omega}{13.7k\\Omega} = 0.207V<br>\\]<\/p>\n\n\n\n<p>Both values are well within the admitted range.<\/p>\n\n\n\n<p>On the output side, minimum and maximum values will be the following:<\/p>\n\n\n\n<p>\\begin{equation} \\label{eq:voutmin}V_{out}^{-} = V_{ref} + (V^+ &#8211; V^-) \\cdot R_{G} = 1.235V-0.015V \\cdot 20 = 0.935V \\end{equation}<\/p>\n\n\n\n<p>\\begin{equation} \\label{eq:voutmax}V_{out}^{+} = V_{ref} + (V^+ &#8211; V^-) \\cdot R_{G} = 1.235V+0.06V \\cdot 20 = 2.435V \\end{equation}<\/p>\n\n\n\n<p>Again, both values are within the amplifier limits.<\/p>\n\n\n\n<p>Sampling the amplifier output voltage with ATmega328P ADC, requires setting the proper voltage reference on the <em>A<sub>ref<\/sub><\/em> pin. In this case, a +2.5<em>V<\/em> reference will allow the 10 bit ADC to sample the whole amplifier output range. <\/p>\n\n\n\n<p>The current sensing precision depends on the ADC resolution and corresponds to the minimum change in current that the circuit will be able to sense. The ADC voltage resolution (<em>ADC<sub>Vres<\/sub><\/em>) is given by:<\/p>\n\n\n\n<p>\\[<br>ADC_{Vres} = \\frac {A_{ref}}{2^N}<br>\\]<\/p>\n\n\n\n<p>Where <em>N <\/em>is the ADC resolution in bits which equals to 10 in case of ATmega328P. The variation of amplifier output voltage can be written as follows:<\/p>\n\n\n\n<p>\\[<br>\\Delta V_{out} = \\Delta (V^+ &#8211; V^-) \\cdot G = \\Delta V^+ \\cdot G = R_{shunt} \\cdot \\Delta I_{shunt} \\cdot G<br>\\]<\/p>\n\n\n\n<p>So,<\/p>\n\n\n\n<p>\\[<br>\\Delta I_{shunt} = \\frac{\\Delta V_{out}}{R_{shunt} \\cdot G}<br>\\]<\/p>\n\n\n\n<p>Substituting <em>\u0394V<sub>out<\/sub> <\/em>with the ADC voltage resolution, we get the minimum variation <em>I<sub>shunt<\/sub><\/em> that can be sampled by the ADC, which is:<\/p>\n\n\n\n<p>\\begin{equation} \\label{eq:1}<br>I_{res} = \\frac {A_{ref}}{2^N \\cdot R_{shunt} \\cdot G} = \\frac {2.5V}{1024 \\cdot 0.1\\Omega \\cdot 20} = 1.23mA\/unit<br>\\end{equation}<\/p>\n\n\n\n<p>The formula to calculate the current flowing on the shunt resistor is the following:<\/p>\n\n\n\n<p>\\begin{equation} \\label{eq:2}<br>V_{out}=V^+ \\cdot G + V_{off} = I_{shunt} \\cdot R_{shunt} \\cdot G + V_{off} <br>\\end{equation}<\/p>\n\n\n\n<p>The value sampled by the ADC depends on the ADC input voltage (<em>V<sub>out<\/sub><\/em>), the voltage reference value (<em>A<sub>ref<\/sub><\/em>) and the ADC bit resolution (<em>N<\/em>) and can be written as follows:<\/p>\n\n\n\n<p>\\begin{equation} \\label{eq:3}<br>ADC = \\frac {V_{out} \\cdot 2^N}{A_{ref}}<br>\\end{equation}<\/p>\n\n\n\n<p>Getting <em>V<sub>out<\/sub> <\/em>off (\\ref{eq:3}) and substituting in (\\ref{eq:2}) results in:<\/p>\n\n\n\n<p>\\begin{equation} \\label{eq:4}<br>\\frac {ADC \\cdot A_{ref}}{2^N} = I_{shunt} \\cdot R_{shunt} \\cdot G + V_{off} <br>\\end{equation}<\/p>\n\n\n\n<p>Finally, we can get <em>I<sub>shunt<\/sub><\/em> off  (\\ref{eq:3}):<\/p>\n\n\n\n<p>\\begin{equation} \\label{eq:5}<br>I_{shunt} = \\frac{\\frac {ADC \\cdot A_{ref}}{2^N}-V_{off}}{R_{shunt} \\cdot G}<br>\\end{equation}<\/p>\n\n\n\n<p>Equation (\\ref{eq:5}) can be used in the sample algorithm to calculate the shunt current from the ADC sample value. <em>I<sub>shunt<\/sub><\/em> will be negative if battery is discharging and positive if battery is charging.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Sampling accuracy considerations<\/h3>\n\n\n\n<p>There are some principles that is worth taking into account  to improve sampling accuracy. Their application is general and not strictly related to the use case described in this article.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Adjusting the voltage offset<\/h4>\n\n\n\n<p>You might have noticed in the above description that the sampling interval of the ADC is not fully used. This is because the offset value of the amplifier is almost centered within the ADC sampling interval (1.235<em>V<\/em> vs. [0<em>V<\/em>-2.5<em>V<\/em>]). This is good if the extension of the negative input range of the amplifier is the same as the positive input range. However, in our case, negative range is [-0.015V, 0V] and positive range [0V, +0.06V]. With a centered offset, the amplifier output will never go below 0.935<em>V<\/em> and above 2.435<em>V<\/em>  (see (\\ref{eq:voutmin}) and  (\\ref{eq:voutmax})) while the ADC will still be able to sample the whole range from 0<em>V<\/em> to <em>Aref<\/em>=2.5V. In other words, we are wasting part of the sample interval which means reducing the practical ADC voltage resolution. Figure 4 shows the amplifier input range vs output range in case of (a)  centered voltage offset value (b) adjusted voltage offset value:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"272\" src=\"http:\/\/addictek.com\/wp-content\/uploads\/2020\/03\/CurrentSensor7-1.jpg\" alt=\"\" class=\"wp-image-704\" srcset=\"https:\/\/addictek.com\/wp-content\/uploads\/2020\/03\/CurrentSensor7-1.jpg 1024w, https:\/\/addictek.com\/wp-content\/uploads\/2020\/03\/CurrentSensor7-1-300x80.jpg 300w, https:\/\/addictek.com\/wp-content\/uploads\/2020\/03\/CurrentSensor7-1-768x204.jpg 768w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><figcaption>Figure 4. Amplifier input range vs output range in case of (a)  centered voltage offset value (b) adjusted voltage offset value<\/figcaption><\/figure>\n\n\n\n<p>Let&#8217;s see how to calculate the appropriate voltage offset for the amplifier according to the negative and positive extension of the input range, ADC reference voltage <em>A<sub>ref<\/sub><\/em> and shunt resistor value <em>R<sub>shunt<\/sub> <\/em>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"549\" height=\"142\" src=\"http:\/\/addictek.com\/wp-content\/uploads\/2020\/03\/CurrentSensor3-2.jpg\" alt=\"\" class=\"wp-image-527\" srcset=\"https:\/\/addictek.com\/wp-content\/uploads\/2020\/03\/CurrentSensor3-2.jpg 549w, https:\/\/addictek.com\/wp-content\/uploads\/2020\/03\/CurrentSensor3-2-300x78.jpg 300w\" sizes=\"(max-width: 549px) 100vw, 549px\" \/><figcaption>Figure 5. Amplifier input vs. output <em>range <\/em><\/figcaption><\/figure>\n\n\n\n<p>Being the amplifier input voltage going from  -0.015<em>V<\/em> to 0.06<em>V<\/em> the correct voltage offset can be calculated solving the following equality (see Figure 5):<\/p>\n\n\n\n<p>\\[<br>(V_{off}-V_{out}^{min}):(V_{out}^{max}-V_{out}^{min}) = (V^+_0-V^+_{min}):(V^+_{max}-V^+_{min})<br>\\]<\/p>\n\n\n\n<p>\\[<br>V_{off} = \\frac {(V_{out}^{max}-V_{out}^{min}) \\cdot (V^+_0-V^+_{min})}{(V^+_{max}-V^+_{min})} + V_{out}^{min}<br>\\]<\/p>\n\n\n\n<p>Since amplifier output range must match the ADC sample the interval that is [<em>V<sub>out<\/sub><sup>min<\/sup><\/em>, <em>V<sub>out<\/sub><sup>max<\/sup><\/em>] = [0, <em>A<sub>ref<\/sub><\/em>], we get:<\/p>\n\n\n\n<p>\\[<br>V_{off} = \\frac {A_{ref} \\cdot (V^+_0-V^+_{min})}{(V^+_{max}-V^+_{min})}<br>\\]<\/p>\n\n\n\n<p>Substituting actual values:<\/p>\n\n\n\n<p>\\[<br>V_{off} = \\frac {A_{ref} \\cdot 0.015V}{0.075V} = 0.5V<br>\\]<\/p>\n\n\n\n<p>Setting the amplifier offset to +0.5<em>V<\/em> would allow us to increase the gain:<\/p>\n\n\n\n<p>\\[<br>G = \\frac {V_{out}^{max}-V_{out}^{min}}{V^+_{max}-V^+_{min}} = \\frac {A_{ref}}{V^+_{max}-V^+_{min}} =  \\frac {2.5V}{0.075V} = 33<br>\\]<\/p>\n\n\n\n<p>Setting the new gain value in equation (\\ref{eq:1}), will give us the updated  sampling precision :<\/p>\n\n\n\n<p>\\[<br>I_{res} = \\frac {A_{ref}}{2^N \\cdot R_{shunt} \\cdot G} = \\frac {2.5V}{1024 \\cdot 0.1\\Omega \\cdot 33} = 0.75mA\/unit<br>\\]<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Averaging and Oversampling<\/h4>\n\n\n\n<p>Signal averaging and oversamplig are common techniques used to increase current sampling accuracy. In order to make these techniques effective we need to make some assumptions:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Sampled variable is the sum of two components: signal and noise<\/li><li>Signal <em>s <\/em>and noise <em>z<\/em> are uncorrelated<\/li><li>Signal measurements <em>s(t)<\/em> over sampling interval <em>\u0394T<\/em> are strongly correlated, ideally signal power is constant<\/li><li>Noise measurements <em>z(t)<\/em> are random (uncorrelated) over sampling interval <em>\u0394T<\/em>, the mean is 0 and the variance is constant.<\/li><\/ul>\n\n\n\n<p>Under these assumptions the Signal To Noise Ratio (SNR) can be calculated as follows:<\/p>\n\n\n\n<p>\\[<br>SNR = \\frac{P_{signal}}{P_{noise}} = \\frac {E[s^2]}{E[z^2]} = \\frac {E[s^2]}{\\sigma_z^2}<br>\\]<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Averaging<\/h5>\n\n\n\n<p>Averaging consists in sampling a random variable mutiple times and then calculating the average value of the samples. Let&#8217;s suppose we are sampling a random variable at <em>dt<\/em> time interval. The random variable is the sum of the signal <em>s<\/em> and random noise <em>z<\/em>. Averaging by a factor <em>n<\/em> consists in sampling the the value of the random variable <em>n <\/em>times in the <em>dt<\/em> time interval (the sample is taken every  <em>dt\/n<\/em>) and then averaging the <em>n<\/em> values. Another possibility is to keep the sampling interval at <em>dt<\/em> and implementing a moving average filter. The value of the sample is calculated every time the value is sampled by averaging on the last <em>n<\/em> values. The first teqnique implies increasing the sample frequency on <em>n<\/em> factor which is basically oversampling. Oversampling can also be used to increase the ADC resolution as decribed below.<\/p>\n\n\n\n<p>Let&#8217;s go back to the noise component of our samples and assume that we just sample the noise. Let&#8217;s consider <em>n<\/em> consecutive samples of the noise, <em>Z<\/em>={<em>z<sub>1<\/sub><\/em>, <em>z<sub>2<\/sub><\/em>, \u2026, <em>z<sub>n<\/sub><\/em>}, having per-sample variance of <i>\u03c3<sup>2<\/sup><sub>z<\/sub><\/i>. It can be demostrated that (see [<a class=\"papercite_bibcite\" href=\"#paperkey_2\">3<\/a>]):<\/p>\n\n\n\n<p>\\[<br>E(\\overline{z}^2) = Var(E[Z]) =  Var({ \\frac {1}{n} \\sum_{i=1}^{n}  z_i}) = \\frac {1}{n} \\sigma ^2_z <br>\\]<\/p>\n\n\n\n<p>The variance of the average of <em>n <\/em>samples is reduced by a factor of  <em>n<\/em> thus improving the SNR by the same factor:<\/p>\n\n\n\n<p>\\[<br>\\overline {SNR} = \\frac{P_{signal}}{P_{noise}} = \\frac {E[s^2]}{E(\\overline{z}^2)} = n \\frac {E[s^2]}{\\sigma_z^2} = n \\cdot SNR<br>\\]<\/p>\n\n\n\n<p>Following picture shows how visible is the benefit in noise reduction averaging a random variable by a factor of 10:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"887\" height=\"444\" src=\"http:\/\/addictek.com\/wp-content\/uploads\/2020\/03\/CurrentSensor6-1.jpg\" alt=\"\" class=\"wp-image-611\" srcset=\"https:\/\/addictek.com\/wp-content\/uploads\/2020\/03\/CurrentSensor6-1.jpg 887w, https:\/\/addictek.com\/wp-content\/uploads\/2020\/03\/CurrentSensor6-1-300x150.jpg 300w, https:\/\/addictek.com\/wp-content\/uploads\/2020\/03\/CurrentSensor6-1-768x384.jpg 768w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><figcaption>Figure 6. Noise distribution. Plain noise vs noise averaged on 10 samples <\/figcaption><\/figure>\n\n\n\n<h5 class=\"wp-block-heading\">Oversampling<\/h5>\n\n\n\n<p>Oversampling consists in sampling the ADC input signal by a frequency that is higher than the required sampling rate <em>f<sub>s<\/sub>.<\/em> In accordance with the Nyquist Theorem, this is usually the Nyquist frequency <em>f<sub>n<\/sub><\/em> corresponding to, the double of highest frequency component <em>f<sub>m<\/sub><\/em> of interest in the input signal.<\/p>\n\n\n\n<p>\\[<br>f_s \\gt f_n = 2 \\cdot f_m<br>\\]<\/p>\n\n\n\n<p>For each additional bit of resolution (see ref.  [<a class=\"papercite_bibcite\" href=\"#paperkey_3\">4<\/a>] and  [<a class=\"papercite_bibcite\" href=\"#paperkey_4\">5<\/a>]), the signal must be oversampled by a factor of four, in general the oversampling frequecy <em>f<sub>os<\/sub><\/em> is given by:<\/p>\n\n\n\n<p>\\[<br>f_{os} = 4^w \\cdot f_s<br>\\]<\/p>\n\n\n\n<p>Where <em>w<\/em> is the number of additional bits of resolution desired.<\/p>\n\n\n\n<p>For example to increase the resolution of the ATmega328P ADC of 2 additional bits (from 10 to 12 bits) we need to oversample by a factor of 16 (4<sup>2<\/sup>). We need to cumulate 16 consecutive readings and then average the result by 4 (or right shift the sum by 2 bits). This is also referred as <em>decimation <\/em>process. This way we will have a 12 bit effective sample with an improvement in the SNR (Signal to Noise Ratio).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Circuit design considerations<\/h2>\n\n\n\n<p>Following pictures shows the final circuit schema for the current sensor:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"697\" height=\"457\" src=\"http:\/\/addictek.com\/wp-content\/uploads\/2020\/03\/CurrentSensor4-1.jpg\" alt=\"\" class=\"wp-image-709\" srcset=\"https:\/\/addictek.com\/wp-content\/uploads\/2020\/03\/CurrentSensor4-1.jpg 697w, https:\/\/addictek.com\/wp-content\/uploads\/2020\/03\/CurrentSensor4-1-300x197.jpg 300w\" sizes=\"(max-width: 697px) 100vw, 697px\" \/><figcaption>Figure 7. Current sensor circuit schematic<\/figcaption><\/figure>\n\n\n\n<p>The circuit implements the current sensor design described in previous paragraphs. There are few additional modules required by the circuit whose components need correct sizing:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Instrumental amplifier <strong>input low pass filter<\/strong><\/li><li><em>V<sub>off<\/sub><\/em> and <em>V<sub>ref<\/sub><\/em> <strong>voltage shunt regulators<\/strong><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Amplifier input low pass filter<\/h3>\n\n\n\n<p>An input low pass filter to the instrumentation amplifier is highly recommended to filter out RF interference. In fact, instrumentation amplifiers can rectify high frequency out of-band signals. Once rectified, these signals appear as dc offset errors at the output. RF interference can be present for example in circuits making use of buck-boost converters that usually operate at frequencies beetween 600Khz and 1.5Mhz. For example the UPS module used in this circuit is using a boost converter to generate a regulated 5V output from the battery voltage.<\/p>\n\n\n\n<p>To suppress these RF interferences, a low pass RC filter can be added to the amplifier input. Resistor R3 and capacitor C1 provide such filter, with a cut off frequency (-3db frequency corresponding to 50% signal power reduction):<\/p>\n\n\n\n<p>\\[<br>f_c = \\frac {1}{2 \\pi \\cdot (R3 \\cdot C1)} = \\frac {1}{2 \\pi \\cdot (4,7k\\Omega \\cdot 100nF)} = 338Hz<br>\\]<\/p>\n\n\n\n<p><em>R<\/em>3 and<em> C<\/em>1 provide a 338<em>Hz<\/em> cut off frequency that is enough for the purpose of the current sensor, assuming that the measured signal (shunt voltage drop) is quite stable over time. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Voltage shunt regulators<\/h2>\n\n\n\n<p>Shunt regulators diodes are used in the circuit to provide a precise and stable voltage reference to the instrumentation amplifier (offset voltage) and the ADC (reference voltage). Diodes used here belong to LM285\/385-xxx series from Texas Instruments (see ref. [<a class=\"papercite_bibcite\" href=\"#paperkey_5\">6<\/a>]). The LM285\/385-xxx are micropower 3-terminal current band-gap voltage reference diodes. They are designed to operate over a wide current range as low as 10<em>\u00b5A<\/em> up to 20<em>mA<\/em>, with low noise and stable operation over time and temperature. In particular, we selected the LM385z-1.2  for the amplifier offset voltage (<em>V<sub>off<\/sub><\/em>) and LN385z-2.5 for the ADC reference voltage (<em>A<sub>ref<\/sub><\/em>). The regulator circuit is very simple and only requires some precautions in sizing the series resistor to provide the current required by the diode to operate correctly. One important thing to consider is the input impedance of the circuit receiving the refrerence voltage. Following picture shows the typical application circuit:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"215\" height=\"280\" src=\"http:\/\/addictek.com\/wp-content\/uploads\/2020\/03\/CurrentSensor5-2.jpg\" alt=\"\" class=\"wp-image-572\"\/><figcaption>Figure 8. Shunt voltage regulator application circuit<\/figcaption><\/figure><\/div>\n\n\n\n<p>Where:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><em>V<sub>cc<\/sub><\/em> is the unregulated input voltage<\/li><li><em>V<sub>z<\/sub><\/em> is the shunt regulator (zener) voltage<\/li><li><em>R<sub>s<\/sub><\/em> is the current limiting resistor<\/li><li><em>R<sub>o<\/sub><\/em> is the input impedance of the receiving circuit<\/li><\/ul>\n\n\n\n<p><em>I<sub>z<\/sub><\/em> must be greater than the maximum value of the minimum operating current (<em>I<sub>z<\/sub><sup>min<\/sup><\/em>) required by the reference diode. In case of LM385z-xxx the maximum value of the minimum operating current is between 20<em>\u00b5A<\/em> and 30<em>\u00b5A<\/em> over the whole operating temperature range depending on the regulator voltage (see ref. [<a class=\"papercite_bibcite\" href=\"#paperkey_5\">6<\/a>]):<\/p>\n\n\n\n<p>\\begin{equation} \\label{eq:Iz}<br>I_z = I_s &#8211; I_o \\gt I_z^{min}<br>\\end{equation}<\/p>\n\n\n\n<p>Where:<\/p>\n\n\n\n<p>\\begin{equation} \\label{eq:Is}<br>I_s = \\frac{V_{cc} &#8211; V_z}{R_s}<br>\\end{equation}<\/p>\n\n\n\n<p>\\begin{equation} \\label{eq:Io}<br>I_o = \\frac{V_z}{R_o}<br>\\end{equation}<\/p>\n\n\n\n<p>Hence, substituting (\\ref{eq:Is}) and (\\ref{eq:Io}) in (\\ref{eq:Iz}):<\/p>\n\n\n\n<p>\\[<br>I_z = \\frac{V_{cc} &#8211; V_z}{R_s} &#8211; \\frac{V_z}{R_o} \\gt I_z^{min}<br>\\]<\/p>\n\n\n\n<p>Finally, the constraint on <em>R<sub>s<\/sub><\/em> is the following:<\/p>\n\n\n\n<p>\\begin{equation} \\label{eq:Rs}<br>R_s \\lt \\frac {R_o \\cdot (V_{cc} &#8211; V_z)}{R_o \\cdot I_z^{min} + V_z} = \\frac {V_{cc} &#8211; V_z}{ I_z^{min} + \\frac {V_z}{R_o}}<br>\\end{equation}<\/p>\n\n\n\n<p>To reduce power consumption we want to determine the maximum possible value for <em>R<sub>s<\/sub><\/em> that results from (\\ref{eq:Rs}) replacing the maximum value of the <em>I<sub>z<\/sub><\/em> current and the minimum value of <em>R<sub>o<\/sub><\/em> resistance. These values can be read from components datasheets.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Amplifier offset shunt regulator<\/h3>\n\n\n\n<p>For the shunt regulator connected to the <em>V<sub>off<\/sub><\/em>  pin of the instrumentation amplifier we have:<\/p>\n\n\n\n<p>Minimum input impedance for AD627 <em>V<sub>off<\/sub><\/em> pin (see ref. [<a class=\"papercite_bibcite\" href=\"#paperkey_1\">2<\/a>] &#8220;AD627 Reference Input Impedance&#8221; on Table 3, pag 6) is:<\/p>\n\n\n\n<p>\\[<br>R_o \\gt 125k\\Omega<br>\\]<\/p>\n\n\n\n<p>Maximum value of the minimum operating current for the shunt diode (see ref. [<a class=\"papercite_bibcite\" href=\"#paperkey_5\">6<\/a>] &#8220;Minimum Operating Current&#8221; max value for LM385-1.2) is:<\/p>\n\n\n\n<p>\\[<br>I_z^{min} \\lt 20 \\mu A<br>\\]<\/p>\n\n\n\n<p>Substituting above values in (\\ref{eq:Rs}):<\/p>\n\n\n\n<p>\\[<br>R_s \\lt 126k\\Omega<br>\\]<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ADC reference shunt regulator<\/h3>\n\n\n\n<p>For the shunt regulator connected to the <em>A<sub>ref<\/sub><\/em>  pin of the ATmega328P we have:<\/p>\n\n\n\n<p>Minimum input impedance for ATmega328P <em>A<\/em><i><sub>ref<\/sub><\/i> pin  (see ref. [<a class=\"papercite_bibcite\" href=\"#paperkey_6\">7<\/a>] &#8220;Reference input resistance&#8221; on Table 28-8):<\/p>\n\n\n\n<p>\\[<br>R_o \\gt 22.4k\\Omega<br>\\]<\/p>\n\n\n\n<p>Maximum value of the minimum operating current for the shunt diode (see ref. [<a class=\"papercite_bibcite\" href=\"#paperkey_5\">6<\/a>] &#8220;Minimum Operating Current&#8221; max value for LM385-2.5) is:<\/p>\n\n\n\n<p>\\[<br>I_z^{min} \\lt 30 \\mu A<br>\\]<\/p>\n\n\n\n<p>Substituting above values in (\\ref{eq:Rs}):<\/p>\n\n\n\n<p>\\[<br>R_s \\lt 18k\\Omega<br>\\]<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n<div id=\"paperkey_0\" class=\"papercite_entry\">[1]                      Massimo Morlupi. <a href='..\/..\/..\/2020\/01\/27\/solarheatingcontroller'>Solar Heating Controller<\/a>. , 2020. <br\/>    <a href=\"javascript:void(0)\" id=\"papercite_0\" class=\"papercite_toggle\">[Bibtex]<\/a><\/div>          <div class=\"papercite_bibtex\" id=\"papercite_0_block\"><pre><code class=\"tex bibtex\">@article{solarheatingcontroller,\nauthor = \"Massimo Morlupi\",\ntitle = \"{<a href='..\/..\/..\/2020\/01\/27\/solarheatingcontroller'>Solar Heating Controller<\/a>}\",\nyear = \"2020\"\n}<\/code><\/pre><\/div>       <div id=\"paperkey_1\" class=\"papercite_entry\">[2]                      Analog Devices. <a href='https:\/\/www.analog.com\/media\/en\/technical-documentation\/data-sheets\/AD627.pdf'>Micropower, Single- and Dual-Supply, Rail-to-Rail Instrumentation Amplifier<\/a>. , 2013. <br\/>    <a href=\"javascript:void(0)\" id=\"papercite_1\" class=\"papercite_toggle\">[Bibtex]<\/a><\/div>          <div class=\"papercite_bibtex\" id=\"papercite_1_block\"><pre><code class=\"tex bibtex\">@ad627{ad627,\nauthor = \"Analog Devices\",\ntitle = \"{<a href='https:\/\/www.analog.com\/media\/en\/technical-documentation\/data-sheets\/AD627.pdf'>Micropower, Single- and Dual-Supply, Rail-to-Rail Instrumentation Amplifier<\/a>}\",\nyear = \"2013\"\n}<\/code><\/pre><\/div>       <div id=\"paperkey_2\" class=\"papercite_entry\">[3]                      Howard E. Haber. <a href='http:\/\/scipp.ucsc.edu\/~haber\/ph116C\/iid.pdf'>Class Handouts &#8211; iid random variables<\/a>. , 2012. <br\/>    <a href=\"javascript:void(0)\" id=\"papercite_2\" class=\"papercite_toggle\">[Bibtex]<\/a><\/div>          <div class=\"papercite_bibtex\" id=\"papercite_2_block\"><pre><code class=\"tex bibtex\">@iid{iid,\nauthor = \"Howard E. Haber\",\ntitle = \"{<a href='http:\/\/scipp.ucsc.edu\/~haber\/ph116C\/iid.pdf'>Class Handouts - iid random variables<\/a>}\",\nyear = \"2012\"\n}<\/code><\/pre><\/div>       <div id=\"paperkey_3\" class=\"papercite_entry\">[4]                      Silicon Labs. <a href='https:\/\/www.silabs.com\/documents\/public\/application-notes\/an118.pdf'>Improving ADC Resolution by Oversampling and Averaging<\/a>. , rev 1.3. <br\/>    <a href=\"javascript:void(0)\" id=\"papercite_3\" class=\"papercite_toggle\">[Bibtex]<\/a><\/div>          <div class=\"papercite_bibtex\" id=\"papercite_3_block\"><pre><code class=\"tex bibtex\">@an118{an118,\nauthor = \"Silicon Labs\",\ntitle = \"{<a href='https:\/\/www.silabs.com\/documents\/public\/application-notes\/an118.pdf'>Improving ADC Resolution by Oversampling and Averaging<\/a>}\",\nyear = \"rev 1.3\"\n}<\/code><\/pre><\/div>       <div id=\"paperkey_4\" class=\"papercite_entry\">[5]                      Atmel. <a href='http:\/\/ww1.microchip.com\/downloads\/en\/appnotes\/doc8003.pdf'>AVR121: Enhancing ADC resolution by oversampling<\/a>. , 2005. <br\/>    <a href=\"javascript:void(0)\" id=\"papercite_4\" class=\"papercite_toggle\">[Bibtex]<\/a><\/div>          <div class=\"papercite_bibtex\" id=\"papercite_4_block\"><pre><code class=\"tex bibtex\">@avr121{avr121,\nauthor = \"Atmel\",\ntitle = \"{<a href='http:\/\/ww1.microchip.com\/downloads\/en\/appnotes\/doc8003.pdf'>AVR121: Enhancing ADC resolution by oversampling<\/a>}\",\nyear = \"2005\"\n}<\/code><\/pre><\/div>       <div id=\"paperkey_5\" class=\"papercite_entry\">[6]                      ON Semiconductor. <a href='https:\/\/www.onsemi.com\/pub\/Collateral\/LM285-D.PDF'>LM285\/LM385 Micropower Voltage References<\/a>. , 2016. <br\/>    <a href=\"javascript:void(0)\" id=\"papercite_5\" class=\"papercite_toggle\">[Bibtex]<\/a><\/div>          <div class=\"papercite_bibtex\" id=\"papercite_5_block\"><pre><code class=\"tex bibtex\">@lmx85{lmx85,\nauthor = \"ON Semiconductor\",\ntitle = \"{<a href='https:\/\/www.onsemi.com\/pub\/Collateral\/LM285-D.PDF'>LM285\/LM385 Micropower Voltage References<\/a>}\",\nyear = \"2016\"\n}<\/code><\/pre><\/div>       <div id=\"paperkey_6\" class=\"papercite_entry\">[7]                      Atmel Corporation. <a href='http:\/\/ww1.microchip.com\/downloads\/en\/DeviceDoc\/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdf'>ATmega328P &#8211; 8-bit AVR Microcontroller with 32K Bytes In-System Programmable Flash<\/a>. , 2015. <br\/>    <a href=\"javascript:void(0)\" id=\"papercite_6\" class=\"papercite_toggle\">[Bibtex]<\/a><\/div>          <div class=\"papercite_bibtex\" id=\"papercite_6_block\"><pre><code class=\"tex bibtex\">@atmega328p{atmega328p,\nauthor = \"Atmel Corporation\",\ntitle = \"{<a href='http:\/\/ww1.microchip.com\/downloads\/en\/DeviceDoc\/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdf'>ATmega328P - 8-bit AVR Microcontroller with 32K Bytes In-System Programmable Flash<\/a>}\",\nyear = \"2015\"\n}<\/code><\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>\u00a0 In this article we will see how realize a current sensor circuit in a device using Arduino or more in general the ATmega328P CPU. The current sensor will leverage the built-in ADC of ATmega328P and few external components. The current sensor will have a good level of accuracy and low power consumption, making the &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/addictek.com\/en\/2020\/03\/14\/low-power-current-sensor\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Low Power Current Sensor&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-401","post","type-post","status-publish","format-standard","hentry","category-general-engineering"],"_links":{"self":[{"href":"https:\/\/addictek.com\/en\/wp-json\/wp\/v2\/posts\/401"}],"collection":[{"href":"https:\/\/addictek.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/addictek.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/addictek.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/addictek.com\/en\/wp-json\/wp\/v2\/comments?post=401"}],"version-history":[{"count":308,"href":"https:\/\/addictek.com\/en\/wp-json\/wp\/v2\/posts\/401\/revisions"}],"predecessor-version":[{"id":1015,"href":"https:\/\/addictek.com\/en\/wp-json\/wp\/v2\/posts\/401\/revisions\/1015"}],"wp:attachment":[{"href":"https:\/\/addictek.com\/en\/wp-json\/wp\/v2\/media?parent=401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/addictek.com\/en\/wp-json\/wp\/v2\/categories?post=401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/addictek.com\/en\/wp-json\/wp\/v2\/tags?post=401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}