How to detect my source of power, from Vin to USB?

I read, in the NodeMCU pinout that it can be powered from the Vin pin.

I want your help to know how can I detect that my nodemcu is being powered only from this pin, instead of USB, which will be my main source of power.

Context:
I need that to put the NodeMCU into sleep mode and make it only count the time passed, using a rtc library (which will be defined yet, but it will probably be DS3231.h), to be used when the main power be on again, waking it up and doing my applications. This is to make my code no need to sync with the NTP every time I power it up, because my code can't run without it being synced, else my application won't work, and because the Vin pin will be powered by a battery or capacitor, so I need to reduce the energy consuption.

My code is probably not neccessary

Thanks for the help

There must be a voltage regulator on board that converts the 5V USB to 3.3V controller voltage. So you have a chance to measure the Vin voltage and if it drops below 5V you know that USB is detached.

Details depend on the actual power control circuit, that should prevent your battery from being overloaded or shorted by the 5V USB. The Uno disconnects the USB power if a reasonably high voltage (>7V) on the external supply pin is detected.

I didn't understand. How can I measure the Vin or USB voltage on code? Is there a circuit you can show to clearfy my mind?

I'm not using UNO, I'm using NodeMCU V3, and the Vin won't go over 7V in my application.

I did several tests using osciloscope, to make sure the correct tension was coming, and although the NodeMCU datasheet says the Vin should receive 3.3V, my board only worked with ~5V, so I don't think the idea of converting the voltage will work. I don't know, maybe I understand things wrong.

Connect it to analog input pins. Use a voltage divider for voltages higher than Vcc.

And that can be made by the Vu pin, right? I mean, the measurement of USB voltage.

I made a test and it gave 0V in every case. Should I configurate it first?

Solution

I made a tension divisor. It worked.

With the USB, the pin VU sent 5.66V. With the Vin pin, VU sent an destabilized value, that goes below 0.07V.

My divisor uses resistors of 3.9k[ohm] and 2.85k[ohm], decided by using potenciometers in laboratory with the help of an oscilloscope. It also seemed to work with 6.45k[ohm] and 400[ohm], but I haven't made the circuit analysis for this case because I should avoid resistances with low values.

The tension was sent to the pin D6.

The tension is from D6 to VU by the 2.85k resistor.

The board could, then, understand the situation with the code below:

    if(!digitalRead(12)){
      if(millis()%400<10){
        if(luz){
          digitalWrite(LED_BUILTIN, HIGH);
          luz = !luz;
        }
        else{
          digitalWrite(LED_BUILTIN, LOW);
          luz = !luz;
        }
      }
   }

'luz' is a boolean.

In my future application, the code will put the board in sleep mode, which is something I'm researching at the moment.

I will be testing the circuit and giving feedback if it gives any problems.

OBS: Do not use the pins with the S, like I did.

Leaving this topic open for any discussions.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.