Sense a 12v battery with Arduino

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
dlglaz
 
Posts: 17
Joined: Wed Nov 30, 2011 11:53 am

Sense a 12v battery with Arduino

Post by dlglaz »

I am having trouble understanding how to wire an Arduino to a 12v car battery to monitor when one of two circuits closes, or when they both do. Both circuits have a common input, and go out as two seperate channels. I attempted to use relays, but could not get the Arduino to dependably count both circuits as closing when it occured at the same time. My best guess was because of relay bounce.

I am now trying to monitor one circuit (first) with the following diag (which someone was nice enough to post somewhere) but can not get it to work. I actually took out the BC547 being a little frustrated and I think I fried the Arduino, two more coming FedEx tomorrow. Any suggestions would be appreciated, in laymen's terms please. :?
Attachments
TwoButton.gif
TwoButton.gif (16.36 KiB) Viewed 6631 times

meseta
 
Posts: 82
Joined: Tue Aug 02, 2011 7:57 pm

Re: Sense a 12v battery with Arduino

Post by meseta »

Hmm, that a bit of a weird way of doing things for monitoring just one channel since you can just use a pair of resistors to do the same. Anyway, I'll try to explain:

When the car battery is not connected, the BC547 will be "off", and so it won't let any current flow, and so the Arduino input will see +5V because it's connected to +5V through a resistor.

When the 12V car battery is connected to the circuit, it will turn on the BC547 transistor. The R1 resistor there is there to avoid killing the transistor (and is also necessary since the transistor is a current-controlled device, but that's not important in this circuit). When the BC547 turns on, it effectively acts like the Arduino pin was connected directly to ground. In this case, the Arduino would read a "low" signal on the pin. Even though it's being pulled up to +5V via the resistor, it will see close to 0 volts because the resistance of that resistor is far bigger than the resistance of the wire and transistor.

The 1N4004 diode I think is there for protection in case you get transient spikes from the 12V car side (and the 12V car side is really noisy with all the stuff going on and spark plugs going off, etc.)

I would have recommended an optocoupled circuit myself for safety reasons (avoids killing your arduino).

dlglaz
 
Posts: 17
Joined: Wed Nov 30, 2011 11:53 am

Re: Sense a 12v battery with Arduino

Post by dlglaz »

Thank you for the input, if you have a better way of doing can you point me to that?

meseta
 
Posts: 82
Joined: Tue Aug 02, 2011 7:57 pm

Re: Sense a 12v battery with Arduino

Post by meseta »

Can you tell me what your application is?

dlglaz
 
Posts: 17
Joined: Wed Nov 30, 2011 11:53 am

Re: Sense a 12v battery with Arduino

Post by dlglaz »

The following attempt at a schematic shows a machine A & B. There is up to 100ft of extension cord run from the + side of the circuit to a switch that can either close one, or both (independantly or together). The return is to a switch and is still the positive side of the 12V battery. There is a switch on the machine of some type that closes and allows the machine to operate when closed. The two relays shown on the left close for a short period (less than a second) so it is really a quick pulse of 12v + that allows the machine to operate.

Hope that helps, thanks.
Attachments
Machine Schematic.gif
Machine Schematic.gif (39.12 KiB) Viewed 6589 times

User avatar
adafruit_support_bill
 
Posts: 88088
Joined: Sat Feb 07, 2009 10:11 am

Re: Sense a 12v battery with Arduino

Post by adafruit_support_bill »

run from the + side of the circuit to a switch that can either close one, or both (independantly or together). The return is to a switch and is still the positive side of the 12V battery.
You mean both sides of the switch are connected to +12v :?:

dlglaz
 
Posts: 17
Joined: Wed Nov 30, 2011 11:53 am

Re: Sense a 12v battery with Arduino

Post by dlglaz »

Yes, I am only closing the circuit to allow it get back to the machine and do it's thing.

User avatar
adafruit_support_bill
 
Posts: 88088
Joined: Sat Feb 07, 2009 10:11 am

Re: Sense a 12v battery with Arduino

Post by adafruit_support_bill »

Yes, I am only closing the circuit to allow it get back to the machine and do it's thing.
Not sure I understand that. It is not a circuit if everything is connected to the same thing.

Are you trying to say that that one side of the switch is connected to the +12v on the battery and the other is connected to the machine? It might be clearer if you actually showed the switches in your diagram.

You diagram has wires labeled "+ Battery 12v" and "Return 12V+". Are these the same thing?
Where is the battery in the circuit?
What are the connections to the Relay?

dlglaz
 
Posts: 17
Joined: Wed Nov 30, 2011 11:53 am

Re: Sense a 12v battery with Arduino

Post by dlglaz »

Sorry, I did not save that schematic. The Arduino will basically act as a watchdog, counting the number of times the two relays close the two circuits (chanels might be a better description). So all I'm really trying to do here is have the Arduino keep track of each machine and how many times it works. It is then output to an LCD. I do not have an electronics/eng back ground so my use of terms is off I know.

meseta
 
Posts: 82
Joined: Tue Aug 02, 2011 7:57 pm

Re: Sense a 12v battery with Arduino

Post by meseta »

If I've understood your schematics and explanation correctly, what you want is really just to monitor two separate lines for the presence of 12V (I assume this be the two lines labelled "12 V return", and count how many times each of these lines turns on, am I right?

If so, the next question is: the 12V power supply, you're saying this is a 12V battery, will this be the same power source you are going to use to power the Arduino? Or will the arduino have its own power supply system. Can you tell me about these power systems, and what else might be connected to them?

The reason I ask is because firstly, there are some situations where you might want to isolate the two power systems, and therefore require a slightly different circuit. Also large power-hungry systems connected to the system may also cause voltage fluctuations and spikes, which you would want to protect the Arduino from, and so again would need a slightly different circuit.

dlglaz
 
Posts: 17
Joined: Wed Nov 30, 2011 11:53 am

Re: Sense a 12v battery with Arduino

Post by dlglaz »

I will be using a 9v battery to power the Arduino if I can go to some type of field use for this, but otherwise I think you interpreted my intentions well, thanks.

meseta
 
Posts: 82
Joined: Tue Aug 02, 2011 7:57 pm

Re: Sense a 12v battery with Arduino

Post by meseta »

Measuring whether there is 12V on the "12V return" line is very very simple, since you only need to detect whether the 12V is switched on or not, then simply use a potential divider, this scales the 12V down to 5V, and your Arduino pin just reads this as a digital value, HIGH if 12V is on the line, and LOW if 12V is not. here's a quick schematic for one channel. Just duplicate this for both channels
Image

The above example requires the GND of the Arduino to be tied with the GND of the 12V system, from what you've described of your system, this seems to be a fairly safe thing to do, however in the even that you need electrical isolation between the Arduino and the 12V side, here's a schematic that uses an optocoupler/optoisolator. I haven't included resistor values because these depend on the exact optocoupler chip used.
Image

I'm don't use Arduinos myself (I use Forebrain boards, which are ARM Cortex-M3 microcontroller dev boards), so you'll have to ask someone else to fill you in with the Arduino-specifics and coding, but I hope this helps.

dlglaz
 
Posts: 17
Joined: Wed Nov 30, 2011 11:53 am

Re: Sense a 12v battery with Arduino

Post by dlglaz »

That was very helpful. I did get a Optocoupler to work, but I had to put a 1k resistor in line and when I ran the return to the machine as a field trial this weekend it did not fire. I was told by the manufacturer that if more than 800ohms were present it would not work, for them that is equivalent of two extension cords. I don't know if I can get an optocoupler that can take the full 12v in or not, but I assume I can. I will have to look up the specs, otherwise I like your idea and will try that next. I have th code working so I have gotten past that point.

meseta
 
Posts: 82
Joined: Tue Aug 02, 2011 7:57 pm

Re: Sense a 12v battery with Arduino

Post by meseta »

Yes, most optocouplers are fine with 12V since you would normally size the resistor correctly for the optocoupler's spec and your supply voltage.

1k resistors are fairly high, but it depends on what optocoupler you use - they have different properties, some will work with 1k at 12V, others wont. You can also increase the other resistor on the Arduino side of the circuit if you think the 1k on the 12V side is too large.

If you tell me the model/part number for the optocoupler, I can tell you how to choose the correct resistor value.

dlglaz
 
Posts: 17
Joined: Wed Nov 30, 2011 11:53 am

Re: Sense a 12v battery with Arduino

Post by dlglaz »

The Part Number is 4N33....thanks.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”