Go Down

Topic: Help reading voltage on an analogread pin (Read 9545 times) previous topic - next topic


Hey all, i am posting this aside of the main forum post (http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=69887&postdays=0&postorder=asc&start=60) that has been going on for a few months now. I recently had time to work on some of it and i am not getting the results that i need. So i am looking for help on the problem.

Here is part of the post from my main forum post:
Here a start how you can detect 12V wit a pin of the AVR.

Fuse 10 (Accessory Socket) looks to me like a good 'victim' to get 12V when the switch in in 'Acc' position. NOTE THAT THE PIN IN THIS PICTURE IS NOT CONNECTED TO A ANALOG PIN.. THAT HAS BEEN CORRECTED.

Ok, going back to this.. i got the resistors today to do this and its all hooked up... but i am unable to get the values i was expecting using this code:
Code: [Select]

int analogPin = 3;
int val = 0;

void setup()
 pinMode(analogPin, INPUT);

void loop()
 val = analogRead(analogPin);

When connected to the volts from pin 3 I get values of:
Code: [Select]


And when i disconnect the voltages from pin 3 it looks like this:
Code: [Select]


My voltmeter reads the volts going into the Arduino as 4.12 volts dc.

So why am i getting those other off the wall values when i am looking for a number from 1 to 4? And if there is nothing connected to it then the number should be 0?

I have been looking at this design here by member Shadowcat

Now some background: I am attempting to make my own Start up/Shutdown controller for my laptop for the car. Once it detects current on a 12v line then i want it to wait 5 seconds until it sends to start the pc (push the button) and if it detects no volts on the 12v line then wait 5 seconds and send the push again to turn off the pc.

So any help would be great! :)



And if there is nothing connected to it then the number should be 0?

No, not necessarily. The Arduino pins are hight impedance inputs, and cat 'float' if left unconnected.  That is, they can take on virtually any voltage value between 0 and 5V.  Unless, that is, you have a resistor connected that will 'pull' the pin down to ground.  The 65k resistor should do this in your system.

When you disconnect from pin 3, do you leave the 56k resistor connected, or do you leave the pin completely open-circuit?

And, are the grounds connected together, from the 12V supply's ground to the Arduino's ground?


Anachrocomputer: Where would i connect the 65k resister to pull 0v when there is no 12v current coming through?

The 56k and 100k are connected still when power is cut off to the 12v.

No, the ground is not connected together. They are both separate. I'm just using a 12v brick for testing right now. The Arduino is powered off of the USB cable.

So how can i read the volts as my multimeter does? I need to know when its 1v or less so i know when the 12v has been cut off and i also need to know when its 2v or higher so i know that the 12v is turned on...



Jan 31, 2009, 04:45 am Last Edit: Jan 31, 2009, 04:47 am by halley Reason: 1
You need to connect the grounds between two power systems.  Otherwise, the analog pin has no idea how to relate its own ground (USB ground) to the ground nor 12v supply.  Just run a black wire between your 12V brick's negative pole and the GND pin of the Arduino.

The "65k resistor" was a typo, he was just referring to your first drawing, the 56k resistor.  The 56k and the 100k form a voltage divider to ensure you are not exceeding 5V on the input.  (But only if the Ground below the 56k resistor is the same as the Arduino's GND.)


No, the ground is not connected together. They are both separate.

There's the problem!  Connect those grounds together!

And yes, 65k was a typo; sorry for not spotting it and causing confusion.  The 56k should remain connected from Arduino pin to (common) ground at all times, to ensure that the analog input pin does not float.


Jan 31, 2009, 07:34 pm Last Edit: Jan 31, 2009, 08:23 pm by StealthRT Reason: 1
YAY!! Grounding it with the Arduino ground fixed that problem. Now i am getting 0 when disconnected form the 12v supply and 4 when connected!

Now i have another question... How would i get the 5v that comes out of digital pin to 0v so that i can use that as the "switch" to turn on the laptop? Would i just use a 56k resistor?



Grounding it with the Arduino ground fixed that problem.

Good news!  Glad to hear that it worked!

How would i get the 5v that comes out of another analog pin to 0v

But that doesn't make sense, to me.

Would i just use a 56k resistor?

There's nothing magic about 56k resistors.  It's the ratio of 56k to100k that makes the voltage divider work:


Now, to drive a relay that can switch on the power, use the circuit in the lower right-hand corner of the diagram that you posted earlier.  That is, a resistor, transistor, diode and relay.


OK i see the parts for the relay stuff:

1 - 1000k resistor (1K?)
1 - 5v relay
1 - 1N4004 transistor

But how about the BC548 and the HE3321? I cant really find anything on them...



Jan 31, 2009, 08:53 pm Last Edit: Jan 31, 2009, 08:54 pm by anachrocomputer Reason: 1
Hmm.. the base resistors are labelled as 2000, which is of course 2k Ohms.  Not a value that you'll usually find!  1k should be OK there.

The 1N4004 is a diode, not a transistor.  It's rated 1A and a few hundred volts.  Similar diodes are 1N4001, 1N4002 up to 1N4007, differing only in voltage rating.

The BC548 is the transistor, and any general purpose NPN transistor should do.  BC-numbers are common in Europe, but 2N-numbers are (I think) more common in the USA.  A 2N2222 transistor, or 2N3704 would be a good substitute.

Looking at it again, the diagram is not well laid out to show clearly which part number relates to which device.
The HE3321 is the part number of the relay.  As it says, "5V mini relay".


Jan 31, 2009, 09:14 pm Last Edit: Jan 31, 2009, 09:15 pm by StealthRT Reason: 1
OK Anachrocomputer, do you mind checking my parts list to make sure its what i am in need of for this?

  • 2k Ohms = (mfr. Part#: 271-2K-RC)1/4WATT Metal Film Resistors 1/4watt 2Kohms 1%

  • 1n4004 = (mfr. Part#: 1N4004RLG) Rectifiers- Standard and Fast Recovery 400V 1A Standard

  • BC548 = (mfr. Part#: BC548CTA) Small Signal Transistors NPN 30V 100mA HFE/8

  • HE3321 = (mfr. Part#: JS1A-5V) Power Relays 10A 5VDC SPST SEALED PCB



My only worry is that the 5v relay's output needs to be a ground since its just acting as a momentary switch for powering up the laptop... In the drawing, it looks as though its sending some volts to the laptop? Or is the 1N4004 taking it down to 0v once it exits the relay?



You can wire up the contacts of the relay any way you like, they don't have to connect to the 12V supply.  That's one of the benefits of a relay, the contacts are completely isolated from the rest of the circuit.  The 1N4004 diode isn't even connected to the contacts.

You didn't say which supplier those part numbers refer to!  But all the descriptions sound OK.  You can make the resistor 1k if you like.  Also, if you're just operating the laptop's power switch, you won't need a relay rated at 10Amps, a 1Amp one will be plenty.

Since the diagram(s) are causing some confusion, I've drawn another (yet another), which I think is clearer.  Could you take a look and see if it makes things clear to you?


Probably best to view it full size!


Hey thanks for taking the time to draw that up for me.

The part numbers come from http://www.mouser.com

So i take it that the NC is what is connected to the laptop button? But i know that there is another wire that is needed in order to make the contact across the laptop switch.. So the other side has to be a ground as well? So hook that up to the "common" part?
Code: [Select]

NC=======>[L SWITCH R]<=======

Where the NC connects to the left side of the button pin and the Common connects to the right side of the button pin.

Or is it as simple as soldering just the NC wire to both sides of the button?



Take the two wires from the button, connect one to "common" on the relay, and the other to "normally open".


Alright, ill connected one side to Common and the other to NO. Was that parts list OK? I'm looking to order it today if so.

Also, The power brick that comes with the Asus EEE PC is rated at 12v output so i fiugred i would just hook up the 12v from the battery to the laptop. What kind of resistors or caps would i need in order to protect the 12v from not going over it when going into the laptop say, when the car is starting or something?


Go Up