I want to control a DC motor with arduino digital pin.
when i connect DC motor with 5 v it works, but when i connect the motor to a digital pin it doesn't work.
I tried also to connect the motor to 5v by using a transistor used as a switch but it doesn't turn on, but if i use a led to check the power it lights up.
How can i turn on and of the DC motor by the Arduino Uno board?
Sorry for bad english
You have to use a motor driver IC. L293D or L298 will work.
A digital pin can supply no more than 40 mA of current. Your motor requires much more.
The Arduino UNO running on USB power can supply no more than 450 mA before it shuts down. Your motor may exceed that limit so it should be run from an external power supply. You could use four AA cells in series to get 6V. You will need a transistor to switch the current.
How did you wire the transistor? If it is the right kind of transistor and wired correctly it should have worked. I suspect you tried to power from the 5V pin and hit the 450 mA limit.
Here is a MOSFET transistor motor driver. You must use an external motor power supply capable of supplying the motor rated voltage and stall current like @johnwasser pointed out.
The ancient and inefficient L293 and L298 are very poor motor drivers compared to modern motor drivers. Don't waste your money on them (my opinion). Pololu has a good line of DC brushed motor drivers. Pololu has good prices, customer support and instructional pages for each of their products.
Choose the motor driver or transistor based on the stall current of the motor and the motor supply voltage. The stall current can be several times the running current. The stall current will be drawn, briefly, every time that the motor is started. The stall current should be listed in the motor data sheet. In the absence of a data sheet, the stall current can be estimated. To estimate the stall current, measure the motor winding resistance. Zero your meter lead resistance* before measuring the motor coil resistance. Take several measurements rotating the motor a bit between readings. Use the lowest reading in the calculation. The estimated stall current is the motor supply voltage divided by the measured resistance.
- Short the meter leads together and note the reading. Subtract the noted reading from subsequent winding resistance readings to get the corrected measured winding resistance.
Some motor driver come with current limit circuitry built in. You don't need to supply stall current when using those drivers. See >> Using Current Regulation in DC Motor Drivers |Article | MPS
if i connect a 9V cell to the board can i use 5 V pin to power the motor or i need to connect it externally?
A 9V smoke alarm battery like this?

it's not the same brand, but i already used it also for other projects.
No. The 5V pin can only used for small sensors or a couple of LEDs.
Use a 4xAA battery pack for the motor, and connect all the grounds.
I connected a 9 volt cell transformed into 5v.
If i connect the motor directly to 5v it works.
If i connect the motor to 5v after the transistor it doesn't turn on.