# Controlling Two LEDs with Center Tapped Pot

#### jwilk13

Joined Jun 15, 2011
228
I'm glad you're here to set me straight on this stuff...I'm starting to confuse myself

I'm going to grab my laptop and do some simulations just to make sure I have all of this understood. As always, thank you.

#### jwilk13

Joined Jun 15, 2011
228
That's odd. Your pot is 20k, so if center tapped, that would be a 10k load on your I/O pin for 330uA current - if the I/O pin could make it to 3.3v!
Let me smack myself real quick before I post again...*smack*

The pot isn't 20k...I forgot I replaced it after I posted that schematic. The 20k was a 3-pin potentiometer I was working with previously. It's a 1k pot, which still should only be 6.6 mA when the device is capable of up to 25 mA per pin. The limit of ALL pins (sink/source) is 200 mA, and I know I hadn't reached that limit yet because my power supply was only supplying about 20-30 mA.

Anyhow, when I replaced the 1k pot with a 10k pot (5k load on the I/O pin instead of a mere 500 ohms), The output voltage of the pin went up to 3.3V.

My apologies to SgtWookie, who faithfully helped me when the mistake was caused by me supplying incorrect information. With the number of silly mistakes I make, I'm worried one of these days you're going to get tired of helping me

#### vrainom

Joined Sep 8, 2011
126
The only problem with that is doing the math in the microcontroller to set the duty cycle[...]

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
It's actually quite easy, check this steps:

----
activated led is led_1 by default

else (if adc value is 512 or higher) activated led is led_2
and substract 512 from the adc value so range becomes 0 - 511

rotate left adc so the range 0 - 511 becomes 0 - 1023
----

#### jwilk13

Joined Jun 15, 2011
228
It's actually quite easy, check this steps:

----
activated led is led_1 by default

else (if adc value is 512 or higher) activated led is led_2
and substract 512 from the adc value so range becomes 0 - 511

rotate left adc so the range 0 - 511 becomes 0 - 1023
----

Interesting...I never thought of doing it like that. Good suggestion, I'll give it a shot and let you know how it goes.

#### jwilk13

Joined Jun 15, 2011
228
One thing I didn't notice about your solution before posting is that 511-0 has to become 0-1023, not 0-511, resulting in the same issue as before.

#### SgtWookie

Joined Jul 17, 2007
22,210
I was wondering when you were going to catch the loss in resolution.

#### jwilk13

Joined Jun 15, 2011
228
I was wondering when you were going to catch the loss in resolution.
It took me a full 8 minutes

I started working it out and realized it wouldn't work. I'm working something else out at the moment though that I don't think will require the "reverse" scaling (so to speak). I'm going to try to use the PWM as active low in one direction instead of active high, but compare to minimum values instead of maximum. This is using a standard 3-pin pot, by the way. My brain is telling me it should work, but I won't be entirely convinced until I see that beautiful PWM signal vary just like I want The direction corresponding to 512-1023 will still be active high, and like you said, there is still going to be some loss in resolution with the scaling involved, but that should be acceptable.

#### vrainom

Joined Sep 8, 2011
126
haha sorry, I actually was thinking about it too but it's eeeeasy with uc to do all that stuff and more. But you see, I usually just answer with a general idea and hope the recipient develops it to their need.

Look:
----
compare it to 512
if it's the same or higher goto is_led_2

is_led_1:
active led is led 1
//this would make the lower half of the pot to control led 1 with 0v being full scale and 1/2 vdd being off

is led_2:
active led is led 2
//this would make the higher half of the pot to control led 2 with 1/2 vdd being off and vdd being full scale
----

The range would actually be 0-1022 but come on seriously it's only one bit to full scale and in 2 bits steps. 512 brightness steps instead of 1023 for a led is not at all noticeable!

#### jwilk13

Joined Jun 15, 2011
228
Good solution, vrainom.

As I mentioned before, I went with active low PWM in one direction and compared it to minimum values instead of maximum values, resulting in pretty much the same thing you're describing. I got it working (with a 3-pin pot, not the center tapped one) and I did end up using some of your logic in the process, so thanks to all who contributed

#### jwilk13

Joined Jun 15, 2011
228
haha sorry, I actually was thinking about it too but it's eeeeasy with uc to do all that stuff and more. But you see, I usually just answer with a general idea and hope the recipient develops it to their need.

Look:
----
compare it to 512
if it's the same or higher goto is_led_2

is_led_1:
active led is led 1
//this would make the lower half of the pot to control led 1 with 0v being full scale and 1/2 vdd being off

is led_2:
active led is led 2