3 (easy?) clarifications about switches.

Okay, this is actually part of something bigger against which I'm banging my head, which is the real reason I'm visiting the forums and asking for help.

But before I start with the bigger question I want to make sure I get the underlying premise right.

I am brand spanking new to all things Arduino... [edit to remove irrelevant narrative adventures of the Noob]

QUESTION 1 -- Can someone please confirm that, referencing the attached (forgive my fabulous artistry) circuit diagram, I should be able to do a digitalRead(input_pin) and get HIGH when the switch is closed and LOW when it's open?

I direct you in particular to the fact that the power supply for the switched circuit is separate and independent from the arduino. it's just its own little 9v battery.

QUESTION 2 -- Do I need the resistor in the circuit? If so, QUESTION 2A - why? Is it only to prevent a raw 9v from feeding to the Arduino input? or do I somehow need the load to make the measurement work? And then again, why?

QUESTION 3 -- does it matter where I put the wire going to the input pin. Right now it's attached to point A, which is after the switch, before the resistor. Could I hook it up to point B or C ? I suppose I could breadboard that... but please answer me anyway.

switch diagram bdm01.jpg

As noted, this is part of a bigger problem I'm having, but before I start even trying to explain that one, I want to make sure that I've got my brain wrapped around this bit.

Thanks in advance!

--BDM.

switch diagram bdm01.jpg

Too late here to try to understand your problem.

  1. You never place a voltage larger than the Arduino supply voltage (5v or 3.3v) on an input.
  2. You can use an opto isolator circuit to monitor larger voltages with an Arduino.
  3. See image below for typical switch monitoring.

2017-08-05_20-33-24.jpg

bdmanevitz:
Okay, this is actually part of something bigger against which I'm banging my head, which is the real reason I'm visiting the forums and asking for help.

But before I start with the bigger question I want to make sure I get the underlying premise right.

I am brand spanking new to all things Arduino... [edit to remove irrelevant narrative adventures of the Noob]

QUESTION 1 -- Can someone please confirm that, referencing the attached (forgive my fabulous artistry) circuit diagram, I should be able to do a digitalRead(input_pin) and get HIGH when the switch is closed and LOW when it's open?

I direct you in particular to the fact that the power supply for the switched circuit is separate and independent from the arduino. it's just its own little 9v battery.

QUESTION 2 -- Do I need the resistor in the circuit? If so, QUESTION 2A - why? Is it only to prevent a raw 9v from feeding to the Arduino input? or do I somehow need the load to make the measurement work? And then again, why?

QUESTION 3 -- does it matter where I put the wire going to the input pin. Right now it's attached to point A, which is after the switch, before the resistor. Could I hook it up to point B or C ? I suppose I could breadboard that... but please answer me anyway.

switch diagram bdm01.jpg

As noted, this is part of a bigger problem I'm having, but before I start even trying to explain that one, I want to make sure that I've got my brain wrapped around this bit.

Thanks in advance!

--BDM.

Voltage is electrical pressure measured between 2 points. Call them + and - or VCC and GND (ground) the measure is only those 2 relative to each other.

So you have a circuit built around a battery run by the voltage between the ends of the battery. The + end and the - end.
You want to take some of that and measure it with an input pin, but is Arduino ground the same as the batteries'? Is there a flow path from your battery circuit to the input pin and back to battery -?

You stand some chance of your wire being negative relative to Arduino ground and killing the input pin that can't take more than -.5V.
You stand a chance of your wire being > +5.5V relative to Arduino ground and killing the input pin.
You stand a chance of not burning the pin and believing whatever garbage result you do get.

A very good thing for you would to be learning basic DC electric as a full course from what electrons and wires are to Ohm's Law at least. Did you take physics in school?

bdmanevitz:
QUESTION 1 -- Can someone please confirm that, referencing the attached (forgive my fabulous artistry) circuit diagram, I should be able to do a digitalRead(input_pin) and get HIGH when the switch is closed and LOW when it's open?

NO. The Arduino inputs measure a voltage relative to Arduino Ground. Since Arduino Ground is not connected to your 'circuit' the Arduino input sees only an open connection. The input voltage has to be less than 6V so connecting Arduino Ground to your 9V battery circuit would endanger the Arduino.

bdmanevitz:
QUESTION 2 -- Do I need the resistor in the circuit?

Yes.

bdmanevitz:
If so, QUESTION 2A - why?

Without a resistor, closing your switch would cause a dead short (near zero resistance) across the battery. This will drain the battery very quickly and may cause the battery or wire to heat up and catch fire.

bdmanevitz:
QUESTION 3 -- does it matter where I put the wire going to the input pin.

No. Without a Ground connection all your input pin sees is an open circuit (unconnected pin). The pin and wire act as an antenna and the input will read HIGH or LOW based on what kind of radio noise it sees.

QUESTION 1 -- Can someone please confirm that, referencing the attached (forgive my fabulous artistry) circuit diagram, I should be able to do a digitalRead(input_pin) and get HIGH when the switch is closed and LOW when it's open?

I direct you in particular to the fact that the power supply for the switched circuit is separate and independent from the arduino. it's just its own little 9v battery.

If we pretend you have a "safe" voltage (safe for the Arduino), say 3 AA batteries in series to give you 4.5V...

It can't be completely independent... You need a common ground*, then you can wire a battery like the [u]Digital Read Serial Example[/u]. But, you'd connect the switch to the battery instead of to the Arduino's 5V.

QUESTION 2 -- Do I need the resistor in the circuit? If so, QUESTION 2A - why? Is it only to prevent a raw 9v from feeding to the Arduino input? or do I somehow need the load to make the measurement work? And then again, why?

The resistor is needed when the switch is open to "pull up" or "pull down" the input.

That's because when there is no connection to an Aduino input it "floats"... It's undefined and it may read high or low. Its also very-high impedance which means very-little current is required to make the input high or low, it can pick-up electrical noise and switch between high and low.

When the switch is on, the pull-up or pull-down resistor is "overpowered" by the switch, which essentially has zero resistance. When the switch is on (closed) current flows through the resistor (higher resistance = less current). ...If you had a solid-connection instead of a resistor, you'd have a short when the switch is turned-on... Too much current would flow, voltage would drop to zero, and the power supply and/or switch might get "blown".

QUESTION 3 -- does it matter where I put the wire going to the input pin. Right now it's attached to point A, which is after the switch, before the resistor. Could I hook it up to point B or C ? I suppose I could breadboard that... but please answer me anyway.

Again, you need a ground reference, so you need 2 connections. The voltage on the Arduino's digital input should be (about) +5V or (about zero volts) relative to the Arduino ground.

You can connect through the resistor as long as the resistor is below the megohm range, because the Arduino's inputs are very-high impedance/or resistance (and because of Ohm's Law and Kirchhoff's Laws).

If you connect it backwards you'll have negative voltage (relative to ground) and that can damage the Arduino.

  • Voltage need a reference and that's usually "ground". It's not necessarily earth-ground and the reference doesn't have to be ground at all, but it needs a reference. For example, if you measure height you need to know the height relative to something... Maybe the height above ground, or the height above the floor in the room, or the height above sea level, etc.

PLEASE DO NOT TRY THIS, but you can touch the power line voltage if you don't touch ground and as long as that there's not a current-path through your feet to ground you won't get a shock. With no ground reference there is no voltage difference so you no current flows and without a reference you can't even measure the voltage.

Hi BDM,

Why do you want the switch to use its own little 9v battery?

First off, thank you all for your various helpful replies. I understand most, though honestly I've done some breadboard experiments that don't 100% match up to some of the info I'm getting here, but I can go play with that later.

To answer ChrisTenone:

My project involves putting the switches (and some relevant LEDs, but that's not important here) in positions relatively remote from the brains. I'm using stereo jacks and cable to do the connections, so I've got three wires to work with. I'm using one to power an LED, one for GND (on that LED) and then I want one to go to the switch-reading-input.

I'm using 9v batteries because I have a bunch of 9v battery compartments and 9v batteries lying around.

That said, from what y'all have told me... I have a follow up question. Can I read across a switch that is connected in PARALLEL to another, not-a-switch piece of circuitry, so that I can pull the power for the switch and run the switch ground to the other thing?

So like this:

new_idea 2res.jpg

or even, if I want to get greedy, like this:

new_idea 1res.jpg

I understand that the switch is not controlling the LED. I also understand that the switch will only be able to read closed (HIGH) when the LED is being powered.

new_idea 1res.jpg

new_idea 2res.jpg

“and some relevant LEDs, but that's not important here”
Says you :wink:

Suggest you ‘not use’ the SPST switch and go with a DPST/DPDT switch.

Monitor the second pole with the Arduino.

Okay - had to Google that. Now I know what a SPST and DPST switch are, but I don't really see how that answers my question.

Either way, though - I'm a bit committed to the hardware at this point.

I have a button. As far as I can tell, it's a simple switch - one lead in, one lead out. When the button is down, I want to be able to read HIGH. When the button is not down I want to read LOW.

Honestly, I thought I had this whole thing working...

(I don't know if I can link to Facebook pictures, or if so necessarily how to do so...)

But once I tried soldering it up it wasn't working, so here I am.

You can attach your image here.

Posting images:

bdmanevitz:
new_idea 1res.jpg

This will make the input read high all the time. If you take the wire from the led cathode to the input pin out then the input will only read high when the switch is closed. The led won't turn off then unless you make the output low at which point the input will read low.....

Both the output pin and the input pin measure voltage relative to chip gnd, those circuits are in the chip.

You could wire the led between the output pin and ground and wire the switch between the input pin and ground then pinMode the input as INPUT_PULLUP which supplies the input pin with weak 5V (through 20K to 50K resistance) that is safe to ground directly through the switch. Sure, switch open reads high and switch closed reads low but so - - what? You just code for what it is and you're okay, when the input is low you make the output high to light the led and when the input is high you make the output low to turn it off. That achieves an indirect connection between action of the switch and action of the led through software, demonstrates a form of abstraction. Don't say that in class unless you can say what abstraction is and just how it is demonstrated, of course.

Oooh, GoForSmoke, you beautiful son of a gun.

For your first suggestion (removing the line from the LED cathode to the input pin) - See (GoForSmoke idea 1), do I need a resistor between the switch and the input pin? I'm thinking no, since it's just reading the voltage off the output pin, so there's no risk of short. Unless I'm misunderstanding how shorting works.

GFS 1.jpg

On (GoForSmoke idea 2), I understand that the OPEN switch will read HIGH because of the PULLUP voltage, but won't the closing the switch still read HIGH because it'll be getting the voltage through the LED? (a la the pink dashed line?)

GFS 2.jpg

Thanks again!

GFS 1.jpg

GFS 2.jpg

GFS #2 is the way to go.

I have done a more-extreme version with only 2 wires. It looks a bit like your diagrams in reply #6 but I use the output pin as an input for very short periods so the eye can't detect that the LED went off 100 times per second.

This was for a button with an LED in it so you could not see that the LED was shorted out while your thumb was on the button.

Hi,
d415ab87995104020c39bda94468b88b33dca2ad.jpg
The LED current as you have drawn it will not occur, the LED current will flow to the gnd pin.
The input pin with pullup will be 5V with the switch open and at gnd when the switch is closed.
Because the switch is not part of the LED current circuit loop, it will not pass any LED current.

Tom… :slight_smile:

Sorry Tom but AVR inputs are through millions of ohms of impedance. A single non-INPUT_PULLUP read drains 1 microAmp of current. In that circuit, the input pin is also a 5V source.

This is in the datasheets.

I have used that 1 uA read current to drain a charged wire at over 50K reads/sec, counting the reads between HIGH and LOW as a form of very fast low-res ADC. That was for force-sensing piezo buttons.

GoForSmoke:
Sorry Tom but AVR inputs are through millions of ohms of impedance. A single non-INPUT_PULLUP read drains 1 microAmp of current. In that circuit, the input pin is also a 5V source.

The input pin with pullup will be 5V with the switch open and at gnd when the switch is closed.

I think that what is I said?
The OP is worried about LED current flowing through the switch and keeping the input HIGH, which it won't because of the gnd connection.
Tom... :slight_smile:

You're right. The magenta dashed line arrow and way the words were got me turned around.

4 more karma and you are at 1284. The forum should add a P to that just because.

OKAY… Everybody rocks, but I gotta say GoForSmoke rocked this one the hardest. That layout solved my problems and answered most – not all – of my questions.

Here’s the last question (for now):

When I first assembled the circuit, I reversed… something. I’m not 100% clear what. (I should have paid more attention.) I think it was where the switch comes in – that is to say I ran the switch between the OUT and IN instead of the IN and GND.

Here’s what happened, though - The circuit worked as planned for a few seconds, then didn’t.

I would have been fine with it just not working, or doing something else. But it worked fine for about two clicks of the switch, and then stopped.

Can anyone explain why?

Thanks.
–B

“When I first assembled the circuit, I reversed... something. I'm not 100% clear what.”
???
Now we find this out. :wink:

We cannot answer this, you said “something” which could mean anything.

Never ever reverse something, it let’s all the smoke out of something.

When the smoke is let out, nothing works as it should.

bdmanevitz:
Here's the last question (for now):

When I first assembled the circuit, I reversed... something. I'm not 100% clear what. (I should have paid more attention.) I think it was where the switch comes in -- that is to say I ran the switch between the OUT and IN instead of the IN and GND.

Here's what happened, though - The circuit worked as planned for a few seconds, then didn't.

I would have been fine with it just not working, or doing something else. But it worked fine for about two clicks of the switch, and then stopped.

Can anyone explain why?

Thanks.
--B

An IO pin set to INPUT won't get hurt by an OUTPUT pin. You filled the wire from the switch to INPUT pin with charge, it'll read HIGH for a whole lot of reads with the switch open before it reads LOW again. If the INPUT was INPUT_PULLUP the switch wouldn't work the first time either.

You gotta be way more careful or you will burn pin(s). If you have an Uno with socketed chip and you have (can make or buy) pre-bootloaded 328P chips then you can replace the burned-pin chip with a new one.

Had you wired an OUTPUT HIGH pin directly to an OUTPUT LOW pin, it could burn BOTH PINS AT THE SAME TIME!
Those pins max at like 40mA current source (HIGH) or sink (LOW) that YOU have to regulate (resistors) from going over. Direct wired, the LOW pin will suck current from the HIGH pin in excess of the limit so don't do that!