This is an old revision of the document!
Table of Contents
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 (via a Windows terminal program) 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:
- Two Wire Network
- 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.
Software
- Bricx Command Center(Download:https://sourceforge.net/projects/bricxcc/files/bricxcc/)
- Windows terminal program, in this tutorial I used Hercules 3.2.8 program (Download: https://www.hw-group.com/software/hercules-setup-utility)
Hardware components
(links are at the bottom of tutorial)
This photo shows all hardware components are used on this tutorial.
Each individual component can be found below:
- 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
Connection Block
This section will show the two ways to create the connection block - making a new cable and using a breadboard.
Connection Block Diagram for RS485 AND RS232
- T/R+ — Logic level RS485 in +ve polarity
- T/R- — Logic Level RS485 in -ve polarity
DB-9 Connection wires
Colour Diagram of Lego Connecting Cable
Steps to make new cable
* Step-1 Firstly cut the cable with the help of stripping tool.
* Step-2
Identify the DB-9 points as well as connecting wires.
* Step-3
After identification, Solder them according to the diagram.
* Step-4
New wire is created !!!
To use the wire, insert one side into port 4 on the NXT Brick. Insert the other side into your computer's USB port.
Connecting NXT and Computer via Breadboard
Connection Diagram between NXT adapter and RS-485. The end of the usb gets plugged into the computer.
Using Hercules
Download Hercules and install.
Plug in the NXT using the wire we created above.
Open Hercules and go to the Serial tab. Choose an open port under the “Name” section. If you do not know how to check which ports are open, go to the Windows device manager and check under the “ports” section. You can update baud rate and port by right clicking the specific device and selecting “properties.”
Set the baud rate to 115200, then click open. Now you will be able to interface with your NXT Brick via RS485.
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); } }
Video
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
Materials
- DTECH RS232 to RS485 Converter DT9000 (Buy Link:https://www.amazon.com/RS-232-Female-Adapter-Converter-Passive/dp/B01K7X9EGK/ref=sr_1_1_sspa?ie=UTF8&qid=1537385474&sr=8-1-spons&keywords=rs232+to+rs485&psc=1)
- NXT Breadboard adapter (Buy Link:https://www.amazon.com/Breadboard-Adapter-for-LEGO-MINDSTORMS/dp/B00A0P5FY6)