Rocket switch 3 pin with LED not working

I am trying to wire a Toogle Switch on the Analog port to detect if it is switch on or off. This I do succeed to do. The only thing is this switch has a LED and I am unable to have it turn on when toggleling the switch.

I am attaching a sketch of the wiring

And here is an extract of the code:

const int In5 = A5;

int inVal5 = 0;

void loop()
{
 inVal5 = analogRead(In5);  // read the input pin
 if( inVal5 > 0) {
    lightUp();
  } else {
    lightOut();
  }
  delay(100);
  Serial.println(inVal5);
}

any ideas to have the switch led turn on and still have the arduino pick up on A5 if the switch is on or off ?

You can use the analog pins as digital pins. just use pinMode and digitalRead on them just like they were normal digital pins.

You haven't posted the whole code, so we can't see what's in the lightUp and lightOut function. But the way it is wired I don't see what that is going to light.

Please read the "How to use this forum" post at the top of any of the message boards. It will give you some helpful tips on how to get helpful help here. It will also explain how to properly post your code. Reading the instructions first is a skill that will help you more than anything else in this game.

I was able to find a solution.
I've switch wire 1 and 2 on the switch:

And altered the code to be like this

const int In5 = A5;

int inVal5 = 0;

void loop()
{
 inVal5 = analogRead(In5);  // read the input pin
 if( inVal5 > [b][color=red]1000[/color][/b]) {
    lightUp();
  } else {
    lightOut();
  }
  delay(100);
  Serial.println(inVal5);
}

This works, the only thing is that A5 never reaches 0.

lighUp or lightout function just turn on or off a led for now on D1 it will do more later.

It still isn't smart to use an analog reading for a digital device. The switch is either on or off, it should never be in between. So you should really be using digitalRead here instead of analog.

Delta_G:
It still isn't smart to use an analog reading for a digital device. The switch is either on or off, it should never be in between. So you should really be using digitalRead here instead of analog.

Thanks.
I did as you suggested and it does not work because the switch's led seems to still be giving some output current, so it is always HIGH, never LOW.
This is why so far the Analog Read seems to be the only way to figure out the switch position and still power the led on the switch.

idefaye:
Thanks.
I did as you suggested and it does not work because the switch's led seems to still be giving some output current, so it is always HIGH, never LOW.
This is why so far the Analog Read seems to be the only way to figure out the switch position and still power the led on the switch.

Add a pull-down resistor.

Or wire it the right way round with the internal pull-up so it reads LOW when the switch is made.

An LED normally needs a current limiting resistor - is that present inside that switch? There's none in your circuit. If there's also none in the switch you won't be able to get that LED to light, ever, as it's now quite certainly blown.

There's also no pull-up or pull-down resistor on your switch. That's another major issue.

idefaye:
Thanks.
I did as you suggested and it does not work because the switch's led seems to still be giving some output current, so it is always HIGH, never LOW.
This is why so far the Analog Read seems to be the only way to figure out the switch position and still power the led on the switch.

Delta_G:
Add a pull-down resistor.

Or wire it the right way round with the internal pull-up so it reads LOW when the switch is made.

I am still new to this. How would I go about adding a pull-down resistor in my circuit?

I've tried making D5 INPUT_PULLUP with digitialread, but still no success.

A pull down resistor goes from your pin to GND.

As R4 in this schematic.

larryd:
A pull down resistor goes from your pin to GND.

As R4 in this schematic.

Thank you. That helped me understand what a pulldown is.
I have followed you advice and it works now... Thanks for helping me learn.
I've added a 220 resistor, I don't know what the best value would need to be, but it seems to fit the bill.

Here is the final diagram as it is today.

220 Ohm is fine but a bit low. Up to 100k should be fine.

But really, I would change it to the setup of S3 in that image, way simpler :slight_smile:

septillion:
220 Ohm is fine but a bit low. Up to 100k should be fine.

But really, I would change it to the setup of S3 in that image, way simpler :slight_smile:

I tried Internal pullup but that did not work and gave me the same results. So the pulldown remains the only working solution for this switch.

idefaye:
I tried Internal pullup but that did not work and gave me the same results. So the pulldown remains the only working solution for this switch.

Did you reverse the logic in the code as well? There's more than just adding the pull-up in code. You have to rewire the switch to ground and reverse all your HIGHs and LOWs in the code.

idefaye:
I tried Internal pullup but that did not work and gave me the same results. So the pulldown remains the only working solution for this switch.

That's BS :wink: It just tells me you did something wrong :wink: