Hello i have just started the arduino beginner project book and i'm currently on the very first project (the spaceship led one) and i'm not sure about something :
Why we should connect the cathode of led to to ground and the anode to an arduino pin instead of wiring the anode to 5V and the cathode to an arduino pin, same thing for thje switch why connecting one side to power and the other one to to pin 2 instead of connecting one side to ground and the other to pin 2 ?
I know that pin are used to read or write voltage and why we use those pin.
Also how do they know that the switch require a 10k ohm resistor (i know they calculate that using ohm law but we just know that arduino provide us 5V it is not enough information to know how much resistance we should use).
See the image below for different Switch and LED connections.
Note, circuitS3 is recommended as you can incorporate the built in PULL-UP resistor, a LOW on the pin indicates the N.O. switch is closed.
10k is used to keep the power consumption low.
10k is a standard value that everyone should have in their collection.
10k is a reasonable value to keep an input pin from floating (note up to 100k would work).
Your questions are fantastic for a ‘newbie’…(no offence, we were all there once). They show that you’re not just ‘copying and pasting’ everything you see, read and touch.
The fundamentals of all ‘electronic circuits’ always come back to Ohms Law - which you refer to…
You don’t have to remember it, just why it’s there, and you can pull it out when you need it.
If you go a fraction further, you can design circuits from scratch.
Probably the next most important rules are about voltage, current and power.
You can’t start a car engine with a small coin cell - and there’s a reason (several of them actually), but they all stem from those three words.
You’ll get there, and the good thing is that you can decide when ‘here’ is far enough.
If you wire the LED between pin and +5V you have to write LOW to turn it on and HIGH to turn it off. That's counterintuitive to most people.
Basically the same answer. The Arduino examples tend to use an external pull-down resistor so the switch will read HIGH when the switch is closed and LOW when the switch is open. This is intuitive but wasteful because the Arduino UNO/Nano/Mega has built-in programmable pull-up resistors. Using pinMode(pin, INPUT_PULLUP) and a switch between the pin and Ground avoids the need for an external resistor but it reads LOW when connected and HIGH when disconnected which is counterintuitive to many beginners.
Why is it usual to pull up rather than down?
One might think it makes more sense to pull a pin down, with a resistor to 0V. The input would be at 0 Volts when the button is not pressed and be a Vcc (HIGH) when pressed.
A pull-up resistor connection is opposite to this. A pull-up input is HIGH when the button is released and LOW when the button is pressed.
Having the pin high when the button is not pressed is, you might think, back to front.
Understanding why it is common practice to have a pull-up rather than a pull-down resistor we have to look at the wider context of the equipment the micro-controller might be installed in. Typically, some piece of equipment (from a microwave oven to a huge piece of industrial machinery) will have some control electronics buried inside along with cables going to a control panel. Maybe the control panel has buttons to be pressed to activate various functions. The case or at least the chassis will probably be made of metal, and the metal connected to 0V or ground. If there is a cable, then there is the possibility of the cable being damaged. If the cable comes into contact with the chassis or case, then the wires might short to the metal. If the only thing the wires are connected to is 0V or inputs, then the short will not do much harm. If, on the other hand they are connected to Vcc then a short to the chassis will short out the power supply. For this reason, it is normal practice to connect buttons between 0V and their input and to pull the input up.
And without reading Perry's reference, it should be mentioned that "negative logic" - LOW for "1" - is a natural consequence of early logic systems using the more readily fabricated NPN transistors.
In DTL and TTL logic , the NPN transistors pull to ground when they conduct, and only weakly pull toward the 5 V supply (as to do so they can only be emitter followers). There is in fact, relatively little need to pull an input up to 5 V as it is more important to pull the input down (i.e., you require a "stiff" pull-down rather than a strong pull-up). This favoured the use of "open-collector" outputs which could also be wired together as an effective gate function.
So in that logic system, it makes vastly more sense to have a switch or external component to pull down to ground and a pull-up might or might not be required.