Controlling Two LEDs with Center Tapped Pot

Thread Starter

jwilk13

Joined Jun 15, 2011
228
Hey all,

A while back I posted about some center tapped potentiometers I came across, and I'm finally getting around to using them. I'm trying to use the configuration shown to control the brightness of two LED's with PWM. The two LED's are each on a different output (RC2 and RD5 on a PIC18F45K20) with the switching being done by high-side MOSFETs...but that's not important right now :p

My main issue is coming up with a way for the microcontroller to be able to tell the difference between turning the potentiometer CW and CCW. Details: when the potentiometer is at center, the voltage at the wiper is zero. When the shaft is rotated CW, the voltage increases from 0 to 3.3V. The same thing occurs when the shaft is rotated CCW. This analog voltage is being read through the A/D of the PIC, which controls the duty cycle of the output PWM.

So, the problem is that the PIC doesn't know whether to output to RC2 or RD5 because the input voltage at the A/D varies from 0-3.3V no matter which direction the pot is turned. Does anyone have any ideas on how to make the PIC differentiate the two?

It may seem like a trivial task, but it's a task nonetheless :). I think I'm having one of these moments :p

 

SgtWookie

Joined Jul 17, 2007
22,230
Connect one end of the pot to an I/O pin that you can set to either 0v or 3.3v.
Toggle the I/O pin for the uC to tell which end the wiper is on.
 

Thread Starter

jwilk13

Joined Jun 15, 2011
228
Thanks Sarge,

Just to make sure I'm understanding so far, I would take one end of the potentiometer that is currently always at a logic HI and connect it to an I/O pin instead? I'm still a little unsure how toggling the pin would show which direction the potentiometer was turned.
 

SgtWookie

Joined Jul 17, 2007
22,230
1) Have the I/O pin high.
2) Take a reading from the ADC. If it's zero, you're done.
3) Lower the I/O pin to 0v.
4) Take another reading.
5) If the reading changed from the prior one, you're on the side of the pot attached to the I/O pin; otherwise not.
 

Thread Starter

jwilk13

Joined Jun 15, 2011
228
Thanks Sarge, I think that should work. I wasn't sure what you were saying before, but now I get it. With only one side of the pot at 3.3V, you will only get voltage at the wiper if the potentiometer is rotated in that direction. Simple, yet should be effective. Thanks again.
 

BMorse

Joined Sep 26, 2009
2,675
or, connect center tap to IO pin, connect one leg of pot to +, and the other to -, when wiper is in center position, you should have 1/2 the 3.3V, from there you should be able to tell when it is turned in either direction from the input level..... the way it is wired now seems that if pot is turned to one extreme or another, it would just short out Vcc to Gnd :eek:
 

Thread Starter

jwilk13

Joined Jun 15, 2011
228
True, and I did try that :)

The only problem with that is doing the math in the microcontroller to set the duty cycle. The CCPR1L register is the main one controlling duty cycle, and re-scaling the A/D values to correspond to CCPR1L register values results in a significant loss of resolution. For example, when I did it that way the duty cycle would jump from 0% to 5%, then 5% to 8%, then 8% to 15%, etc. Here's why I think it happened:

When the pot is at center, the A/D value is approximately 500. When turned one direction, this increases to 1023. When turned the other direction, this decreases to 0. I basically tried to scale these numbers to something that would fit nicely in CCPR1L. so like this:

500 -> 1023 becomes 0 -> 1023
500 -> 0 becomes 0 -> 1023

The formula required to do this results in a pretty big loss of resolution. There's probably a better way of doing it, but I haven't figured one out yet. Thanks for the suggestion though :)
 

djsfantasi

Joined Apr 11, 2010
9,156
I suspect that the loss of accuracy that you are seeing has more to do with the accuracy or resolution of the potentiometer than with the scaling formula for the ADC values. You have approximately 500 units on either side of center, which mathematically implies .2% resolution (1/500). Ideally, you should be able to convert up to 1% resolution to reduce jitter.

I wouldn't scale from 500-1023 to 0-1023, but convert 500-0 and 500-1023 to 0-500 and use this value (a range of 500 units) instead. That is what I meant by you should be able to calculate with .2% resolution.

I would test your potentiometer by placing one end at 0v and the other at 3.3v and capturing the results as you slowly rotate the knob from one end to the other. In a perfect world, you should see the values smoothly increase/decrease to/from 0 and 1023. In the practical world, there will be jumps and not distributed evenly.

In my case, I performed statistical analysis of the ADC results to determine the minimum resolution. For my application, much less resolution was required (20% was acceptable :) ) But I felt your pain while developing my application... http://forum.allaboutcircuits.com/showthread.php?t=44738
 

Thread Starter

jwilk13

Joined Jun 15, 2011
228
That makes sense, I'll check that out. The only thing that confuses me is I've used potentiometers in a similar manner to this where the duty cycle doesn't jump like it does here. The biggest change is the math I'm making the uC do. I'm still working on SgtWookie's suggestion as it turned out to be a little more modification to my original code than I thought would be the case. I'm debugging right now to figure out where my problems are, but I'll update when I get it working.
 

Thread Starter

jwilk13

Joined Jun 15, 2011
228
Thanks to everyone for the input; I'm almost there. It took me a bit to figure out that I wasn't using the pulse steering correctly on my PIC, but once I took care of that, the output switching works perfectly using (roughly) the logic that SgtWookie provided.

As it turns out, I had to do some scaling anyways because of the value in PR2. The good thing is I only had to scale in the positive direction, which made it relatively simple. A few more kinks to work out and it should be good to go :)
 

Thread Starter

jwilk13

Joined Jun 15, 2011
228
An interesting observation: The load that the potentiometer places on the digital output of the microcontroller results in ~0.4V drop at the pin of the pot. This means the A/D never reaches its max value, and limits my duty cycle when the pot is rotated in the direction of the I/O pin.

I'm thinking of just using a logic level MOSFET to switch 3.3V on and off to the potentiometer using the digital output of the microcontroller instead of direct from the output. Any thoughts?
 

Thread Starter

jwilk13

Joined Jun 15, 2011
228
I thought it was strange too, especially since those I/O pins should be able to source/sink 25 mA each.

Good thought on the P-channel MOSFET. I was going to use an N-channel (2N7002T) and just change my software around a bit, but why do that when I can high-side drive it. Thanks for that.

Just to make sure I'm on the right track, it's basically the same as using an N-channel but slightly different. I drew up something that I think would work. I'm just not sure about the resistance between drain and ground.

 

SgtWookie

Joined Jul 17, 2007
22,230
You've drawn the P-ch MOSFET upside-down. The source terminal should be on the top, not the bottom.

Change the 1 MEG resistor to 100k. You just need the pot connected to the drain.

You wouldn't have been able to use a 2N7002 unless you had a high-side driver to use with it; and those usually want more than 3.3v for Vgs.

I know there are some lower threshold P-ch MOSFETs around; I just don't have my reference stuff with me.
 

Thread Starter

jwilk13

Joined Jun 15, 2011
228
DOH! I just snagged that schematic symbol off the internet without even looking :p

I re-attached something else. What is the affect of changing that resistance value? I'm assuming it has to do with switch times based on the RC time constant formed by the resistance and the gate capacitance...? Also, is the reason for not using that drain resistor because the pot already has a resistance and path to ground?

Sorry for all the questions, but I might as well understand it while I'm at it :)

 

SgtWookie

Joined Jul 17, 2007
22,230
DOH! I just snagged that schematic symbol off the internet without even looking :p
Stuff happens. ;)

I re-attached something else. What is the affect of changing that resistance value? I'm assuming it has to do with switch times based on the RC time constant formed by the resistance and the gate capacitance...?
It's actually there in case you decide to remove the uC; and the resistor is supposed to go from the gate to the source terminal. I forgot to type that part in.
Also, is the reason for not using that drain resistor because the pot already has a resistance and path to ground?
There was really no purpose for the resistor except to turn the MOSFET off, but it should have been attached to the source terminal instead of ground.

Attaching it to GND would leave the MOSFET on by default if you removed the uC.
 

Thread Starter

jwilk13

Joined Jun 15, 2011
228
I also have a question about the 2N7002T and why that wouldn't work.

The datasheet lists the Vgs(threshold) at a max of 2.0V, but does that just mean that is when the device BEGINS to conduct, not necessarily that the device will be fully on? I recall reading about that somewhere but never fully understood it.
 

Thread Starter

jwilk13

Joined Jun 15, 2011
228
Stuff happens. ;)
Attaching it to GND would leave the MOSFET on by default if you removed the uC.
This is what happens when I don't work with P-Channel devices enough...I forget the basics :p A logic zero at the gate would turn the device on, unlike an N-channel device. New schematic shown...maybe for the last time...??



EDIT: This can't be right though...the potentiometer would always have 3.3V at the input whether the I/O is high or low, right?
 

SgtWookie

Joined Jul 17, 2007
22,230
I also have a question about the 2N7002T and why that wouldn't work.

The datasheet lists the Vgs(threshold) at a max of 2.0V, but does that just mean that is when the device BEGINS to conduct, not necessarily that the device will be fully on? I recall reading about that somewhere but never fully understood it.
Yes. The threshold voltage is where a certain amount of current will flow through the drain when a voltage is placed across the drain and source. That certain amount is much lower than the rated current of the device; 250uA is very commonly used.
 

SgtWookie

Joined Jul 17, 2007
22,230
This is what happens when I don't work with P-Channel devices enough...I forget the basics :p A logic zero at the gate would turn the device on, unlike an N-channel device. New schematic shown...maybe for the last time...??
For a P-ch MOSFET, it's off when Vgs=0, and begins to conduct when Vgs is between the min and max threshold values, and is ON when Vgs is the voltage specified for Rds(on). Vgs for P-ch MOSFET is specified with a negative number.

EDIT: This can't be right though...the potentiometer would always have 3.3V at the input whether the I/O is high or low, right?
If the MOSFET Vgs is 0v, there won't be any current flowing through the drain, so the pot will measure ~0v on that "side" of the center ground.
 
Top