Turn power on and off to ultrasonic sensor (HC-SR04)

I have a project that runs on batteries and uses an ultrasonic sensor (HC-SR04). Every 15 minutes it takes one reading from that sensor. I would like to lower the power needed by turning off the power to the sensor for the time when it wasn’ t needed.
Is that possible?

Right now the power is attached to the +5 on the Uno. Can power to that pin be turned off? (The HC-SR04 is the only thing using it.) Can I move the power wire to another pin that I turn on when I need it and off when I am done?

Any advice is welcome. Anyone know of something similar a url is welcome.

Thanks.

The HC-SR04 only draws about 6 mA so you can power it from a digital output pin. I test mine by plugging it into four consecutive digital pins. Set the Ground pin to OUTPUT and LOW. Set the Vcc pin to OUTPUT and HIGH.

..and let arduino sleep- many examples of sleep-modes can be found in forum

Hello all, I deal with the same issue - I want to run the HC-SR04 from battery for several months and this means I need to turn it off and on between measurements.

HC-SR04 works for me correctly when powered from VCC (without cutting him from power).

when I try to power HC-SR04 from digital pin and set the pin LOW between measurements, the HC-SR04 does not returns meaningful values. I also tried to power the HC-SR04 via digital pin + NPN transistor (just to make sure there is not some issue with high current draw via digital pin), but this approach also did not made the HC-SR04 work.

I tried to wait several miliseconds between powering the HC-SR04 on and measuring distance, but no success.

I logged out the output from pulseIn(echoPin) which corresponds to microsecods between ultrasonic trigger and echo - when not working properly, it returns value like "40" (microseconds) and when I put my hand directly on the HC-SR04 so that the proximity = 0 cm, the returned value changes to cca "20", so it seems the device is trying to measure something, but to me it seems like it lacks power to send more poweful ultrasonic wave.

here is the source code:
https://gist.github.com/petervojtek/8970698

just one more think - I am confused by the answer from " johnwasser": "Set the Ground pin to OUTPUT and LOW".
Why do you use digital output pin as ground pin? I cannot see any sense in it - the same should be achieved by simply using ground pin, or I missed some point?

petervojtek:
I am confused by the answer from " johnwasser": "Set the Ground pin to OUTPUT and LOW".
Why do you use digital output pin as ground pin? I cannot see any sense in it - the same should be achieved by simply using ground pin, or I missed some point?

The point is that the sensor has four pins that easily plug directly into four consecutive pins.

petervojtek:
when I try to power HC-SR04 from digital pin and set the pin LOW between measurements, the HC-SR04 does not returns meaningful values. I also tried to power the HC-SR04 via digital pin + NPN transistor (just to make sure there is not some issue with high current draw via digital pin), but this approach also did not made the HC-SR04 work.

HC-SR04 specifies 5V at source. With a 5V board, digital pins have enough voltage to drive it. When you operate at 3.7V, it will barely operate if VCC goes straight to the sensor; but voltage drop on a digital pin makes the sensor unstable. (BTW, HC-SR04 specifies 15mA working current; 2mA is "Quiescent".) Similarly, a silicon transistor causes 0.7V drop when "on". (Most NPN ones are silicon.) Germanium ones has lower drop but bigger "off" leakage. In this discussion, it is mentioned that MOSFET could achieve extremely low drop (a resistive 0.05 ohm). That could be your best bet.

BTW, you have a most interesting project. I have a perfect use for it in my Hackerlings kiddie club:-)

Hi there,

I want to do exactly the same thing. Everything in my project wants to run at 3V3, a DS3231 RTC, an SD card reader, and a TMP36 temperature sensor that will be happy with 3V3. Everything except the ultrasonic sensor. I am thinking of using a 5V LDO regulator with a shutdown/control pin. Off the top of Google's head I found a Toshiba TCR2EE50, which accepts 6V in and provides 5V out, up to 200mA. If the control pin is above 1V then the output is on, so I thought an Arduino pin at 3V3 would turn it on and off nicely.

Any warnings/caveats/better suggestions?

Thanks!

sillyvalley:
MOSFET could achieve extremely low drop (a resistive 0.05 ohm). That could be your best bet.

thank you sillyvalley, I repeated the experiment with mosfet (IRF530) and it works - I am able to power down the HC-SR04 between distance measurements and read the distance properly.

I tested the circuit in both 5V and 3.7V mode.
in 5V mode i checked the exact distance via serial console and the measured distance is precise.

in 3.7V mode I was powering the whole circuit directly from lion battery so that I lack exact output (and I am currently unable to put some digital display to print it out) but my threshold distance based human counter project seem to work properly.

Here is my working code:

  digitalWrite(distanceSensorPowerPin, HIGH); // we enable the mosfet current flow
  delayMicroseconds(1); // I give HC-SR04 some time to recover, not sure if this is necessary
  
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  long durationInMicroseconds = pulseIn(echoPin, HIGH); 
  digitalWrite(distanceSensorPowerPin, LOW); // disable mosfet current flow
  long cm  = durationInMicroseconds / 58;

I'm going through the exact same issues with my own SR04 project (Ultrasonic Parking Aid without Arduino | Hackaday.io). The first version powered the sensor from VCC, and worked, but drew 11 mA at idle with the sensor plugged in but not ranging, measured at the batteries (I don't know where the 2 mA "quiescent current" draw is supposed to come in; maybe the boost converter increases the draw?). First revision tried drawing power from a digital output pin, but that didn't work at all, although one of the above responses indicated that it did? There was a thread on AVRFreaks that indicated that these sensors will draw 100 mA peak current, even though the average current draw is ~10 mA, so powering it from a digital pin is not an option. Doing some more looking around found another instance of trying to power it from a digital pin that said it only worked for a fraction of the modules that they tried it with.

I soldered a 330 uF capacitor across the GND and VCC pins on the sensor to try to up the power available. It worked when being powered continuously, but returned nonsense values when I tried to switch it on and off, even with large delays to fill up the capacitor. Maybe it doesn't like power being ramped up?

So I guess a MOSFET switch is the answer?

Would anyone have a suggestion as to a suitable SMD MOSFET? The IRF530 mentioned is a large power MOSFET, and I don't think I need something that big. Also, does it matter if I switch VCC or GND for the sensor?

Jin:
First revision tried drawing power from a digital output pin, but that didn't work at all, although one of the above responses indicated that it did? There was a thread on AVRFreaks that indicated that these sensors will draw 100 mA peak current, even though the average current draw is ~10 mA, so powering it from a digital pin is not an option.

I soldered a 330 uF capacitor across the GND and VCC pins on the sensor to try to up the power available. It worked when being powered continuously, but returned nonsense values when I tried to switch it on and off, even with large delays to fill up the capacitor. Maybe it doesn't like power being ramped up?

Powering from a digital pin worked for me.

If you put a capacitor across Vcc and Ground and power from a digital pin you should switch from OUTPUT,HIGH to INPUT to disconnect power. If you keep the pin as an OUTPUT and switch it to LOW it will draw current (probably too much current) from the capacitor.

Even then, switching to INPUT, the HC-SR04 will continue to draw power from the capacitor until it is empty. That will be a waste of power and the load on the digital pin trying to re-charge the capacitor will probably be too high.

It would be better to use a MOSFET to turn off the HC-SR04. A quick search on DigiKey.com turned up the 49 cent Toshiba SSM3K318T,LF (http://www.digikey.com/product-detail/en/SSM3K318T,LF/SSM3K318TLFCT-ND/4304399) and 55 cent Rohm Semiconductor RSQ015N06TR (RSQ015N06TR Rohm Semiconductor | Discrete Semiconductor Products | DigiKey).

Some of my latest findings:

I used a MOSFET (BS170) to switch the SR04 on the low side (connected to GND). The BS170 is not an ideal low voltage MOSFET for this purpose, but it will do. When disabled by bringing the gate pin low and going into low power mode, the power draw is beneath the ability of my multimeter to measure, as opposed to ~11 mA when the sensor is connected but not otherwise busy. I don't know where the "<2mA quiescent current" from the datasheet comes into play.

As I said above, I was not able to power it directly from a digital pin; I don't understand why other people appear to be able to do so. I did read on one site that it depended on the unit, some could and some couldn't.

When using a MOSFET, don't forget to put a pulldown resistor on the gate to avoid issues during power up when the pins are high impedance, as well as a current limiting resistor.

Switching low side does seem to introduce another issue. Several of the "datasheets" on the SR04 mention that it is necessary to connect GND first before VCC, or unspecified issues will arise. I think I ran into one of them. The echo pin after it comes up is high, and results in a short read. My "solution" to this is to do a sacrificial ping after enabling the sensor after a small delay, which resets things.

You must also make the trigger pin an input pin when disabling, or the sensor will find ground through it and continue to draw power.

Here is some code:

void disable_sr04() {
  digitalWrite(PING_ENABLE, LOW);
  // As well as disconnecting GND with a low side MOSFET, we have to disconnect
  // the trigger pin, or the sensor will find ground through it and remain powered up.
  pinMode(TRIGGER_PIN, INPUT);
}

void enable_sr04() {
  // Reset the trigger pin mode to output.
  pinMode(TRIGGER_PIN, OUTPUT);

  // Connect GND to the sensor.
  digitalWrite(PING_ENABLE, HIGH);

  // Various data sheets exhort you connect GND before VCC, but never say what will happen if you don't.
  // What appears to happen is that the echo pin stays high, and you get a short read on the first ping.
  // A sacrificial ping after a short delay after power up appears to make everything happy again.
  delay(20);
  sonar.ping();

  // Enforce a settling delay.
  delay(29);
}

Hi,

this thread is a little bit old, but topic is actual for me :wink:

I'm very interesting how to use the HC-SR04 in combo with the MOSFET IRF530.

I have TinyTx Transmitter like here: http://nathan.chantrell.net/tinytx-wireless-sensor/

The problem is the same then in the arduino thread: Turn power on and off to ultrasonic sensor (HC-SR04) - Sensors - Arduino Forum about that I can't use the ATtiny85 Digital Pin to Power the HC-SR04 on/off. HC-SR04 directly powered measurement ok. But HC-SR04 powers via dig. PIN and powered down does faulty measurements.

Therefore I will also switch the HC-SR04 on and off by using the IRF530.

But how to connect the IRF530?
The Pin from ATtiny will be for example D10 (set HIGH/LOW).
The HC-SR04 (and also the ATtiny board) will get the power from the batterie pack which is between 3,7 and 5V and the HC-SR04 should be switched from the IRF530.
Do I need additional resistors? Where?

Can someone please help me how to connect it?

Thanks giovanne

i want to konw also, thanks

My tries get me to this solution, I don't know exactly why.

My HC-SR04 has a peak ok 15 mA as it turns on, and then it consumes 6 mA.

After putting a SI2302 MOSFET this is the code that works with my Arduino (0 mA):

  // Connect GND to the sensor.
  digitalWrite(POWER_PIN, HIGH);
  delay(100);   //With 70ms doesn't work, with 100ms yes.
  
  Serial.print("Ping: ");
  Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
  Serial.println("cm");

  //disable_sr04
  digitalWrite(POWER_PIN, LOW);
  
  delay(3000);  //Not to do it constinuosly

With the method of Jin doesn't work in my case. I don't know why.

Jin, what configuration are you using ? I'm using ONE PIN option (echo and trigger the same pin). May be that is the difference ?

With my configuration I have also checked with a BC337 transistor, and it works fine. So I can use NPN transistor or MOSFET.

Direct with an Arduino pin DOESN'T WORK.

NPN or MOSFET ----> OKv(but other method than Jin wrote)
Arduino Pin ----> NO

I haven't had any problems shutting down the power to the sensor for low current applications. You need to give the sensor some boot time, but it's fairly short (maybe 50ms). You also want to be sure you're not supplying power via the trigger or echo pins, which can drain current. I've found that setting all pins to output and low uses the least amount of power. A quick way of doing this is as follows:

  DDRD  = B11111111; // D7 - D0 set to OUTPUT.
  DDRB  = B00111111; // High 2 not used, D13 - D8 set to OUTPUT.
  DDRC  = B00111111; // High 2 not used,  A5 - A0 set to OUTPUT.
  PORTD = B00000000; // Set to LOW.
  PORTB = B00000000; // Set to LOW.
  PORTC = B00000000; // Set to LOW.

This is obviously only for ATmega328/168 microcontrollers. Also, when you wake the sensor back up, be sure to set all the ports to the correct mode. Using this along with putting the microcontroller to sleep, you can achieve 1uA of current usage (batteries drain with no load faster than that).

Tim

Hey there,

I am trying to turn the HC SR 04 off and on. I tried it with different types of transistors and circuitry.
The sensor works but it just gives back measurements up to 100-110mm.

So could you please post your working circuit diagram?
Please let me know how you guys got it to work.

physox

i want to do similar thing but for making the sensor a switch to off the buzzer , here is the project i am working on .

i want to add an ultrasonic sensor at the receiver end and when the transmitter triggers the alarm i want the buzzer to go on until someone stop it using the ultrasonic sensor [ more like switch off ]
here is the code for receiver , but i need the code to stop the buzzer at the end of the [full code ]
here --- if(buf == 'a' &&) {

  • digitalWrite(led, LOW);*
  • digitalWrite(speaker, LOW);*

}
what i will write after && and where do i need to change inside the code ??
i have already removed (time) to make the buzzer always on but need the rest of it
R2.txt (1.37 KB)

Sorry for resurrecting such an old thread. I've been fighting with my HC-SR04 for days, and I'm completely unable to turn it off for lower consumption. During sleep, it still consumes between 4-15mA.

Since powering it from a digital pin does not draw enough current and gives invalid values, I have to power it directly from a 5v step-up regulator. I tried using a transistor (2N2222 NPN, I'm not sure if it's ideal for this task) with a 1kΩ resistance, but the consumption is roughly the same, even if you shut down the GND line. Actually, if you just disconnect the GND the sensor still gives correct values (other cables must act as ground). Moreover, setting the trigger pin to output didn't help either.

What I am doing wrong? I think I've tried everything already. Maybe it's the kind of transistor I'm using?

i used the IRF530, IRF540 & IRF640, none of these can switch the positive side of the load (i.e load between source and ground).

the the reason i wanted to do this, is because i want to control the flow of current the DC-DC booster (3.3v to 5v), as it would still consume power even there is no load on the 5v side.

any ideas ?