Previously, I wrote about RS485 communications between two Arduinos using a MAX485 IC. The link for that post is HERE. It showed one Arduino as a tranmitter and the other as a receiver. This post is very similar, however, I am using a different IC, the SN75176BP. This post is about SN75176 RS485 communications between two Arduinos which allows either one to transmit and receive at the same time.
The SN75176 IC is also an RS485 transceiver like the MAX485 however it is full duplex where as the MAX485 is only half duplex. Full duplex allows sending and receiving at the same time.
Here is the pin layout for the SN75176BP IC which I am using:
This chart shows the function of each pin of the SN75176:
SN75176 +----U-----+ RO | 1 8 | Vcc RE | 2 7 | B DE | 3 6 | A DI | 4 5 | Gnd +----------+ RO - Receiver out RE - Receiver enable (enabled when this pin is LOW) DE - Driver enable (enabled when this pin is HIGH) DI - Driver in (the transmitter pin) GND - Ground (0V) A - Connect to pin A of the other 485 IC B - Connect to pin B of the other 485 IC Vcc - Power (Vcc)
Using two Arduinos, each with an SN75176BP, we can establish two way communications:
Serial / USB <-> Arduino <-> SN75176 (RS485) <-> SN75175 (RS485) <-> Arduino <-> Serial / USB
The Fritzing diagram source is HERE in case you would like to modify it. Note that the RE (Receiver Enable) pin is wired permanently low. The DE (Driver Enable) pin is turned on (high) only when data is sent.
The Arduino UNO that is used here only has one serial device, so the software serial library is used to add another serial device on pins 8 and 9. The software serial port is used to communicate (via RS232) to the SN75176 and the built in serial port (via USB) is used to communicate (via RS232) with the host computer the Arduino is attached to.
Load the following code into each Arduino:
/* Arduino Sketch to allow RS232/RS485 communications via two Arduinos with attached SN75176 ICs. From: earl@microcontrollerelectronics.com */ #include <SoftwareSerial.h> SoftwareSerial mySerial(8, 9); // RX, TX #define DE 7 void setup(){ Serial.begin(115200); mySerial.begin(115200); pinMode(DE,OUTPUT); digitalWrite(DE,LOW); } void loop() { if (mySerial.available()) Serial.write(mySerial.read()); if (Serial.available()) { digitalWrite(DE,HIGH); mySerial.write(Serial.read()); digitalWrite(DE,LOW); } }
Attach a serial connection to each Arduino and data can be sent back and forth thru the connection between the two SN75176 ICs. Use my handy updated Python serial communications program for that purpose:
#!/bin/env python #Python Serial Communications Script #from earl@microcontrollerelectronics.com import serial,sys,glob,select def scan_ports(pat): scan = glob.glob(pat) if (len(scan) == 0): return("") else: return(scan[0]) serport = "" rate = "115200" if (len(sys.argv) > 1): l = len(sys.argv) - 1 while(l>0): if (sys.argv[l][0] == '/'): serport = sys.argv[l] else: rate = sys.argv[l] l = l - 1 if (serport == ""): serport = scan_ports("/dev/ttyACM*") if (serport == ""): serport = scan_ports("/dev/ttyUSB*") if (serport == ""): print("Unable to find any ports scanning for /dev/[ttyACM*|ttyUSB*]") sys.exit() ser = serial.Serial(port=serport,baudrate=rate,parity=serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE,bytesize=serial.EIGHTBITS,timeout=1) print("connected to: ",ser.portstr," ",ser) while True: try: tr = ser.inWaiting() if tr > 0: print(ser.read(tr).decode("utf-8"),end='') # for x in line: print (x.encode('hex'),end='') sys.stdout.flush() except KeyboardInterrupt: sys.exit() except: pass while sys.stdin in select.select([sys.stdin], [], [], 0)[0]: # line = sys.stdin.readline() # for x in line: print (x.encode('hex'),end='') # print ser.write(sys.stdin.readline().encode("utf-8")) ser.close() sys.exit()
Once this is working, you have a ‘prototype’ to emulate other equipment or use ‘half’ (one Arduino and one SN75176) to attach to other devices using the RS485 interface such as Modbus RTU.
1 comments
You said that the SN75176BP is a full duplex. However, the description of the product here (https://www.ti.com/product/SN75176B#tech-docs?HQS=ti-null-null-verifimanuf_df-manu-pf-octopart-wwe)
says it’s a half duplex. Could you please explain why there’s a difference between your post and the product description?