I have an arduino mega that will be communicating through serial to a pc. One of the things I want it to tell the pc is whether or not it is connected to a wall power to 12V converter (where there is a switch between the arduino and the wall power). I was just wondering if there was a way to do that in software without having any additional hardware.


1 Answer 1


No, you can't do it without extra hardware.

You need to pass VIN through a voltage divider so it sits within 0-5V when powered by 12V, then feed that into an ADC input. Protect the on-board voltage regulator with a diode between 5V (anode) and VIN (cathode). Then you can measure the voltage on VIN and make a decision on if it is around 4.5V or 11.5V

A 22K/10K voltage divider will reduce 12V down to 3.75V, which gives plenty of "headroom" for over-voltage situations (where your 12V unregulated power supply actually provides, say, 15V):

enter image description here

  • I don't think the diode is needed. You say it is needed for protection. Are you sure the diode is needed?
    – jantje
    May 24, 2016 at 11:52
  • Yes. Without it you will be drawing current backwards through the 5V voltage regulator when powered by USB. The diode bypasses the regulator when VIN is at a lower voltage (i.,e., not connected) than 5V.
    – Majenko
    May 24, 2016 at 11:54
  • Ah, oh well. Thanks for the hardware implementation too, at least its super simple!
    – NotCras
    May 24, 2016 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.