User Tools

Site Tools


lego_nxt_rs485

This is an old revision of the document!


Communication between LEGO NXT and Computer using RS-485 communications

This is a step-by-step tutorial for interfacing between a LEGO NXT and computer using RS-485 serial communication. The goal of this tutorial is to learn to communicate to/from a computer and an NXT using RS-485. Through this tutorial, you will be able to do the following:

  • Send and display string data from computer to NXT Brick.
  • Hit an arrow key on the Brick and send a string back to the computer.
  • Send and receive strings between the computer to the Brick as a call and response.

How Does RS-485 Work ?

RS485 is a high speed data communication standard that 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 up to 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

(links are at the bottom of tutorial)

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

  • LEGO NXT:

lego_nxt.jpg

  • Cable,NXT:

nxt_cable_2_.jpg

  • USB cable:

nxt_cable.jpg

  • Jumper wire:

jumper_wire.jpg

  • USB to RS232 cable:

usb_serial.jpg

  • DTECH RS232 to RS485 Converter DT9000:

rs232_rs485_convertor.jpg

  • Breadboard:

breadboard.jpg

  • NXT Breadboard adapter:

nxt_adapter.jpg

  ===== 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
      RS485Uart(HS_BAUD_115200, HS_MODE_DEFAULT);
      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(0, 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!");
    }
 
    else if(i!=0)
    {
    SendRS485String(" No match" );
    }
 
    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 [email protected],[email protected]

Download

Materials

lego_nxt_rs485.1541192317.txt.gz · Last modified: 2018/11/02 13:58 by jkreitz