Pages: [1] 2   Go Down
Author Topic: Toggle LED with Switch (off same Adruino PIN)  (Read 1289 times)
0 Members and 1 Guest are viewing this topic.
Hull
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Faraday Member
**
Karma: 80
Posts: 3785
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Although I answer in good faith, remember always: Your Mileage May Vary.
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 596
Posts: 33283
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Faraday Member
**
Karma: 80
Posts: 3785
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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



* toggle.jpg (4.2 KB, 225x225 - viewed 14 times.)
Logged

Although I answer in good faith, remember always: Your Mileage May Vary.
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 108
Posts: 4013
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Hull
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 108
Posts: 4013
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Hull
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 596
Posts: 33283
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 :-)


* schema.png (2.68 KB, 309x447 - viewed 22 times.)
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 596
Posts: 33283
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 596
Posts: 33283
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 52
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, so add a pulldown to the pin
Logged

Pages: [1] 2   Go Up
Jump to: