Toggle LED with Switch (off same Adruino PIN)

Hi I am Mike Harmer, I am new to the forum.

I have been given the Adruino Console - Shield from Teeside University. Currently I have no lesson notes available and I am just working out how everything goes together.

I am currently struggling to toggle an LED which is on the same Adruino PIN as the switch. I believe that it may be a debounce issue but not 100% sure. All examples look at toggling an LED which is on a different PIN to the switch which doesn't really assist me with what I am trying to achieve.

If somebody could guide me in the right direction rather than telling me the answer I would be grateful.

I'm not sure you can have the LED and switch on the same pin Mike.... a switch is a sensor, so the pin is in input mode, while to drive the LED it needs to be an output. I'm open to correction, though.

But I'm intrigued by what you call the Arduino Console.... is that one of these?

Jimbo is right, it's either an input or an output.
You can have it as an input pin and have the LED light up depending on the switch state, is that what you want?
For inputs look at:-
http://www.ladyada.net/learn/arduino/lesson5.html
or
more general
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

Although what you "could" do is have a toggle-switch in series with the LED.... 8) Then you wouldn't even need an i/o pin or any code, just the Arduino's 5v....

toggle.jpg

Welcome to the forum

As Jimbo says, it is not normal to use a pin as an input and an output. Is there a particular reason why you chose to do it this way ?
How have you got the switch and LED wired ?

Please post your code here, but before doing so read the advice in the sticky posts at the start of this forum page, particularly in respect of using code tags when posting the code.

That is correct.

I will attach the wiring diagram and table etc. once I get home tonight. We cannot start making the board fit to suit what we are trying to achieve.

All other functions seem straight forward to operate and I have no problem with them, but this is a problem which I need to work to continue with the project assignment (I am developing my own software flow diagram, so I am defining the project scope oppose to Teeside).

I appriciate that a pin can only be an input or an output at any single point in time. Is there a way in which it can be switched between within a void loop() rather than void setup(), not really sure what the propagation delay is like on the Adruino.

I am trying to acheive a multiplexer arrangement so that I can set the simulation up in four different modes dependant upon the output status of the LEDs.

Cheers

Mike

You can certainly change the pinMode in loop() or another function.

I would be interested in knowing the details of your project assignment. Unless it specifically tells you to use the same pin as an input and an output you may not have chosen the optimal solution to the assignment.

There was a long thread earlier this year by someone else that wanted to use the same pin for both input and output. The problem comes once you turn the LED on. The pin is then an OUTPUT pin at that time, for the LED to be on. In order to read the switch state, you need to switch the mode to INPUT, which turns off the LED.

I seem to recall that a lot of external hardware needed to be added, and, even then the results were not really satisfactory.

I've been told to come up with something I want to produce rather than the assignment. So I am currently putting together a flow diagram to define the specification of the code.

I am just trialing what can be achieved and what can't be achieved.

I've been told to come up with something I want to produce rather than the assignment.

As you are a beginner I would say that you should abandon the idea of using a pin for both input and output at the same time. It is an advanced concept and you are not advanced.
It can only be achieved in a limited way when you know exactly what you want to do, and as you are not telling or don't yet know then I think this is far above your pay grade.
I used to lecture at Manchester Met University and this is something I would not have expected my students, even the good ones, to do.
Try and be a bit more conventional.

P.S. Are you going to the Maker Faire in Newcastle at the end of April, I will be showing stuff there it you want to have a chat.

Hi,

I suggest you to try the attached schema. The arduino PIN can turn the LED ON/OFF either if the swith is ON or OFF.
To read the switch, set the PIN as input for a very short time. You have to calculate the values of the resistor
to have a logic one on the pin when the switch is ON. As I see in the datasheet, it seems to be 0.6Vcc (arround 3V if
the power is 5V)

Hope it works :slight_smile:

schema.png

If you just want to light up the LED when the switch is presses and read the state of the switch then the arrangement on the reed switch inputs of this project will do it.
http://www.thebox.myzen.co.uk/Hardware/Pendulum.html

That last circuit will not read a logic zero when the switch is not made due to the forward volts drop on the LED plus the voltage across the resistor will mean that the voltage on the input can never reach logic zero levels.

That last circuit will not read a logic zero when the switch is not made due to the forward volts drop on the LED plus the voltage across the resistor will mean that the voltage on the input can never reach logic zero levels.

Yes it works, but you have to ensure the internal pullup is not enabled.

Yes it works,

It depends on how you define works.
There is not enough pull down through a resistor and forward biased diode to ensure that the pin is always at a logic zero. In effect the pin is floating. If it appears to be stable for you then that is just a fluke, this is because the noise margin is severely marginalized. It will be very susceptible to picking up interference.

Ok, so add a pulldown to the pin

Yes that would fix it.

Below is the code which I used to create a multiplexer for Arduino - Console Shield

const int PIN9 = 9; //LED 1 and Push Button 1
const int PIN8 = 8; //LED 0 and Push Button 0

int SW1Count = 0;
int SW1State = 0;
int LSW1State = 0;

int SW0Count = 0;
int SW0State = 0;
int LSW0State = 0;

void setup () {
pinMode(PIN9, INPUT);
pinMode(PIN8, INPUT);
}

void loop() {

//Switch 1 and LED 1 (MSB Multiplexer)
SW1State = digitalRead(PIN9);

if (SW1State != LSW1State) {
if (SW1State == HIGH) {
SW1Count++;
}

LSW1State = SW1State;

}

if (SW1Count == 1) {
pinMode(PIN9,OUTPUT);
digitalWrite(PIN9, HIGH);
pinMode(PIN9, INPUT);
}

if (SW1Count == 2) {
SW1Count = 0;
}

//Switch 0 and LED 0 (LSB Multiplexer)
SW0State = digitalRead(PIN8);

if (SW0State != LSW0State) {
if (SW0State == HIGH) {
SW0Count++;
}

LSW0State = SW0State;

}

if (SW0Count == 1) {
pinMode(PIN8,OUTPUT);
digitalWrite(PIN8, HIGH);
pinMode(PIN8, INPUT);
}

if (SW0Count == 2) {
SW0Count = 0;
}

}

Sorry there aren't any comments with the code just yet. I will populate with comments later this evening.

Cheers

Mike

I checked over the code yesterday and tested it for some time to see if I could find any issues or errors.

The only issue I could find was in some instances when the LED is on when I push the button it doesn't seem to switch off the LED but then can work ok for a number of times.

If anybody could provide a little feedback it would be great thank you.

You are switching the pin between input and output this normally is not a good idea.
You need to do two things, read the how to use this forum sticky and post the code correctly.
Then you need to post a schematic of your hardware wiring.