title: Control motors with Arduino using serial connection with computer
body: <p>I am working on an automated dispensing project where I intend to control ten different 12V 600mA DC pumps with an Arduino. A computer transmits messages over usb connection to the Arduino in the following form: </p>
  
  <pre><code>lh = pumps off
  0h = pump 0 on
  1h = pump 1 on
  etc.
  </code></pre>
  
  <p>The Arduino gives power to the pumps by activating a TIP120 npn resistor connected to a 12V 2A DC power supply. Only one pump will be running at any given time.</p>
  
  <p>When I test this by powering LEDs, the serial connection works flawlessly. However when I try it with a pump, the Arduino begins to respond sluggishly to commands, and eventually I lose serial connection with the Arduino. I can only imagine that there is some sort of power issue causing the Arduino to lose connection. Any ideas where I am going wrong?</p>
  
  <p>Arduino Code:</p>
  
  <pre><code>#define PUMP5 5 //pin for turning transistor on/off
  const byte numChars = 32;
  char msg[numChars];
  
  void setup() {
    Serial.begin(9600,SERIAL_8N1);
    Serial.println(msg[0]);
    pinMode(PUMP5, OUTPUT);
  }
  
  void loop() {
    read_msg();
    command_pumps();
  }
  
  void read_msg() {
    char rc;
    if (Serial.available() &gt; 0) {
      rc = Serial.read();
      msg[0] = rc;
    }
    if(Serial.available() &gt; 0) {
      rc = Serial.read();
      msg[1] = rc;
    }
    Serial.print("msg[0]: ");
    Serial.println(msg[0]);
    Serial.print("msg[1]: ");
    Serial.println(msg[1]);
  }
  
  //PUMP5 is used for each of the scenarios since I am only testing with one pump
  void command_pumps() {
    if (msg[0] == 'l'||msg[1]=='l') {
      digitalWrite(PUMP5, LOW);
    } else if (msg[1] == 'h') {
      if(msg[0]=='0') digitalWrite(PUMP5, HIGH);
      else if (msg[0]=='1') digitalWrite(PUMP5, LOW);
      else if (msg[0]=='2') digitalWrite(PUMP5, HIGH);
      else if (msg[0]=='3') digitalWrite(PUMP5, HIGH);
      else if (msg[0]=='4') digitalWrite(PUMP5, HIGH);
      else if (msg[0]=='5') digitalWrite(PUMP5, HIGH);
      else if (msg[0]=='6') digitalWrite(PUMP5, HIGH);
      else if (msg[0]=='7') digitalWrite(PUMP5, HIGH);
      else if (msg[0]=='8') digitalWrite(PUMP5, HIGH);
      else if (msg[0]=='9') digitalWrite(PUMP5, HIGH);
    }
    return;
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/fKZ0G.jpg" rel="nofollow noreferrer">My Circuit</a></p>