Browse our Blog. You will find multiple applications, solutions, code examples. Navigate using the tag cloud or search using specific criteria

Raspberry Pi 3 B+ with MAX485 module


Introduction


In this post will be shown how to communicate a Raspberry Pi 3 B+ with an M-Duino by RS485 using a MAX 485 module.  


Requeriments


M-Duino / Ardbox family

Raspberry Pi 3 B+

MAX485 module


MAX485 Module

On-board MAX485 chip is a low-power and slew-rate-limited transceiver used for RS485 communication. Adopting half-duplex communication to implement the function of converting TTL level info into RS485 level, it can achieve a maximum transmission rate of 2.5 Mbps. 

Pinout:

Resultado de imagen de max485 pinout

Dependig on RE and DE connection the module works as receiver or transmitter. Connected to VCC transmits data and connected to GND receives data. 

It is a cheap module. You can find lots of 5 unit for less than one euro. 


Connections


Raspberry Pi 3 B+ Pinout


Resultado de imagen de raspberry pi 3 b+ pinout


Raspberry Pi to MAX485


Raspberry Pi 3 B+ PinsMAX485 Module Pins
UART_TXDRX
UART_RXDTX
GPIO17RE
GPIO27DE

Digital pins GPIO17/27 used to establish the communication type. Receiver or transmitter mode.


MAX485 to M-Duino/Ardbox 


MAX485 Module PinsM-Duino / Ardbox Pin
VCC3.3 V
BB-
AA+
GNDGND


Diagram

 Code Python


In this code example we send a character from the PLC to the Raspberry, print it, and answer to the PLC with the same character, that is an "echo". Note that in case of receive data, pin 17 and 27 are disable and enable in case of transmit data.

#!/usr/bin/env python3

' IMPORTANT: remember to add "enable_uart=1" line to /boot/config.txt

from gpiozero import OutputDevice
from time import sleep
from serial import Serial

' RO  <-> GPIO15/RXD
' RE  <-> GPIO17
' DE  <-> GPIO27
' DI  <-> GPIO14/TXD

' VCC <-> 3.3V
' B   <-> RS-485 B
' A   <-> RS-485 A
' GND <-> GND

re = OutputDevice(17)
de = OutputDevice(27)

' enable reception mode
de.off()
re.off()

with Serial('/dev/ttyS0', 19200) as s:
	while True:
		' waits for a single character
		rx = s.read(1)

		' print the received character
		print("RX: {0}".format(rx))

		' wait some time before echoing
		sleep(0.1)

		' enable transmission mode
		de.on()
		re.on()

		' echo the received character
		s.write(rx)
		s.flush()

		' disable transmission mode
		de.off()
		re.off()


Do you want more information?

Open Source technology allows you to develop your installations.

Just fill the form and we will contact you as soon as we can.

Send