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.