12v to arduino pin - zener diode?

Hello,

So I used the voltage divider strategy where I have the 100K going to ground of the arduino and 200K going to pin. Just need to confirm should i use the ground of the arduino or the ground of the switch?

The solution seems to work... kinda. When I turn the switch on , I do see the switch ON state but it also revert right away to the OFF state even if switch is still ON. If I switch it off and back on, same result.

Here is my code:

const int ArmSwitchPin = 8;
int ArmSwitchState = 0;
int ArmSwitchText1 = 0;
int ArmSwitchText2 = 0;

void setup() {
  pinMode(ArmSwitchPin,INPUT);
  Serial.begin(9600);
}

void loop() {
  ArmSwitchState = digitalRead(ArmSwitchPin);
  if (ArmSwitchState == 0) { //ARM SWITCH OFF!
    ArmSwitchText1 = 0;
    if(ArmSwitchText2 == 0){ArmSwitchText2 = 1; Serial.println("SYSTEM DISARMED!"); delay(500);}
  }else if (ArmSwitchState == 1){ //ARM SWITCH ON!
    ArmSwitchText2 = 0;
    if(ArmSwitchText1 == 0){ArmSwitchText1 = 1; Serial.println("SYSTEM ARMED!"); delay(1000);}
  }
}

The ArmSwitchText variable is just a flag so my serial print doesnt get flooded.
So if I look at my serial monitor, I see SYSTEM ARMED and after the 1 second delay, SYSTEM DISARMED appears.

Any idea?

When you inline your code, it may be more compact, but it's harder for the rest of us to read. I would prefer you structure it this way:

const int ArmSwitchPin = 8;
int ArmSwitchState = 0;
int ArmSwitchText1 = 0;
int ArmSwitchText2 = 0;

void setup() {
  pinMode(ArmSwitchPin,INPUT);
  Serial.begin(9600);
}

void loop() {
  ArmSwitchState = digitalRead(ArmSwitchPin);
  if (ArmSwitchState == 0) { //ARM SWITCH OFF!
    ArmSwitchText1 = 0;
    if(ArmSwitchText2 == 0){
      ArmSwitchText2 = 1; 
      Serial.println("SYSTEM DISARMED!"); 
      delay(500);
    }
  }else if (ArmSwitchState == 1){ //ARM SWITCH ON!
    ArmSwitchText2 = 0;
    if(ArmSwitchText1 == 0){
      ArmSwitchText1 = 1; 
      Serial.println("SYSTEM ARMED!"); 
      delay(1000);
    }
  }
}

That said, it appears, to me, that the code is not the culprit, so I'm guessing it has to do with how you have your grounds connected. And, for the sake of consistent languaging, I'm going to assume, when you say "ground of the switch", you mean, the "ground on the side with the switch" (since the switch, itself, doesn't have a ground [unless I don't understand your switch]).

You need to connect the ground on the switched side, to the Arduino ground. They need to be connected together--you know, like I showed in my diagrams. Not showing, in my diagrams, is how the ground of the source, that's supplying the Arduino, also needs to have it's ground connected to the Arduino ground [in case you didn't know that is implied]. I'm guessing you don't have one or more of these grounds connected together, and the intermittent nature of your result is due to a capacitive effect.

In fact, the upper circuit, in post #9, should be similar to your voltage divider circuit, if you replace the diode with a 100k resistor, and replace the 10k resistor with a 200k resistor.

Other than that, I have no clue as to what is going on, and probably a schematic of your set up, and even a photo or two, would help, especially if anything I said, above, is incorrect.

Thanks for your reply. when i said ground to the switch i meant on the 12v circuit where the switch is, maybe i got confused as well since the switch does have a ground since its illuminated with a led :slight_smile:

Right now i only have 1 ground connected... coming from the 100k to the ground on arduino.

i know fritzing sucks and ill start using diagrams but the one i had attached is how it is right now. am i missing a ground?

sorry i am not home in front of it so ill post a pic when i get home if necessary

Something like this?
div1.png

yes except the part where the ground of the switch circuit connects to arduino.

would the ground coming from the switch circuit require a resistor before going to the arduino ground? yes im still learning :slight_smile: so expect stupid questions

No, connect Arduino GND directly to 12V - (ground).
NOTE: Assuming illuminated switch is made for 12V and has proper current limit resistor for LED installed.

Bingo! Thanks alot :slight_smile: :slight_smile:

and yes the switch has a builtin resistor.

All working good now... no smoke!
Thanks alot for your time... learned a great bunch already!

nope charging is done not connected. That circuit is only used to provide 12v to the COM on my relays but I wanted to know if there is power going to my relay or not.. like a system check basically. If the switch is on... its live!

Its not a system running 24/7 so I dont need "online" charging

NickLavigne:
i know fritzing sucks and ill start using diagrams but the one i had attached is how it is right now.

You know, Fritzing also has a "Schematic Capture" feature. You just gotta click on the Schematic tab. But, there are other options -- including numerous free options.

I use DipTrace. It's an easy [intuitive] to use full CAD package including Schematic Capture, and PCB layout -- plus it has features for creating your own parts. There is a full feature Free version that limits the number of connections and board size, but it's generous enough for most hobbyist projects.
There is also Eagle by AutoDesk [with a Free version with limitations similar to DipTrace]. The interface is old-world CAD, and thus is not like the typical WYSIWYG editor, like Word or Photoshop, so you have to learn a different GUI standard. Sparkfun has a great tutorial: Using EAGLE: Schematic - learn.sparkfun.com
I became quite adept at Eagle, then after a long spell away from Electronics [developing websites], I dusted Eagle off and was at the bottom of the learning curve! So, I opted for DipTrace's more standard [i.e. Common User Interface or CUI] style GUI.
Another option is ExpressPCB. I started out on this, decades ago. But then they upgraded their software, rendering ALL of my designs uneditable! They provided NO TRANSLATION TOOL, at the time!! Years later they came up with a way to upgrade old designs, but by then I had moved to Eagle CAD. They provide, for FREE, a very powerful, unlimited CAD system, with the whole deal [Schematic/PCB/Component Editor/etc.] -- and it's been stable ever since that unfortunate event.
And there are other options, but having never tried any of them, I can't vouch :wink:

Thank ill check them out. I just installed KiCad which ill try as well... the diagram tab in fritzing looked weird :slight_smile:

Alternatively....... could even consider an illuminated double pole single throw switch. One side energises the circuit that requires 12V .... and the other switch portion could connect arduino 3.3V or 5V to an arduino monitoring pin.

actually... this answered my question i had... i've seen these and stayed away from them because... i only needed 2 pins haha... see? newbie here :slight_smile:

ReverseEMF:
Probably should use more like a 4V Zener. 5.1V Zener will, likely, allow too high a voltage to reach the Arduino input.

What are you talking about?

bos1714:
What are you talking about?

I'm not sure what you're unclear about? But, assuming you mean "why 4V and not 5.1V?": Because, in a worse case scenario, a 5.1V zener will result in a voltage that might exceed the maximum input voltage on an Arduino.

No tolerance was specified on that 5.1V Zener, so the OP could easily wind up with something like a 1N5918A, with a 10% tolerance. 10% of 5.1V is an upper voltage of: 5.61V.

The ATmega328/P datasheet states that an input voltage not exceed VCC+0.5V.

On an Uno, the lowest voltage expected [based on the datasheet] from the NCP1117ST50T3G regulator, is 4.90V [i.e a *VCC* of **4.9V**]. 4.9 + 0.5 = 5.4V So, if 5.61V is applied to an input, on an Arduino running at, worse case, 4.9V, that input will very likely be damaged.

A 4V 10% Zener will, even at the lowest end of the 10% tolerance [3.6V], produce a high enough voltage to be interpreted as a solid HIGH, by the Arduino, yet, even at the top of it's tolerance range [4.4V], will never exceed the maximum input voltage of the Arduino.

A major problem is the slope resistance.
see

A 4.3 volt BZX85 series vishay zener has a slope resistance of up to 500 ohms at 1mA.

Hence a change of current of 1 to 2 mA makes a difference in voltage of up to 1/2 V.

As previously noted, zeners are b* awful.

For this job a resistive divider would be better and cheaper.

Allan

allanhurst:
For this job a resistive divider would be better and cheaper.

Absolutely agree :wink:

The resistor value selection is a bit more critical. You need to select values that will keep the input voltage within acceptable range. I.e. between 0.6VCC and VCC+0.5V. And, this is in an environment where the voltage being tested can vary quite a bit. Also, VCC can vary by as much as 0.2V [from part to part]. That’s why I suggested my two circuits, to pin those variations down and remove as much ambiguity as possible.

allanhurst:
For this job a resistive divider would be better and cheaper.

Agreed

NickLavigne:
actually... this answered my question i had... i've seen these and stayed away from them because... i only needed 2 pins haha... see? newbie here :slight_smile:

hahaha! Yep ..... sometimes, it's like 'why 4 pins when you can use 2?', and sometimes .... 'why 2 pins when you can use 4?'.