simulate this circuit – Schematic created using CircuitLab

I'm working on a proximity sensor for my garage, and I'm using a HC-SR04 ultrasonic sensor to measure distance. The microcontroller I'm using to control it all is a PIC16F88. I have a first version that's working perfectly fine, the distance is accurate and consistent.

However, I am planning on powering the project from a battery (not sure what kind of battery yet), and so I'd like to reduce the power consumption as much as possible. In idle state, the distance sensor draws about 3mA, which is too much. I started modifying the project to use the sleep mode of the PIC, this part is working fine.

I am now trying to power the HC-SR04 from a pin of the PIC, and a MOSFET (2N7000). I have the sensor wired as follows:

  • Vcc: +5V
  • Trig: Output pin of the PIC
  • Echo: Input pin of the PIC
  • Gnd: Drain of MOSFET

The MOSFET is wired as:

  • Drain: Gnd pin of the HC-SR04
  • Source: Gnd of the circuit
  • Gate: Output pin of the PIC

The logic of my C program is the following:

  • Apply logic level 1 to the gate of the MOSFET
  • Measure distance
  • Apply logic level 0 to the gate of the MOSFET
  • Put the PIC to sleep for a little bit (about 500ms)
  • Repeat

The problem is that the distances measured are not consistent at all. It seems to vary around the correct distance, but goes 10 to 20 centimeters above and below the actual distance. If I connect the gate of the MOSFET to +5V, then it works perfectly fine and I have consistent, correct distances. I tried waiting for up to 500ms after setting the gate to +5V before measuring the distance but it didn't fix it.

Have someone experienced something similar, or has an idea what I could try to power the HC-SR04 this way?

Thank you for your help!

  • \$\begingroup\$ would you please post a schematic. there is a button for an editor when you are posting or updating a question \$\endgroup\$
    – jsotola
    Commented Dec 13, 2017 at 1:22
  • \$\begingroup\$ Are you sure the mosfet is fully on? \$\endgroup\$
    – Ron Beyer
    Commented Dec 13, 2017 at 1:29
  • \$\begingroup\$ @jsotola I added a schematic that shows the connections for the HC-SR04. \$\endgroup\$
    – Asct20
    Commented Dec 13, 2017 at 1:39
  • \$\begingroup\$ @RonBeyer I checked with a multimeter and it is. Also, when connecting the gate of the MOSFET to the +5V power supply the circuit is working perfectly fine. \$\endgroup\$
    – Asct20
    Commented Dec 13, 2017 at 1:40
  • \$\begingroup\$ What does the datasheet say about startup time? \$\endgroup\$ Commented Dec 13, 2017 at 14:02

2 Answers 2


After much experimenting, it looks like it all came down to the breadboard connections not being strong enough. When I try to "push" the MOSFET and wires connected to it in a way that forces them to touch, it seems to be much more consistent; when I manage to get a good connection and don't move the board, the distance is very stable. I also added a capacitor between +5V and Gnd for the HC-SR04, to absorb any inconsistency in the signal given by the pin of the PIC and the MOSFET. I will soon solder everything on a prototype board and will be able to confirm that this was the issue.

The wake up time of the HC-SR04 seems to be about 60ms. The minimum suggested time between 2 measures to avoid collisions from a previous signal is about 60 ms, so I tried to wait 60 ms after powering the sensor also. Waiting less that that does give some incorrect values, so I would recommend keeping a delay of 60 ms or more every time you power the sensor back up, as well as between every measure.

I guess the solution is simply that the HC-SR04 needs to be powered by a stable signal, which was not the case on my breadboard. Anyway, thanks a lot for the help!


It's a bit of a long shot, but you might consider turning your I/O pins which are connected to the HC-SR04 to high impedance before shutting down the FET, in case you are leaking power into the HC-SR04 through them when it's ostensibly off. The module might get in a funky state in that case, I reckon.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.