I have this code:
And a small 5v motor connected to pin 12 and ground but nothing happens :~
How do i solve this problem?
Unless the motor is VERY small (like a phone vibrator motor) it draws much more power than the Arduino pin can supply. You need to add a transistor to switch that much power.
Another problem is that you have very little delay between setting the pin LOW and setting it HIGH again. The output will be HIGH 99.9% of the time. You probably want another 500 millisecond (1/2 second) delay AFTER the digitalWrite(12,LOW) so the power noticeably goes on and off.
it IS a small phone vibrator xD
but it won't work by changing the delay
The last shaker motor I took out of a dead phone needed about 200mA to run properly. The arduino is only good for 40mA absolute max. per pin.
What transistor do i use and how do i wire it up?
You could use a small 2N2222 for example, or an ULN2803 chip. Search in the playground wiki for how to run a small DC motor (on/off only).
I've been looking over the playground all night but i can only find ones where you also control forward and backward :S
You can use this cicuit for solenoids using the 2N2222 instead of TIP102, and ofcourse the motor instead of the solenoid.
Take a look at this tutorial, i think that someting like this is missing in the Playground
If it has a coil... like a relay, solenoid, or motor, you NEED a diode... if you want your Arduino to continue to work.