Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Issues With PWM -> Low-Pass Filter -> ADC

Status
Not open for further replies.

theo92

New Member
I'm using a PIC uC to generate PWM signal (varying the duty cycle using two up-down push buttons) which powers up a small lamp. Then to measure the voltage and current by ADC of the uC.

I've used a NPN Darlington to switch the lamp. PWM freq. is 36KHz. To meaasure the voltage I've connected the lamp terminals to the ADC inputs using a passive RC low pass filter. the RC filter is made with 100K R and 10nF C. So, the cutoff freq. is nearly 160Hz.

I've connected the emitter of the NPN darlington TIP122 to ground through a 1Ω resistor. And measure the voltage drop using ADC through another RC low pass of same configuration.

The voltage measurement is quite accurate. It increases approx. linearly with increasing duty cycle.

But the problem appears in measuring the current (or the voltage drop through the resistor). The load draws 0mA to 500mA current, so I'd expect 0 to 0.5V voltage drop. Most of the time it shows correct values but sometime it doesn't. Suddenly the voltage drops. I'm showing a log of (logged through UART) voltage vs current(the voltage drop) here in this format {voltage, current}
{0.000, 0.05371},
{0.087, 0.08300},
{0.190, 0.10253},
{0.322, 0.12207},
{0.439, 0.13671},
{0.551, 0.14648},
{0.639, 0.16113},
{0.805, 0.17089},
{0.805, 0.18554},
{0.922, 0.19531},
{0.996, 0.20507},
{1.088, 0.21972},
{1.166, 0.22949},
{1.245, 0.23925},
{1.328, 0.28808},
{1.425, 0.16601},
{1.484, 0.26855},
{1.533, 0.27832},
{1.669, 0.28320},
{1.723, 0.35644},
{1.791, 0.30273},
{1.914, 0.31250},
{1.992, 0.31738},
{2.070, 0.32714},

Can you point out the fault? In RC filter or in ADC config? I think it's not an ADC fault because in most of the time it does measure correct one.

The swordfish code snippet is:
Code:
Function ReadVoltage() As Float     
result = ADC.Read(0) - ADC.Read(1)
result = result * 5 / 1024 
End Function

Function ReadCurrent() As Float
result = (ADC.Read(2) + 1)
result = result * 5/1024 
End Function
 
Last edited:
What kind of lamp is it? Could it be that the resistance of the lamp increases when it heats up and thats why less current is getting through it?
 
Last edited:
What kind of lamp is it? Could it be that the resistance of the lamp increases when it heats up and thats why less current is getting through it?

The lamp is an incandescent type, 4V, generally used in torch lights.

That's right. The resistance increases with increasing voltage. But see some sudden abnormal changes in current (In red) . It should never be like that.

{1.328, 0.28808},
{1.425, 0.16601},
{1.484, 0.26855},
 
Last edited:
Are those samples taken at constant time intervals? Replace the lamp with a resistor and see if the problem still exists.
 
Last edited:
Yes. Those were taken at constant time interval of 1 Second + ADC acquisition + AD conversion time.
I tried replacing the lamp with a resistor. The same problem persists. Though most of the readings are quite correct except two or three out of fifteen.

I forget to mention the ADC reference is set to Vdd. And a 0.1uF decoupling capacitor is connected in the ADC inputs pins.
 
Strange problem. The next thing I would try is to increase the low-pass filter capacitor and decrease the resistor value. Maybe 1k and 1uF. This way the analog pin leakage current causes less error.

EDIT: You could also take a look at the raw ADC values in case there is a problem with your math. Could be a rounding error etc. How does your compiler handle something like "result = result * 5/1024"?
 
Last edited:
Hello there,

I assume you are measuring current through the 1 ohm resistor.
What i see in the code is ADC.Read(1)-ADC.Read(0).
Are these two different ADC channel input pins (1 and 0 that is)?
If so, are they both filtered like the voltage measurement is with 100k and 10nf?
If you are making a differential measurement you need to filter both channels. If you are not doing that try a 100k and 10nf on both channels, with the caps connected to ground. I assume you have the 100k in series with the cap, take the ADC input from the junction, and other end of cap to ground for each filter.
It actually might be simpler to just use one channel instead of two also.

Oh i read your post wrong. I see you are using channel 2 for the current measurement.
In this case try connecting the cap ground right to the PIC ground and make sure that your PIC ground connects directly to the resistor ground also with no other connections to that lead. The other end of the cap should be close to the ADC input channel pin.
If that doesnt help, then the problem may be a dip in the supply voltage as the bulb is driven high. That might require some impedance isolation between the PIC power supply pin and the bulb positive power terminal, or perhaps an additional electrolytic cap right across the bulb-transistor-resistor power points.
 
Last edited:
I assume you are measuring current through the 1 ohm resistor.
Yes. I'm doing so.

What i see in the code is ADC.Read(1)-ADC.Read(0).
Are these two different ADC channel input pins (1 and 0 that is)?

Yes. I use ADC0 and ADC1 for voltage measurement. And ADC2 for current measurement.

If so, are they both filtered like the voltage measurement is with 100k and 10nf?
If you are making a differential measurement you need to filter both channels. If you are not doing that try a 100k and 10nf on both channels, with the caps connected to ground. I assume you have the 100k in series with the cap, take the ADC input from the junction, and other end of cap to ground for each filter.

Yes. All the channels are connected through the RC Low Pass filter.

In this case try connecting the cap ground right to the PIC ground and make sure that your PIC ground connects directly to the resistor ground also with no other connections to that lead. The other end of the cap should be close to the ADC input channel pin.

cap ground means? Which capacitor? The low pass filter capacitors? Actually, I've two power supplies. One 5V (regulated by LM7805) supply for the PIC and another 5V supply(regulated by 7805 too) for the bulb. The grounds of the both are connected together. I'll try isolating the two grounds. And also connecting the resistor ground directly to the PIC ground.

That might require some impedance isolation between the PIC power supply pin and the bulb positive power terminal, or perhaps an additional electrolytic cap right across the bulb-transistor-resistor power points.

I suspect it too. First I'd try adding an electrolytic capacitor to the transistor collector (where the +ve supply of the lamp enters), resistor, bulb points. Any idea for complete isolation? Op-amp voltage follower?

You could also take a look at the raw ADC values in case there is a problem with your math. Could be a rounding error etc. How does your compiler handle something like "result = result * 5/1024"?

It's quite simple. The documentation tells, it takes the value of result, do the operations and them assign it on the variable. As it is floating point, so the range is -1e37 to +1e38. Surely I'll take a look at the raw ADC data.
 
Last edited:
Hello again,


Yes the capacitor of the RC filter section for averaging the voltage across the sense resistor. That cap should go right to the PIC ground and right to the PIC input pin.

Let me see if i understand your setup completely here...

You have a 1 ohm resistor that is grounded, and the other end goes to the emitter of a transistor, and the bulb connects to the collector, and the other side of the bulb goes to the V+ of the bulb regulated power supply. The base of the transistor is driven from a PIC port pin through a current limiting resistor of say 1k to 10k. (Note the 1k to 10k resistor, if you dont have one now is the time to add that feature).
Anything wrong with that description?
 
Last edited:
I'm rebuilding it on a perfboard with a better layout. The previous layout had some terrible flaws. Meanwhile, here's the incomplete schematic.
 

Attachments

  • Schematic.png
    Schematic.png
    20.9 KB · Views: 410
Any better luck yet?
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top