Electronic – Power HC-SR04 from microcontroller pin and MOSFET

mosfetpicsensor

schematic

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!

Best Answer

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!