Controlling Two LEDs with Center Tapped Pot

Discussion in 'The Projects Forum' started by jwilk13, Sep 21, 2011.

  1. jwilk13

    jwilk13 Thread Starter Member

    Joined:
    Jun 15, 2011
    Messages:
    228
    Location:
    California
    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

    [​IMG]
  2. SgtWookie

    SgtWookie Expert

    Joined:
    Jul 17, 2007
    Messages:
    22,094
    Location:
    In the vast midwest of the USA; CST
    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.
  3. jwilk13

    jwilk13 Thread Starter Member

    Joined:
    Jun 15, 2011
    Messages:
    228
    Location:
    California
    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.
  4. SgtWookie

    SgtWookie Expert

    Joined:
    Jul 17, 2007
    Messages:
    22,094
    Location:
    In the vast midwest of the USA; CST
    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.
  5. jwilk13

    jwilk13 Thread Starter Member

    Joined:
    Jun 15, 2011
    Messages:
    228
    Location:
    California
    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.
  6. BMorse

    BMorse Senior Member

    Joined:
    Sep 26, 2009
    Messages:
    2,673
    Location:
    Vermontville, Michigan (GMT -5)
    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:
  7. jwilk13

    jwilk13 Thread Starter Member

    Joined:
    Jun 15, 2011
    Messages:
    228
    Location:
    California
    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 :)
  8. djsfantasi

    djsfantasi Well-Known Member

    Joined:
    Apr 11, 2010
    Messages:
    1,146
    Location:
    Northeast US
    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
  9. jwilk13

    jwilk13 Thread Starter Member

    Joined:
    Jun 15, 2011
    Messages:
    228
    Location:
    California
    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.
  10. jwilk13

    jwilk13 Thread Starter Member

    Joined:
    Jun 15, 2011
    Messages:
    228
    Location:
    California
    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 :)
  11. jwilk13

    jwilk13 Thread Starter Member

    Joined:
    Jun 15, 2011
    Messages:
    228
    Location:
    California
    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?
  12. SgtWookie

    SgtWookie Expert

    Joined:
    Jul 17, 2007
    Messages:
    22,094
    Location:
    In the vast midwest of the USA; CST
  13. jwilk13

    jwilk13 Thread Starter Member

    Joined:
    Jun 15, 2011
    Messages:
    228
    Location:
    California
    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.

    [​IMG]
  14. SgtWookie

    SgtWookie Expert

    Joined:
    Jul 17, 2007
    Messages:
    22,094
    Location:
    In the vast midwest of the USA; CST
    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.
  15. jwilk13

    jwilk13 Thread Starter Member

    Joined:
    Jun 15, 2011
    Messages:
    228
    Location:
    California
    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 :)

    [​IMG]
  16. SgtWookie

    SgtWookie Expert

    Joined:
    Jul 17, 2007
    Messages:
    22,094
    Location:
    In the vast midwest of the USA; CST
    Stuff happens. ;)

    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.
    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.
  17. jwilk13

    jwilk13 Thread Starter Member

    Joined:
    Jun 15, 2011
    Messages:
    228
    Location:
    California
    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.
  18. jwilk13

    jwilk13 Thread Starter Member

    Joined:
    Jun 15, 2011
    Messages:
    228
    Location:
    California
    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...??

    [​IMG]

    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?
  19. SgtWookie

    SgtWookie Expert

    Joined:
    Jul 17, 2007
    Messages:
    22,094
    Location:
    In the vast midwest of the USA; CST
    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.
  20. SgtWookie

    SgtWookie Expert

    Joined:
    Jul 17, 2007
    Messages:
    22,094
    Location:
    In the vast midwest of the USA; CST
    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.

    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.
Similar Threads
Forum Title Date
The Projects Forum Controlling lamps/LEDs with transistors Aug 4, 2012
The Projects Forum General Electricity Questions and Controlling LEDs with Parallel Port Sep 19, 2009
The Projects Forum Controlling Multiple Solenoids by varying voltage Aug 5, 2014
The Projects Forum Controlling Fans' speed with potentiometers! Jul 22, 2014
The Projects Forum Need help with relay for controlling electric projector screen, schematic included May 11, 2014

Share This Page