Sunday, 14 July 2019

A simple circuit for measuring electrical current with Arduino

The current measurement requires the use of an ammeter placed in series with the load. An ideal ammeter has no voltage drop, i.e. it is a short circuit. But most of the current sensors are based precisely on the measurement of the voltage drop on a resistor which, according to Ohm's law, is proportional to the current that passes through it.
For what has been said, a good real ammeter must have a very small voltage drop, this in order not to alter the circuit under measurement with its insertion. The sensor I present has a voltage drop of only 50 mV and uses easily available components.

The shunt
The most accurate system is to use a shunt, i.e. a resistor with very low resistance compared to that of the load and the generator. Of course today we also find on the market sensors based on the Hall effect, that is a device that measures the magnetic field generated by the current passing through a wire for the well-known Ampere’s law, but these semiconductor sensors do not have comparable stability and precision. Figure 1 shows the typical appearance of a 10A and 75mV shunt.

For low currents it is possible to realize the shunt with 0.1 Ω wire resistors, possibly put in parallel, or with a piece of constantan wire of adequate section and length.

Measurement techniques
There are two ways to insert the shunt:
1)      low-side: the shunt is connected between the load and the mass.
2)      high-side: the shunt is connected between the power supply and the load.
Figure 2 shows the two connection modes.

The first mode has as advantages a low common mode voltage, inputs and outputs related to ground and circuit simplicity but is affected by disturbances of ground returns
The second way is not affected by mass disturbances but the amplifier must work with high common mode voltage values and the circuit is more complex.

A low-side solution
There are several breakout boards available on the market ready to measure the current with Arduino, but for those who love self-construction and also to understand the functioning of the circuits, I suggest to carry out my project. It is a fairly accurate current sensor while using common components.
I use the shunt in low-side mode connected to a differential amplifier with Arduino-compatible output with internal reference, therefore with ADC overall voltage measurement range equal to 1.1 volts. With this solution a more stable and less noisy reference is obtained with a resolution of 1100/1024 = 1.074 mV.
Using operational power amplifiers with a single power supply there are problems if the signals approach zero. Even using special operational amplifiers called "rail to rail", whose output swing is closer to the rails, rarely they have outputs below 20 mV at zero input. A similar output can also be obtained using a most common LM358 suitable for working even with single power supply.
An expedient to overcome this drawback is to operate on a value higher than zero and therefore to subtract this constant at software level. To have a zero in output and also to eliminate the off-set I added a Vsh constant of about 100 mV. For this circuit I used a differential amplifier U1a, whose output is equal to:
Vo = (Vin+ - Vin-)R2/R1+Vsh
The second operational U1b, connected as voltage follower, generates the voltage Vsh by means of a divider on the Arduino Vref.
For correct operation the following relations must be respected: R1=R3 and R2=R4 with very low tolerances. I have selected them among metal film resistors with a 1% tolerance, now easily available, using a good ohmmeter. Since the double LM358 does not have a very low offset I preferred not to overdo the gain, so R2/R1 = 20.
The ADC converter has a full scale of 1100 mV and I take 100 out of it to make the zero, so I can have 1000 mV for the measurement, this means that the maximum input voltage must be 1000/20 = 50 mV.
The following table shows some shunt values and related flow rates.
I max [A]
Shunt [Ω]
It is also possible to use commercial shunts. Many of them have a voltage drop of 75 mV so we can vary the gain that becomes 1000/75 = 200/15 = 13.33 or calculate the new full scale which is equal to 2/3. For example, a 10 A and 75 mV shunt has a resistance Rs = 7.5 mΩ and a full scale I = 50 / 7.5 = 6.66 A.
To have a sensitivity of 75 mV, suitable for many commercial shunts, it is sufficient to set R1 = R3 = 15kΩ and R2 = R4 = 200kΩ, standard and easily available values.
Figure 3 shows the circuit and its connections with Arduino.
To test this circuit I used a 0.1 Ω shunt resistor and a 0.1 A current source.

Significant improvements are achieved by replacing the LM358 with a low off-set input like the OP290, OPA2196, OPA2277 and similar chips.
In figure 4 we can see the layout and the connections of the components of my prototype, made on a small pre-drilled pcb board.
The op-amp offset
Not considering here the problem of saturation already seen for the single power supply, a real operational amplifier has no zero-volt output with zero input, the input offset is defined as the voltage I have to give to the input to have zero in output.
Figure 5 shows the model for evaluating the offset of a differential amplifier.
Considering the Vos offset voltage, the output voltage with zero input is:
Vo = (1+R2/R1)Vos
For example: if an LM358 has an offset of 2 mV, with R2/R1= 20 there would be a difference of 44 mV (2x21 + 2, including that of U1b) between the voltage Vo and Vsh. The offset is temperature dependent and causes a thermal drift, this operational has an input offset voltage drift of about 7 µV/°C.
For this application the input offset is very important as this constant is amplified by the amplifier.
Ignoring the thermal drift, we can remove the offset using a trimmer on the divider that produces Vsh. The rest will do the software by subtracting a constant. For Vsh= 100 mV this number is equal to:
Nsh =100*1023/1100 = 93
This is the constant to be removed and we should set the Rp1 trimmer until zero is read for zero current. In the test phase I increased this value to 98 to work better with the trimmer.
Components list
R1, R3
11 kW ± 1% metal film
500 Ω multi-turn trimmer
R2, R4
220 kW ± 1% metal film
0.1 Ω  shunt
10 kW ± 2% metal film
10 µF,25V Aluminum electrolytic
1 kW ± 2% metal film
Arduino UNO or Nano board
10 kW ±5%
Dual op amp LM358

The program
The example program is very simple, the values used are relative to my system. To calibrate the Rp1 trimmer, just run the program and turn the trimmer to read zero on the serial monitor with zero input current. If it is not possible to zero the trimmer, it is necessary to slightly modify the Nsh constant to be subtracted from the measurement.
The constant mVtomA is derived from experimental measurements using a good precision ammeter, a power supply and some resistive loads such as car bulbs or wire resistors of adequate power.

/* program ArduAmmeter.ino Arduino current meter
 Giovanni Carrera, rev. 11/07/2019 */

float NtomV;
const float VREF = 1095;// in mV, this value can be read on VREF pin
const int Nsh = 98;// shift value corresponding to about 100 mV
const float mVtomA = 2.17;// value obtained by a calibration

void setup() {
  analogReference(INTERNAL); // internal ADC reference input = 1100 mV
  NtomV = VREF/1023;// constant of conversion into millivolts

void loop() {
  int val = analogRead(A0)-Nsh;// read the current sensor and remove the shift
  float mvolt = NtomV*val;// convert to millivolt
  float mamp = mvolt/mVtomA;// convert to milliampere
  Serial.print("Vo = ");
  Serial.print(" mV - Current = ");
  Serial.println(" mA");

1.       “AN1332, Current Sensing Circuit Concepts and Fundamentals”, Microchip DS01332B, 2011.
2.       “AN39, Current measurement applications handbook”, Peter Abiodun Bode, Zetex Semiconductors, 2008.
3.       “AN105, Current Sense Circuit Collection”, Tim Regan, Linear Technology, dec 2005.
4.       “Fully Integrated, Hall Effect-Based Linear Current Sensor IC with 2.1 kVRMS Isolation and a Low-Resistance Current Conductor”, ACS712-DS, Rev. 15,  Allegro MicroSystems – 2013.
5.       “VARDULOG - Data logger dei consumi di un apparato elettrico”, Giovanni Carrera, rivista FARE ELETTRONICA n. 361-362, Novembre-Dicembre 2015;
6.       “Progetto ArduWattmeter”, Giovanni Carrera, rivista FARE ELETTRONICA n. 367/368 ­ Giugno/Luglio 2016;
7.       “Op Amp Input Offset Voltage”, Analog Devices, MT-037 Rev.0, 10/08, WK.