Power consumption riddle

I am using a Distance Sensor GP2Y0A51SK0F

I connected it directly to Arduino Pro Mini 5V:
red to vcc 5v
black to ground
white to A0
no resistors or other hardware.

When going to sleep it still takes about 15 ma.

I need help to understand if I can make the sensor sleep from code or do I need to add a resistor to make it the power consumption way lower.

void setup() {
  Serial.begin(115200);

}
// Maximum value of times_read_average:64  (2^16/1024)
unsigned int times_read_average = 32;
unsigned int sensorA_v = 0;
 
int sensor_diff = 0;

unsigned int sensorAB_distance = 10; //cm

// 2~15cm
//                      2     3    4    5    6    7   8     9   10   11   12   13   14   15
float table_voltage[] = {2.08,1.68,1.32,1.06,0.91,0.81,0.72,0.66,0.59,0.52,0.48,0.45,0.43,0.39};

typedef struct {
  float offset; //cm
  float distance; //cm,
  unsigned int adc; 
  float voltage; //v
  int pin;
}SHARP;
SHARP sensorA = {0,0,0,0,A0};
 

void loop() {
  
  for(int i=1;i<=times_read_average;i++){
    sensorA.adc = (sensorA.adc*(times_read_average-1) + analogRead(sensorA.pin))/times_read_average;
  }
  
  
  sensorA.voltage = sensorA.adc * (5.0 / 1023.0);
  

  for(int i=0;i<14;i++){
    if(sensorA.voltage<= table_voltage[i]){
        sensorA.distance =i+2;
    }
     
  }
 

   
  Serial.print("R: ");
  Serial.print(sensorA.adc);
  Serial.print(" , ");
  Serial.print(sensorA.voltage);
  Serial.print("v, | ");
  Serial.print(sensorA.distance);
  Serial.println("cm");
  
  

    
     LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);    // 8s is max for low.power lib, but with a 65 loops = 10mins
       
 
}

Thanks!

Use a MOSFET to switch ground of the sensor. (100 Ohm resistor in series with gate and one 10k to ground from gate) attach the gate resistor to a digital pin. Send output LOW before going to sleep and HIGH before taking a reading.

Per page 3 of the datasheet, the sensor consumes 12-22mA, so 15mA power consumption is expected.

You need to power it off (wolframore's suggestion is appropriate here) while sleeping if that power consumption is not acceptable.

I'm not sure what gave you the idea that a resistor could be used some how to lower power consumption here; it can't. At least if you want the sensor to continue giving accurate results. You need to switch it off, and a MOSFET is the correct tool (N-channel to switch the low side, or P-channel to switch the high side).

Dr Azzy I started to suggest a PFET but I just don’t like the idea of the pin being hot while off. It’ll work just fine it’s just me. In fact one of those thing the bugs me so much I might even invert the signal. Maybe it’s from working with things that need to fail safely. In this case it wouldn’t matter.

wolframore:
Use a MOSFET to switch ground of the sensor.

Output of the sensor is a ground referred voltage, so it's safer to disconnect VCC of the sensor.
Disconnecting ground could release the magic smoke.

Use a logic level P-channel fet, and switch high-side (the 5volt line).
A PNP transistor with suitable base resistor could also work here, and is easier to find (BC327).
Emitter to 5volt, collector to sensor VCC, 330 ohm resistor between base and pin.
A LOW on the pin turns the sensor on, a HIGH turns it off.
Leo..

Wawa:
Output of the sensor is a ground referred voltage, so it's safer to disconnect VCC of the sensor.
Disconnecting ground could release the magic smoke.

Use a logic level P-channel fet, and switch high-side (the 5volt line).
A PNP transistor with suitable base resistor could also work here, and is easier to find (BC327).
Emitter to 5volt, collector to sensor VCC, 330 ohm resistor between base and pin.
A LOW on the pin turns the sensor on, a HIGH turns it off.
Leo..

I don't see how switching ground would be risky here? Since the arduino isn't driving any pins connected to it, just measuring the voltage, there isn't a risk of a voltage being applied to it when it's not powered.

Though, on closer consideration, switching the high side will produce slightly more accurate readings, since there would be a small but non-zero voltage drop across the FET.

A PNP BJT like the BC327 is not suitable - the Vce(sat) is generally around 0.7v, so with 5v supply, the device would only be getting 4.3v, but the datasheet for that sensor specifies that it required 4.5~5.5v. Use a PFET.

A 0.7 volt drop across a BC327 sounds remarkably high and a worst case at the fringes of the saturation region. Properly driven, it should be a fraction of that according to the data sheet‘s saturation region graph.

Edit:
High side driver packages are also available which contain an N and P channel mosfet so LOW switches it all off. Example TPS27081

@Bamanio

DO NOT CROSS POST.
OTHER POST DELETED.

Please read the posts at the top of the forum.

Bob.

Ok it’s a good point that a 0.1 Ohm Rds could drop maybe a few mV depending on the resistance of the sensor. But I’m not sure how a PFET would not drop any voltage? Usually the P channel cost a little more for the same Rds. In fact the highside switching would reduce the max voltage which might have a worse effect on accuracy than a slightly high zero. Transistors are great but not for this situation where we are trying to save power. The 4mA needed for saturation and the voltage drop from the diode junction makes it a worse choice.

Another possibility is to actually power the sensor from a Digital Pin -- with, of course, the recommended 10uF [or larger] capacitor across the sensor -- also include a series 130Ω resistor, to protect the output from the surge current caused by the initial charging of that capacitor, when that pin goes HIGH for the first time, and the surge that occurs when the pin goes LOW -- consider adding a Schottky Diode, with the arrow pointing towards the capacitor, to prevent discharging the cap -- BUT, such an addition might not allow the cap to charge high enough to consistently power the sensor [at least, not in time].

An Arduino UNO pin can source as much as 40mA without damage [though the recommended operating limit is 20mA -- but, in this case, because the 40mA demand is brief, not even that concern is pertinent]. Thus, the output can easily power the sensor. I leave it to the OP [or my fellow Forum-Flunkies] to determine if environmental, or safety concerns render this solution preposterous :wink: Also, take into consideration that, for the UNO, there is an overall ABSOLUTE MAXIMUM current limit of 200mA.

Powering the sensor from a Digital Pin, makes it very easy to power it down before going to sleep. Just send a LOW to that pin, before going to sleep, and upon waking, send a HIGH, then wait 35mS [if using a 10uF cap] before taking a reading.

Where did I get 35mS? A summation of the following:

  • Enough time to allow the 10uF cap to charge high enough to properly power the Sensor. The sensor needs at least 4.5V to be in spec. Since the cap will be charged by a relatively constant voltage, through a resistor, the simple Time Constant formula should be adequate to get us in the ballpark. And at 4 "Time Constants" the capacitor will be at 98.2% of full charge, and that works out to a voltage of 4.9V across the cap, providing 400mV "ripple room". Thus:
T1 = 4*130Ω*10µF = [b]5.2mS
[/b]

If it turns out that last 100mV is needed, then recalculate with 5 time constants.

  • Enough time for the sensor to get at least one reading. Since the worst case sample rate on this sensor is 50Hz, that's ~20mS, so lets call that T2.
  • The Datasheet stipulates a 5mS [maximum] delay before "First Output". So, T3 is 5mS
  • And to give the thing a little wiggle room , let's round that up to 35mS. So, T4 = 4.8mS
T1 + T2 + T3 + T4 = [b]35mS[/b]

CAVEAT: I based this on my best effort to collect all the pertinent information on using this sensor -- but I didn't actually try any of this, so, if that first reading is flaky, then consider giving the sensor more time, before accepting input from the sensor.

Please read datasheet page 314 notes 3 & 4. It is the VCC than can handle up to 200mA. Sourcing and sinking is by port. If this device is the only thing that’s in the project. It may be ok. Not ideal but ok. As a rule of thumb I like to switch or source accessories separately. Actually the power for the device may not matter as much as you guys are making it out to be. It’s not an absolute value that it sends but something that needs to be calibrated.

I am sorry, but your discussion is way beyond my understanding :slightly_frowning_face:

The only one I pretty much got was @wolframore

Can you please look at the board I attached and tell me if that was your intention or did I mess it up?

The orange/Blue/white are the Sensor Red/Black/White

wolframore:
Ok it’s a good point that a 0.1 Ohm Rds could drop maybe a few mV depending on the resistance of the sensor. But I’m not sure how a PFET would not drop any voltage?

It will, but the output per datasheet is spec'ed as a voltage referenced to ground, not as a fraction of Vcc. So a voltage drop between sensor ground and arduino ground will impact the readings, but one between arduino Vcc and sensor supply voltage should not (as long as the supply voltage is still in spec)

Good point. Either way a distance sensor would get calibrated. So the point may be moot

Hi @wolframore @DrAzzy

So I found this:

http://robojax.com/learn/arduino/?vid=robojax-sharp_IR

and it made me change the board to:

I attached the red to PIN 2 instead of VCC and did (also added 100uf Cap)

pinMode(2,OUTPUT);// define pin 2 as output
digitalWrite(2, HIGH);// set pin 2 HIGH so it always have 5V

then I added:

digitalWrite(2, LOW);// set pin 2 HIGH so it always has 5V
      delay(100);
      for (int i = 0; i < 65; i++) {
        LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);    // 8s is max for low.power lib, but with a 65 loops = 10mins
      }
 
      delay(100);
     digitalWrite(2, HIGH);// set pin 2 HIGH so it always have 5V

      delay(100);

everything works and the power consumption is down! :).

But, I noticed 2 things:

  1. the PIN2 gives different results (slightly) than the vcc pin
  2. it takes a few reading until it stays fixed on a certain voltage.

any suggestions?

Do I still need to implement:

MOSFET to switch ground of the sensor.  (100 Ohm resistor in series with gate and one 10k to ground from gate)

?

Nice glad you got it working. I don’t like sourcing something approaching max current on a I/O pin what happens if it fails short? Basically there are other ways to switch the power off on your sensor which is what you are doing. One possibility is a relay but too complicated and bulky and consumes power. Personally mosfet is a great solution which take almost no current to run and other benefits.

It should benefit from a small capacitor 10-22uF on the sensor power to ground.

Suggest reading through the datasheet it’s always a source of great information.

Bamanio you were warned about cross posting.

You are on a TIME OUT until you gave read and understand the rules of the forum as this is the SECOND TIME TODAY !

Bob.