A digital I/O pin as ground
Your first snippet is the correct solution.

pinMode(pinnumber,OUTPUT)
digitalWrite(pinnumber,LOW)


Be careful though, you can only sink up to 6mA or 9mA per pin (as per the documentation ["Input and Output" section], I never tried more). If you need more current, use a transistor (this looks like a good example, you just need to invert the output pin to HIGH).

When the pin is configured as input it is supposed to be high impedance, not what you want.