# PWM nonlinearity simulate this circuit – Schematic created using CircuitLab

I am using the PIC16F1786 in a current transducer to measure 0-1 Amp DC. I use an A/D converter to measure voltage from a Hall Effect and along with some software calculations use the Programmable Switch Mode Controller to produce a 12-bit PWM. Using that PWM to produce a final output of 0-5VDC.

I use software to calibrate two end points at 0 Amp DC input and 1 Amp DC input to create a y = mx + b equation to map my A/D readings from the Hall Effect to my PWM output.

Near my end points of calibration my final output is exactly where I expect it to be, however in between seems to exhibit some nonlinearities. I have double checked my mapping from A/D to PWM and it seems correct.

What I am seeing is, if using my two calibrated end points and I find the exact mid-point I would expect to see exactly (or pretty darn close) to half my Final Output. However, I am seeing between 20-25mV higher than I am expecting. Unfortunately this needs to be a high precision product and this is too large of an error.

Is there any known issues with PWMs being slightly non-linear? Everything from my oscilloscope readings tells me that it is a perfectly square waveform. Or perhaps my Final Output stage may be slightly off? All of my components are 1%, would it be worth trying to use 0.1% resistors?

• I'd suspect the analog parts first. Have you considered offsets in the opamp? The 358 is far from the worst opamp you could use, but I don't think it is often used together with the words "high precision." – JRE Jul 26 '18 at 15:18
• I have also experimented with the OPA2227 and had similar if not the same results as with the LM358. – Swinee16 Jul 26 '18 at 15:24
• I think you should start by setting precise digital inputs to the PWM (forget the ADC) and see what you get from the analog output. Make measurements at 0/20/50/80/100%. That will tell you more than we can guess. Resistor tolerance basically has no effect on linearity, but saturation does and perhaps there are dynamic effects. You did not mention the op-amp supply rails and actual desired output voltage limits. That would be good to know. – Spehro Pefhany Jul 26 '18 at 15:42
• INL and DNL of your ADC may acount for three LSB error. What is your 25 mV error in terms of peak full scale output and zero i.e. give absolute numbers because 25 mV means nothing. Also why are you using an op-amp gain of MINUS 5? – Andy aka Jul 26 '18 at 15:52
• Andy had made a similar remark and I think you are both right, I think it has to do with a small change in the power supply. It does seem to slightly vary over the duty cycle – Swinee16 Jul 26 '18 at 20:29

## 1 Answer The INL and DNL error for your ADC could account for an error of 3 LSbs. Given that you might be using the 10 bit ADC, and may have (say) a reference span of 5 volts, the real voltage error could be as high as: -

3/1024 x 5 volts = 14.6 mV. Given that your op-amp has a gain of 5 this could be seen as a value as high as 73 mV.

If you are using the 12 bit ADC configuration then the data sheet for the PIC does not give maximum values for DNL and INL but, my estamate based on typical values would be about half of the 73 mV potential error.

What is ADC INL: - It stands for Integral Non-linearity. DML is similar except it's more related to bit errors like this: - ## EDITED SECTION

Also, your PWM signal may be causing this error. It appears that your PWM output is bipolar in nature capable of highs of +2.5 volts and lows of -2.5 volts? If so then how good is the PIC at controlling the amplitude of those limits. That is to say, how matched are they as PWM voltage levels? Any duty cycle related changes in amplitude could be a big source of error.

I think you might have it Andy, I took a look and it looks like they vary just enough that it is causing problems. Which is leading me to believe my Vref+ and Vref- are not as constant as I thought.

• I can see how this causes issues with my ADC,which I will definitely need to revisit. But if I disable my A/D and just assign fixed numerical values to my PWM Duty Cycle for testing purposes. I still see an issue. I am reading my "Final output" with a digital multimeter not with an A/D, I don't know if that was causing confusion, and my bad. The A/D is part of another part of my circuit which is not shown in my original post. The 25mV i was mentioning before is the difference between what I should theoretically be getting and what I actually am reading from my digital multimeter – Swinee16 Jul 26 '18 at 17:06
• You said the ADC was measuring the Hall effect sensor and that the ADC result was defining the PWM mark space ratio yes? I’m unsure what the “other” circuit is and only assumed the signal string I’ve just stated. – Andy aka Jul 26 '18 at 17:57
• Please also explain why you are using an inverting virtual earth amplifier and how you generate a PWM waveform that can produce a positive output voltage on the opamp output. This is next on the list because I smell problems of linearity here too. – Andy aka Jul 26 '18 at 18:03
• You are correct, ADC measures hall sense and PWM is calculated from that value. So an INL or DNL error might cause the PWM calculation to be off, I get that, makes total sense. But say I give my PWM a constant value of 3000 and as a result I get a voltage of 5.00VDC from my Op amp circuitry, then a PWM value of 1000 which gives a 0.00 VDC from my Op amp. I would assume then a PWM value of 2000 would give 2.50 VDC. However I get 2.52 VDC, this is the type of problem I was getting – Swinee16 Jul 26 '18 at 18:07
• To your second question, my PIC reference is a +2.5 and -2.5, total of 5V. That way I am able to produce both positive and negative values depending on the currents direction through a Hall Sensor. Hall sensor is also powered by +2.5 and -2.5. So it will able to indicate current direction. – Swinee16 Jul 26 '18 at 18:10