0

I am trying to talk to Arduino Uno over RS485, i am unable to receive anything, i receive complete garbage values. I am using Software UART on Arduino and Hardware on Raspi. code on Raspi is as below, please ignore comments.

import serial
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)
def get_temp_val(): # function for getting some value.
    ser=serial.Serial('/dev/ttyAMA0',9600) #enable the serial port
    ser.close()
    ser.open() 
    while True:   #loop for reapeated actions.
        GPIO.setup(7,1) #enable  transmit mode on MAX485
        time.sleep(4)
        val=raw_input("enter something:") 
        ser.write(val)
        time.sleep(.5)
        GPIO.setup(7,0) #switch the MAX485 to receive mode.
        time.sleep(4)
        temp=ser.read(ser.inWaiting())
        print "received data is", temp 
        GPIO.cleanup()
get_temp_val()

EDIT : one way communication from arduino to raspberry pi was achieved, baudrate=9600. I am using ADM485(0-5v logic) chip for RS485 interface. I am giving input to raspi through a voltage divider to make sure that pins are not damaged. But i am unable to send from raspi to arduino. Whats my apprehension is whether 3.3v logic from pi to ADM485 is culprit? Should i use any booster to make 3.3v pi logic to 5v.

  • can i know why it was downvoted??? – Ruthvik Vaila May 28 '14 at 12:09
  • Most likely because there is no explicit question, just what is happening and some code. You may want to include what you have tried to fix this issue so far, and any errors or results you are getting (edit them into the question). – RPiAwesomeness May 28 '14 at 14:40
  • 1
    This question appears to be off-topic because it is about an Arduino issue, probably with software, one that may fit better on either arduino.se or stackoverflow. However, I am unable to raise a migration flag apparently, so this was the next best. – RPiAwesomeness May 28 '14 at 21:30
  • 1
    @RPiAwesomeness: i dont think its off topic, i edited the question i added some more details of my experiments. – Ruthvik Vaila May 30 '14 at 7:13
  • Thank you. That has cleared up some things and made your question a bit clearer. I think that your issue may be that you are sending 3.3 volt signals to the Arduino, as you mentioned. Try converting the signals to 5 volts and try again. – RPiAwesomeness May 30 '14 at 14:46
3

You will need whats called a logic level shifter to control 5v things with rpi's 3.3v gpio.

This is a nice one: http://www.adafruit.com/products/757

To make life easier I would suggest using usb to rs485 on the pi -- http://www.amazon.com/Converter-Adapter-ch340T-Support-64-bit/dp/B009SIDMNM/ref=pd_bxgy_pc_img_z

With that your device would be something like /dev/ttyUSB0 instead of /dev/ttyAMA0 -- it will depend on what all usb devices you have connected and you can check by using the command lsusb

2

actually there was some issue with delays while switching the rs485 module, make sure that rs485 on arduino side is in reading mode when Pi is in writing mode and viz. edited code looks like this:

import serial
import sqlite3
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12,GPIO.OUT)
GPIO.setup(7,GPIO.OUT)
conn=sqlite3.connect('temp_table.db')
c=conn.cursor()
def get_temp_val():
    ser=serial.Serial('/dev/ttyAMA0',9600,timeout=1)
    ser.close()
    ser.open()
    try:
        while True:
            GPIO.output(7,1) 
            time.sleep(.5)
            val=raw_input("enter something:") #send some value to ARDUINO
            ser.write(val)
            time.sleep(.5)
            GPIO.output(7,0) 
            time.sleep(5)
            temp=ser.read(ser.inWaiting())
            print "received temp is :", temp
    except KeyboardInterrupt:
        GPIO.cleanup()
get_temp_val()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.