Go Down

Topic: Turn power on and off to ultrasonic sensor (HC-SR04) (Read 31182 times) previous topic - next topic

ks1000

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.

johnwasser

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.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

knut_ny

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

petervojtek

#3
Feb 13, 2014, 07:19 am Last Edit: Feb 13, 2014, 07:21 am by petervojtek Reason: 1
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?

johnwasser


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. 
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

sillyvalley


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

amekor

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!

petervojtek


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:

Code: [Select]

  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;


Jin

I'm going through the exact same issues with my own SR04 project (http://hackaday.io/project/1218-Ultrasonic-Parking-Aid-without-Arduino). 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?

Jin

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?

johnwasser


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 (http://www.digikey.com/product-detail/en/RSQ015N06TR/RSQ015N06CT-ND/3769399).
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Jin

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:
Code: [Select]

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);
}

giovanne

Hi,

this thread is a little bit old, but topic is actual for me ;-)

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: http://forum.arduino.cc/index.php?topic=216079.0 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

juan3211


juan3211

#14
Apr 09, 2015, 05:05 pm Last Edit: Apr 09, 2015, 05:18 pm by juan3211
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):

Code: [Select]

  // 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

Go Up