50KOhm Pot Angle Sensor:
Pete Sevcik discovered that the Bourns 3310 series potentiometers make great LEGO angle sensors. Pictured below is the 50KOhm linear taper 3310C-1-503 from Digi-Key @ $2.59. They are very small and you only need to drill out the hole in a Technic beam a little to make a mount for the sensor. I have soldered the leads of the pot to a cut electric plate so that it can be easily connected to the RCX. I connected the CCW lead and the wiper (center) lead together as shown with the dashed line in the circuit. This just makes the connection to the electric plate easier. To use the sensor the RCX is set to RAW sensor type.
In addition to the pot being very small the shaft is just the right size to create a friction fit with the 16 and 40 tooth gears as well as a few other Technic beams. The fit allows slippage only if the pot is forced past the end of its 270 degree travel.
Unfortunately the RAW RCX reading will not relate to the angle the shaft is turned in a linear way as shown in the plot below.
A little math will show us how to fix this problem in software. (I love fixing things in software.) First of all the RCX RAW reading is related to the sensor resistance by the following equation where X is the reading and R is the sensor resistance in Ohms.
X = 1023 R 10,000+R
The 1023 is the maximum RAW value and the 10,000 comes from the internal 10KOhm pull up in the RCX. Now what we really want is an equation for sensor resistance so we solve for R.
R = 10,000 X 1023-X
Now the pot resistance is linear with angle A. The following equation shows how at 0 resistance the pot is at 0 degrees, and at 50,000 (50KOhms) the pot is at 270 degrees.
A = 270 R 50,000
Ok, now we can substitute the equation for R above and get an equation for angle A in terms of the RAW value X.
A = 54 X 1023-X
This looks like we are done, but there is a problem. If you coded this with the integer math the RCX uses when X gets larger than 607 the result of will be larger than 32,768 and there would be an overflow. Instead of the above equation we will use the one below. It breaks up the multiply into two steps and prevents the overflow.
A = 2 | 27 X | | 1023-X |
The VB code fragment below implements the above equation with the nice results shown in the plot.
.SetVar 1, 9, 0 'x1=input with pot .SetVar 2, 2, 27 'x2=27 .MulVar 2, 0, 1 'x2=x2*x1 .SetVar 3, 2, 1023 'x3=1023 .SubVar 3, 0, 1 'x3=x3-x1 .DivVar 2, 0, 3 'x2=x2/x3 .MulVar 2, 2, 2 'x2=x2*2=angle in degrees
Click Here to Return to the Main Page