Controlling Two LEDs with Center Tapped Pot

Thread Starter

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 :p

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.
 

Thread Starter

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 :p
 

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
compare adc to 512
if adc value is lower go to "rotate_adc"

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_adc:
rotate left adc so the range 0 - 511 becomes 0 - 1023
----

tadaaaa
 

Thread Starter

jwilk13

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

----
activated led is led_1 by default
compare adc to 512
if adc value is lower go to "rotate_adc"

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_adc:
rotate left adc so the range 0 - 511 becomes 0 - 1023
----

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

Thread Starter

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.
 

Thread Starter

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 :p

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:
----
get the adc reading
compare it to 512
if it's the same or higher goto is_led_2

is_led_1:
active led is led 1
rotate left adc
two's complement adc
return to main
//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
substract 512 from adc
rotate left adc
return to main
//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!
 

Thread Starter

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 :)
 

Thread Starter

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:
----
get the adc reading
compare it to 512
if it's the same or higher goto is_led_2

is_led_1:
active led is led 1
rotate left adc
two's complement adc
return to main
//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
substract 512 from adc
rotate left adc
return to main
//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!
I'm back to working this through, and I'm doing it similar to the way you described here. It's a much more efficient way of doing it compared to how I was. My method required a lot of division and floating point math to get the resolution halfway decent. This resulted in a VERY slow program. With your method, however, everything is done with bitwise operations, making it much faster while still giving good resolution.

Great suggestion, thanks for the post.
 
Top