Help reading potentiometer, please!
AwesomeCronk
Posts: 1,055
I have been out of the spin game for quite a while. I am now trying to get my model train remote built, but have run into an issue. I want to use potentiometers to serve as menu selectors and a throttle knob. To get them working, I have breadboarded a circuit to charge the cap, wait for it to discharge through the potentiometer, then print out on the LCD how long that took. Attached is my code and a pdf of my circuit.
When I program the prop in RAM, I manage to get the LCD's backlight to come on, as it should. Elsewise, the display does nothing. I suspect it is getting stuck in the look or is printing and clearing too fast for me to see anything.
Prop Mini, Spin1
When I program the prop in RAM, I manage to get the LCD's backlight to come on, as it should. Elsewise, the display does nothing. I suspect it is getting stuck in the look or is printing and clearing too fast for me to see anything.
Prop Mini, Spin1
Comments
Could you elaborate on this?
He is saying that pin 17 is connected directly to ground at one end of the potentiometers travel. That may blow the pin when you set it as an output to charge the capacitor. Put a ~1K resistor between ground and the potentiometer terminal.
Like here: http://www.rayslogic.com/propeller/Programming/ADC.htm
I have since used the MCP3202.
Mike
An external ADC chip is probably always better.
But, for just reading a pot, this can work very well.
Only real drawback is that you have to calibrate the maximum and minimum positions then turn the reading into something like a 0..255 answer.
Remember, Spin functions do not have persistent variables so aidx must be global to the object.
There's an OBEX object that reads two from a single cog.
Advantages are:
Prop pins are input only, so very hard to make BIG SMOKE;
No need to calibrate or average readings, as a step is a step;
Upon reboot you can ensure the throttle comes up at zero reading, rather than having trains run away on you if people have fiddled with controls with the power off;
With more involved code you can implement velocity control, where not only the distance turned, but the rate at which you turn the control is relevant;
Menu selection can be rock solid, rather than risking dithering or needing to allocate hysteresis zones to avoid dithering; and
Pots can develop annoying, hard to diagnose, patchy response over time, while rotary encoders tend to work until they fail.
Disadvantages are:
You generally feel the steps as you turn rather than turning smoothly; and
You don't have the built-in positional feedback indication that you can have with the knob on a pot.
Simpler again is a pair of push buttons per control, but rotary encoders can include the debouncing provisions that you would need to add for switches, and I can understand the lack of aesthetic appeal with the push button approach.
I will likely have a system initialization like a drone remote to ensure that throttle is zeroed before sending the signals to the trains.
All of this, even the throttle lever, sounds like a good job for rotary encoders, rather than pots.
You can also get absolute encoders, but they tend to be significantly more expensive and the interface can require more pins.
Edit:
Something like this can give you the select button built in too.
People tend to believe a small DC motor is a linear device. Under load, it really isnt, especially at the low end. You may need to scale the speed commands by maybe 3x at the bottom end, and maybe 1x as you get near “full throttle” in order to yield an apparently linear relationship between throttle position and speed.
Sounds like a fun project! Do post your results!
I am thinking I want an RC decay circuit, like the one on page 139 of the book 'Programming and Customizing the Propeller Multicore Microcontroller'. I have attached a schematic of the circuit they use. Only difference is they have theirs rigged up to pin 18. My biggest holdout with this is that I am wanting to use a parallel LCD in the final product and keep my serial one for quick debugging, etc. That means I need to keep other pins to a minimum.
Suggestions:
-- insert 100 ohm resistor between pin and cap (limits pin current to safe level)
-- change cap to 0.01uF
-- change pot to 10K
Now the cap will fully charge in in about 5 microseconds, and discharge to the pin threshold (with the pot set to 10K) in under 100 microseconds.
Use good quality parts and then do a bit of empirical testing. You'll notice that at the end of the code (see read_pot() posted above) I do a bit of scaling so that the method returns 0 to 100.
Edit: A few minutes later...
I just opened the book you referenced and the circuit they suggest uses 10K and 0.01uF. They don't use the 100 ohm series resistor like I do. That's okay, you just have to make sure that you don't set the IO pin used HIGH and leave that way for very long, otherwise you could damage the output driver if the pot wiper is connected to ground. A 100 ohm resistor is cheap insurance.
Section 7 of the Prop 1 datasheet shows 40mA max per pin so 100 ohm limits the current to 33mA on that pin. Also, the datasheet lists a max current on the power pins of 300mA. The pin currents may need to be planned such that the total of pin currents don't exceed the max allowable for the chip.
@AC, You can use a parallel LCD display with three prop pins and a shift register chip. There are a few examples of this in the OBEX.
Whether you use the PropTool or not, it is a good thing to install. The help button will pull up multiple prop documents including the Datasheet, Prop Manual, Prop education kit manual and many other goodies. I still prefer to use BST for spin/PASM.