title: Serial communication between raspberry pi and teensy (using UART / GPIO pins)
body: <p>I am trying to communicate from my raspberry PI to a teensy (a arduino that can pretend to be a mouse and keyboard for those uninitiated).</p>
  
  <p>I want to receive information on the arduino, and based on that information move the mouse.</p>
  
  <p>On the arduino side, I have made this test script:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>void setup() {
      Serial1.begin(9600); // According to the Teensy Docs, this is the RX1 &amp; TX1 on my board.
      // Serial itself corrosponds to the micro-usb port
  }
  String msg = "";      
  
  void loop() {
  
      if(Serial1.available() &gt; 0) {
        msg = "";
        while(Serial1.available() &gt; 0) {
            char read = Serial1.read();
            msg += read;
        }
        Serial1.write('X'); // Acknowledge with reply
      }
      Serial1.println(msg); // Output to console for debugging
      // Should be a number 1-9
      // TODO: further processing
  
  }
  </code></pre>
  
  <p>On the raspberry pi, I am running this test script:</p>
  
  <pre class="lang-py prettyprint-override"><code>import time
  import serial
  import random       
  ser = serial.Serial(            
  port='/dev/ttyS0',
  baudrate = 9600,
  parity=serial.PARITY_NONE,
  stopbits=serial.STOPBITS_ONE,
  bytesize=serial.EIGHTBITS,
   timeout=1
  )
  while True:
      n = random.randint(1,9)
      print("Writing", n)
      ser.write(n)
      time.sleep(1)
      feedback = ser.read()
      print(feedback) // Expecting 'X'
  </code></pre>
  
  <p>When i run the script, I see no output in the serial console as well as an empty message (<code>b''</code>) (Note the timeout parameter)</p>
  
  <p>I have already enabled serial communication with <code>raspi-config</code> and restarted. When I list devices (<code>ls -l /dev/</code>), I can see:</p>
  
  <pre><code>lrwxrwxrwx  1 root root           5 Apr 28 20:21 serial0 -&gt; ttyS0
  lrwxrwxrwx  1 root root           7 Apr 28 20:21 serial1 -&gt; ttyAMA0
  </code></pre>
  
  <p>As an additional test, I ran <code>minicom -b 9600 -o -D /dev/ttyS0</code> with 1 wire connecting RX to TX on the pi, and it successfully echoed back.</p>
  
  <p>Do I have a code issue, or possible hardware issue? Maybe since it is a teensy some different protocol is required? See <a href="https://stackoverflow.com/questions/47101062/python-serial-communication-arduino-teensy-to-raspberry-pi">here</a></p>
  
  <p>I'm out of ideas as to why it isn't communicating correctly. Here is my wiring:
  <img src="https://i.stack.imgur.com/N9Z9L.jpg" alt=""></p>