Full Size Dr. Who K-9 Mk II Robot Dog – Part 24 – The Arduino Connection

For a while now I knew that I was going to be using a Arduino to sub process with the Jetson Nano. Let’s see if we can get them to talk.

JetsonHacks video

The best place to start is JetsonHack.com he has a page and a YouTube on the subject. I am not detailing the steps when he had done such a good job. Then install the Arduino IDE and test it with the blink sketch and make sure it is working. Write down your port address. Mine was /dev/ttyACM0 and it works.

Blinking Arduino Mega

PySerial is part 2 of getting them to send / receive data between one another with code.

Here is the Arduino side reading and writing the response.

Arduino IDE running on the Jetson Nano

Each side has to be complementary to one another and know what to expect. I send light ON and the Arduino reads the text and will reject the command if it is spelled wrong. That’s good though not to do the wrong thing.

Install pyserial then save the python as arduino_com.py on the Jetson and then save and upload your arduino code into its board. In a terminal run the python code. And as you send the commands the arduino responses.

# Importing Libraries
import serial
import time

def write_read(the_port, data):
    try:
        the_port.write(bytes(data, ‘utf-8’))
    except:
        print(“Can not write.”)

    time.sleep(0.05)

    try:
        data = the_port.readline()
    except:
        print(“Can not read.”)

    return data

def test_loop(the_port):
    control = True

    while control:
        num = input(“Enter a command or <q> to quit: “) # Taking input from user

        if num == ‘q’:
            control = False
        else:
            print(”     Jetson sent: ” + num)

            value = write_read(the_port, num)
            print(“Arduino returned: ” + str(value))
            print(“——————————-“)

def open_com(port_name):
    try:
        arduino_object = serial.Serial(port=port_name, baudrate=115200, timeout=.1)
    except:
        print(“Can not open port.” + port_name)

    return arduino_object

# ============================== Main =============================
if __name__ == “__main__”:
    print(“pyserial ver:” + serial.__version__)

    # /dev/ttyACM0 (Arduino Mega or Mega 2560)
    usb_port = open_com(“/dev/ttyACM0”)

    print(usb_port)

    test_loop(usb_port)

=======================================

Arduino code below….

// Serial Communication between Python and Arduino

// https://create.arduino.cc/projecthub/ansh2919/serial-communication-between-python-and-arduino-e7cce0

String load;

void setup() {
  Serial.begin(115200);
  Serial.setTimeout(1);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  while (!Serial.available());

  load = Serial.readString();
  if (load == “light ON”) {
    digitalWrite(LED_BUILTIN, HIGH);
  } else if (load == “light OFF”) {
    digitalWrite(LED_BUILTIN, LOW);
  }

  Serial.print(load);
}

I now have a powerful analog to digital converter and a way to connect to so many different sensors made for the arduino.

Dream it… Print it.

Disclaimer – I am not associated with BBC or Dr Who in any way. This work is for hobby purposes and fan related enjoyment.

Thanks stopping by .


Leave a comment