why is resistor needed for pull up?

Hi,

For the adafruit dht22 sensor it says that you need to place a to place

a 10K resistor between VCC and the data pin, to act as a medium-strength pull up on the data line.

Why is this necessary? and what does the resistor do?

Thanks

a signal line that has no signal will act like an antenna.
a signal line that has no signal can float to any voltage
a pull-up or pull-down resistor, is a simple drain to give that line some way for stray voltages to go.

If you connect the input to Vcc directly (without a resistor) the line is pulled up to a known voltage, but if a LOW is sent to the input there is a short to ground (Vcc to ground). The resistor prevents the short, limits the current flowing and allows the input to be pulled LOW.

You can use the internal pullup and skip external resistors also.
In setup() :
pinMode (pinX, INPUT_PULLUP);

Then in loop(), look for a LOW:

if (digitalRead(pinX) == LOW){
// pin connected to GND with a switch/button. Do the LOW action
}
else{
// still HIGH, ignore or do the HIGH action
}

If I may add to CrossRoads post. The internal pullups are, on the mega328, 20K to 50K. They may not be strong enough in some situations, such as a switch with long wires, to overcome the noise picked up. Then you would use external pullups with a lower value (10K to 1K).