Sunday, 7 May 2017

Communicate between Raspberry Pi and Arduino

I was looking how I could talk between Raspberry Pi and Arduino, because sometimes is much easier to get a sensor, driver motor or else ready in C instead of trying to code something that will take months to get it working.

Raspberry Pi has the advantage to have SPI, I2C and Serial port communication, but for laziness I chose the Serial port. Of course you could try to go for SPI or I2C but it will take a little bit of more time to setup everything.

What you need:
You can use the serial port already existing on the Raspberry's pins but to make sure that I am not going to send some dirty data, because you can access the console via com, I chose to buy a usb ftdi.
You can buy the "official" usb to ttl, that will cost you around 15 pounds or you can go for a standard and cheap one here that works great!

Connect the usb ttl on your Raspberry's usb port and then 3 cables in yours Arduino, remember to dont connect the power pin! It wont damage your Arduino but better dont risk.

 Black pin = > Arduino Ground
 Green pin => Arduino RX
 White pin => Arduino TX

You can choose other two pins for RX and TX but if you need to talk straight to the Arduino, just use the common serial port.

I did few lines of code in Python, or just use any other languages to read and send on serial port.
This is the simple code:

On your Arduino just write whatever to answer or decode from Raspberry Pi, this is a simple example:

It's pretty simple and easy to communicate via Serial port, make sure that you always:

  • select right bitrate
  • remove the usb ttl when uploading a new Arduino project
  • try simple communication if there is any communication problems
  • never connect the red pin to the Arduino board