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)
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!