Question: Read external-powered switch state by Arduino

Hi,

I am a newbie to Electronics as well as Arduino. Currently, I facing a very basic problem and I think someone can help me easily.
Today, I played with an old fuel dispenser board. There is a switch solder on it - you can see my picture below. There were 2 connector in the switch, the left was connected to 12v source, the right connect to ground (I guess). I used a multimeter and measure the 12 volt between the left and the right connector of the switch.
How can I read the button state with Arduino? I want to use a minimum of wire so how many wire at least I do need to solve this?
Thank you,
English is not my native language, I hope you will not mind.

Here's the photo from the OP's post:

And, here's a way to do it:

Connect "To Switch" probably to the terminal without the red wire. But, if that doesn't work, then try the one with the red wire connected to it. Either way, it shouldn't damage anything. If neither connection works, then the switch is not on the "high Side", and the other side of the switch is either not being pulled up to 12V, or the pull up "resistor" [since I can't be sure what is really going on] is of a value that is not allowing a high enough voltage to appear at D2, thus not being interpreted as a HIGH by the Arduino. In which case, some measurements will need to be made [ask].

Then, you need to find a "Ground" on that circuit board. Not sure how to advise you, there.

The two diodes are not mandatory, but I put them there to make this as fool proof as possible. They help to protect the input from over and under voltage conditions.

I showed this connected to D2, but you can use any of the D inputs. Pins D2 and D3 are "External Interrupt" inputs. Using the External Interrupt feature means the Arduino will respond to a change in the switch, without having to do what is called "Polling" (i.e. checking over and over, in a loop, hoping to catch the event when it happens). BUT, for something like this, polling will likely work just fine, since it's a "slow event", and you probably don't need to know when it happens within microseconds of the event :wink:

The Sketch for this, will look something like this [UNTESTED!]:

Polling:

const byte ledPin = LED_BUILTIN;   // To use the onboard LED to indicate the state of the Switch
const byte switchSensePin = 2;     // If using pin D2 for sensing the Switch state
byte state = LOW;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(switchSensePin, INPUT);  //Redundant, since this is the default state
}

void loop() {
  state = digitalRead(switchSensePin);
  digitalWrite(ledPin, state);  
}

Interrupt:

const byte ledPin = LED_BUILTIN;   // To use the onboard LED to indicate the state of the Switch
const byte switchSensePin = 2;     // If using pin D2 for sensing the Switch state
volatile byte state = LOW;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(switchSensePin, INPUT);  //Redundant, since this is the default state
  attachInterrupt(digitalPinToInterrupt(switchSensePin), blink, CHANGE);  // Calls the blink() function whenever a CHANGE is detected
}

void loop() {
  // Do other non-interrupt stuff here [if there ever is any]
}

// This called whenever an interrupt occurs on pin D2
void blink() {
  state = digitalRead(switchSensePin);
  digitalWrite(ledPin, state);
}

Thank ReverseEMF and I sorry because I just travel back from a business trip so I cannot reply you sooner.
I really appreciated your time and your efforts, for drawing the wiring and writing the code.
I found a ground "point", it was the screw hole in the left top corner of the board - it was in the picture I upload - and it works.
Before today, I really don't understand the term "reference voltage point" of GND. From now, I learned that I need to connect the GND of different boards to get them "communicate" - if they were supply from different power source.

khoinp1012:
Thank ReverseEMF

Awesome! Glad I was of help :wink: