Why need resistor and how do I know which one?

Hi!

Im a beginner on arduino and have a very basic question. Im doing this circuit on arduino UNO with the following code for printing the value to the serialmonitor:

int inputPin = 4; // inputpin for pushbutton
int val = 0; // variable for the pinStatus

void setup() {
  Serial.begin(115200);
  pinMode(inputPin, INPUT);    // declare pushbutton as input
}

void loop(){
  val = digitalRead(inputPin);  // read input value
  Serial.println(val);
}

First I was thinking that I could do the connection simply like this:

It reacted when the button was pushed but got really unstable output when it wasnt.

I understand that I have to connect the circuit as following:

but I wounder
Why do I have to have a resistor here and how do I know what value on the resistor to use??

thanks for this very beginner question!

Change this line pinMode(inputPin, INPUT); to pinMode(inputPin, INPUT_PULLUP); // input pin with internal pullup resistor enabled

and wire the switch to connect the pin to Gnd when pressed.

The pin will then reliably read as HIGH when not pressed, and LOW when pressed.

You have to have the digital pin either connected to 5V or ground in order for it to read one or the other. BY connecting it through the resistor, you are making sure it ill be in that state when it is not pressed. Otherwise, when not pressed it wouldn't be actually connected to anything but the pin. A wire sticking out of a digital pin and not connected to anything is called an antenna and it will pick up any stray signal around and give you those unstable readings.

Thanks!

Otherwise, when not pressed it wouldn't be actually connected to anything but the pin. A wire sticking out of a digital pin and not connected to anything is called an antenna and it will pick up any stray signal around and give you those unstable readings.

But how do I calculate what resistor I need here. And I know its not a correct solution as I guess it would be the same thing as bypass the digital output but why do I need a resistor rather then connecting straight to ground after the pushbutton as in image below

And one more question: What exactly does the INPUT_PULLUP do?

You can't do that picture at all. What happens if you press the button? The 5V pin and the GND pin would be shorted together. That will destroy your board.

The actual value of the resistor isn't terribly important. 10K seems like a very common number.

INPUT_PULLUP causes the chip to use its own internal pullup resistors. So you can have the button wired so that one side goes to the pin and the other side goes to ground. The resistor to 5V is internal to the chip. This does invert your logic so that a pressed button reads LOW and a not-pressed button reads HIGH. That's normal to most of us, but seems backwards to beginners for some reason.

The 5V pin and the GND pin would be shorted together. That will destroy your board.

of course you are right but just trying to understand. What is the difference from that and if there was a reistor before the ground as in the second image in my first post? they are both connected in the same way except for the resistor? I guess I dont really get the point of the resistor to 100% and therefor not how to pick the value either.

If you are puzzled have a read of this:- http://www.thebox.myzen.co.uk/Tutorial/Inputs.html

The value is impossible for you to calculate because you do not know a lot of things in your setup. For example what is the local interfering electromagnetic field in the vicinity of your computer. What noise immunity do you want? What current consumption can you afford when the button is pushed. It is far too complex for you and there is no need to calculate it anyway. Use anything between 330R and 22K and you won't be far wrong.

I guess I dont really get the point of the resistor to 100% and therefor not how to pick the value either.

"Resistance" is the resistance to current flow. Without the resistor (zero resistance or a "short circuit" when the button is pushed), you get too much current and the power supply voltage will drop to (nearly) zero.

[u]Ohm's Law[/u] describes the relationship between voltage, resistance and current. More voltage gives you more current. Less resistance gives you more current.

With zero resistance, you get infinite current... But in the real world, of course you can't get infinite current, and you can't really get to zero-Ohms either... What happens in the real world is the voltage drops, and sometimes the power supply fries or something else fries, or maybe a fuse blows, or maybe the power supply safely "current limits" to protect itself.

That will destroy your board.

Actually, it's very unlikely that you''ll "destroy" the board. You'll kill the power supply voltage for as long as the button is pushed, which means the Arduino will stop running, and it will reset (assuming it's not destroyed :) ) when power is restored. The pushbutton probably won't last too long with "short circuit current" going through it, and it' just a bad thing to do!

therefor not how to pick the value either.

Experience! ;) If the resistance is too low (or zero) too much current will flow. The resistor might overheat (even burn-up), or the power supply might get overloaded, etc. And, you'll be wasting energy...

If you short a power outlet in your house, excess current will flow, the voltage will drop and a circuit breaker will trip.

If the pull-up resistance is too high, you can get electromagnetic interference from electrical waves "in the air". There is not a lot of energy in the electromagnetic interference and you can't get much current.... So a lower resistance makes the interference "weaker".

A pull-up value of 10K is very common. But anything from 100 Ohms to 1M Ohms will probably work.

Ok, thanks. So if take this example instead with photoresistor:

and this code:

// The analogRead() function returns a range of about 0 to 600 from this circuit

void setup() {
  // initialize serial communication:
  Serial.begin(115200);  
}

void loop() {
  // read the sensor:
  int sensorReading = analogRead(A0);

  Serial.println(sensorReading);
  delay(100);        // delay in between reads for stability
}

At the moment the current vary between 0.05-0.2 mA and it prints the sensorReading 930 (darkest) and 650 brightest.

How shall I think if I want to change the fixed 10 kOhm resistor to get the output to be the full range of 0-1023??

I guess I can manage this by changing the 10kOhm resistor. But how can I calculate the value?

How shall I think if I want to change the fixed 10 kOhm resistor to get the output to be the full range of 0-1023??

Think very very hard, that is how to think.

I guess I can manage this by changing the 10kOhm resistor.

No you can not do this by changing the value of the resistor. To get the full range is impossible so don't bother trying. Unless you want to play with operational amplifiers.

If you insert a resistor between the LDR and 5V you can bias the range by adjusting the ratio of R1 (resistor between LDR and 5V) to R2 (resistor between LDR and GND.

The total current is (IR1 ) + (ILDR ) + IR2 ).

The resistance range of the LDR is about 300 ohms (under a bright light) to about 20 M ohms (in complete dark).
That is a significant range.

If the LDR is between two 1 k ohm resistors, the maximum total current is 5V / (2.3 k ohms) = 2.17 mA.
The mimimum current is 5V/ (20 M ohms + 2 k ohms) = 0.25 uA

This means the LOWEST reading you will ever get (with the LDR in complete dark) is 51 +/- 1 count.
and the HIGHEST reading you will get ( with LDR in bright light) is about 444 (+/- 1 count)

Why ? Because you have connected the A0 input at the end of R2 so what you are REALLY reading is the voltage drop across the resistor between the LDR and GND.

for V = (0.25 uA * 1000 ohms) = 0.250 V (250 mV) , AnalogRead will return 0.25/0.00488 = 51

V = (2.17 mA * 1000 ohms ) = 2.17 V , AnalogRead will return 0.25/0.00488 = 444

perik: At the moment the current vary between 0.05-0.2 mA and it prints the sensorReading 930 (darkest) and 650 brightest.

How shall I think if I want to change the fixed 10 kOhm resistor to get the output to be the full range of 0-1023??

I guess I can manage this by changing the 10kOhm resistor. But how can I calculate the value?

No, you can't make it read zero without access to a negative rail to bias it.

I think you have something wrong though: If the current goes from 0.05 mA dark to 0.2 mA light that will give you 0.5 V dark to 2 V light across the 10 k resistor. (V = I x R). The analogue input reads 0 to 5 V divided into 1024 steps so you should be getting readings from 102 to 410. A 22 k resistor would give you a more useful range of 224 to 902.

How are you measuring the current in the ldr? The easiest way is to just measure the voltage across the 10 k resistor which is exactly what the Arduino is doing.

Russell.