FREE SHIPPING ON ALL ORDERS OVER £20

60 DAY HASSLE-FREE RETURNS

Gamit ang module ng MAX485 RS485 kasama ang Raspberry Pi

Using the MAX485 RS485 Module with the Raspberry Pi

Ang MAX485 module ay nagbibigay-daan sa Raspberry Pi na makipag-ugnayan gamit ang RS485 protocol, na malawakang ginagamit para sa industrial automation, komunikasyong Modbus, at long-distance serial data transmission. Ipakikita ng gabay na ito kung paano ikonekta, i-configure, at gamitin ang MAX485 RS485 module sa Raspberry Pi gamit ang Python.


Mga Kakailanganin

  1. Raspberry Pi (anumang model na may GPIO support, hal. Pi 3, Pi 4)
  2. MAX485 RS485 Module
  3. RS485 Device (hal. Modbus sensor, industrial controller)
  4. Jumper Wires
  5. Python na naka-install sa Raspberry Pi

Hakbang 1: Pag-unawa sa MAX485 Module

Ang MAX485 ay isang TTL-to-RS485 transceiver na nagpapahintulot sa Raspberry Pi na magpadala at tumanggap ng mga RS485 signal.

Mga Pangunahing Tampok:

  • Sinusuportahan ang half-duplex RS485 communication
  • Gumagana sa 5V power (compatible sa 3.3V logic ng Raspberry Pi)
  • Gumagamit ng TX, RX, DE, at RE pins para sa kontrol ng komunikasyon

Hakbang 2: Pag-wire ng MAX485 sa Raspberry Pi

MAX485 Pin Raspberry Pi Pin Function
VCC 5V (Pin 2) Power Supply
GND GND (Pin 6) Ground
RO GPIO15 (Pin 10) RS485 Data Receive (RX)
DI GPIO14 (Pin 8) RS485 Data Transmit (TX)
RE GPIO18 (Pin 12) Receive Enable (LOW para tumanggap)
DE GPIO18 (Pin 12) Driver Enable (HIGH para magpadala)
A RS485 A Line Ikonekta sa RS485 Device
B RS485 B Line Ikonekta sa RS485 Device

Paalala: Ang mga RE at DE pins ay kinokontrol nang sabay ng GPIO18, para i-enable o i-disable ang transmission.


Hakbang 3: I-enable ang UART sa Raspberry Pi

  1. Buksan ang Raspberry Pi configuration tool:

    sudo raspi-config
    
  2. Pumunta sa Interface Options > Serial Port.

  3. I-disable ang serial console pero i-enable ang serial hardware.

  4. I-reboot ang Raspberry Pi:

    sudo reboot
    
  5. I-verify kung ang serial device ay available:

    ls /dev/serial*
    

    Dapat mong makita ang /dev/serial0.


Hakbang 4: I-install ang mga Kinakailangang Python Library

  1. I-update ang iyong Raspberry Pi:
    sudo apt update && sudo apt upgrade -y
    
  2. I-install ang Python serial communication library:
    pip3 install pyserial
    

Hakbang 5: Python Code para sa Komunikasyon via RS485

Basic Python Code para Magpadala at Tumanggap ng Data via RS485

import serial
import RPi.GPIO as GPIO
import time

# I-define ang GPIO pin para sa RE/DE control
RS485_CONTROL = 18  

# I-set up ang GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(RS485_CONTROL, GPIO.OUT)

# I-configure ang serial connection
ser = serial.Serial(
    port='/dev/serial0',  # Raspberry Pi UART port
    baudrate=9600,        # I-set ang baud rate para tumugma sa RS485 device
    timeout=1
)

def send_data(data):
    GPIO.output(RS485_CONTROL, GPIO.HIGH)  # I-enable ang transmission
    time.sleep(0.01)  # Maikling delay bago magpadala
    ser.write(data.encode())  # Magpadala ng data bilang bytes
    time.sleep(0.01)  # Maikling delay para masiguradong naipadala ang data
    GPIO.output(RS485_CONTROL, GPIO.LOW)  # I-enable ang pagtanggap

def receive_data():
    GPIO.output(RS485_CONTROL, GPIO.LOW)  # I-enable ang pagtanggap
    data = ser.readline().decode('utf-8').strip()
    return data

try:
    while True:
        send_data("Hello RS485 Device!\n")
        print("Data sent!")

        # Maghintay ng response
        response = receive_data()
        if response:
            print(f"Received: {response}")

        time.sleep(2)
except KeyboardInterrupt:
    print("Exiting...")
finally:
    ser.close()
    GPIO.cleanup()

Hakbang 6: Mga Aplikasyon ng RS485 Communication sa Raspberry Pi

  1. Industrial Automation – Makipag-ugnayan sa mga PLC, sensor, at controller gamit ang Modbus RTU.
  2. Long-Distance Communication – Sinusuportahan ng RS485 ang long-range data transmission hanggang 1200 metro.
  3. Multi-Device Networks – Ikonekta ang maraming device sa iisang RS485 bus.
  4. Smart Energy Meters – Magbasa ng mga Modbus-compatible na energy meter.
  5. IoT at Data Logging – Mangolekta ng data mula sa sensor at ipadala ito sa mga cloud server.

Troubleshooting

  1. Walang Komunikasyon

    • Suriin ang mga wiring connection, siguraduhing ang A at B lines ay tama ang pagkaka-konekta.
    • I-verify kung ang UART ay naka-enable gamit ang sudo raspi-config.
    • Siguraduhin na ang RS485 device ay gumagamit ng parehong baud rate na naka-set sa Python.
  2. Permission Denied Error

    • Idagdag ang user sa dialout group para sa access sa serial port:
      sudo usermod -aG dialout $USER
      sudo reboot
      
  3. Garbage Characters sa Natanggap na Data

    • Siguraduhin na parehong device ay gumagamit ng parehong baud rate at serial settings.
    • Suriin kung kailangan ng shielded cables para sa mas malalayong distansya.

Konklusyon

Ang MAX485 module ay nagbibigay-daan sa Raspberry Pi na makipag-ugnayan sa pamamagitan ng RS485, na ginagawa itong perpekto para sa industrial automation, Modbus devices, at long-distance serial data transmission. Sa pagsunod sa gabay na ito, makakapagtatag ka ng bidirectional communication sa pagitan ng Raspberry Pi at RS485 devices para sa mga proyekto sa IoT at embedded system. 🚀

Notice an Issue? Have a Suggestion?
If you encounter a problem or have an idea for a new feature, let us know! Report a problem or request a feature here.

Mag -iwan ng komento