Opening Serial monitor affects DC motor

I’m using Arduino Duemilanove (ATmega328) to build a simple prototype.

In this prototype, a 1.5v DC motor is directly connected to the DIGITAL PIN 9 (with PWM) and GND, while a triple axis accelerometer is connected to the POWER 3V3 pin and ANALOG IN pin 5.

What I’m trying to do is to vibrate as soon as program starts, then keep reading accelerometer data and send to serial monitor via serial write.

Here is my code:

#include<Wire.h>

const int MPU = 0x68;                // I2C address of the MPU-6050
const int ANALOG_OUT_PIN = 9; // Analog output pin that the LED is attached to

int nVibIntensity = 255;

int nAccX, nAccY, nAccZ, nTimeStamp, nGyroX, nGyroY, nGyroZ;

void setup() {

  // setup acc sensor
  Wire.begin();
  Wire.beginTransmission(MPU);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);

  // setup vibrator
  analogWrite(ANALOG_OUT_PIN, nVibIntensity);

  // setup serial
  Serial.begin(115200);
}

void loop() {

  Wire.beginTransmission(MPU);
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU, 14, true); // request a total of 14 registers
  nAccX = Wire.read() << 8 | Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
  nAccY = Wire.read() << 8 | Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  nAccZ = Wire.read() << 8 | Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)


  Serial.print(nAccX); Serial.print(", ");
  Serial.print(nAccY); Serial.print(", ");
  Serial.println(nAccZ);

}

This program works fine if I set the nVibIntensity to small value, say 50, but if I set it a little larger, e.g., 200, then there will be no serial output anymore and the TX LED is OFF.

Also, I notice that, even when the nVibIntensity to small value, each time I open the serial monitor on my computer, the motor stops vibration for a while and then back to vibration mode.

Does this problem related to the power supply to the DC motor? How to solve this problem?

each time I open the serial monitor on my computer, the motor stops vibration for a while and then back to vibration mode.

Opening the serial monitor causes the arduino to reset by design unless you defeat the reset.

Directly connecting a motor to an Arduino pin is never recommended, its one way to fry the chip.