Does Microcontroller read current values - Page 1
Forum section: electronics / general

author: ajoseph88
author_rank: Contributor
timestamp: June 25, 2015, 07:50:02 pm
content: I am using an ATMEL ATMEGA 324p. So, we use microcontroller's analog input pin to give an input voltage of 0 to 5V from sensors, etc (first, we scale down the voltage to 0 to 5V using voltage dividers, as microcontroller only accepts maximum of 5V)... what about current values... what if i am using a current transformer and need to give that input to microcontroller...And I have another doubt as well, what is AREF pin forShould we give an input of 5V to that pin compulsorily or what it is for.Suppose I have to compare two analog values of 0 to 5V (one is reference which is set by POT and another is coming from speed feedback) and based on the error of these two signals i have to control a variable... how should it be done...Thank youTime and effort spend for helping - Very much appreciated.

author: suicidaleggroll
author_rank: Super Contributor
timestamp: June 25, 2015, 08:12:32 pm
content: You'd pass the current through a low value resistor (sized appropriately for the intended current draw) and use the microcontroller's A/D to measure the voltage drop across the resistor, possibly with an amplifier in the mix to get more resolution.AREF is the voltage reference used for the A/D. If it's a 10-bit A/D, then it has 1024 steps between 0 and AREF. If AREF is 5, then the resolution is 5/1024 = 5mV and the highest voltage you can measure is 5V. If AREF is 2, then the resolution is 2mV and the highest voltage you can measure is 2V.

author: Jeroen3
author_rank: Super Contributor
timestamp: June 26, 2015, 05:41:55 am
content: Note that not all micros support AREF being far off from VCC supply.Also not that if you exceed the maximum pin ratings of an ADC pin (a little bit), you're most likely to kill it.Especially with shunts, you should provide protection circuitry with clamping diodes. As simple as bat54s or more complicated with tvs diodes etc.

author: ajoseph88
author_rank: Contributor
timestamp: June 26, 2015, 11:56:21 am
content: Thanks... My feedback signal to ADC might exceed 5 V , so a simple 5V zener diode protection will save the micro controller i think...Is there any micro controller which accepts 10 V as input to its ADC.... up to 10V....

author: Jeroen3
author_rank: Super Contributor
timestamp: June 26, 2015, 12:39:40 pm
content: Any ADC is capable of reading above it's reference voltage if you add a resistor votlage divider.Minimalistic implementation could look like this.Does not protect against transients.Depending on the impedance of the resistance divider and ADC input you might want to add a Voltage Follower opamp.

author: ajoseph88
author_rank: Contributor
timestamp: June 26, 2015, 12:51:26 pm
content: What you mean is the impedance of the voltage divider should be same as the impedance of input ADC, or else put a voltage follower op-amp to match the impedancesHow does a voltage follower op-amp match the impedance, can you explain a bit further please...

author: Jeroen3
author_rank: Super Contributor
timestamp: June 26, 2015, 01:12:09 pm
content: An ADC in a micro-controller is almost always a Successive approximation ADC. This means it relies on a internal sample and hold circuit.Each time you sample the signal, the sample and hold needs to recharge a small capacitor (pF scale). If the impedance (resistance) up to the ADC is too high, this capacitor is not able to fully charge, resulting in a lower measured voltage. Or, you'll not be able to charge it at a high frequency. Limiting you to a low sample-rate.With an high resistor value divider (mega ohms) you'll have low power dissipation in the resistors and lower capacitance filters, but you'll need a voltage follower opamp to buffer the ADC signal.Opamps (theoretically) require no current at the inputs, because their inputs are high-impedance.With an low resistor value divider you'll have higher power dissipation in the resistors, higher capacitance filter, but you can skip the opamp because there is enough juice left to power the ADC.You'll typically find a small capacitor near to the ADC input pin to help reduce noise and the above described impedance issue. With buffer this cap is small (pF), without driver this is often higher (nF) and part of a RC filter resulting in a lower sample-rate.Dave must have a video about this explaining it better. Maybe one of these?https://en.wikipedia.org/wiki/Successive_approximation_ADChttps://en.wikipedia.org/wiki/Sample_and_holdhttps://en.wikipedia.org/wiki/RC_circuitHow annoying the youtube links automatically expand to a youtube embed. How to prevent that?

author: suicidaleggroll
author_rank: Super Contributor
timestamp: June 26, 2015, 01:13:28 pm
content: Quote from: ajoseph88 on June 26, 2015, 12:51:26 pmNo, they should NOT be the same, far from it. The output impedance of the source should be several orders of magnitude lower than the input impedance of the ADC, to keep the ADC from changing the very voltage it's trying to measure. An opamp buffer is an easy way to accomplish that.

author: madires
author_rank: Super Contributor
timestamp: June 26, 2015, 01:22:03 pm
content: AREF can be either an external reference voltage or you could add a small cap to stabilize/decouple the selected internal voltage reference (1.1V, 2.56V or AVCC). And you should be aware that the 324 supports single ended and differential ADC input modes.The ADC (here: successive approximation type) needs a specific charge for the S/H cap. So the signal/source to be measured should be able to deliver enough current for a proper conversion, i.e. fast sampling. If the signal's impedance is higher than the ADC's you would have to wait until the S/H cap is charged. The source impedance for single ended mode should be about 10kOhms or less, but the ATmegas are quite tolerant. If your source impedance is much higher you can use the mentioned voltage folower to decrease the impedance.

author: nixfu
author_rank: Unknown
timestamp: June 26, 2015, 01:34:14 pm
content: Julian Llett has a great video explaining how this is done:

author: ajoseph88
author_rank: Contributor
timestamp: June 26, 2015, 01:45:03 pm
content: Thanks all... I think I got it... We use a buffer or voltage follower so that the output impedance is lower than the input impedance of the ADC pin. So my design is to get feedback signal (0 to 50 V), put up a low pass filter, and give that to ADC (0 to 5V). In between speed feedback signal and ADC input, so many smaller designs come, yeah... And also even when the signal is reversed (0 to - 50V), i should get the same positive output (0 to 5V) to ADC. So I'll try to design the circuit and post it here, so that you guys can check it and can improve the circuit.Can you suggest a good software, where I can simulate this circuit, software where I can use all these op-amps, transistors, etc for precise correction.

author: Jeroen3
author_rank: Super Contributor
timestamp: June 26, 2015, 01:59:55 pm
content: The input bandwidth of the ATmega324 is only 38.5KHz. And with differential mode only 4 KHz.The input resistance is 100 Mohm, is that for real?

author: ajoseph88
author_rank: Contributor
timestamp: June 26, 2015, 02:30:45 pm
content: @Jeroen3, just in the example circuit that you have provided with 10K and 3K resistors, the output impedance comes out to be 2.3 K (R1||R2)... And the input impedance of the op-amp is 10M, so is that fine... or do we need a buffer op-amp...I found this on the datasheet of ATMEL. "The ADC is optimized for analog signals with an output impedance of approximately 10 k? orless.:

author: Alexei.Polkhanov
author_rank: Frequent Contributor
timestamp: July 07, 2015, 06:40:09 am
content: There is a special kind of amplifiers called Current Loop Receivers - they are use in industrial automation and designed to convert standard 4-20 mA current signal to voltage with precision.I case if you need to read current signal from standard 4-20 current loop you may want to use a component made specifically for that like RCV420 from TI. And there are ADCs that read 4-20 directly.http://www.ti.com/product/rcv420

author: ajoseph88
author_rank: Contributor
timestamp: July 07, 2015, 12:18:18 pm
content: Perfect... That's good information...

author: Chris C
author_rank: Frequent Contributor
timestamp: July 07, 2015, 03:31:53 pm
content: One thing I don't see explicitly mentioned is that a current transformer is an AC device. It measures only AC current, not DC. And produces an AC output voltage. Unless you've centered the output signal in the middle of your ADC range (typically 0V-5V), the negative portion will go below 0V, get clamped by either the protection diodes you've added or those within the MCU's pin protection circuitry, and be unreadable.Here is possibly the simplest way to connect a current transformer to an ADC pin, and it is the method I'm currently using with success. I use a 3.3V PIC, but I've altered my schematic to be appropriate for a 5V AVR:L1: Represents the secondary of the current transformer (with the primary being the wire you pass through it to be measured, not shown).R1 & R2: A voltage divider, centering the voltage of L1 at 2.5V.My current transformer has a turn ratio of 1,000. So for every 1A of current in the primary, it produces 1A/1000=1mA of output on the secondary. To measure that with an ADC, I need to convert that current to a voltage, and that's done simply by putting a resistor across the leads of the current transformer secondary, R3. But what value does it need to be?The device under measurement is designed for up to 5A, and is fused at that. But it's possible it may go higher without blowing the fuse. And if it does I want to be able to detect that, without having the measurement clipped, so the MCU can shut down the device. So I allow measurement up to 10A. That reduces the resolution of the ADC by half, but that's acceptable to me.Time for good old Ohm's Law. Since the AC output from the transformer will be superimposed on the 2.5VDC voltage from the divider (R1/R2), it can swing up to +/- 2.5V in either direction before it exceeds the MCU's 0-5V range; so E=2.5. At 10A, the transformer produces 10mA; so I=0.01. We want to know R, so we use Ohm's Law form R=E/I, resulting in 250=2.5/0.01. R3 needs to be a 250ohm resistor. That's not a standard value, and I don't need exactly 10A range; so in reality I just use the closest value I have on hand, and adjust the ADC reading-to-current conversion code accordingly.For better accuracy, I use 1% tolerance resistors for R1, R2, and R3. And we're done. Well, sort of.Even though I've provided a 2X safety margin, Murphy's Law says that at some point I'll get a larger surge. Maybe one that will very briefly drive current up to 20A before the fuse blows - resulting in an theoretical output of 10V (or -7.5V). In the real world it will actually be less, since my current transformer is only rated for 5A, its output drops off above that. And the MCU's built-in clamping diodes should handle it, it's precisely their job to handle brief transients. But I want just a little more reassurance.At this point I could add two external clamping diodes, as  showed in his schematic.Instead, I added R4 (3.3K), which will reduce the peak current flowing through the MCU's clamps. At 10V, and assuming 5.7V clamping voltage, that'll be a mere (10-5.7)/3300=1.3mA through the MCU's internal clamp for a very brief period. Not as good as if I've provided external clamps, but enough to satisfy my paranoia.How did I choose 3.3K? Somewhat arbitrarily. I was already using 3.3K for R1/R2, so it simplifies construction to reuse the same value. I did at least make sure that the value is low enough that according to my MCU's documentation, it shouldn't interfere with the charging of the ADC's S/H capacitor. In case I'm wrong about that (or perhaps your AVR requires a lower impedance), I also added C1. Being several orders of magnitude larger than the size of the ADC's S/H cap, it will stabilize the voltage even if R4 is too large, so long as samples aren't taken at the ADC's maximum rate. Assuming you're measuring a common 50/60hz signal like me, you won't need extremely high sample rates anyway.Finally, R4/C1 form an RC filter. So in addition to providing extra protection (eliminating two external diodes), and impedance matching (eliminating an op-amp buffer), they also help to filter any high frequency noise picked up from nearby digital circuits, which improves ADC reading accuracy. Pretty good for just two extra components.Your specific requirements (protection, frequency, accuracy, etc) may differ from mine, and I don't consider myself a guru on this topic. So take my advice accordingly. And I welcome peer review of what I've presented.

author: ajoseph88
author_rank: Contributor
timestamp: July 07, 2015, 04:34:21 pm
content: very interesting... I will study and try to design your schematic and see how it goes...

author: Jeroen3
author_rank: Super Contributor
timestamp: July 07, 2015, 05:47:03 pm
content: Relying on internal clamp diodes is something I cannot recommend. They're not actually tested in productions, but "guaranteed" by design.Their ratings are often described as Injected current on 5 volt tolerant pins.Using them will generate a glitch on in-chip power rail. I'd recommend using external TVS/ESD diodes (not simple bat54s, they're too slow) is you expect lots of surges.Mains has lots of surges.Never apply above schematic with AC shunts, you need opto or galvanic isolation for that.It never hurts to lookup some application notes from good sources, such as TI, Analog Devices or Linear Technology. They often produce excellent application notes for specific (analog) topics.

author: Chris C
author_rank: Frequent Contributor
timestamp: July 07, 2015, 11:52:17 pm
content: One thing that may not have been clear from my description. The current transformer my schematic is designed for is a little one, just 1" wide and similar to this:It's a transformer, so it's galvanically isolated. Permanently soldered into the circuit along with the MCU, all of which is in an enclosure, so there are no ESD events to contend with. Any surges or overcurrents that occur on the primary are reflected on the secondary, but only up to a point. Beyond 5A, the core starts to saturate, and secondary response starts to drop off exponentially. Rise time is also limited by the frequency response of the core material. Plus fusing, the designed-in 2X safety margin, and the MCU's ability to shut down the power; all for something that should very rarely (if ever) happen. So my requirements for conditioning out-of-range signals are greatly relaxed, and that's why I feel using the MCU's internal clamping diodes is acceptable here. I am relying on them, but to do very little.**This wouldnotbe the case if I were using a big clamp-on current transformer, that could produce bigger surges on the secondary. Or if it's detachable from the interface circuit, since ESD could then occur. If either of those applied, yes, I would have included external clamping diodes!**If you want to see serious reliance on internal clamping diodes, check out Microchip App Note AN521 - which suggests one can implement a zero-crossing detector by wiring 110VAC to an MCU pin through only a resistor! I have no doubt it would work, but for multiple reasons, I'd never do that.

author: Jeroen3
author_rank: Super Contributor
timestamp: July 08, 2015, 05:33:37 am
content: Quote from: Chris C on July 07, 2015, 11:52:17 pmYou can just use an antenna wire and capacitive coupling to mains live. But a resistor might be more reliable. Those are thats nA or uA, not mA.Remember that every mW you put in the package limits your temperature range.