User Tools

Site Tools


lego_nxt_rs485

Communication between LEGO NXT and Computer using RS-485 communications

A step-by-step tutorial for interfacing a LEGO NXT and Computer, read/write data between them over RS-485 communications. The goal of this tutorial is to use the Windows terminal program to communicate to/from your laptop and NXT. To achieve this goal we have to follow three practicals:

  • Send string data from Laptop to NXT and which is then displayed on NXT Brick.
  • One hits an arrow key on Brick and sends a string e.g. “left” which is displayed on Hercules.
  • Send string “hello” from Laptop to NXT and NXT send back to Laptop string “World!” which is then displayed on Hercules.

How RS-485 Works ?

RS485 is a high speed data communication standard which provides the 10 mbps data rate upto 50 feets. It can also extended upto 4000 feets with the speed of 100 mbps. RS485 allows multiple communication device (up-to 32) at half duplex rate on single wire.

In RS485, Data is transmitted differentially via twisted pair cable because this wire have specific properties such as high noise immunity as well as long distance capabilities. RS485 network is further categorized into parts:

  1. Two Wire Network
  2. Four Wire Network

In two wire network, transmitter as well as receiver are communicated with the help of twisted wire pair. In the four wire network, one transmitter is worked as master port and other one is connected as slave via one twisted pair. The other twisted pair cable helps to allow and connect slave ports to the master port.

In either configuration, devices are addressable, allowing each node to be communicated to independently.

Implementation cost of two wire network is less but it is limited upto half duplex. four wire network allows the full duplex communication.The main features of the RS485 software helps to configure and handle the addressing and turn around delay for the devices.

Hardware components

lego_nxt.jpg

nxt_cable_2_.jpg

nxt_cable.jpg

jumper_wire.jpg

usb_serial.jpg

rs232_rs485_convertor.jpg

breadboard.jpg

nxt_adapter.jpg

This photo shows all hardware components are used on this tutorial.

Software

  1. Windows terminal program, in this tutorial I used Hercules 3.2.8 program (Download: https://www.hw-group.com/software/hercules-setup-utility)

Connections

Connection Block Diagram for RS485 AND RS232

Connection Diagram between NXT adapter and RS-485

Internal Connection Diagram In this Diagram, we show that “How DB-9 Connector is connect with Lego”

  • T/R+ — Logic level RS485 in +ve polarity
  • T/R- — Logic Level RS485 in -ve polarity

db-9_connector.jpg

DB-9 Connection wires

connector.jpg

Colour Diagram of Lego Connecting Cable

Steps to make new cable

* Step-1 Firstly cut the cable with the help of stripper.

* Step-2 Identify the DB-9 points as well as connecting wires. connection.jpg

* Step-3 After identification, Solder them according to the diagram. solderpic.jpg

* Step-4 New wire is created !!! dsc_0444.jpg

Video


Commands for RS485

  • void UseRS485 (void)

Use RS485 port. Configure port 4 for RS485 usage.

  • char RS485Enable (void)

Enable RS485 port and also turn on hi-speed port.

  • char RS485Uart (byte baud, unsigned int mode)

Configure the RS485 UART parameters which includes baud rate, data bit, stop bit and parity.Baud Rate: HS_BAUD_115200( It means baud rate setup at 115202, if we change this baud rate so we have to change the baud rate of second device).HS_MODE_DEFAULT(It means we have use constant parity, stop and parity bits but we can change according to the need)

  • bool RS485DataAvailable (void)

Check the RS485 hi-speed port for the available date.

  • char RS485Read (byte & buffer[ ])

Read data from RS485 hi-speed port.buffer [] means a byte array which contains data.

Code

NXC Code for read string from laptop

ReadFromPC.c
// -------------------------------------------------------------
// R+ connect SDA
// R-         SCL
// -------------------------------------------------------------
task main() {
  byte inbuffer[];
  UseRS485(); // (1) Port S4 configured for RS485
  RS485Enable(); // (2) turn on RS485
  //RS485Uart(HS_BAUD_57600, HS_MODE_DEFAULT); // (3) initialize UART 57600
  RS485Uart(HS_BAUD_115200, HS_MODE_DEFAULT); // (3) initialize UART 112500
  TextOut(0, LCD_LINE1, "  NXT <> laptop");
  while (true) {
    while (!RS485DataAvailable()) {}     // wait for a message to arrive.
    RS485Read(inbuffer);
    ClearScreen();// clear screen before dislay new string
    TextOut(0, LCD_LINE4, ByteArrayToStr(inbuffer)); //Dislay message
    inbuffer=0;
    Wait(1000);
  }
}

NXC Code for send string to laptop when buttons are pressed

SendByButton.c
// -------------------------------------------------------------
// R+ connect SDA
// R-SCL
// -------------------------------------------------------------
task main() {
  bool Rbutton, Lbutton, Mbutton;
  UseRS485(); // (1) Port S4 configured for RS485
  RS485Enable(); // (2) turn on RS485
  RS485Uart(HS_BAUD_57600, HS_MODE_DEFAULT); // (3) initialize UART 57600
  Wait(100);
  TextOut(0, LCD_LINE1, "  NXT <> laptop");
  while (true) {
     if(ButtonPressed(BTNRIGHT, FALSE)){        // if Right Button is pressed
         while(ButtonPressed(BTNRIGHT, FALSE)); // Wait for Button is released
         SendRS485String(" Right "); // then send string
     }else
     if(ButtonPressed(BTNLEFT, FALSE)){
         while(ButtonPressed(BTNLEFT, FALSE));
         SendRS485String(" Left ");
     }else
     if(ButtonPressed(BTNCENTER, FALSE)){
         while(ButtonPressed(BTNCENTER, FALSE));
         SendRS485String(" Center ");
     }
  }
}

NXC Code for reading a string from the laptop, if string are "hello", send string "World!" to the laptop.

ReadAndWrite.c
// -------------------------------------------------------------
// R+ connect SDA
// R-SCL
// -------------------------------------------------------------
task main() {
  byte inbuffer[];
  UseRS485(); // (1) Port S4 configured for RS485
  RS485Enable(); // (2) turn on RS485
  RS485Uart(HS_BAUD_115200, HS_MODE_DEFAULT); // (3) initialize UART 112500
  Wait(100);
  TextOut(0, LCD_LINE1, "  NXT <> laptop");
  while (true) {
    while (!RS485DataAvailable()) {}
    RS485Read(inbuffer);    // read data
    ClearScreen();          // clear Screen
    TextOut(0, LCD_LINE1, " NXT <> laptop");
    TextOut(26, LCD_LINE4, ByteArrayToStr(inbuffer));   // dislay data
    int i = strcmp(ByteArrayToStr(inbuffer), "hello");  //compare string was received with "hello" , returns -1, 0, or 1
    // if the string was received are "hello", send back string " World!" to the laptop.
    if(i==0)
    {
    SendRS485String(" World!");
    }
    Wait(1000);
  }
}

Final Words

The baud rate was set up in NXT and Windows terminal program ( Hercules ) should be the same. If you have any questions, please contact me at phamquyenanh.qb@gmail.com,vikrampuri03@gmail.com

Download

lego_nxt_rs485.txt · Last modified: 2018/09/21 11:07 by vpuri