PIC 16F877A ADC problem (virtual gnd/gnd)

Discussion in 'Embedded Systems and Microcontrollers' started by alekvodnjan, Mar 18, 2015.

  1. alekvodnjan

    Thread Starter New Member

    Mar 18, 2015
    12
    0
    Hello to all of you!
    I need a little advice.

    I'm doing ADC with PIC16F877A of an AC signal that is comming from inductive proximity sensor.

    -First of all I have made an voltage divider to get +-12 V DC from 24 V DC adapter (needed for supplying OPAmps).

    -With Wien oscillator I have got an AC signal with peak amplitude 5 V and frequency 5kHz (needed for supplying inductive sensor).

    -Then with second half of Wheatstone bridge I get ballance between sensor and that bridge. Now I have differential AC voltage that goes from 0-50 mV.

    -I have amplified this voltage 50 times with instrumentation amplifier. Now I have voltage changing from 0 to 2.5 V AC.

    -With summing amplifier I added 2.5 V DC because microcontroler can't measure negative voltage. Signal of 2.5 V AC now swings around 2.5 V DC. Minimum peak of amplitude is 0 V and maximum 5 V. This is perfect because PIC's ADC is doing conversion between 0 and 5 V. I'm only interested in max amplitude so I made program in mikroC that's doing that. Max amplitude is changing between 2.5 and 5 V and I'm reading this change. Works very well in Proteus simulation.

    Conclusion: I have an 0-2.5 V AC signal with 2.5 DC offset that now must go to PIC's ADC.


    I have made a real circuit and measured values with oscilloscope and everything works fine. At the end of summing OP amp I get voltage that I want (I have measured this before connecting to PIC).

    Here is schematic:
    [​IMG]
    http://postimg.org/image/88m152mb7/

    But the problem begins when I connect output signal to PIC's ADC. When I measure input to PIC with multimeter I get constant DC signal of -3.5 V that is not changing like before did! I move sensor but nothing happens.

    Of course, I have measured this between Input and Virtual GND. Every time I start AD conversion with push button I also get, at the output to LCD, constant value that is not changing.

    Now I'm thinking. PIC is doing coversion between Vcc that is 5 V and GND. But my input signal is between opamp output and VIRTUAL GND!

    I tried to simulate this in Proteus and I was right. I can't do ADC if my signal that I'm bringing to PIC is tied to VIRTUAL GND and PIC to GND. When I make connection in proteus between VIRTUAL GND and GND everything works fine.

    But in real circuit I can't do that! One forum member told me that the main problem is that the virtual ground isn't designed to source or sink DC current. Loading it with the processor supply current would cause severe inbalance and he was absolutely right.

    What should I do now? How can I do ADC of an signal that is between output of my summing OP amp and VIRTUAL GND?

    Thanks!
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,387
    1,605
    There are huge problems in your power design. You can't have a "virtual" ground (which has nothing virtual about it) and another ground in the same circuit without shorting something or tying something to the wrong reference.

    Try running everything off two 9V batteries for a real ground ground centered between the two batteries and see if this thing works any better.
     
  3. alekvodnjan

    Thread Starter New Member

    Mar 18, 2015
    12
    0
    I understand. But if I do this with two batteries even then I'll have, for example, Wien oscillator in which output is AC current and PIC which is DC, on a same ground. Wouldn't that be also a problem?
     
  4. alekvodnjan

    Thread Starter New Member

    Mar 18, 2015
    12
    0
    And sorry, I forgot to mention that I must supply all circuit with 24V DC adapter because this is the task they (professors) gave to me. I don't know any other way to do this except the way I did it on schematic (and in real circuit of course). In the past I have done examples similar to that (pure DC or pure AC circuit) but this is the first time I have to mix AC with DC component with the same power supply.
    You also told me about 'virtual'. This is just a name I gave to that I'll say 'refference'.
     
    Last edited: Mar 18, 2015
  5. Papabravo

    Expert

    Feb 24, 2006
    10,140
    1,790
    If the only way you know how to do something does not work, then your professor is going to give you a well deserved `F' for a grade. You should not be whining to us for a solution; you need to get on the stick and solve the problem, just like you would have to do if your job depended on it. It just might some day.

    BTW voltage dividers do not create plus and minus supplies from a unipolar supply.
     
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,387
    1,605
    Quite the opposite: currently you have two grounds (using the term as a reference point) that are at least 12 volts apart. How much voltage is measured between "ground" to "virtual ground"?

    You want both the DC and the AC sides to be referenced to the same (ground) point. That will probably be what you currently call "virtual ground".

    I gave the battery example as a quick and dirty way to try the circuit out. There are many ways to get a +/- supply out of a single supply; the method you used is not working. You would be better served by using the 24V in to make 12V and 5V, and then take the +12V to make a -12V with a Switched Capacitor Regulated Voltage Inverter (Google it).
     
  7. alekvodnjan

    Thread Starter New Member

    Mar 18, 2015
    12
    0
    Thank you for your answers. Really appreciate it. I have little language difficulties in matter of technical terms. I'm not using right ones (like virtual or so). I'll try your solution. Also between GND and VGND is about 12V as you told me.

    Papabravo I presented and explained my schematic which I have made from beginning to end. I wrote which problem I have with it. I came to this forum to see if someone can tell me in what way I can improve my schematic. I'm not asking people to make me a whole new schematic or something like that. I'm not whining 'cause I'm only asking for an advice and I think forum uses for that purpose. Yes, you were right. The only way I know in not working and that is not good so I need a new way. I didn't tell that I MUST use the only way I know. I only told that I have to get +-12V from one adapter and my knowledge is limited to only one solution I presented on schematic. I came to this forum to get advice on other solution.
    I don't know why you have to tell me about F grade, and my job or things like that. We are here to help each other and every time we learn something new through consultation and coversation with others. In generall terms I did a very big mistake but, well this is first time I faced this kind of problem. There is nothing wrong with that. At some point, we always must start from zero if we are lerning something new. It's nothing wrong to ask people for help. I really want to improve my knowledge. Sometimes is hard to go around the internet or search books for solution because I don't know where to start. Here in Croatia we don't have a single literature that is written in my native language. So it can be difficult trying to find solution on other languages. Because of that I want to talk to 'real' persons that can help me by directing me to some point. I did it wrong and ErnieM explained me what I did wrong and also gave me one of the solutions that I'll try. I asked only that.
    I'll be very thankfull if you also have some advice on how to change my schematic. We are here to talk and help each other.
     
  8. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,387
    1,605
    Papabravo tends to be a bit harsh in his advice, that happens on the interwebs. Do not take it personally. (Either of you!) :D

    Besides, using resistors to generate a plus and minus voltage is one acceptable way of working. It's big drawback is the waste of power it requires. I have not gone thru the numbers to check but if your digital stuff (processor, display, everything off the +5V power) is not too large you may get this to work by simply reconnecting the 5V ground to the virtual ground as these need be the same potential.

    I just noticed this is yout first thread, so welcome to the forums!
     
  9. alekvodnjan

    Thread Starter New Member

    Mar 18, 2015
    12
    0
    Thanks!
    Haha, maybe I took it too pesonal thou :). But everything is fine now. I'm not mad at all :). That was my moment of inspiration...
    I apologyze to Papabravo if I have somehow offended him.

    Interesting is what you say about waste of power with resistors. This explains the fact that resistors get very hot while operating.

    Hmmm I already thought about connecting VGND and GND together. But one guy told me that I quote: the main problem is that the virtual ground isn't designed to source or sink DC current. Loading it with the processor supply current would cause severe inbalance.

    Also what about if I put a single supply op amp instead of summing OP amp. Instead of using Virtual Ground for DC bias in summing opamp, what if I use actual GND (0V) and adjust the DC bias so that the output changes between 0 to 5V (in respect to GND) and bring that signal to ADC?
     
    Last edited: Mar 19, 2015
  10. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,387
    1,605
    That just may work. I would consider also putting a cap between U3-8 and R19 to toss out any DC imbalance from the now wildly different "ground" references.
     
  11. alekvodnjan

    Thread Starter New Member

    Mar 18, 2015
    12
    0
    Ok. What value should capacitor be? I have one 10 uF. Is that enough?
    I'm also thinking of putting diodes at VCC and VSS on PIC for protection of over voltage or reverse polarity
     
  12. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,387
    1,605
    The cap and R19 form an RC low pass filter. Pick anything making a lower break to pass your 5K signal.

    10uF sounds like an electrolytic. Look for something ceramic.
     
  13. alekvodnjan

    Thread Starter New Member

    Mar 18, 2015
    12
    0
    Ok. Yes this is an RC. I did not notice that because capacitor and R19 are in series before connecting to opamp.not as regular pictures of RC circle( second pin on resistor to ground).

    You mentioned before that I can use two nine volt batteries for supplying. this mean that I can also use dual voltage DC supply (Dc generator). What is the difference between voltage divider I created and dual supply with batteries.both must have ground in between if you know what I mean. Why then I get unstability with divider's ground connected to pic's ground and not in case with batteries.

    I'm just courious so I would like to know that. Where is the difference?

    ps: sorry for all this questions but I'm really unfamilliar with all this we are talking about... :)
     
    Last edited: Mar 19, 2015
  14. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,387
    1,605
    Look at your voltage divider up on the top left corner: Your analog stuff uses the center part you name "virtual ground" as their zero volt reference but then you nail the digital "ground" (another zero volt reference) to what the analog stuff sees as -12V.

    You need a single "ground" or zero volt reference. Mixing two like that leads to trouble, unless you can somehow make some DC isolation between them. A capacitor makes just that sort of DC isolation while allowing the AC signal to pass (and get re-referenced).

    When I say try batteries I wanted to change the digital ground point to their center. I was guessing you could get 2 batteries faster then you could get two power supplies.
     
  15. alekvodnjan

    Thread Starter New Member

    Mar 18, 2015
    12
    0
    Yes I understand that. Sorry I didn't explain my question very well.

    My question was this:

    My creation of voltage divider and VIRTUAL GND in between (not talking about whole schematic I posted, interested only in the first part without PIC's GND connected to anything) can't sink or source DC current and because of that I cannot directly tie PIC's GND to dividers 'VIRTUAL' GND but if I do like that I might get inbalance. But in case with batteries, if I have understood well, I can directly connect PIC's GND to GND created between batteries because in this case GND between batteries can sink and source DC current and I will not get severe inbalance like in the first case. So I wanted to know what is the difference between VIRTUAL GROUND in the middle of voltage divider and GROUND between two baterries.

    Second thing you explained to me is about capacitor. I'm not shure but I think this is called AC coupling. AC signal before capacitor swings around -12 V refference (as you have said, analog part of circuit sees GND as -12 V). After capacitor it'll swing around 0 V because DC component (-12V) will be filtered with capacitor. After that I'll rise up AC signal with summing opamp by adding DC component of 2.5 V (reffered to GND and not to ViRTUAL GND like I did on schematic). AC signal will now swing around 2.5 V (reffered to GND). I'll use single supply OP amp that work only between GND and +12V with reffence to GND and not to VIRTUAL GND.

    Please correct me if I'm wrong
     
  16. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,387
    1,605
    Divide and conquer is a very good strategy.

    A resistice devider is very limited. As a rule of thumb to do a good job it should waste a minimum of 10 times the current it suppplies. Do note it can both source and sink current, just not much of it. It is very good at things like making a "midpoint supply" on a single rail supply to to bias op amps such as you are doing.

    Batteries need not waste current to create or maintain their midpoint voltage: it exists there naturally.

    Yep, "AC coupling cap" is one good name for it as that is why you are putting it there.

    I'vr not develed very far into the analog side details but I would expect the signal to swing around the "virtual ground" voltage and not -12V. They certainly can't swiing below the -12V anyway.

    Looking again the op amp side there needs to be a resistor to ground (not virtual) to reestablish the ground into the signal. While the amp as is has a nice defined input resistance the DC path is not there, and input offset currents from the amp can drive the cap to either rail, not a good thing.

    But here's another thing to consider: you are trying to move the peak sensor signal up to 5V to get a good reading at the PIC. You don't really need anything else out of that signal. To make your measurement you need to do several fast samples and evaluate them for a peak. As an alternative a peak detecting op amp circuit will always provide this signal, though it is a bit delayed due to the nature of how peak detector work.

    Another alternative is to dump the level translator altogether: the signal is going to go -2.5 to +2.5V. You can get the PIC to clamp off the negative side via the internal EDS diode and some series resistance. To get back the 5V peak just increase the gain of the instrument amp.
     
    • rc.gif
      rc.gif
      File size:
      2.7 KB
      Views:
      25
  17. alekvodnjan

    Thread Starter New Member

    Mar 18, 2015
    12
    0
    Your explanation is really really good. I can't thank you enough. You were patient and explained all I have asked even though I had lot of questions.
    I'll put two clamp diodes on input of ADC. One tied to VSS and one to VCC to have more robust protection from negative or over voltage. Also with this diodes I'm not risking ADC or PIC to be damaged. Your advice is really good because I'll have better ADC by using all 1024 levels between 0-5V and not only half.

    Now I'm wonderring. So if RC filter brings my signal from VGND to GND I don't really need opamp after that do I? Can I bring signal dirrect from RC filter to ADC? I wanted to use opamp to add DC component, and now with clamp diodes I don't need that. Or, I must have opamp even without using it for adding 2.5V because eventually it plays some rule in this AC coupling?
     
  18. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,387
    1,605
    If you look at Figure 4.1 of that PIC's data sheet you will see a note those diodes are already inside the PIC; I've even used them to clamp a signal that was literally the full AC line when working on a light dimmer (but as that was a direct from AC line project discussing it further here is a TOS violation). Microchip has slightly backed off from a full endorecement of this practice, though their main objection seems to be "we might take them out of some future product we claim to be 100% comparable."

    So adding diodes is somewhat redundant, and you cannot be sure the external or the internal diodes are actually doing the clamping unless the external ones are Schottky types.

    That's just what I was trying to say: the best part to use is the one you don't put in at all.
     
  19. alekvodnjan

    Thread Starter New Member

    Mar 18, 2015
    12
    0
    I see.
    You know, people are always giving me advice not to bring negative or over voltage to PIC's ADC 'cause certainly but CERTAINLY I'll damage PIC. Due to that I've been very carefull and taking all the precautions.

    As I can see on your schematic you posted, R21 is 10k. PIC datasheet recommends input impedance not to be over 2.5k. So I'll rather put 1 k resistor or rather stick to 270 ohms I have used until now.

    I suppose that 0.1 capacitor means 0.1 uF.
     
    Last edited: Mar 21, 2015
  20. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,387
    1,605
    Huh. You're right about 2.5K. I must have misread it when I looked this morning.

    You are not really overdriving a pin when you use the ESD diode to clamp the voltage, as long as you limit the current.

    And yeah, cap is in uF.
     
Loading...