DC Motor Control

Hello all,
I want to be able to control a DC motor via arduino, and cobbled together this script and electronics layout:

int motorPin = 3;
int mSpeed = 130;
void setup() 
  pinMode(motorPin, OUTPUT);
void loop() 

    int speed = mSpeed;
    if (speed >= 0 && speed <= 255)
      analogWrite(motorPin, speed);

Click this link to my 123D model:https://circuits.io/circuits/2343940-motor-switch
The simulation works perfectly, as you can see with yourself. However, when I duplicate it in real life, nothing happens. Any ideas on why this difference is happening? Thanks for the help.

Hi, we use real Arduino here, not simulators.

It is not clear from your diagram how the motor is powered. I see 3xAA cells, but they are not connected to anything. It looks like the motor is powered by USB. The motor may be drawing to much current from the USB and causing the Arduino to reset.


Hi, we use real Arduino here, not simulators.

I did not mean I don’t use a real arduino, just that the simulator works, but the real arduio doesn’t. Otherwise, thank you for the insight. The 3 AA were an old component I forgot to take out, but I will hook my real batteries up. You are right, I only was using usb power.

You 'restart' the PWM to fast. Try adding a delay (some seconds) in your loop