US-016 $3 Analog Ultrasonic Sensor - Page 1
Forum section: electronics / projects

author: tuvix
author_rank: Newbie
timestamp: April 22, 2016, 02:02:15 am
content: I haven't used an ultrasonic sensor since college where I used the Maxsonar modules which are very nice but also very pricey. I wanted something cheap to be a basic "wall detector" and I noticed a lot of people like the HY-SRF04 ( $1 on ebay) and HY-SRF05 ($3 on ebay). I have never been a fan of the required overhead for pulse width data and these two both look to require 2 pins ( There is some disagreement if the HY-SRF05 can work on one pin or not). I much prefer an analog output, it requires a single channel and does not require any special code on the uC other than reading the ADC value. I purchased one of each from ebay and while I wait for them to arrive I thought I would start contributing to a forum and ask if anyone has experience with this particular sensor and why people seem to prefer pulse width over analog output?

author: Fungus
author_rank: Super Contributor
timestamp: April 22, 2016, 03:38:05 am
content: Quote from: tuvix on April 22, 2016, 02:02:15 amWhy is "special code" a problem?Quote from: tuvix on April 22, 2016, 02:02:15 amMicrocontroller ADCs can typically only read 1024 different values (actually less, the last bit is usually noise even under ideal conditions).Pulse width doesn't have that limitation. Those sensors work up to 5 meters with sub-millimeter accuracy (or better, accuracy only depends on how good your timing loop is).Even better would be if the chip on the sensor gave you a binary number using SPI/I2C.

author: tuvix
author_rank: Newbie
timestamp: April 22, 2016, 03:08:10 pm
content: I love writing the code but I'm trying to find the best solution for my application. This is for a telepresesnce robot to keep it from bumping into walls and travel down the center of a hallway so I only need resolution in the 1" range. I have 4 sensors (one for each direction) two motors to PWM and a bunch of other I/O's to process so I'm really looking to save pin counts and processing overhead which is why the analog sensors are so attractive. Right now I only have 1 pin per sensor to dedicate so the only other choice would be to make them I2C ( All the SPI pins are used as I/O's)The resolution of the US-016 is 1mm with a sensing distance of 20-3000mm so the full range is 2080mm. With a 10bit ADC that's a 2080/1024 ~2mm per LSB so as you stated full resolution can't be achieved with this sensor and a 10bit ADC. Since I am looking for 25mm resolution this should still work nicely for my needs.The HY-SRF04 and HY-SRF05 say .3mm so that means they have ~7X the resolution of the analog sensor!CORRECTION!!!The ebay auction I was using as a reference says .3mm but the app note PDF I just downloaded saysup to .3cmresolution so it's actually 1/3 that of the US-016 but who knows what they actually are.If the US-016 is total trash I'm hoping the HY-SR05 can work with 1 wire, if not I'm thinking of gluing an ATMEGA48 to the back and either use a PWM output through and RC network for a crude DAC or make them I2C and connect them to a bus. I'm surprised no one has done that yet, mate one of these $1 sensors with an AVR and offer a bunch of output options and sell it for $10 or something.As soon as they arrive I will do some tests and post my findings.

author: Fungus
author_rank: Super Contributor
timestamp: April 22, 2016, 07:20:29 pm
content: Quote from: tuvix on April 22, 2016, 03:08:10 pmProcessing overhead is severe, yes. You could do it with interrupts if you don't need much resolution (a bit of wibble in the interrupt response time won't matter).I write an Arduino driver that uses a hardware timer to measure the pulse length ("Timer 1 - Input Capture Unit"). That works nicely and hardly uses any CPU at all. Downside: You only get one of those pins per Mega328 and it takes a whole timer to do it (you lose 2 hardware PWMs).nb. A Leonardo has a lot more timers and two pins that can be used for event timing.Quote from: tuvix on April 22, 2016, 03:08:10 pmOne pin per sensor is all you need for that new version.The other option is, as you note, to add another Arduino. Pro Minis are cheap and small and work well as very fast I2C slaves.

author: tuvix
author_rank: Newbie
timestamp: April 22, 2016, 09:12:17 pm
content: After I purchased them I read the cheapo Ebay blue board HY-SRF05 I purchased might not function on 1 pin like the green board versions. I guess the OUT Pin is functionally unknown and not the same as the MODE pin on the other models.I've actually never used the Arduino IDE or bootloader, mostly because I was raised on AVR Assembly and grew into AVR C but also because of the hardware abstraction limitations, particularly in situations like this where ISR timing is critical. I have some DIP-8 Tiny25's that might be tight but it should work for this. That's another advantage of working in AVR C you don't need all the Arduino overhead and you can pack a lot into a 2k chip.Input capture really makes it easy but like you said there is only one per chip. You can also use the pin change interrupts and use the same 8 bit timer for as many inputs as you like with the main limitation being sample rate.

author: Fungus
author_rank: Super Contributor
timestamp: April 24, 2016, 08:30:57 am
content: Quote from: tuvix on April 22, 2016, 09:12:17 pmIt's quite easy to use the Arduino IDE without all of that.The real advantage of the IDE is that you don't have to learn all the parameters for a dozen command-line tools just to be able to upload a program.Quote from: tuvix on April 22, 2016, 09:12:17 pmThe main reason I wrote that code was that I was controlling a WS2812 LED strip at the same time and that requires long periods of time with no interrupts. The hardware input capture works with no CPU interaction.

author: tuvix
author_rank: Newbie
timestamp: April 25, 2016, 03:28:15 am
content: Quote from: Fungus on April 24, 2016, 08:30:57 amI don't know why people use command line tools but I don't use any of that nonsense, I stick with the official Atmel AVR Studio for development. You may have heard horror stories about Atmel Studio 5+ which is made for everything else and "kinda" supports AVR's. I use AVR Studio 4.19, It's a little antiquated as far as creature comforts but it works with the latest compiler and is highly optimized for working with the AVR. You can download it from Atmel's website and it still widely used. There are no command-line tools at all, it's all in the GUI, it's actually really simple and slick. You load the project, connect to the ISP and program, that's it. The ISP can program up to 8Mhz depending on your clock frequency ( I usually program at 1Mhz but 4Mhz is reliable). At that speed you can program 16k of flash in a few seconds. I still have a boot loader so after I'm done developing I load that and I can update firmware over the serial port just like Arduino.The MKII is discontinued but it's actually a really simple device and a lot of clones are on ebay for under $20. the AVR is an awesome little uC but it has limited resources and Arduino "makes it easy" by using some of those resources to let you interact with it at a higher level. If you want to take it to the next level and see how things work under the hood and write some honest code I recommend trying AVR C. I think most of these Ardiuno boards have the 6-pin ISP header on them so all you need is an ISP and the free software and I would of course be happy to help get you started.

author: Fungus
author_rank: Super Contributor
timestamp: April 25, 2016, 02:07:47 pm
content: Quote from: tuvix on April 25, 2016, 03:28:15 amThe problem is that it's difficult to send code to other people who don't work that way.Quote from: tuvix on April 25, 2016, 03:28:15 amYou can do all that from the Arduino IDE as well.I often use ISP programmers and no bootloader. It lets me use all the flash, set my own fuse bits and avoid the three-second bootloader delay on startup.It's a crappy text editor, but it works.

author: tuvix
author_rank: Newbie
timestamp: April 26, 2016, 08:24:02 pm
content: AVR C predates Arduino and there is a large support community out there such as AVR Freaks. Arduino is a higher level IDE so knowing AVR C actually makes it easier to understand what's really going on in the Arduino code. I guarantee you will have a lot of "A-HA" moments and become a master Arduino debugger.