Five steps to connect Jetson Nano and Arduino

Yesterday's post showed how to link a micro:bit to the Jetson Nano.

One of the members of the (unofficial) NVIDIA Jetson Nano group on Facebook asked about connecting an Arduino to the Jetson.

Here's a simple recipe for getting data from the Arduino to the Jetson Nano. It should work on all the Jetson models, not just the Nano, but I only have Nanos to hand.

On request, I've added a recipe at the end of this post which sends data from the Jetson Nano to the Arduino; it turns the default LED on the Arduino on or off.

The recipe for sending data from the Arduino to the Jetson has just 5 stages:

  1. Program the Arduino. (I used the ASCIITable example).
  2. Connect the Arduino to the Jetson using a USB connector
  3. Install pyserial on the Jetson
  4. Download a three-line Python script
  5. Run the script.

 

Programming the Arduino


I used an Arduino Uno, and checked it on a verteran Duemilanove (above), but any Arduino should work.

You'll need to do this step using a workstation, laptop or Pi with the Arduino IDE installed. (There seems to be a problem with the Arduino IDE on the Nano, so I couldn't use that).

  1. Connect the Arduino to your workstation vai USB.
  2. Open the Arduino IDE.
  3. Select File/Examples/04/ Communication/ASCIITable.
  4. Upload the example sketch to your Arduino.
  5. Open the Serial Monitor from the Tools menu. You should see a list like this one:

Connect to the Nano


Unplug the Arduino from your workstation and plug it into one of the USB prots on the Jetson Nano.

Install pyserial


If you've been following my tutorial series, open a browser and open the Jupyter lab page. Open a terminal.

If you're using the Nano with a monitor, mouse and keyboard, open a terminal window by typing Ctrl-Alt-T.

Type sudo -H pip3 install pyserial

Once the installation is complete, you're ready to download a short Python script.

Download the script


In the terminal window, type

wget http://bit.ly/jetson-serialpy -O ser.py

(That's a capital O, not a zero!)

When that has finished, type

cat ser.py

The contents of the file should look like this:

import serial
with serial.Serial('/dev/ttyACM0', 9600, timeout=10) as ser:
    while True:
        print(ser.readline())

Run the file


Type python3 ser.py


You should see a display like this:



Sending data the other way


You'll need this program on the Arduino.

void setup() {
  // start serial port at 9600 bps:
  Serial.begin(9600);
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
  while (!Serial) {
    ; // wait for serial port to connect.
  }

}

void loop() {
  char buffer[16];
  // if we get a command, turn the LED on or off:
  if (Serial.available() > 0) {
    int size = Serial.readBytesUntil('\n', buffer, 12);
    if (buffer[0] == 'Y') {
      digitalWrite(LED_BUILTIN, HIGH);
    }
    if (buffer[0] == 'N') {
      digitalWrite(LED_BUILTIN, LOW);
    }
  }
}
And this one on the Nano (call it serial_send.py)

import serial

with serial.Serial('/dev/ttyACM0', 9600, timeout=10) as ser:
    while True:
        led_on = input('Do you want the LED on? ')[0]
        if led_on in 'yY':
            ser.write(bytes('YES\n','utf-8'))
        if led_on in 'Nn':
            ser.write(bytes('NO\n','utf-8'))
 

Plug the Arduino's USB lead into the Nano, and run the program on the Nano by typing

python3 serial_send.py 

The prorgram will ask you repeatedly if you want the LED on or off.

Answer Yes and the LED on the Arduino will turn on.
Answer No and the LED will turn off.

Have fun!






Comments

  1. Hello,

    I am trying to connect PLC and Jetson Nano via RS485. I used RS232/RS485 converter between PLC and Jetson Nano. PLC needs a register addres to read on Jetson Nano. What should I write as an addres, which holds data in Jetson Nano.

    ReplyDelete
  2. Many Thanks! Can I use EPS32 instead of Arduio?

    ReplyDelete
    Replies
    1. I have only programmed the ESP32 in Python, not C, but I imagine you could. You might need to change the code.

      Delete
  3. After running python3 ser.py I am getting this error, "FileNotFoundError: [Errno 2] No such file or directory: '/dev/ttyACM0'" Do you know why this could be?

    ReplyDelete
  4. That's usually a sign that the USB cable is a power cable, not a data cable.

    ReplyDelete

Post a Comment

Popular posts from this blog

Controlling a Raspberry Pi Pico remotely using PySerial

Raspberry Pi Pico project 2 - MCP3008