Control DC Motor on the Arduino directly from Analog

Is is possible to control a toy DC motor (5V) on an Arduino directly from the Analog (or Digital with PWM) pin without damaging the board? I know that DC motors are usually controlled via a transistor but I am thinking of controlling it this way. My Arduino code is:

    void setup() {
         pinMode(6, OUTPUT);
         Serial.begin(9600);
    }
    void loop() {
         int val = Serial.parseInt();
         if( val >= 0 && val <= 1024) {
             analogWrite(6,val);
             delay(5000);
         }
    }

My schematic:

GND---------------------MOTOR--------------|<|-------D6 (PWM)

Not a good idea. DC motors can draw a lot of current, especially when stalled. The chances of you blowing the Arduino are very high. The specs say not to exceed 40mA. If the motor you are using has a peak current less than 40mA then maybe you are okay.

Do you have the motor’s spec?

[I had the current limit as 400mA but it is 40mA. Sorry]

Thank you! I'll try to find it tomorrow morning :)

400mA.

IO pins are 40mA not 400..... See here

They operate at 5 volts. Each pin can provide or receive a maximum of 40 mA

JimboZA:

400mA.

IO pins are 40mA not 400..... See here

They operate at 5 volts. Each pin can provide or receive a maximum of 40 mA

Thank you for the correction. My bad.

Even if it did work, (which it won't ) that is your motor takes less than 40mA then you also need a reverse biased diode across the motor to prevent the back EMF doing any damage to your Arduino.