Fan and Digital Input

Hello everybody,
I’ve recently bought an Arduino and have been getting used to it’s language and system (so I’m really sorry if this is an obvious/stupid question).

I have a fan that works with about 5V. Based on the http://arduino.cc/en/Tutorial/DigitalReadSerial tutorial I created a system in which, when a pushbutton is pressed, the fan starts spinning. When the pushbutton is released, the fan stops (I attached a circuit project).

However, what I’d like to accomplish is, when a digital pin (say, 3) is HIGH, the fan recieves power and when the digital pin is LOW the fan recieves no power (so, pretty much the same as with the pushbutton, but with a pin).

I’ve looked it up on the Internet and can’t find anything like this, so any help would be highly appreciated.
Thanks in advance.

It looks like your moter is not connected to any of the I/O pins. Connect one leg of the motor to Pin 3 and the other to ground, designate pin 3 as output You might designate pin 4 as input then connect your switch between +5 V and pin 4... then make the code in your loop turn on pin 3 when you turn on pin 4 by pressing the switch.

This will be a much more complicated way of doing just what you are now, but at least it will be under program control and you will have written your firt program! You should be able to get some good ideas from looking at the examples that come with the IDE.

RPCoyle:
It looks like your moter is not connected to any of the I/O pins. Connect one leg of the motor to Pin 3 and the other to ground, designate pin 3 as output You might designate pin 4 as input then connect your switch between +5 V and pin 4... then make the code in your loop turn on pin 3 when you turn on pin 4 by pressing the switch.

This will be a much more complicated way of doing just what you are now, but at least it will be under program control and you will have written your firt program! You should be able to get some good ideas from looking at the examples that come with the IDE.

Thanks for your promptly reply!
Anyway, I do realize the motor's not connected to any of the I/O pins. What I'd like to do is supply 5v to the motor whenever one of the digital pins is HIGH. (also, just connecting the motor to one of the I/O pins and the ground won't supply enough voltage, hence why I want to use the 5V pin)

Generally, the arduino can't provide enough current from the digital i/o pins to power any motor but the very tiniest. You have to switch an external power supply instead with a transistor or relay. Here's a link to the playground page showing how to do it for a solenoid Arduino Playground - HomePage Same principle applies to motors. Unusually in your case, you'd be switching the power from the arduino's regulated supply. How much current does your motor draw?

I see... Thank you very much!

I've googled transistors a bit... From what I've got, when the pin connected to it is HIGH, the transistor lets the power supply through it to the motor? And with PWM pins, the higher the output, the more voltage it lets through? (There's a big chance I got it wrong.)

Also, the motor actually works with 4.5V (I was using 5V for a matter of convenience, as it's proven to work fine with that voltage as well. I suppose I was doing wrong?), drawing 225 mA.

miguelmurca:
I see... Thank you very much!

I've googled transistors a bit... From what I've got, when the pin connected to it is HIGH, the transistor lets the power supply through it to the motor? And with PWM pins, the higher the output, the more voltage it lets through? (There's a big chance I got it wrong.)

The tutorial at Transistor Circuits is a good read. The "transistor as switch" section is what you want to study. Make note of the protection diode and resistor used in that circuit as you need to employ both to protect your Arduino.

PWM (pulse width modulation) pulses the voltage to the motor, so strictly speaking it's jumping between 5V and 0V.

Thank you! I'll be sure to read it carefully.

--edit--
Done reading. Tanks a lot, you've solved my problem.