Shop OBEX P1 Docs P2 Docs Learn Events
SR04 5 volt to 3.3 volt — Parallax Forums

SR04 5 volt to 3.3 volt

prof_brainoprof_braino Posts: 4,313
edited 2013-06-21 19:30 in General Discussion
I saw this thread, but I got a little lost.

http://forums.parallax.com/showthread.php/143148-Four-input-5-volt-to-3.3-volt-level-shifter

SR04 is on pin 16 trigger and pin 17 echo on the quickstart. Its powered by Vin.

The instructions (set up for littlerobot propforth demo) say to use a 220-330 ohm resistor in series from echo to pin 17 to keep to 5v output manageable for the prop input. Other folks say this works fine.

The code is SR04.f from the propforth extensions directory, also posted here http://code.google.com/p/propforth/wiki/LittleRobotDemoUltrasonic

The code is this:
- When the echo pin goes hi, get cnt
- When the echo pin goes low again, get the count again.
- Multiply the difference by the speed of sound (over two) to get millimetres.
There is the case where we get a weird number when the counter rolls over.

In my case, I notice these things:
* pin 16 flickers dimly when reading are being taken
* pin 17 flickers brightly when reading are being taken
* pin 18 ALSO flickers when objects are greater than 100 mm distant. There is nothing connected to pin 18, only 16 and 17 are being used.
* 18 pin dims are the target draws closer, and stops flashing when the target is less then about 50 mm aways
* when the target is farther than about 1.5 meters, the echo pin stay lit, no longer flashing, and the last (long) reading is is displayed.

It looks like the SR04 echo pin doesn't go low anymore after the long reading.

Questions:
Is 220-330 ohm resistor causing my problem?
Why is LED p18 flashing?
Anybody see the SR04 get stuck with echo hi like this?

Thanks!

Asnwers:

The resistor should be 10k in this case. 4.6k minimum, but siince there is risk of someone using 4 AA 1.5 volt alkaline batteries instead of 1.2 v NiMH , we should go a little higher.

Pin 18 iwas falshing because the built in input clamping diode was functioning, and preventing damage to the prop. It worked!

Did not get to the bottom of the SR04 getting stuck. It always hppens when the resitor is too low, and a long pulse comes in and causes the clamping diode to clamp. Its also happens with 10k resistor, but only after the steppers run, but not always. A reboot fixes the issue, does not need a power cycle. It could be noise from the steppers, or I might need a timeout in the SR04 driver.

Comments

  • Martin_HMartin_H Posts: 4,051
    edited 2013-06-11 16:36
    The essence of the prior thread was 5.5 to 3.3 volt transition coupled with multiplexing the echo pin. If you are only dealing with a single sensor then you only need to worry about the voltage mismatch. In that thread we used a voltage divider, but if you are using a single series resistor you need to use one around 3.9 kOhm to keep the current low enough to avoid damaging the prop. Your other symptoms sound related to this voltage overload.

    Undoubtedly one of the more experienced forum members will jump on this thread talking about clamping diodes. It will make sense to me for a while, but then those neurons will die and it will confuse the heck out of me.
  • ercoerco Posts: 20,256
    edited 2013-06-11 17:58
    Martin_H wrote: »
    It will make sense to me for a while, but then those neurons will die and it will confuse the heck out of me.

    Must be that Sam Adams Boston lager taking its cumulative toll! Tough tradeoff, but IMO you made the right call.
  • prof_brainoprof_braino Posts: 4,313
    edited 2013-06-12 08:17
    I tried the method from propellerpowered http://propellerpowered.com/shop/?page_id=1789 but 10k did not work on my rig. It only read 54 mm max reading.

    I looked at the Sparkfun page https://learn.sparkfun.com/tutorials/voltage-dividers/all

    and this suggested using a voltage divider with 1.7k ohms near the echo pin and 3.3k ohm going to ground, with the prop pin in between. I found 1.8 k and 3.3 k resistors. Tried it, this totally functions great, so I know that at least the parts are working. The pin 18 flashing has stopped, and it reads up to 5 meters.

    Phase 1 - Success! At least I know the SR04 works, and I didn't burn the prop inputs.
  • prof_brainoprof_braino Posts: 4,313
    edited 2013-06-12 08:28
    Martin_H wrote: »
    .. but if you are using a single series resistor you need to use one around 3.9 kOhm

    So I had the original in line resistor off by around an order of magnitude... Makes more sense now.

    Thanks for the help!

    Phase 2 - In Progress: I still need to use only one part, if possible.

    Now I will go try the single 3.9k ohm resistor in line from echo to the prop input.
    How do we figure the best value for the single in line resistor?
    Just ohm's law, 5 volts divided by .oo1 amp to get 5000 ohm, then find what closest in the parts box?
    I do have 3.9 k ohm resistors, so this would be 5 / 3900 =.0012.. so 1.2 milliamps is ok?

    Later ...

    OK, I tried a single 3.9k ohm resistor in line from ECHO to the prop pin. I also have a 1K from the prop to the SR04 trigger (per the propeller powered instructions) but I suspect this isn't strictly necessary. Same bad result as with 330 ohm resistor, when ever the SR04 see a long distance, the echo pin goes HI and never completes the reading (the LED on the echo pin 17 stays on continuously, and the reading stops updating).

    I guess I could use the resistor voltage divider, it would add 1 more resistor and 1 more wire to the parts list. I guess I could live with that on 100 kits.

    Any other ideas?

    Thanks!
  • Martin_HMartin_H Posts: 4,051
    edited 2013-06-12 08:41
    I tried the method from propellerpowered http://propellerpowered.com/shop/?page_id=1789 but 10k did not work on my rig. It only read 54 mm max reading.

    I looked at the Sparkfun page https://learn.sparkfun.com/tutorials/voltage-dividers/all

    and this suggested using a voltage divider with 1.7k ohms near the echo pin and 3.3k ohm going to ground, with the prop pin in between. I found 1.8 k and 3.3 k resistors. Tried it, this totally functions great, so I know that at least the parts are working. The pin 18 flashing has stopped, and it reads up to 5 meters.

    Phase 1 - Success! At least I know the SR04 works, and I didn't burn the prop inputs.

    I think the 1 k to the trigger pin is optional and you could get rid of that to save a part. The 1.7k ohm / 3.3 k ohm voltage divider is the textbook correct solution. It's weird the 3.9 k ohm doesn't work as Duane has used that successfully.
  • prof_brainoprof_braino Posts: 4,313
    edited 2013-06-12 09:38
    Martin_H wrote: »
    I think the 1 k to the trigger pin is optional and you could get rid of that to save a part. The 1.7k ohm / 3.3 k ohm voltage divider is the textbook correct solution. It's weird the 3.9 k ohm doesn't work as Duane has used that successfully.

    Sal also uses that, and tested it a couple time as I keep complaining about it. So it SHOULD work, so something must be different about my rig....

    I bet I burned the internal diode on the prop input or something? Could that account for this?
  • Martin_HMartin_H Posts: 4,051
    edited 2013-06-12 10:11
    Sal also uses that, and tested it a couple time as I keep complaining about it. So it SHOULD work, so something must be different about my rig....

    I bet I burned the internal diode on the prop input or something? Could that account for this?

    Someone like Phil understands this much better than me. When the pin voltage goes above 3,3 volts the clamping diode prevents it from going elsewhere in the Propeller chip. But it can only handle a limited amount of current before it burns out. A 3.9 k ohm resistor keeps it within safe limits, so I'm surprised the 5 volts at that current didn't do more damage to the chip. But the SR04 might not be outputting enough current compared to other 5 volt sources. But seeing the voltage on one pin at another pin sure sounds like the clamping diode burned out.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-06-12 23:53
    1.2 milliamps is ok?

    No! Not when the voltage is higher than 3.3V. You need to bring down to microamps (I'm remembering 500uA but I'm not sure).

    Are you sure your sensor is good? It seems like a 20% dud rate is common.

    You ought to also make sure your I/O pin is still working. A Prop pin can often handle 5V for a bit before burning out but the time 'til burnout seems to vary a lot.

    I've used 10K and 4.7K on the echo line successfully. I was also able to multiplex the echo line of four sensors (I think details are in one of the threads mentioned). I don't suggest using any more than 1K on the trigger line (I don't think you need even that). The 3.3V logic can have trouble triggering the sensor is the resistor is too large.

    Edit: I see Martin has already addressed the issue of clamping diodes. It's not uncommon for a Prop to have a damaged pin (if you're a bit sloppy about what gets hooked up to what (like I am)). A damaged pin can sure make debugging a pain if you don't know a pin is bad (ask me how I know).
  • prof_brainoprof_braino Posts: 4,313
    edited 2013-06-16 16:49
    Ok, I asked about the prop clamping dioodes in this thread.

    http://forums.parallax.com/showthread.php/148527-Input-clamping-diode-5v-to-3.3-volt-Max-and-Min-resistors

    Turns out that since we are running of of 4 AA batteries, there's a couple more isses.

    Alkaline batteies are 1.5 volts, so I was over-volting the SR04. I have to re calculate using 6 volts.
    Rechargeables are 1.2 volts, have to recalculate at 4.8 volts.
    USB is 5 volts, I guess I didn't really do the caluculation in the first place, so I should do this now and make sure I get the right answers
    USB power supplies can be all over the place, I need to measure the one(s) I'm using and see if there are any problems.

    But not right now, there's a BBQ going on...
  • Duane DegnDuane Degn Posts: 10,588
    edited 2013-06-16 18:37
    Have you seen these cheap regulators?

    After becoming frustrated trying to get my cheap bot to perform a figure 8, I decided to regulate the voltage to the servos. It made a huge difference.

    BTW, four NiMH cells can be over 5V when fully charged. I've been trying to figure out a good power solution for an inexpensive robot and I've just about decided to go Li-Ion or LiPo. The chargers for these batteries are getting pretty darn cheap.

    I've been watching your efforts with great interest. I'm still hoping/planning on holding some sort of robot school for our neighborhood kids sometime. Thanks for sharing your experiences.
  • ercoerco Posts: 20,256
    edited 2013-06-16 20:20
    According to the PicAxe gurus, one-pin mode (saves a pin) on the HC-SR04 requires adding a 1.8K resistor (unrelated to the voltage dividers discussed previously).
    http://www.picaxeforum.co.uk/showthread.php?22639-HC-SR04&p=223675&viewfull=1#post223675
  • prof_brainoprof_braino Posts: 4,313
    edited 2013-06-16 22:11
    Duane Degn wrote: »
    Have you seen these cheap regulators?

    Thanks for the input and interest. I saw that or similar from another thread. They look great, but even at a buck, they are too expensive for this go-round. Even a second resistor for the voltage divider is too much. (!) I'm tempted, but have to follow through with the "no extra parts" this pass. Its already costing money I don't have.

    If sufficient demand develops and this catches on, I'd definitely pursue this route.

    EDIT - those regulators are looking good though. Must... resist... temptation... to buy more stuff....!
  • ercoerco Posts: 20,256
    edited 2013-06-17 14:29
    @Braino: Definitely resist the temptation to buy those fabulous and efficient $1.75 regulators. I forbid it!

    Do not throw me into that briar patch, Bre'r Braino!
  • prof_brainoprof_braino Posts: 4,313
    edited 2013-06-21 09:15
    OK, I updated post 1 with my results.

    I broke down and got 1.2v NiMH batteries, but I won't include these in the LittleRobot kits.

    Better power options will be in the works in case we get a chance to make a custom board.

    The SR04 still gets stuck sometimes after the steppers run. It might be noise or new a timeout. When the counter rolls over, it can give a very large reading, (over 41 meters) so we have to look at that anyway.

    By lighting the Quickstart LED's in response to the distance reading, we can see how jumpy the SR04 signal can be. Also how sensative it is to the angle the face of the target. There's definitely more fun to be had with these gizmos!
  • prof_brainoprof_braino Posts: 4,313
    edited 2013-06-21 19:30
    Turns out one of the SR04 was bad. It has been punished.

    It would only read 64mm until I flexed the board. I looked at it under a microscope, but I didn't see any bad solder joints etc. Maybe the lower pin on the transmit can was maybe a tad too close to the shell? That was the only thing that looked like it would change when flexed. Unfortunately, it did not survive the exploratory surgery. It won't be giving anyone any trouble ever again.

    SR04-punished.jpg
    816 x 612 - 28K
Sign In or Register to comment.