title: Python + Arduino controlling DC Motor
body: <p>Hi This is my Arduino code, since I want the loop only once, I used the while(1) {} construct in the void loop()</p>
  <pre><code>int motorPin = 3;
  int motorDir = 12;
  int motorBr = 9;
  
  void setup() {
   //pinMode(motorPin, OUTPUT);
   pinMode(motorBr, OUTPUT);  
   pinMode(motorDir, OUTPUT);
   
   if (Serial.available() &gt; 0) {
  
    if(Serial.read() == '1') {    
      digitalWrite(motorBr, LOW);
      digitalWrite(motorDir, HIGH);
      digitalWrite(motorPin, HIGH);
      delay(500); 
      digitalWrite(motorBr, HIGH);
  
  
    } else if(Serial.read() == '0') {
      digitalWrite(motorBr, LOW);
      digitalWrite(motorDir, LOW);
      digitalWrite(motorPin, HIGH);
      delay(500); 
      digitalWrite(motorBr, HIGH);
    }
   }
   
  }
  
  void loop() { while(1) {}
    }
  </code></pre>
  <p>This is my Python code:</p>
  <pre><code>import serial
  import time
  
  ser = serial.Serial('COM3', 9600, timeout=1)
  time.sleep(2)
  
  
  #I am forcing the script to write 1 to Arduino to make the motor turn
  
  ser.write(b'1')
  
  ser.flush()
  
  time.sleep(2)
  
  ser.close()
  </code></pre>
  <p>However, the communication isn't happening. Any insight should help. I am using Python 3.5 and Arduino Uno with the updated drivers.</p>
  <p>Edit:</p>
  <p>Hi Julien, yes the following code does its job:</p>
  <pre><code>int motorPin = 3;
  int motorDir = 12;
  int motorBr = 9;
  
  void setup() {
   // put your setup code here, to run once:
   //pinMode(motorPin, OUTPUT);
   pinMode(motorBr, OUTPUT);  
   pinMode(motorDir, OUTPUT);
   
   digitalWrite(motorBr, LOW);
   digitalWrite(motorDir, HIGH);
   digitalWrite(motorPin, HIGH);
   delay(500); 
   digitalWrite(motorBr, HIGH);
  
   delay(2000);
   
   digitalWrite(motorBr, LOW);
   digitalWrite(motorDir, LOW);
   digitalWrite(motorPin, HIGH);
   delay(500); 
   digitalWrite(motorBr, HIGH);
  }
  
  void loop() {
    // put your main code here, to run repeatedly:
  }
  </code></pre>
  <p>I have also made the following changes:</p>
  <pre><code>ser.write('1') --&gt; ser.write(b'1')
  
  Serial.read() == 1 --&gt; Serial.read() == '1' 
  
  Serial.read() == 1 --&gt; Serial.read() == 0x31 
  </code></pre>
  <p>doesn't seem to have any effect.</p>
  <p>The way I am accomplishing this is first uploading the Arduino program to memory, then running the Python script. No errors show up either.</p>
  <p>Execution of the Arduino code via Subprocess call in Python:</p>
  <pre><code>import subprocess
  
  actionLine = &quot;upload&quot;
  projectFile = &quot;C:/Users/Tomography/Desktop/DCM2/DCM2.ino&quot;
  portname = &quot;COM3&quot;
  boardname = &quot;arduino:avr:uno&quot;
  
  #I added the ardiono.exe to path, the command automatically sources the 
  Command = &quot;arduino&quot; + &quot; --&quot; + actionLine +&quot; --board &quot; + boardname + &quot; --port &quot; + portname + &quot; &quot; + projectFile
  
  print(Command)
  
  result = subprocess.call(Command)
  
  if result != 0:
   print(&quot;\n Failed - result code = %s --&quot; %(result))
  else:
   print(&quot;\n-- Success --&quot;)
  </code></pre>