Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serial communication between raspberry pi and teensy (using UART / GPIO pins)

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).

I want to receive information on the arduino, and based on that information move the mouse.

On the arduino side, I have made this test script:

void setup() {
    Serial1.begin(9600); // According to the Teensy Docs, this is the RX1 & TX1 on my board.
    // Serial itself corrosponds to the micro-usb port
}
String msg = "";      

void loop() {

    if(Serial1.available() > 0) {
      msg = "";
      while(Serial1.available() > 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

}

On the raspberry pi, I am running this test script:

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'

When i run the script, I see no output in the serial console as well as an empty message (b'') (Note the timeout parameter)

I have already enabled serial communication with raspi-config and restarted. When I list devices (ls -l /dev/), I can see:

lrwxrwxrwx  1 root root           5 Apr 28 20:21 serial0 -> ttyS0
lrwxrwxrwx  1 root root           7 Apr 28 20:21 serial1 -> ttyAMA0

As an additional test, I ran minicom -b 9600 -o -D /dev/ttyS0 with 1 wire connecting RX to TX on the pi, and it successfully echoed back.

Do I have a code issue, or possible hardware issue? Maybe since it is a teensy some different protocol is required? See here

I'm out of ideas as to why it isn't communicating correctly. Here is my wiring:

like image 730
retep Avatar asked Apr 29 '20 02:04

retep


1 Answers

You have the Rx line connected together and the Tx lines connected together. What one transmits the other needs to receive. You need to go Tx-Rx and Rx-Tx.

like image 165
Delta_G Avatar answered Oct 18 '22 21:10

Delta_G