Professional Documents
Culture Documents
arduinoinfo SoftwareSerialRS485Example
SoftwareSerialRS485Example
(/SoftwareSerialRS485Example)
Edit
2(/SoftwareSerialRS485Example#discussion)
13(/page/history/SoftwareSerialRS485Example)
(/page/menu/SoftwareSerialRS485Example)
SoftwareSerialRS485Example
RS485 is
a physical
1/8
11/10/2016
arduinoinfo SoftwareSerialRS485Example
2/8
11/10/2016
arduinoinfo SoftwareSerialRS485Example
3/8
11/10/2016
arduinoinfo SoftwareSerialRS485Example
The second Arduino runs the code in the second example below.
You can copy and paste these into the Arduino IDE. (NOTE: You
can run two copies of the Arduino software at once if you wish.
Keep track of which Arduino is on which Serial Port!).
You use this by bringing up both connected Arduinos and running
the Serial Monitor on the "Master". Type in the top window of
the Serial Monitor and it should be echoed back as it is sent to
the second Arduino and looped back. You should see the Pin13
LED on the Remote Arduino blink as the data is received and
sent back.
Master Arduino Sketch:
/*YourDuinoSoftwareSerialExample1
ConnecttoanotherArduinorunning"YD_SoftwareSerialExampleRS485_1Remote"
ConnectthisunitPins10,11,Gnd
Pin3usedforRS485directioncontrol
TootherunitPins11,10,Gnd(Crossover)
OpenSerialMonitor,typeintopwindow.
ShouldseesamecharactersechoedbackfromremoteArduino
Questions:terry@yourduino.com
*/
/*(Importneededlibraries)*/
#include<SoftwareSerial.h>
/*(DeclareConstantsandPinNumbers)*/
#defineSSerialRX10//SerialReceivepin
#defineSSerialTX11//SerialTransmitpin
#defineSSerialTxControl3//RS485Directioncontrol
#defineRS485TransmitHIGH
#defineRS485ReceiveLOW
#definePin13LED13
/*(Declareobjects)*/
SoftwareSerialRS485Serial(SSerialRX,SSerialTX)//RX,TX
http://arduinoinfo.wikispaces.com/SoftwareSerialRS485Example
4/8
11/10/2016
arduinoinfo SoftwareSerialRS485Example
/*(DeclareVariables)*/
intbyteReceived
intbyteSend
voidsetup()/******SETUP:RUNSONCE******/
{
//Startthebuiltinserialport,probablytoSerialMonitor
Serial.begin(9600)
Serial.println("YourDuino.comSoftwareSerialremoteloopexample")
Serial.println("UseSerialMonitor,typeinupperwindow,ENTER")
pinMode(Pin13LED,OUTPUT)
pinMode(SSerialTxControl,OUTPUT)
digitalWrite(SSerialTxControl,RS485Receive)//InitTransceiver
//Startthesoftwareserialport,toanotherdevice
RS485Serial.begin(4800)//setthedatarate
}//(endsetup)
voidloop()/******LOOP:RUNSCONSTANTLY******/
{
digitalWrite(Pin13LED,HIGH)//Showactivity
if(Serial.available())
{
byteReceived=Serial.read()
digitalWrite(SSerialTxControl,RS485Transmit)//EnableRS485Transmit
RS485Serial.write(byteReceived)//SendbytetoRemoteArduino
digitalWrite(Pin13LED,LOW)//Showactivity
delay(10)
digitalWrite(SSerialTxControl,RS485Receive)//DisableRS485Transmit
}
if(RS485Serial.available())//LookfordatafromotherArduino
{
digitalWrite(Pin13LED,HIGH)//Showactivity
byteReceived=RS485Serial.read()//Readreceivedbyte
Serial.write(byteReceived)//ShowonSerialMonitor
delay(10)
digitalWrite(Pin13LED,LOW)//Showactivity
}
}//(endmainloop)
http://arduinoinfo.wikispaces.com/SoftwareSerialRS485Example
5/8
11/10/2016
arduinoinfo SoftwareSerialRS485Example
/*(DeclareUserwrittenFunctions)*/
//NONE
//*********(THEEND)***********
Questions:terry@yourduino.com
Youarenotamemberofthiswiki.Joinnow(http://arduino
*/
info.wikispaces.com/space/join)Dismiss
/*(Importneededlibraries)*/
#include<SoftwareSerial.h>
/*(DeclareConstantsandPinNumbers)*/
#defineSSerialRX10//SerialReceivepin
#defineSSerialTX11//SerialTransmitpin
#defineSSerialTxControl3//RS485Directioncontrol
#defineRS485TransmitHIGH
#defineRS485ReceiveLOW
#definePin13LED13
/*(Declareobjects)*/
SoftwareSerialRS485Serial(SSerialRX,SSerialTX)//RX,TX
/*(DeclareVariables)*/
intbyteReceived
intbyteSend
voidsetup()/******SETUP:RUNSONCE******/
{
//Startthebuiltinserialport,probablytoSerialMonitor
Serial.begin(9600)
Serial.println("SerialRemote")//Canbeignored
http://arduinoinfo.wikispaces.com/SoftwareSerialRS485Example
6/8
11/10/2016
arduinoinfo SoftwareSerialRS485Example
pinMode(Pin13LED,OUTPUT)
pinMode(SSerialTxControl,OUTPUT)
digitalWrite(SSerialTxControl,RS485Receive)//InitTransceiver
//Startthesoftwareserialport,toanotherdevice
RS485Serial.begin(4800)//setthedatarate
}//(endsetup)
voidloop()/******LOOP:RUNSCONSTANTLY******/
{
//Copyinputdatatooutput
if(RS485Serial.available())
{
byteSend=RS485Serial.read()//Readthebyte
digitalWrite(Pin13LED,HIGH)//Showactivity
delay(10)
digitalWrite(Pin13LED,LOW)
digitalWrite(SSerialTxControl,RS485Transmit)//EnableRS485Transmit
RS485Serial.write(byteSend)//Sendthebyteback
delay(10)
digitalWrite(SSerialTxControl,RS485Receive)//DisableRS485Transmit
//delay(100)
}//EndIfRS485SerialAvailable
}//(endmainloop)
/*(DeclareUserwrittenFunctions)*/
//NONE
//*********(THEEND)***********
(http://www.wikispaces.com/user/view/nickmaat)
Smallsuggestionsforthepinning
description
nickmaat
(http://www.wikispaces.com/user/view/nickmaat)
Sep5,2016
http://arduinoinfo.wikispaces.com/SoftwareSerialRS485Example
7/8
11/10/2016
arduinoinfo SoftwareSerialRS485Example
Firstofallthankyouforthispage,it
helpedmealot.
Iwouldsuggestasmall
enhancementconcerningthewiring
exampleDI,RO,etc.Namelyto
changethemfrom"DI(datain)topin
11"to"DI(datain)toTXO",s.t.they
applynotjusttotheArduinosyou
usebuttoallotherversionstoo.
Furtheranexplanationwhy"DE(data
enable)andRE[are]jumpered
together"whouldbenicetoo,
somethinglike:becauseoneisLow
Active,theotherisHighActive.
Best
(http://www.wikispaces.com/user/view/lasanthalikitha)
howtoaddsensortomaster
arduino?
lasanthalikitha
(http://www.wikispaces.com/user/view/lasanthalikitha)
Dec17,2015
Iwanttoknowhowtoadd
temperaturesensortomaster
arduinoandhowtosent
temperaturereadingtoslave
arduino?PleasereplyASAP..
HelpAboutBlogPricingPrivacyTermsSupport
Contributionstohttp://arduinoinfo.wikispaces.com/arelicensedunderaCreativeCommonsAttribution
NonCommercial3.0License.
PortionsnotcontributedbyvisitorsareCopyright2016TangientLLC
TES:Thelargestnetworkofteachersintheworld
http://arduinoinfo.wikispaces.com/SoftwareSerialRS485Example
8/8