How do i serially communicate between them, just using the usb ports on the raspi and the rx, tx pins on the arduino? Are there any better ways to communicate between raspi and arduino?
You may be finding the UART(or USART) communication, which is what Arduino RT/TX pins and some of RPi GPIO pins used for. USB ports uses 5V and UART pins are for 3.3V, thus you may need additional 5V-3.3V interconvert circuit to use as your first describe. If you choose to use direct UART communication instead, you don't need such additional circuit.
I've tried it and succeeded to serial communicate between a RPi and an AVR ATMEGA chipset which is the same MCU chipset used in Arduino.
To use this, firstly, you need to use
sudo raspi-config to disable serial shell. It's because this service is running on those pins by default and we need to use the pin by ourselves. It's in the advanced settings.
Secondly, wire RX/TX pins of RPi with of arduino. The RX pin of RPi should be connected with TX pin of Arduino and vice versa (Think of the crossover cable in TCP). You can see that pin 8 is TX and pin 10 is RX from Official GPIO Guide. This goes the same in RPi2 too, FYI.
Lastly, choose or program a terminal program to use them. I chose the easiest one:Python3 with pySerial library. This'll let you do it like this.
from serial import Serial from time import strftime, time, localtime, sleep def write(s, t=None): if t is None: t = strftime('s%H:%M:%S', localtime(time() + 1)).encode('ascii') else: t = ('s%s' % t).encode('ascii') s.write(t) while int(time()%1*1000): pass # wait for ms to be 0 s.write(b'\r') print(t) if __name__ == '__main__': from sys import argv s = Serial('/dev/ttyAMA0') for i in range(3): write(s, None if len(argv)<2 else argv) sleep(.1)
It's sending internet-synchronized RPi clock in command string format(for ex.
s05:55:20) to AVR on exact O'second clock to let it have real time system clock and repeat that 3 times to ensure it.
Here's my reading function for your comfortness. It requires the AVR have to send a message ended with a line break.
def read(s): buf=rst=b'' while buf != b'\n': buf = s.read() rst += buf rst = rst.rstrip() return rst
Well, I strongly recommend you to wrap them up with a class inheriting
Serial for better code reusability.
Also, note that
minicom kind of terminal programs could be useful if you need to send manual remote control signals.
Finally, you may need a service to forget about running python script everytime you reboot your RPi.
I have my own very simple approach about this and it's used in this project.
raspberry_pi/gpio_clock would be fitted for this.
Don't be confused.. This project is not using serial connected AVR but RPi GPIO itself to show the time.