Cheap and simple way to measure distance / absolute position with ~5mm accuracy? - Page 1
Forum section: electronics / projects

author: ndictu
author_rank: Regular Contributor
timestamp: August 03, 2017, 07:55:46 pm
content: Hey,I have a height adjustable desk which can be moved up and down to switch between standing and sitting. I like to do that at least few times a day so that I'm not sitting the entire time.The desk has two buttons - up and down - which move it while held. I would like to change the buttons to be the sitting and standing position and automatically move between them. The driving part is pretty simple - the buttons can be detached from the motor control box, I'll just throw an Arduino in the middle and replace the button press with hold.The tricky part is how to know what position the desk is at. The motors are just regular DC motors, not steppers, so no feedback there (I can't replace the motors unfortunately). Just sending the pulse for a constant amount of time is not good enough - it can vary by up to 1cm on the length of travel and also depends on weight so if I put things down on the table it goes up less. 1cm might not seem like a lot, but it is enough to make using keyboard/mouse uncomfortable. I would like to avoid getting RSI. I found that +- up to 5mm feels good enough. With short button pulses I can move the desk by about 2-3mm at a time.So I need to mount some kind of distance measuring thing on the desk. I found ultrasonic/laser distance sensors, but those with required precision are quite expensive and also if I were to put my backpack/something under the desk it might throw it off.I have considered some kind of strip with optical reader, based on Gray code. The range of travel is about 60cm, so 8 bits would be enough for 2.5mm increments. However I couldn't find any off-the-shelf solution, just rotary encoders. I thought about wrapping a string around a spring-loaded encoder and tying the other end to ground but that just seems silly...Do you have any suggestions on how to approach this?Are there any good, cheap linear distance sensors from ~60cm to ~120cm with at least 5mm accuracy?Thanks!

author: ebastler
author_rank: Super Contributor
timestamp: August 03, 2017, 08:09:01 pm
content: Do you really need to measure distance? Wouldn't two simple digital sensors which define the two end positions be enough? If those end stops can be mechanically moved/adjusted by the user, you could even change the desired target heights without programming or teaching anything.I am thinking about a mechanical (tactile) switch, light sensor, or magnetic sensor (reed contact or hall sensor) which travels up and down with the drive mechanism. Then mount two mechanical notches, light reflectors or interruptors, or magnets on the desk, at heights which are user-adjustable. Depending on how your desk is built, magnets might be easiest since they could hold themselves in place on a metallic leg?Kind of a low-tech solution... But if the design of your desk accommodates it, it may be simple, reliable, and actually quite user-friendly.

author: daqq
author_rank: Super Contributor
timestamp: August 03, 2017, 08:09:56 pm
content: QuoteYou can buy some cheap ones and calibrate them:http://www.dx.com/p/hysrf05-ultrasonic-distance-sensor-module-for-arduino-443899#.WYOB_4jyiMoAre you sure you are not overcomplicating this? From the description of the problem you don't really need to measure the exact position of the desk but rather switch between two predefined positions. A simple reflective photodector and two marked positions should do the trick just as well.

author: JXL
author_rank: Regular Contributor
timestamp: August 03, 2017, 08:43:39 pm
content: Quote from: daqq on August 03, 2017, 08:09:56 pmFor 2 predefined positions, daqq's suggestion is perfect.The system is likely using a screw drive. For variable and programmable positions, mount a 10-turn or 20-turn pot on the screw. Use the pot as a voltage divider and read the level (position) with your Arduino.

author: DaJMasta
author_rank: Super Contributor
timestamp: August 03, 2017, 08:44:11 pm
content: If you know that the drive for the motors are directly linked and isn't slipping, I think an encoder of some sort on the output is the most reliable way to go. You could even just through in a pushbutton to zero at some point and a simple quadrature output should give you very accurate feedback even if the motor control isn't as reliable.Ultrasonic is alright, but to get that fine of a resolution and that level or repeatability, you may need to get specialized transducers with temperature compensation or something.IR reflectance could be a good alternative to an encoder, as you could put something like one of those combined sensor modules and embed it in or partly covered by the legs of the desk, making it more immune to interference. At that point you'd need to calibrate the output to distance, but it should be reliable provided you're always shining on the same target surface.Depending on the construction of the desk, there could be a capacitive option too. Something like a plate attached to the top leg and a plate attached to the bottom leg, and as they approach each other and overlap, the capacitance goes up. You'd have to protect it and it would be susceptible to changes in environment so it could be difficult to stay really repeatable, but you could at least get good relative readings while you made the adjustment.Digital sensors, be it mechanical switches or whatever, of course, is the simplest option. If, for example, you made a mounting bracket to adjust the height of the switches, you could get an extremely repeatable two position sensor that doesn't even require any real logic to drive.

author: ndictu
author_rank: Regular Contributor
timestamp: August 03, 2017, 08:51:33 pm
content: Well yes, a simple piece of tape + light sensor is my backup plan. It should work once adjusted properly and hopefully only needs to be done once in a long while (eg. changing chairs).However I was hoping for some more versatile solution, possibly just for the fun and challengeQuote from: daqq on August 03, 2017, 08:09:56 pmWell the issue with the clutter under desk remains - with 30deg FOV from 120cm it captures about 60cm diameter. I would need something with much narrower FOV. Also even if I calibrate it, how stable it is with temperature change for example? I'm afraid ultrasonic isn't a way to go here...So far the gray code reader with a strip I print seems the best choice to me, but I can't find existing solutions and creating one myself is too much effort.

author: ndictu
author_rank: Regular Contributor
timestamp: August 03, 2017, 08:54:33 pm
content: Quote from: DaJMasta on August 03, 2017, 08:44:11 pmThat is a great point. As JXL said, screw drive is very likely. The mechanism itself is not visible, I'll have to see if there is an easy way to expose it. But if so and if I can mount an encoder on the output that would definitely be a great and accurate solution.

author: ebastler
author_rank: Super Contributor
timestamp: August 03, 2017, 08:56:31 pm
content: Quote from: ndictu on August 03, 2017, 08:51:33 pmWell, you got to make up your mind whether you want a challenge or want to build something practical!

author: ndictu
author_rank: Regular Contributor
timestamp: August 03, 2017, 09:04:17 pm
content: Quote from: ebastler on August 03, 2017, 08:56:31 pmThere comes a point where it is not worth itThe encoder on the motor shaft or something like that is still fun, but trying to build a reader more complex than the entire desk probably isn't.Thank you all for your input! I'll try to take it apart tomorrow and see what can be done.

author: Fungus
author_rank: Super Contributor
timestamp: August 03, 2017, 09:23:24 pm
content: Quote from: ndictu on August 03, 2017, 07:55:46 pmThose cheapass ultrasonic sensors are plenty accurate enough for that.http://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/

author: ebastler
author_rank: Super Contributor
timestamp: August 03, 2017, 09:29:32 pm
content: Quote from: Fungus on August 03, 2017, 09:23:24 pmYes, but -- as discussed back and forth in the above posts -- they have a wide-angle field of view, which leads to an ill defined response in this application (with clutter below the desk). It sometimes pays off to read beyond the first post before you fire off a reply.

author: BBBbbb
author_rank: Unknown
timestamp: August 03, 2017, 11:32:28 pm
content: Regarding clutter on the floor and us sensor, he can always point it up not down if he can find a nice way to integrate it visually.But anyway OP if you decide to go the practical way, I'd go with the magnetic sensors. Cheap and robust. Two small magnets and 2 small magnetic switches. If you wanna go optical I'd isolate it in a tube of some kind. Mechanical micro switches might break if they are frequently engaged.Whatever you do double it for safety reasons.

author: tooki
author_rank: Super Contributor
timestamp: August 03, 2017, 11:40:13 pm
content: I wonder how my desk does it. I know there is a procedure to calibrate the end stops, but I don't know whether it uses any sensing in between. It's rather accurate at finding the saved heights.

author: BBBbbb
author_rank: Unknown
timestamp: August 03, 2017, 11:43:26 pm
content: Quote from: tooki on August 03, 2017, 11:40:13 pmTeardown time!Who needs warranty...

author: cdev
author_rank: Super Contributor
timestamp: August 03, 2017, 11:58:39 pm
content: Digital calipers use a linear optical encoder. You likely can make your own by copying the pattern in one of them. Use something like that.But you are making it way too complicated, IMHO. You could move it with reduction gearing and two stepper motors, one on each side.

author: tooki
author_rank: Super Contributor
timestamp: August 04, 2017, 12:53:41 am
content: Quote from: BBBbbb on August 03, 2017, 11:43:26 pmThe problem is, I need the desk! :pI got mine on clearance (showroom demo) for 60% off, I don't have $3K laying around to replace it at full price if I fuck it up!And I have nowhere to put all the stuff that lives on the desk now! :p

author: tooki
author_rank: Super Contributor
timestamp: August 04, 2017, 12:56:54 am
content: Quote from: cdev on August 03, 2017, 11:58:39 pmNo, they don't. They use capacitive sensing, not optical.That said, I agree that linear optical encoder could be a good solution. (They're common in inkjet printers, which is where I would look for inspiration.)

author: CatalinaWOW
author_rank: Super Contributor
timestamp: August 04, 2017, 01:09:45 am
content: Since you want creativity and a challenge hang a heavy chain from an inexpensive strain guage. Measure the weight off floor as a measure of hight. The chain will tell you where not to put your backpack.

author: Rerouter
author_rank: Super Contributor
timestamp: August 04, 2017, 02:21:35 am
content: The adjustable desks i have seen use a single motor in the middle, and have an index sensor on the shaft. (A microswitch pressing a filed flat spot on the shaft) it relies on the assembler making sure its level before the axel is fitted.To calibrate it you hold the down button, it reaches the end, doesnt see the index count in 5 seconds then stops the motor. And raises to its default heigh just relying on that index count.

author: tooki
author_rank: Super Contributor
timestamp: August 04, 2017, 04:50:31 am
content: On mine, the legs aren't really connected to each other, it's got separate motors in each leg.

author: MK14
author_rank: Super Contributor
timestamp: August 04, 2017, 06:02:13 am
content: What if you got a cheap pair of ultrasonic transducers. Fixing the transmitter to the top of the underside of your bench.The receiver to the floor, where it WON'T be directly blocked.Then time how long the transmitted ultrasonic pulse takes, to get to the receiver.You had problems with the earlier ultrasonic suggestions, because that was trying to "bounce" the signal between the sensors and the floor. But your belongings may have caused incorrect readings.My suggestion, is a direct, one way path, so even if it is partly blocked. It should still work. The shortest path, should give the first (correct) timing reference (which can be converted to distance, once you know the speed of sound, at your room temperature and pressure).Obviously you would have to do the circuitry and software. It could be a fun and educational project, even if it does not work out that well.Downsides: 5mm accuracy may either be impracticable and/or overly complicated for you to achieve. I think the better designs get 1mm resolution. But that might get very complicated.EDIT: In practice, I expect it is too difficult to do accurately that way and it is still possible that objects stored under the bench, could still affect the readings. Although they probably won't.Another way would be to put encoder wheels on the motor(s) or gearbox. Which makes it go up and down. You can then count how many rotations, and hence determine the distance traveled.

author: Ian.M
author_rank: Super Contributor
timestamp: August 04, 2017, 07:18:21 am
content: Hmmm . . .What about Grey code + some contrasting colour lines outside it to help locate the code, as a vertical strip on the wall behind the desk and a Raspberry Pi Zero + Pi Camera. Then its just a matter of image processing to pick out the stripes either side of the Grey code (to cope with the desk being moved slightly, and reading the brightness of 16 squares in 2 rows of 8 just above and below the horizontal centerline of the image, then driving the intercepted buttons accordingly via its GPIO port.However it would still be worth adding some means of tracking the motor speed by feeding pulses to a GPIO pin so it can release the button if its stalled and also so it can interpolate the position between image captures so it can blip the button for the last fraction of an inch to slow down and avoid overshooting.

author: alanb
author_rank: Frequent Contributor
timestamp: August 04, 2017, 12:33:36 pm
content: I wonder if cheap electronic Vernier callipers could hacked to provide an output?

author: BBBbbb
author_rank: Unknown
timestamp: August 04, 2017, 02:03:42 pm
content: I do believe if we continue giving suggestions sooner or later there will be a suggestion about an AI powered robot that would autonomously and without direct command push the button and move the table to the desired positions after analyzing OPs daily routines.

author: Kalvin
author_rank: Super Contributor
timestamp: August 04, 2017, 02:08:51 pm
content: Optical limit switches should do the trick unless you want to do some fancy stuff. Two limit switches are good for sitting / standing. Quite easy to adjust mechanically.

author: alm
author_rank: Super Contributor
timestamp: August 04, 2017, 02:09:19 pm
content: If you put an accurate clock on it, you could measure the difference in height through time dilation.Depending on the mechanical construction, hacking cheap electronic calipers could be an easy way to get an accurate linear position sensor for cheap. Some, if not most of them, have a serial output you could monitor.

author: Nusa
author_rank: Super Contributor
timestamp: August 04, 2017, 02:11:00 pm
content: Quote from: alanb on August 04, 2017, 12:33:36 pmEasy, since most of them already have output pins under the cover. But they aren't so inexpensive if you need more than 6 or 12 inches!

author: Kalvin
author_rank: Super Contributor
timestamp: August 04, 2017, 02:15:22 pm
content: Or use a rubber band / spring connected to a force sensing resistor or similar, giving analog output read by the Arduinohttp://www.robotshop.com/en/force-sensors.htmlEdit: The rubber bands may work as springs, but the results are not that reliable and repeatable as with springs:http://www.wired.com/wiredscience/2012/08/do-rubber-bands-act-like-springs/

author: Ian.M
author_rank: Super Contributor
timestamp: August 04, 2017, 02:19:34 pm
content: Quote from: BBBbbb on August 04, 2017, 02:03:42 pmhttps://www.theregister.co.uk/2017/07/28/bofh_2017_episode_10/More seriously, if there's a flat surface that moves past another part of it parallel to the surface, a small rubber tired idler wheel carrying a small magnet, in sprung contact with the surface + a hall sensor could work. Its far more dust resistant than an opto. Add at least one fixed magnet mid travel with another hall sensor so you can reset the position as it goes past to eliminate any errors due to culminative slip.

author: alexanderbrevig
author_rank: Frequent Contributor
timestamp: August 04, 2017, 02:31:39 pm
content: Maybe use time? Chances are you get within 5mm just by'holding' it for x ms for uip, and y for downEdit: was on the phone, did not see page two and all my typos. I'll keep them for future embarrassment

author: Kalvin
author_rank: Super Contributor
timestamp: August 04, 2017, 02:34:54 pm
content: Quote from: alexanderbrevig on August 04, 2017, 02:31:39 pmOne mechanical limit switch for getting the home position and then use time would be an easy solution.

author: alm
author_rank: Super Contributor
timestamp: August 04, 2017, 02:37:40 pm
content: Time would suffer from drift without a position sensor/top on at least one end. Unless you would periodically do a calibration that goes all the way to the min or max height and then back again. I.e. throw it over the wall and let the software people solve it.

author: NivagSwerdna
author_rank: Super Contributor
timestamp: August 04, 2017, 03:03:52 pm
content: Quote from: ndictu on August 03, 2017, 07:55:46 pmThis is definitely a first world problem....I would use a hall-effect sensor on the moving part and two magnets which I would move up and down for the limits... the logic is simply move until you meet a magnet. The only twist is the extreme limits... microswitch, another magnet or timeout/load limit for those?The Cs133 clock suggested earlier is a good idea though as long as you don't bring heavy objects to work.

author: andyturk
author_rank: Frequent Contributor
timestamp: August 04, 2017, 03:20:34 pm
content: Quote from: ndictu on August 03, 2017, 07:55:46 pmHow about this one?http://www.st.com/content/st_com/en/products/imaging-and-photonics-solutions/proximity-sensors/vl53l1.html

author: _Wim_
author_rank: Super Contributor
timestamp: August 06, 2017, 02:58:49 pm
content: A diy string encoder (https://www.pc-control.co.uk/string_encoders.htm) seems ideal for this job. The sensor could be a 10-ruen pot. You could also look for a "draw wire encoder" (industrial device, same principle) on Ebay...

author: David Hess
author_rank: Super Contributor
timestamp: August 06, 2017, 06:27:59 pm
content: I agree with _Wim_; wrap a thin cable around a pulley attached to a spring loaded ten turn potentiometer.https://en.wikipedia.org/wiki/String_potentiometer

author: ThomasDK
author_rank: Regular Contributor
timestamp: August 06, 2017, 07:05:04 pm
content: Not clear if this is for work or home. If for work, depending on local regulations you will need a way to detect and stop it if something gets stuck as it moves down.