I connected a fan with the arduino using the 5V pin and worked fine.But when i connect it with the digital pins the fan doesn't work.
The processor cannot directly drive a large load like that, you need to use a transistor or relay.
There are examples in the playground.
Can the fan damage the arduino if i keep it connected to the 5V pin? :zipper_mouth_face: :~
EDIT: Assuming you mean an IO pin on the processor, no if you mean the 5v connection on the headers.
dimitrisst: Can the fan damage the arduino if i keep it connected to the 5V pin? :zipper_mouth_face: :~
The Arduino pins are rated for an "absolute maximum" of 40mA. That means 25mA is the practical limit. I've not seen a fan that takes that little current, 150mA is more like (and most will take a larger current spike at start up too).
So if you are lucky you haven't blown-up the output pin or the rest of the chip, but you may have partially damaged it. Time to check with an LED circuit or something.
To power a load that takes more than 25mA you need to look at the examples of driving larger loads like motors and relays (a modern computer fan is a motor plus a controller in one, you can think of it as rather like a motor).