ATtiny LED Control using RS485 protocol from PC

Pinterest icon

ATtiny PC RS485 communication software in CSharp dot net framework

In this tutorial ,we will control 4 LED's on the ATtiny Quad Motor Control Board from a Windows PC through a RS485 link.The Control progam running on the PC which control the LED's are written in C# and runs on the .Net Framework.The control code can also run unmodified on a Linux PC with Mono Framework installed.

SourceCodes

The Source codes for this tutorial can be downloaded from our github page here.

Software Binaries

RS485 PC communication software in Csharp and dot net framework

The prebuilt executable for the control program can be downloaded from below.

 

Hardware Setup

Hardware setup consists of a Windows/Linux PC running the control program which will control the LED's on the ATtiny Quad Motor Control Board.

The Control program communicates with a USB to RS485 converter which converts the USB signals from PC to RS485 signal levels.

The RS485 lines are then connected to the ATtiny Board which then decodes the signals and activates the LED's.

You can see the detailed setup in the image below.

ATtiny C# RS485 communication program

The connection between ATtiny Motor Control Board and USB to RS485 converter.

ATtiny RS485 interfacing project

Please note that the circuit diagram of the respective boards can be downloaded from their product pages.

Video

 

You can find out more information about the two boards, using the links below.

 

 

Software Setup

Since the project involves interfacing an ATtiny board with PC using RS485 protocol ,two different programming languages are used to write the software.

  • The Control Software that runs on Windows PC is written in C# and runs on the .Net Framework.
  • The  embedded software running on the ATtiny board is written in Embedded C and Compiled using AVR-GCC.

C# Control Software

C# RS485 control software for PC based equipment control

Control software is written in C# using Visual Studio community edition IDE.

The control software sends alpha numeric codes through the virtual serialport of the PC to the USB to RS485 converter.The codes are then converted to RS485 signal levels and send to the ATtiny Motor Control board through twisted pair cable.

The main component of the Control program is the RS485Class.cs file which contains the RS485class that controls the FT232 based USB to RS485 converter used here.The File is specific to that particular converter and may not work with other Converters.

The class contains two methods Read() and Write() that are used to send and receive data using the USB to RS485 converter.In this example we are using only the Write() method.

Whenever a user clicks on any one of the 4 LED buttons, a new RS485 port is opened by the software and a particular ASCII character is send through the RS485 line.The send character is then received by the ATtiny on the other end which then activates the corresponding LED.

For Eg,

if we click on the Button labelled "LED D4" on the control software,control is transferred to the Button_Click Event where the program calls the

 RS485Port_TransmitChar("A"); method

opening a RS485 port using CSharp (C#) and dotnet framework

which opens a virtual serial port and transmits the ASCII character 'A'.

 

Inside the RS485Port_TransmitChar("String data"); method,

C# RS485 Port Programming for dot net framework

We are creating a new RS485 Port called RP1 using the Serial Port number and baudrate provided by the user.

Then we are calling the Write() method, here RP1.Write(data) to transmit the character that is passed on to the method (In the above case ASCII A). 

ATtiny Side

On the ATtiny side,

The UART pins of ATtiny2313A (RX,TX) is connected to the RO and DI pins of MAX485 RS485 converter chip.Port pins PD3 and PD4 are used to put the MAX485 in Receive and Transmit mode.The circuit diagram of the RS485 section of the ATtiny Motor Control Board is shown below.

ATtiny2313a RS485 communication project

 

More information about the ATtiny Motor Control Board can be found in the Usermanual

The ATtiny2313A  configures the MAX485 chip in receive mode and waits for a charcter to be send from the PC.

The baudrate for reception is 4800bps and data format is 8N1.

After a character is received, a switch() statement is used to  control the LED's connected to ATtiny2313A as shown below.

code for ATtiny RS485 communication program

When all the LED's are switched ON.

ATtiny LED RS485 control Home automation