Hey Folks!
Maybe one can help me with this problem:
Startet today with my new Arduino Uno (that one with the big, replacable chip), and wrote this scatch:
const int LED = 13;
const int BUTTON = 7;
int val = 0;
void setup(){
pinMode(LED, OUTPUT);
pinMode(BUTTON, INPUT);
}
void loop(){
val = digitalRead(BUTTON);
if (val == HIGH) {
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
}
I found this in a book of Massimo Banzi (Arduino for Beginners). When providing current to pin 7, it should turn on the LED I had attached to pin 13. And that it did sometimes, and sometimes not, but once I had the cable (from the 5V to pin 7) removed, the LED turned off after 8-9 seconds... i tried this several times, and even without attaching the cable to 5V, only by inserting the other end into pin 7, the LED turned on (or not), and off after 8 seconds. It seems this LED does what it wants!
Further, if I touch the 2nd contact on the left upper-side of the processor-chip while the LED is turned on and no cable is attached to pin 7, the LED turns dark.
I don't know what to think about this. Is my Arduino damaged? is there an error in my code? Do I have electrostatic chargings or something like that?
Would be happy about answers. Pleas say if I could have made something better understandable or something like that.
You have a floating input. The chip only needs 1uA to make the input change levels. A hand waving nearby can change it.
Change this line
pinMode(BUTTON, INPUT);
to
pinMode(BUTTON, INPUT_PULLUP); // turn on internal pullup resistor
so the pin sits at known level when nothing is connected.
Did as you say, but now the LED shines all the time, no matter what I do at pin 7...
Man-With-A-Beard:
Did as you say, but now the LED shines all the time, no matter what I do at pin 7...
So show us how it is wired up, you might want to change the wiring after you read this:-
http://www.thebox.myzen.co.uk/Tutorial/Inputs.html
I had wired the 5V pin near the analog input-pins to pin seven.
This text about pullup resistors: I dont' get it! Maybe my english is too bad, maybe I have no electronic skills (which I really dont have). Before buying an Arduino, i've checked out many cool projacts, but even more beginner-toturials, and never heard of this phenomenon or that I had to use a resistor for every digital input...
What to do about it?
I had wired the 5V pin near the analog input-pins to pin seven.
Why?
That ensures you can not use that pin as an input at all.
What to do about it?
Show us a diagram of what you have wired up as I have asked you before.
This text about pullup resistors: I dont' get it!
The read it again and again until you do.
Maybe my english is too bad,
Try and get some one with better English to explain it to you. Or if this is true:-
maybe I have no electronic skills
Then you are trying to do something that is too advanced for you. Take a step back and learn the basics.
i've checked out many cool projacts, but even more beginner-toturials, and never heard of this phenomenon
Then they have all been crap.
My wiring:
I have wired the 5V at the under edge of the board to pin 7 at the opposite edge. I have done this to have an input for testing whether this scatch would work or not... Because an inputpin needs any kind of input. Since you say I have to use resitors for every digital input, I should try this out tomorrow.
And in what way is that show?
It is a description in words of your wiring. A schematic or at least a photograph is what is required.
Man-With-A-Beard:
Did as you say, but now the LED shines all the time, no matter what I do at pin 7...
If you enable the internal pullup resistor, the value of the pin will be HIGH unless you change it. To change the value, connect a wire from ground to pin 7. This will make the value LOW.
To change the value, connect a wire from ground to pin 7. This will make the value LOW.
True, but if you do this:-
I have wired the 5V at the under edge of the board to pin 7 at the opposite edge.
Then you create a short circuit and risk burning out your USB port or your Arduino.
Thank you guys, now I understand what these Pullups do!
Now, when I searched especially for that, I've found a Video where someone shows exact these phenomenon:
Arduino Tutorial #3: Digital Inputs and Pull-Up Resitors - YouTube (if someone has the same problem as I did, this is a good explanation for people more thinking the graphic way...)
By the way, at the end of this video: Pull Up and Pull Down Resistors - YouTube
there is a great drawing making visible the difference between pull-up and pull-down resistors and how they work.
So when I want to have simple digital inputs, I have to build pull-down resistors into my curcuit to make sure there are no other signals except when a real input comes in. Of course a good idea!
So when I want to have simple digital inputs, I have to build pull-down resistors into my curcuit to make sure there are no other signals except when a real input comes in.
OR, use the internal pullups, and react to a LOW signal as a sign that something has happened - like a button being pushed.
Yes, in most cases I could simply do that.
I try to interface an old dialphone, so I can get a serial input to my PC. The matter is that the dial signal is ON everytime, and while dialing, there are short breaks. And these breaks are to count, and for that I dont Think I can use the internal pullup resistor, because I have to ground the wire during the breaks to get a clear, non-floating input.
...I Think I was wrong on that too!
I can use the internal pullup, and you are right: it's truly easier.