How to measure dc current with a microcontroller?

Microcontrollers usually don’t have specific ports for measuring currents, but they do have ADC channels through which you can measure analog voltages of a certain range. This means a dc current can be indirectly measured by a microcontroller’s ADC channel by first converting the current into voltage. The simplest way of doing this is to place a resistance in series with the current path and measure the voltage drop across it. But hold on, if you place an additional resistance in the circuit, it will affect the original current. Therefore, we need to use a very small value resistance so that it’s effect in the circuit current won’t be significant.

Resistors with values less than 1 ? are available in electronics stores. Depending upon the amount of current in the circuit, you need to choose proper power rating for the resistor. Suppose, if you pick 0.47 ?, and the maximum current in the circuit is about 2 A, then the resistor should have the capacity of dissipating 4 x 0.47 ? 2 Watts of heat.

You can also make a small value resistance by yourself. Yes, by simply winding a copper wire into coil. I have made one from a 5 ft long solid copper wire (22 AWG) with plastic insulation on outer side, as shown below.

Current sensing resistance

Now lets measure its resistance. The resistance can be measured directly with a digital multimeter. My digital meter shows its value equal to 0.3 ?. This measurement may have higher uncertainty as it is very small and most multimeter does not show values beyond 1 decimal digit. The resistance can also be measured using Ohm’s law. Connect a 47? ? resistor in series with the coil resistance (Rs) and supply a 5V power as shown below. Next, measure the voltage across Rs and current through it separately using the multimeter. In my case, I found the measured voltage and current values to be 24.1 mV and 84.3 mA, respectively. This gives the resistance of the coil is about 0.286 ?.

Now, suppose that the range of current to be measured using this coil resistance is from 0-2 A. Then the voltage drop across the coil resistance will be somewhere from 0 – 0.57 V. Because of its low dynamic range, this voltage signal may not be accurately measured with a microcontroller’s ADC module. So this requires some sort of voltage scaling. One way to achieve that is by using an operational amplifier circuit as shown below.

Opamp as a voltage scaler

In the circuit, Rs is the low value current sensing resistor (our coil resistor) which is connected in series with the load resistor. Our objective is to derive the load current (I). The low voltage drop across Rs is amplified by the non-inverting amplifier. The gain of the amplifier is set by Rf and Ri resistors. For Rf = 10 K, and Ri = 1.3 K, the gain of the amplifier would be about 8.7. This is enough to linearly scale Vs (0-0.57 V) to Vo (0- ?5 V). Now you have 0-5 V voltage signal that corresponds to 0-2 A current through Rs. This voltage signal is now more appropriate for ADC conversion with Vref = 5 V.

Vo = 8.7 x I x Rs = 2.49I (Rs = 0.286 ?)

=> I = Vo/2.49.

For 10-bit ADC with Vref = 5 V, resolution = 5/1024 = 0.0049 V. For input signal Vo, the ADC O/P will be Vo x 0.0049. Thus,

I = ADC O/P x 0.0049/2.49 = 0.00197 x ADC O/P

The current resolution would be therefore 0.00197 A (? 2 mA).

Related Posts

34 comments

  • I think there is a slight mistake in this statement “For input signal Vo, the ADC O/P will be Vo x 0.0049”. ADC converts/measures analog value in terms of steps(digitally coded) with each step size equal to its resolution (i.e. 0.0049V) so for ADC output we need to calculate number of steps that would be contained in a physical quantity. So instead of multiplying Vo we got to divide by ADC’s resolution i.e Vo / 0.0049.

    Rest of the explanation is fine and very helping.

  • CAn u help me regarding making of true rms digital voltmeter using micro controller measuring both ac and dc. with frequency 100 hz. and dc ranges from 0 to 200v levels for dc 0-10,0-50, 0-100?

  • hiiii i’m doing microcontroller.i have to sense the voltage and current from pv panel.after that i shold give these voltage and currnet sensors to microcontroller.
    can u please suggest me which model voltage and current sensors are best to use and which port i conncet in AT MEGA 16 Microcontrooler

  • hii… i need to amplify my voltage across the resistor which is 20-30mV to 2-3V. I’m using your above circuit as reference and i have used a 1K(Ri) and an 100K(Rf) for getting an output voltage of 3. but i’m not able to achieve this. please help me out.

  • Pingback: STM32 monitoring motor current | embedded experiments

  • Hi

    I am required to design a power meter with an atmega8515 but i have no idea how the adc pins work on it and also how to measure voltages from 2v to 6v and currents ranging from 0-0.5A.. please help as to how a=the adc works and how to implement this

  • Pingback: Measuring current with microcontroller - Do It Easy With ScienceProg

  • Hey,
    That’s a pretty nice post. A good one, describing the intricate details as well! Liked it. 🙂

  • Pingback: Blog J.Schweiss | AVR Tutorials

  • I am a little confused why your computer generated schematic shows Rs connected to Vcc but your hand drawn schematic shows Rs connected to GND side. Please help!

    Thanks, Paul

    • Hi Paul,

      Actually no need to be confused as the computer generated schematic demonstrates the abstract idea of measuring the voltage across the resistor Rs while the hand drawn schematic shows the practical implementation of it.

      The second schematic makes things easier because only one point of connection is required to measure the voltage across Rs and therefore calculate the current.. this is because the other pin of the resistor is connected to the common ground 😉

  • very helpful tutorials, what if we use opto-coupler instead of maybe a very small resistor. The theory is: The LED will produce variable light to photo-transistor depending on the current flowing, then voltage across the photo-transistor will be proportional to the current flowing to the load. Does my proposal practical?

  • Take care of temperature as cooper wires is very sensitive and may false the measument as the measured current warms the wire itself or ambient temperature varies. That’s the reason you should use materials that have low coefficient of resistance variation with temperature.

  • hi,
    i want to measure car battery current 20 amp up to 200 amp ,plz give me a complete solution

  • Hi,

    Very nice and well explained tutorial!

    I’d personally love to see a similar one to this, but measuring resistance, as in my case I’m willing to measure it when my soldering iron is off, and current when it’s on.

    Regards,

  • Hello, I wanted to ask you which opamp did you used to measure current
    ?. Because you conected it directly to VCC and ground. I use the TL072 and i need to feed it with (+)Vcc and (-)Vcc.

  • absolutely, this is very help me. many thanks.

  • Hello your tutorials are very helpful. may you please guide me how to measure current fro AC power supply using a microcontroller. Thanks in advance

  • Thanks a lot. 🙂

  • I found your article very helpful in an stepper motor application… But now I have two questions.

    1. I see your shunt made with two cables, one orange (primary) and a white. The white was added to reach the 5ft long or is another feature made in?

    2. How many feets would use for an 18 AWG cable? The reason is that’s the only I can buy in my area. 🙁

    Thanks

  • I understand the overall concept and I understand the op-amp architecture but I am having trouble with the application of the solution.

    I need to measure the load to a motor, this motor is driven via a L293. I have inserted the 0.47ohm shunt into the 0v return line from the motor driver (the two 0v pins) but I am getting results that are not resprentative of the current.

    I have the pic corrently returning ADC value that represent 0v-5v (ie 0 – 1024). The L293 and the motor works as expected with my bench power supply showing 0.0 thru to 0.4 amps. I need the pic to ensure I do not exceed 0.4 amps when fully assembled.

    Help please. I have Rl as the L293, Rs at 0.47ohm – where am I going wrong?

  • I understand the overall concept and I understand the op-amp architecture but I am having trouble with the application of the solution.

    I need to measure the load to a motor, this motor is driven via a L293. I have inserted the 0.47ohm shunt into the 0v return line from the motor driver (the two 0v pins) but I am getting results that are not resprentative of the current.

    I have the pic corrently returning ADC value that represent 0v-5v (ie 0 – 1024). The L293 and the motor works as expected with my bench power supply showing 0.0 thru to 0.4 amps. I need the pic to ensure I do not exceed 0.4 amps when fully assembled.

    Help please. I therefore have the Rl as the L293,

  • can i use MID 400 to measure the voltage level of ac mains?bcoz i have seen this ic mid 400 and after seeing the application note i realised that it can be use determine whether ac is on or off but i want to measure the exact ac voltage..i dont want to use a transformer.
    reply soon

  • HI,I WANT TO MONITOR AC MAINS VOLTAGE THROUGH ATMEGA16,WITHOUT USING A TRANSFORMER.CAN U PLEASE SUGGEST ME SOMETHING.
    THANX

    • Ok, you can use this technique. But here’s something you need to consider. Suppose you have a shunt resistor, Rsh = 0.47 Ohm. The 10 A of current will create a voltage drop of 4.7 V across Rsh. This means the available output voltage will be only 24-4.7 = 19.3 V. If that’s ok for your load, then no problem. If you don’t want the output to fall that low, you can consider using a different value for Rsh.

  • my source of voltage is ac mains,i am converting it to dc using ac to dc converter and then i have to measure the dc output current going to the load which is connected after ac to dc converter.so what i hv thought is that i will connect a current shunt along with current shunt monitor amplifier(ina193).would it work?

  • hello,
    your post is extremely helpful in my project.i have a doubt that if i want to sense 10 amp of current from power supply then would this circuit work???

    • Nishu,
      The technique is applicable to measure any current but there are few things to be considered. First, the resistance used for current sensing must have enough power rating to dissipate the heat developed due to the current. Suppose, if you are using a .47 Ohm series resistor to sense current up to 10 A, it must be able to dissipate 100 x 0.47 = 47 watts of heat. Similarly, the voltage drop across the resistor for 10 A current is 4.7 V, which is close to 5V, and may not require additional amplification. What is your source voltage for 10 A current? The voltage drop across the current sensing resistor could significantly affect the output voltage in your case.

  • Very good. It was the research I was looking for.

    Do you have to use a rail-to-rail op amp?

    What is the correct op-amp to use? What IC is best for 5v and less than 1amp?

    Most grateful.

    • Rail-to-rail opamp is preferable. These days I am designing a power supply unit with voltmeter and ammeter features. I am using the same current sensing technique for measuring current up to 1 A. I am using LM324 (powered from 5 V) which does not provide rail-to-rail output but you can set the gain such as the output is around 3 V for 1 amp current and program your A/D conversion accordingly. I will post it soon.

Leave a Reply to Tobias Cancel reply

Your email address will not be published. Required fields are marked *