Turn on and off Ultrasonic sensor (HC-SR04)

I got the project where I want to turn on and off Ultrasonic sensor for short amount of time to save the power. I read the old topic about this problem but could not get it work. Currently there are 2 ways I found is:

  1. Connect HC-SR04 to 4 series digital pin.
#include <RTClib.h>

#include <SD.h>

const int trigPin = 7;//2;
const int echoPin = 6;//3;
const int gnd = 4;
const int vcc = 5;

long duration;
int distance;

void setup() {
  // put your setup code here, to run once:
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(gnd, OUTPUT);
  digitalWrite(gnd, LOW);
  pinMode(vcc,OUTPUT);
  digitalWrite(vcc,HIGH);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(gnd,LOW);
  digitalWrite(vcc,HIGH);
  delay(100);
  // put your main code here, to run repeatedly:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  

  distance = duration * 0.034 / 2;

  Serial.print("Distance: ");
  Serial.println(distance);

  
  digitalWrite(gnd, HIGH);
  digitalWrite(vcc,LOW);
  delay(250);

}

Problem with this one is the current consump by sensor is pretty higher than normal. Not sure what is wrong in there??!
2) Control by using MOSFET(here I tried with IRLB8743)

#include <RTClib.h>

#include <SD.h>

const int trigPin = 7;//2;
const int echoPin = 6;//3;

const int mosfet = 5;

long duration;
int distance;

void setup() {
  // put your setup code here, to run once:
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(mosfet, OUTPUT);
  digitalWrite(mosfet, LOW);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(mosfet,HIGH);
  delayMicroseconds(1);
  // put your main code here, to run repeatedly:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  digitalWrite(mosfet, LOW);

  distance = duration * 0.034 / 2;

  Serial.print("Distance: ");
  Serial.println(distance);


  delay(250);

}

The problem with this is I can not measure the current when the sensor TURN OFF. The current is always about 2.93mA.

Can someone please show me the proper way to turn on and off this particular sensor. Thank you.

no0b31:

  1. Connect HC-SR04 to 4 series digital pin.

No need to have pins for Vcc and GND. Vcc is enough; GND can be connected to your common GND, then you simply set Vcc pin to HIGH to enable the sensor, set it to LOW to disable it (that way both Vcc and GND are connected to GND, and there can be no current between the two). This assuming the sensor uses <40 mA - check the spec sheet.

Otherwise the same can be done with a MOSFET. When your port is LOW, and the MOSFET is closed (assuming N-channel, low side switch), no current can flow.

If you're afraid TRIG and ECHO pins produce current, set them to INPUT and they're closed off as well.

I did get it work by pull the VCC to LOW->current is about nearly 0. However, I try to set TRIG and ECHO to INPUT, the current even rise higher than usual :slight_smile: