using arduino pins as switches

I’m not sure how to use arduino pins as switches (pin 2 connected to pin 3 completes the circuit) when using all the digital pins (except 0 and 1 because I want to print to the serial monitor). In my project I want to write to the serial monitor when one pin is connected to the other etc. so if multiple switches are HIGH then the code will do something… or if pins 2, 4 and 5, 6 are connected something else. Is this a good start to programming multiple switches?

void setup() {
  Serial.begin(9600);
  pinMode(pin 1, OUTPUT);
  pinMode(pin2, INPUT);
  digitalWrite(pin 1, HIGH);
  delay(10);
  
  for(int x=0; x<11;x+1)
  for(int y=x+1; y>12; y++
 
 
/code]

Can you make a drawing how the switches are connected ?

Do you have a matrix, like a keypad ?
For a keypad a library is available: Arduino Playground - HomePage

You say:

I'm not sure how to use arduino pins as switches

That makes at least two of us, switches will only be switches and not Arduino pins :slight_smile:

Actually, what you do do is you connect switches to the pins on an Arduino and have your program detect when one or more switches change state.

So, rather than wasting a digital pin as an output that would appear never to change state, but only is there to provide a 'high' or 5 volts to one side of your switch, why not just wire it to the Vcc line which is 5 volts?

In fact, what is normally done is to wire it to 0 volts, not the 5 volt line. and ten set the input of the pin to have the internal 'Pull Up Resistor' enabled.

Now what happens is that when the switch is open or in the 'off' state, the pin will be read as high level due to the internal pull up resistor making your input high. Then, when you close the switch you connect that pin then to 0 volts and you will read a low state when you read that input pin.

Simple, right :slight_smile:


Paul

View Profile
WWW
Personal Message (Online)

Re: using arduino pins as switches
« Reply #2 on: March 01, 2014, 05:18:24 am »
Bigger Bigger Smaller Smaller Reset Reset Reply with quoteQuote

Actually, what you do do is you connect switches to the pins on an Arduino and have your program detect when one or more switches change state.
that is what I want to do..

So, rather than wasting a digital pin as an output that would appear never to change state, but only is there to provide a 'high' or 5 volts to one side of your switch, why not just wire it to the Vcc line which is 5 volts?
then I would not be able to tell which pin is connected to which pin though..just what pin is connected to 5volts

In fact, what is normally done is to wire it to 0 volts, not the 5 volt line. and ten set the input of the pin to have the internal 'Pull Up Resistor' enabled.
oh I didn't know that thank you-that is useful not for this project but thanks

Now what happens is that when the switch is open or in the 'off' state, the pin will be read as high level due to the internal pull up resistor making your input high. Then, when you close the switch you connect that pin then to 0 volts and you will read a low state when you read that input pin.

Simple, right smiley
well for you maybe not for me!

It sounds like to me that you really have a need to know that a specified pin is connected to another specified pin, is this correct?

Can you tell us a bit more about what you are trying to achieve, explain what your project is to do, then I can better understand why you wish to use this technique?

But if that is the case, then what you would normally do in this situation is to define a number of pins as outputs that you will sequentially set and reset. Then you will define other pins that will be defined as inputs and you will scan these pins or do a byte read to know which pins are set in accordance to the output pins. This is the simple concept of a matrix as used by most keyboards.

Again, you need to be more specific with providing clear details or we will all go around in circles.


Paul