Arduino Jeston Nano / Xavier NX Communication using Python via USB

When working on Arduino project with Jetson Nano or Jetson Xavier NX, sometimes we need to retrieve data from the Arduino or send a command to control the MCU. You can surely use Arduino compatible boards with onboard WiFi and BLE, like Wio Terminal or an ethernet shield to communicate with Jetson Nano through the network. But you can also adopt a much easier way, which is to talk to your Arduino simply via USB using Python.

Firstly, connect your Arduino with Jetson Nano through USB cable. Then open the terminal on Jeston Nano. Type in the following command.

ls /dev/ttyA*

If you get /dev/ttyACM0, it means your Arduino is recognized by your Jetson Nano because ttyACM0 will only show up when 2 USB devices are connected to each other. Now let’s try to communicate with Arduino. Firstly, install PySerial using the command pip3 install pyserial. I’m using python3, so my command is pip3. You should use pip if you are using python2.

After the installation of PySerial, give it a test first.

If everything works fine, we can proceed to the next step. Now let’s start coding.

Arduino Side

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
void setup()
{
  Serial.begin(119200);
}

void loop()
{
  if (Serial.available())
  {
    Serial.println("Hello Jetson!");
  }
}

Python Side

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import serial 
import time

arduino = serial.Serial('/dev/ttyACM0', 115200, timeout=1)


while True:
    try:
        data = arduino.readline()
        if data:
            print(data)
            print('Hi Arduino')
    except:
        arduino.close() 

Remember to set the baud rate as the same as Arduino. Name the python file as test.py. Then use sudo to run the code, otherwise, you’ll be denied permission. Now you can make Jetson and Arduino talk to each other!

About Author

Calendar

June 2020
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930