Introduction: 3.3V Mod for Ultrasonic Sensors (prepare HC-SR04 for 3.3V Logic on ESP32/ESP8266, Particle Photon, Etc.)
TL;DR: On the sensor, cut the trace to the Echo pin, then re-connect it using a voltage divider (Echo trace -> 2.7kΩ -> Echo pin -> 4.7kΩ -> GND).
Edit: There has been some debate on whether the ESP8266 is actually 5V tolerant on GPIO inputs. Espressif claims both that it is and that it isn't. Personally, I would only take the risk if I had "leftover" ESP8266s.
If you are anything like me, you have come to know and like the HC-SR04 as the de facto standard for low-cost ultrasonic distance sensing for 5V-based Arduino projects. That's why I have quite a few of them lying around here.
But the world of hobby electronics has been steadily moving from 5V towards 3.3V. The Raspberry Pie and many other boards, like those based on the ESP8266, ESP32 or boards like the Particle Photon, are working with 3.3V logic on their input/output pins.
If we connect the sensor to 5V power and at the same time to 3.3V pins, the Echo pin's output will also be 5V and will most likely destroy the 3.3V pins of our microcontroller board. We could try to connect an as-is HC-SR04 to 3.3V power and will be able to get measurements, but unfortunately, these will often be much less accurate.
The solution is to still connect the sensor to 5V VCC, but to make sure that the Echo signal that reaches the microcontroller only has 3.3V by creating a voltage divider using two resistors. Lucky for us, the HC-SR04's Trigger pin does not need 5V and also the accepts the 3.3V that we get from our microcontroller's pins.
With above description and links, you most likely already have enough information to create a voltage divider as part of your circuit on a breadboard and hook up an ultrasonic sensor correctly.
If you want to learn how to modify one or several HC-SR04s so they are 3.3V-ready as self-contained units, without any extra circuitry, read on below.
Step 1: What You Need
- HC-SR04 Ultrasonic Sensor
- One 4.7kΩ and one 2.7kΩ resistor (or any combination of resistors in the 1-50kΩ range with R1/(R1+R2) = ca. 0.66)
- Soldering equipment
- X-Acto knife (or any knife that is similarly sharp and pointy)
- Acceptable soldering skills -- or the willingness to destroy a HC-SR04 while trying out something new :)
- Optional: magnifying glass, multimeter, oscilloscope, particle collider, ...
Step 2: Find the Trace to the Echo Pin and Cut It
Look closely at the sensor's board (possibly using a magnifying glass) and find the trace which leads to the Echo pin.
Note: Your HC-SR04 might have a different printed circuit board (PCB) layout than the one shown here! The trace might also be on the other side (when a trace ends in a round circle, this is is usually a connection to the opposite side of the PCB).
Optional: Take your multimeter and check that you have identified the correct trace by testing for continuity between the Echo pin and the solder joint where the trace connects to something on the PCB. It should show zero ohms.
Using the knife, cut the trace carefully several times in the same spot. Pay attention not to cut neighboring traces. Then, scrape off the trace until you first see its metal, then see it disappear, and you are sure there's no connection any more.
Note: If you don't completely sever the trace, the Echo pin will still deliver the full 5 volts to your microcontroller's pin.
Optional: With the multimeter, check that you have completely severed the same trace by again testing continuity between the Echo pin and the solder joint where the trace connects to something on the PCB. It should show infinite ohms (if it shows something in the mega-ohms range, that's ok, too).
Step 3: Solder 2.7kΩ Between the Echo Pin and Its Trace's End
If you haven't already, find where the Echo pin's trace (which you severed) directly leads another element, like an IC.
In my example, it is connected to pin 2 of that chip in the middle of the PCB.
Cut and bend the 2.7kΩ resistor's legs to exactly fit between the Echo pin and the other connection.
Then solder the resistor in place (cleaning the parts to solder and applying flux probably won't hurt, either).
Step 4: Solder 4.7kΩ Resistor Between Echo Pin and GND Pin
Cut and bend the 4.7kΩ resistor's legs to fit between the Echo pin and the GND pin (or their solder points on the PCB), and solder them there.
Optional: Use a multimeter to check the resistance between the connections to make sure there are no shorts.
Extremely optional: Hook up the trigger pin to your programmed MCU, don't connect the Echo pin yet, and make sure that the Echo signal is 3.3V and not 5V using your favorite oscilloscope. Okay, I'm 85% kidding on that one. :)
You should now be able to hook up your modified sensor to any 3.3V microcontroller. You still need to power it with 5 volts, but many microcontroller boards (that have a voltage regulator) also accept 5 volts, so this should work out just fine in many projects.
Added bonus: this modded sensor will be backward compatible with 5V projects, because most 5V microcontrollers (like Arduino/ATMEGA) can interpret 3.3V signals the same way as they do 5V.
23 Comments
11 months ago
Pro tip: You can actually use 3 of *any* single value of resistor in the 1-50kΩ range. You pair up two of them in parallel (whose combined resistance is half of a single one), and in series with the third. The net effect is a 2/3 resistor divider, which will get you exactly 3.33v from 5v.
Reply 11 months ago
Extremely useful tip, thanks!
1 year ago
Nice instructable, thanks. I have a few of these sensors I need to convert for 3.3 v logic. Wouldn't it work if you used three equal resistors? Put two of them in series (as the higher resistance) with one resistor (as the lower resistance). Or put two in parallel (as the lower resistance) with a one resistor (as the higher resistance). That would give you a .667 ratio and take 5v to exact 3.3v. It is an extra resistor, but you don't have to root around looking for two with the ideal ratio.
Reply 1 year ago
Thanks! :) Those are good points. For my setting, it was a kind-of good-enough solution, but I like that you pointed out that other configurations are possible, which is useful for someone with different requirements or available components.
4 years ago
Instead of cutting the PCB it is of course possible to install the voltage divider outside the board between the output and ground...
Reply 3 years ago
Do you have a schematic of how this can be done outside the board? I've tried different variants and still my multimeter shows 5V coming out of the ECHO pin.
Reply 3 years ago
Alas, I don’t have a schematic at the ready, but there’s a schematic in the linked article https://www.instructables.com/id/Simple-Voltage-Divider/
If you want to be able to make your own voltage dividers in the future, you could try to figure it out using this article, but the TL;DR is to hook it up like in the schematic in the linked article, except: “9v” would be your sensor’s Echo pin, the top resistor would be 2.7kOhms, and the bottom resistor would be 4,7kOhms.
To test that your voltage divider works correctly (without the sensor), you can hook it up between +5V and GND directly, and the output (between the resistors) should be at ca 3.3V.
Reply 4 years ago
You absolutely could! However, that's not the purpose of this instructable. As I wrote:
"With above description and links, you most likely already have enough information to create a voltage divider as part of your circuit on a breadboard and hook up an ultrasonic sensor correctly.
If you want to learn how to modify one or several HC-SR04s so they are 3.3V-ready as self-contained units, without any extra circuitry, read on below."
4 years ago
Hello, how are you? My name Leandro is from Brazil, first congratulations for the initiative and thank you!
I am in a project that use a senosr hc-sr04 with Esp32, I performed the procedures that oriental in your tutorial, the same is not sending correct distance information, for distances greater than 90cm, what may be happening? if possible help me!
Reply 4 years ago
Hello Leandro, thank you for your nice comment. :)
From the distance, it is kind of difficult to say what may be going wrong. I don't really think the modification can have an effect for larger-distance measurements specifically, so I would assume that something else is going wrong.
One thing is the power supply to the ultrasonic sensor: it should still be 5V (not 3.3V), even after the modification. These sensors often get less accurate if they are run on lower voltage.
What I would try next would be to run a minimal program (only reading the sensor, no servos etc), with long (1s or so) delays between the readings, and even maybe try out the sensor with such a test program on another board like an arduino.
If the results in these are better, this might give some indication on the source of the issue. For example, I had a case in the past where I was trying to read the sensor very very frequently, and the sound did not have enough time to travel the larger distances. Also, I used it on an Arduino together with a Servo, and the servo library changed timers around, making the measurements of the time the sound takes to travel very unreliable.
Hope this helps!
4 years ago on Step 4
Many HC-SR04 units can run fine on either 5.0 volts or 3.3 volts. They are sometimes sold as "HC-SR04+" parts but I never see the "+" printed on the parts. Yours looks like the duel 5/3.3 volt part.
If you have one of these simply connect the Vcc to 3.3 volts and it will work. But perhaps you get better range with 5 volts? I don't know
Reply 4 years ago
PS: To clarify, all of them seem to work with 3.3V, too, but then I found a thread where someone found out about the unreliable measurements on 3.3V and assumed that's true for all of these Sensors. If there are more reliable "+" variants that would be cool indeed!
Reply 4 years ago
Here is a reseller for the "+" version. Look at the parts layout on the PCB. I have some of these and some of an older type.
https://www.openimpulse.com/blog/products-page/product-category/hc-sr04-ultrasonic-distance-sensor-3-3-v-5-v-compatible/
I think the plan might be to test them. Also maybe measure the operating current and see if it is 3ma or 15ma.
Reply 4 years ago
Great info, thanks! :)
Reply 4 years ago
Thanks! I wasn't aware of there being 3.3/5V variants of the sensor. Didn't find a proper datasheet for mine, which would've helped. ;)
Question 4 years ago on Step 1
Perhaps my lack of electronics will show but how many watts are the resistor? 0.5w ?
Answer 4 years ago
As albertson.chris pointed out, the way I talk about "15mA going through the pin" is misleading. It was my rather clumsy attenpt to explain that you can use resistors of just about any power rating, as the power dissipated is negligible (assuming this pin is connected to a high-impedance uC pin -- short-circuiting it to ground might produce different results (and smells ;) ) of course).
Answer 4 years ago
I would just use the lowest power rating resistors I have lying around as there will hardly be any current going through the Echo pin.
I tried to find some more detailed info on the Echo output's maximum allowed current draw and couldn't find any. Even if the sensor's complete power consumption of 15mA were to go through that pin (which it won't), 0.5 W would still be around 20 times of what was needed.
5 years ago
Great information! Thanks for sharing your electronics knowledge.
Reply 4 years ago
"Even if the sensor's complete power consumption of 15mA were to go through that pin" It does NOT work that way. If you placed a low enough value resistor from echo to ground the unit would draw more than 15ma. You could likely get it to draw 20.
Try "Ohm's Law". Let's assume the tiny chip's output pin can supply 1 mA (0.001 A) before it burns up. At 5 volts, the resister that draws 1ma is r= 5.0/0.001 Ohms or 5000 ohms. So long as the resistors add up to over 5K you'd not blow the part. But now the HC-SR01 is not long drawing 15ma from the power supply it is drawing 16ma. the 5K load on the output has caused a jump in power consumption.
So the power consumption of the device has nothing to do with choosing a resister. What if you used a 50-ohm resistor? It would cause the device to consume a total of 115 mA and likely fail to do to some internal heating. The resister is not determined by the power consumption but the other way around, the resister strongly influences the power consumption and therefore you want the value to be hight.
Your resistors are close the about the best values are given but the reasoning was wrong.
People were questioning the value. The best answer is to calculate the answer. Answer: You have placed and about, roughly 1mA load of the the chip. That is likely OK
Now lets computer how many watts Wats is i^2 * r. Assume a 5K resistor and 1ma current: 5000 * 0.001 * 0.001 = 5 milliWatts So any resister you happen to have will work even a 1/8thWatt would be an order of magnitude oversized.
You have the right answers but the answer was maybe arrived at by a correct guess based on experience. Better to get out an old envelope and check