Absolute value amplifier - Page 1
Forum section: electronics / projects

author: mitrynicolae
author_rank: Contributor
timestamp: March 21, 2018, 08:25:45 pm
content: Hello EEVBlog members,Here is what I want to achieve:I want to build a battery charger regulator using the ATMEGA 328 P microcontroller. For current sense I have chosen the ACS712 20 amp. I have designed, build and tested this regulator and it work fine. Now I want to add to it the MCP3425 ADC.The ACS712 current sensor can be used for DC and for AC signals (which is not my case). This means that for one direction of the current the IC will provide a voltage between 2.5 and 5 volts and for the other direction the IC will provide a voltage between 2.5 and 0 volts. This can easily be solved inside the microcontroller by using abs function (although loosing half of the precision). On my design, for the output stage I have used only the 2.5 to 5 volts part of the signal (since I am using only DC voltages) and I have amplified it to full scale (please check the attached image). This means that I now have 0 to 5 volts. This voltage Is supplied to the current regulator (as can be seen in the attached image). U19 is a 3x1 jumper that is used to switch between feedback mode and buffer mode for the amplifier and U35 is a 3x1 jumper used to switch between command input and an external 10 turn potentiometer.The problem:The issue is that as it can be seen from above the circuit is sensitive to how the current is feeded to the ACS 712 sensor. If the wires are connected the other way around the circuit will fail. In the same time even if everything work fine on this part the MCP3425 DAC have no way to read the signal. I want to use this chip for more precision.Now, I want to help me design an absolute value amplifier. This means that the amplifier should provide 0 to 5 volts regardless of the current direction.Restrictions:- the 2.5 volts mark should be translated on the output as 0v. This means that 5 volts will be translated on the output as 5 volts and 0 volts will be translated to output as 5 volts. (for more details please check the ACS 712 datasheet)- all the components that will be used in the design should use ONLY one supply (5v). Dual power supply designs will not be accepted since the current regulator design is based on a single power supply and changing this will be a hell of a work.- the design should use as few components as possible.- the components should be available on this site:www.tme.eu- the design should be tested or at least explained. Only plain text without schematic or only schematic will not be suffice. I intend to buy the specified components and try it.As a conclusion, regardless of how the current will flow through the sensor the output will always be between 0 an 5 volts. For the moment disregard the fact that the sensor only outputs 2.5 to 4.5 volts or to 0.5 volts. Also disregard the fact that MCP3425 only expects 2048 mV max on the input. I only need the requested solution since I am struggling with it for a long time without any results.Feel free at any time to contact me on mitrynicolae@gmail.com, on this forum or by private messages if you have any questions to ask.Thank you all for your time.P.S. the schematics are not complete (there is more that was not uploaded) and also the actual components that I used are not the ones from the pictures. I have added the pictures only to show you how I designed them, but please ignore the components or the fact that the designs are incomplete.

author: MasterT
author_rank: Frequent Contributor
timestamp: March 21, 2018, 09:43:54 pm
content: MCP3425 supports differential mode, with inputs common voltage Vss-Vdd +-0.3V. Plus it has PGA. I'd not use any external amplifier / conditional circuitry, just feed your ACS sensor output to one adc input, and 2.5 V offset voltage to another adc input. If you create 2.5V by two resistor divider from the same power supply as ACS is powered, it 'd additionally help to improve PSRR. Basically from data sheet of the mcp3425 fig 6-4(a) or 6-7, where instead of bridge only two resistors and ACS current sensor.

author: IDEngineer
author_rank: Super Contributor
timestamp: March 22, 2018, 04:20:44 pm
content: Seems like you're working WAY too hard to use the wrong part.The ACS line includes complementary parts, both bipolar and unipolar. You are using a bipolar part in a unipolar application. All you need to do is switch to the corresponding unipolar part and most of what you're fighting will fade away. You'll have a nice (roughly) 0-5V output - and you'll get double the sensitivity in the bargain.The parts are pin compatible. It's a drop-in solution.EDIT: I've done a bit of the legwork for you. You said "I have chosen the ACS712 20 amp". Check out the ACS723. It's available in 10A, 20A, and 40A unidirectional versions. The lower the amperage, the higher the sensitivity. Since you were using 1/2 the range of a 20A part, you must be happy with 10A full scale, and so using the (full part number) ACS723LLCTR-10AU-T will give you 10A full scale with twice the sensitivity (or twice the resolution in a given S/N environment) while completely eliminating the need for all of your "absolute value" analog processing.Here's the spec sheet:https://www.allegromicro.com/~/media/Files/Datasheets/ACS723-Datasheet.ashxHope this helps!

author: Zero999
author_rank: Super Contributor
timestamp: March 23, 2018, 12:10:24 am
content: This is really not the best way to do it. Adding an analogue circuit increases errors. You're better off sacrificing that additional bit of precision or using an ADC with more bits.For educational purposes, here's a schematic, showing how this can be done. It can quite likely be reduced to two op-amps, but I don't have time at the moment. I'll update this later.

author: IDEngineer
author_rank: Super Contributor
timestamp: March 23, 2018, 12:40:24 am
content: I agree, it would be better to reduce his analog component count. That's why I recommended the single-ended version of the current sensor he selected. It will give him more resolution, more sensitivity, and he can drop its output into the A/D. Firmware can handle the 0.5V offset at the bottom, and otherwise he gets almost the entire dynamic range of the A/D. All while reducing component count and complexity.

author: Zero999
author_rank: Super Contributor
timestamp: March 23, 2018, 09:54:26 am
content: I managed to reduce it to two op-amps.Yes, I agree there are better ways to do this and it's much better design practise to just get the correct components in the first place, rather than adding additional circuitry.

author: mitrynicolae
author_rank: Contributor
timestamp: March 23, 2018, 07:43:46 pm
content: Thank you all for your time.I will try to answer each one of you and also show you what I came with.@MasterT Even if the MCP3425 support a differential mode I still see some issues with this approach:1. the 2.5V should actually be seen as a "virtual ground". This means that from this point bellow we have a rise in amperage on the input and from this point above also we have a rise in amperage. This is a point that should be tested and maybe your are correct and my guess of how this will work are incorrect2. The MCP3425 is powered from a single 5v power supply. If on one input we have a "hardcoded" (I am actually a programmer, this is a hobby for me :p) 2.5V and on the other input we have 2V for example you state that the ADC will actually see a 0.5V on its input? Again this has to be tested (if its proven to be correct then I have to revise how differential amplifiers work).3. Even is somehow, this solution will work flawlessly and I can read the voltages correctly, on the regulator part I still get the reversed voltage. This means that at 2V instead of having 5A for example the regulator will see a much greater value and will shutdown the output.@IDEngineer when I initially started this project (around 3 years ago)(I know, I should get a life, but this is what I like to do in my free time) I saw that there are unipolar parts for current sensing. The issue is that as I said, this as a final product should be a battery charger. And as you can imagine there will be people that by mistake or intentionally will connect the charger in the reverse mode to the battery. Yes, in the current design this issue was overcome and I have mechanisms to stop and warn about this issue, but struggling so much by now to keep it backwards compatible with the first version made me search for a solution. Just for a brief story, I am currently on version 16.1.522. Out of these 8 versions were sent to China for PCB printing. As you can imagine it will be a very costly mistake to assume something. For this reason every version that I made have to comply with certain rules. 1. It has to be fully backwards compatible with the live version. 2 It has to fully fix the issues found in live version. 3. Optionally it can contain new features but these features must not alter the live version features. By applying these 3 rules I manage so far to get most of the versions working. (There were some exceptions were was my fault or the manufacturers fault, but this is another story). This is why I got to this version still using the analog inputs of the microcontroller. As you can see, for the moment I have a bug in the design that I am trying to fix, but this doesn't mean that the board cannot do its job.Long story short, I am aware of the unipolar sensors, but until I am not 100% that I cannot make what I want I will still try. (This does not necessarily mean to have a solution. If the solution cannot be implemented on the current board it is still no solution, an I am running out of space on the PCB).@Hero999 It is clear that you invest some time in the design and I really appreciate it, but I must disappoint you.As mentioned to @IDEngineer I am running out of space on the PCB. Currently I have room for placing 4-5 resistors (1206 package) and one 8 pin DIP amplifier. And no, I will not change the form factor of the resistors since all of them use this format. And no I will not use a SMD amplifier since this is an "internal" request that all the amplifiers, optocouplers, RTC and MCU should be placed in sockets. I did not tell you about this request because if we manage to find a solution and the solution will work perfectly (meaning it is not just a compromise solution) then I can change the specifications only for this amplifier. Again, changing even the form factors of the components at this stage of the project will be a hell of a work.My result was very close to Hero999 post but I have some doubts about it due to inconsistent wave form on output . @Hero999 if you have a better CAD, could you please help me and run a simulation on my design. As it can be seen from my simulation there are some points where the output drop correctly to 2.5 volts, but there are cases when it goes crazy.By the way Hero999 if you can design the amplifier to output 2.5 to 5 volts on output with fewer parts then that is also ok with me since I already have in place and amplifier that offset this voltage on 0 to 5 volts range.

author: Zero999
author_rank: Super Contributor
timestamp: March 24, 2018, 12:51:00 am
content: That won't work. The 0V reference to the precision rectifier is 5.14V, not 2.5V. It appears to work somewhat, because you've got the signal generator set incorrectly, probably 2.5V peak, on top of a 5V offset. The waveform looks crooked and poor quality, because the simulator's time step is too large.Is the 12V supply regulated? If not, you can't use it as a reference for the potential divider. You're better off using 5V instead.The circuit also has a high output impedance, equal to R1+R2, when the input signal is high, so some buffering is required, otherwise it won't properly drive an ADC. There's a fairly good description of the full-wave precision rectifier circuit on Linear Technology's website.http://www.linear.com/solutions/1608In my circuit, I reduced the gain of the precision rectifier to 0.5, so it would work off 5V. If the gain were 1, then it wouldn't work, since the diode prevents the output from going to 5V. For a gain of 0.5, I made R1 = 100k and R2 = 50k, but then it needs to drive a load impedance of R1 + R2 = 150k, otherwise when the input signal positive, it'll have a gain of one and when negative, a gain of 0.5 (refer to the Linear Technology linked I posted above and this should become clear). This is where R5 and R6 some in. They form a 2.5V voltage source, with an impedance of 150k (The output impedance of a potential divider is equal to both resistors in parallel). The voltage on R6 will vary from 2.5V to 3.75V, as the input swings from 2.5V to 5V or 0V. U2 is a non-inverting amplifier with a gain of 4 and some biasing to scale between 2.5V and 3.75V, to 0V and 5V. It's based on the application note linked below, with the component values tweaked, to save a resistor.http://www.ti.com/lit/an/sloa097/sloa097.pdfIf the op-amps are powered off 12V, then the circuit can be simplified. The precision rectifier now outputs between 2.5V and 5V. A non-inverting amplifier, with a gain of 2 and biasing, converts that to between 0V and 5V.If this is too big to fit on the PCB, I think you should reconsider doing this in software and sacrificing that one bit of accuracy.

author: MasterT
author_rank: Frequent Contributor
timestamp: March 24, 2018, 03:22:30 am
content: Quote from: mitrynicolae on March 23, 2018, 07:43:46 pm1. Yes, virtual ground 2.5V, current goes in one direction - voltage from sensor rising up, and current goes another direction - voltage falling down from 2.5V to 0. Actually, because you can't predict direction of the current, you have similar situation as with AC, instead of DC.2. Do a test. For programmer it's better to do a "rectification" in software. Get readings from ADC w/o current - store variable zero, than subtract from readings with current. Rectfication in software is simple abs() operation. What more, software could tell which way current flows. Hardware solution based on precision OPA rectifier would not be able to tell the difference.3. I do not understand.

author: mitrynicolae
author_rank: Contributor
timestamp: March 24, 2018, 07:48:25 am
content: @Hero999 Indeed you were right. I missed the fact that I have changed the power supply from a digital 5 volts one to a 12 volt battery.Also you were right about the time step. Anyway I have to disappoint you once again. The output of the amplifier don't look bad. It actually look like .... By the way, what program do you use for the simulations?@MasterT This is how the current circuit works: the voltage from the sensor goes to the MCU (either dirrectly, as currently implemented (and yes I already used the abs in code to solve the issue) or using the new solution through an ADC and then to MCU). In the same time the same voltage from the sensor goes directly to the output op amp which adjust the output based on the command specified by the MCU. This command is send through a DAC which has an eeprom inside it. This means that after power up the board and setting the command I can remove the MCU and the circuit should continue to work normally (exclude the part that if I remove the MCU while it is powered I could damage it; It is pure for explanation). In your case the issue is on the regulator part. What I want to implement is a custom solution that will convert the absolute value of 0 to 2.5 range to 0 to 5 volts range as beautifully described by @Hero999. This subcircuit will be placed right after the sensor but before the MCU or the regulator which means that both of them can benefit from the conversion. The @Hero999's solution is exactly what I need with one small issue. It uses too many components an I am running out of space.

author: mitrynicolae
author_rank: Contributor
timestamp: March 24, 2018, 10:03:32 am
content: I am starting to consider using an unipolar sensor. The adapter circuit is way to complicated and induce a great risk to the project. I have a question though for anyone who has used this sensor. IF by mistake I reverse the input wires, does the sensor gets damaged in any way or it only read 0.5 volts as minimum and everything else should be ok?

author: MasterT
author_rank: Frequent Contributor
timestamp: March 24, 2018, 11:19:35 am
content: Quote from: mitrynicolae on March 24, 2018, 07:48:25 amI see. I had a project with ACS712-5 and arduino UNO. From my experience, sensor is developed for AC current and has wide frequency bandwidth. So it's very noisy. Moreover, it's based on hall effect, so any wire in close proximity, or any magnetic material may shift offset voltage. Even orientation in Earth magnetic field is important.What I'm getting at, if there is MCU you could do oversampling (I run arduino at 10 ksps) to reduce a noise. Sigma-Delta ADC is also great for noise suppression. And MCU can do re-calibration during each boot-up or ones in a while to null out any other errors. If you are inclined for controller-less or analog solution, than you choose wrong sensor, you 'd not expect accuracy or stability in measurements results.I was able to get down to 10 mA (5A sensor) at the lower end only running sophisticated HPF (FFT based, so my project reports harmonics content of the current). W/o software processing minimum measurable value would be around +-50 mA (worse than 1% of scale).

author: IDEngineer
author_rank: Super Contributor
timestamp: March 24, 2018, 03:12:57 pm
content: QuoteIt will work just fine. The unidirectional parts work just as you've described. They are not damaged by current flow in the "wrong" direction. BTW, the current path through the package is rated for 5X overcurrent (in either direction).Quote from: MasterT on March 24, 2018, 11:19:35 amThe parts have a filter pin forexactlythis reason. Put a small cap on there and tune to the bandwidth you want. The spec sheet has complete details.Quote from: MasterT on March 24, 2018, 11:19:35 amThey offer versions that use differential sensingspecificallyto address this issue if you're operating in a magnetically noisy environment. I've tested them, they work exactly the way you'd expect. In a DC environment, with mindful PCB layout, it's quite likely you won't need CM rejection... but it's available if you do. If the earth's magnetic field is an issue, use a ferrous-metal-based enclosure. But I'd test that before worrying about it.Here's the webpage that lists all of these parts, you can just go down the list and shop for the features you want:https://www.allegromicro.com/en/Products/Current-Sensor-ICs/Zero-To-Fifty-Amp-Integrated-Conductor-Sensor-ICs.aspxDisclaimer: I'm not associated with Allegro. But I have used their parts, and have found they are remarkably useful. Sure, you can replicate their functionality in discretes, but unless you have some sort of very specific requirement you'll end up consuming more PCB real estate and very likely more money. This will be a much shorter thread and you'll be done a lot sooner if you just grab the part with the features you want and drop it on the board - you already have the footprint for it!

author: Zero999
author_rank: Super Contributor
timestamp: March 24, 2018, 09:47:12 pm
content: Quote from: mitrynicolae on March 24, 2018, 07:48:25 amI use LTSpice, which is freely available and in my opinion, the best simulator package. It does have a different user interface, compared to most Windows software, which some people dislike, but I found it fairly intuitive.I don't know why to think you're disappointing me.The output from your circuit, is not what you think. If you look at the oscilloscope trace closely, you'll see the output is positioned at -2.5 divisions and the scale is 2V/division, making the the centre line -5V, rather than 0V.You'll find that even if you alter the reference to the precision rectifier, the output voltage will still not be as per your specification. Look at the circuit I posted previously again, with the output of the precision rectifier plotted (blue trace). It ranges from 2.5V to 5V, not 0V to 5V, as you'd like.QuoteThose extra components are required to convert the output of the precision rectifier to 0V to 5V. There is room on the board for them. You just need to employ some tricks to get them to fit. The resistors can be mounted radially and you can get resistor networks, in SIP (Single Inline Package), which often have better matching, than discrete resistors and can save space.http://uk.farnell.com/bourns/4608x-102-104lf/resistor-network-100k/dp/9356533Quote from: mitrynicolae on March 24, 2018, 10:03:32 amGood idea. No it won't be damaged by reverse current.

author: mitrynicolae
author_rank: Contributor
timestamp: March 25, 2018, 08:03:27 pm
content: @Hero999 Only for educational purposes I have continued to investigate your design.I have replaced the diode with a schottky one (1N5818G) and half of the signal vanished and I don't understand why. Shouldn't a schottky diode have a lower forward voltage than a regular one?Oh, wait, I believe that it is exactly the cause ...

author: C
author_rank: Super Contributor
timestamp: March 25, 2018, 08:54:22 pm
content: Why make it hard?If you want to you could make a battery charger function which ever way it's connected to battery.Where you found the MCP3425 you can get a 4 channel version.Your dac could have a 4 channel version.Look around a bit for what you might think of as more expensive parts, then look at how much they save in total or how much better the final product is.

author: Zero999
author_rank: Super Contributor
timestamp: March 25, 2018, 08:57:05 pm
content: Quote from: mitrynicolae on March 25, 2018, 08:03:27 pmI'm glad you've realised this is not a viable option and yes, it is still worth investigating, for educational purposes.QuoteNo, it's got nothing to do with the forward voltage. The op-amp compensates for the forward voltage drop of the diode, by outputting a slightly higher voltage.The precision rectifier circuit works because the diode stops the op-amp's output from sinking current. For negative input voltages, i.e. when the input voltage is lower than the reference connected to the non-inverting input, the op-amp behaves like an inverting amplifier. The op-amp does what it can to ensure the voltages at both inputs are equal. As mentioned above, the forward voltage loss in the diode is compensated for. When the input voltage goes positive, i.e. higher than the reference, the op-amp tries to invert the signal, but it can't because the diode prevents current flowing into its output stage. The output slams into the negative rail and the diode makes the output of the precision rectifier circuit look like an open circuit. The input signal bypasses the precision rectifier circuit via R1 and R1, so the output voltage is equal to the input, as the input impedance of U2 is far higher than 200k and doesn't drop much voltage.The reason half the signal has vanished, when the silicon diode was replaced with a Schottky is because Schottky diodes have a much higher reverse leakage current, than silicon diodes. The Schottky diode allows enough current to flow in the reverse direction, for the op-amp to pull the voltage down towards 0V. Your oscilloscope trace doesn't look right though. The output should be zero, when the input is negative. Perhaps the input is inverted?Here's what I get when I simulate it with a Schottky, rather than a silicon diode. Because of the high leakage, the Schottky diode effectively conducts in both directions, when the impedances are very high. The output of U1, looks very similar to the output of the precision rectifier circuit. The diode is hardly doing anything!