Why do I need a resistor in a vibration sensor

In this circui, I get once vibration happens the circuit connect to pin 3. But why do I need to connect it to the resistor which connects to the ground? All I need is for pin 3 to read as HIGH right?

Without the resistor your input pin will be floating when the sensor is not triggered , this could cause unexpected behaviour

can I not just connect it to the ground without going thru a resistor? iy would still be a complete curcuit right

No , you will create a short when the sensor is triggered

1.why would it short thr curcuit?
2.With resistor,
say the sensor is connected and 5v goes thru the sensor the pin3 reads HIGH. will the current continues to go to resistor and eventually to the ground or will it be channeled to pin 3 completely?

ppok1989:
1.why would it short thr curcuit?

Because, assuming the un-named sensor is just a switch and has about zero resistance when closed, with no resistor you have effectively joined 5V to 0V with a piece of wire.

ppok1989:
2.With resistor, say the sensor is connected and 5v goes thru the sensor the pin3 reads HIGH. will the current continues to go to resistor and eventually to the ground or will it be channeled to pin 3 completely?

An Arduino input is very high impedance* so effectively no current goes from the sensor / resistor joint to the pin.

(* for our purposes here, that's just another word for resistance.)

The sensor / resistor is a voltage divider. To use Wikipedia Figure 1 symbols, when the sensor is closed Z1 is 0 (to all intents and purposes, assuming as I said it is actually just a switch) so Vout (the voltage seen by the pin) is Vin, ie 5V. With the sensor open, Z1 is infinite, so Vout is 0. (See the equation in Wiki there under "General case". Z1 is either 0 or infinity, Z2 is 220)

(Source: Wikipedia)

ppok1989:
In this circui, I get once vibration happens the circuit connect to pin 3. But why do I need to connect it to the resistor which connects to the ground? All I need is for pin 3 to read as HIGH right?

No, you need the pin to read LOW sometimes, HIGH at other times. This means there must be a way to
discharge the pin to ground as well as the vibration switch being able to charge it to +5V. A resistor is used
to limit the current to some small safe value when the switch is on.

Input pins have no default state, they are effective insulated and their voltage "floats" between 0V and 5V
if not controlled in some fashion.

This is because CMOS circuitry uses MOSFETs which have isolated gate electrodes which take effectively
no current at all. An input pin basically behaves like a small value capacitor, storing charge.

arduin_ologist:
Because, assuming the un-named sensor is just a switch and has about zero resistance when closed, with no resistor you have effectively joined 5V to 0V with a piece of wire.

An Arduino input is very high impedance* so effectively no current goes from the sensor / resistor joint to the pin.

(* for our purposes here, that's just another word for resistance.)

The sensor / resistor is a voltage divider. To use Wikipedia Figure 1 symbols, when the sensor is closed Z1 is 0 (to all intents and purposes, assuming as I said it is actually just a switch) so Vout (the voltage seen by the pin) is Vin, ie 5V. With the sensor open, Z1 is infinite, so Vout is 0. (See the equation in Wiki there under "General case". Z1 is either 0 or infinity, Z2 is 220)

(Source: Wikipedia)

thanks mate, it makes sense now.

So the long and short of it is, you connect the "sensor" (switch) between a pin and ground, and set pinMode to INPUT_PULLUP on that pin. The pin will intermittently read LOW when the sensor is shaken. The"resistor" is now inside the microcontroller itself.

End of story. :grinning: