Interfacing Arduino with Raspberry Pi © GPL3+

Interfacing Raspberry Pi with Arduino UNO

  • 59,761 views
  • 8 comments
  • 19 respects

Components and supplies

About this project

Raspberry Pi is great tool for embedded engineers but it lacks ADC. One more draw back is all its IO’s are 3.3V level. On the other side Arduino is good at sensing the physical world using sensors. To get benefits of both the systems one may want to interface them.

We can divide this interfacing problem into two steps. There are many interface options available in both boards like UART, IIC and SPI. The choice of protocol depends on the requirement of the application and roles of the Arduino and Raspberry Pi. The main problem arises when we try to make physical connection between the two boards because they are working on different voltage levels. We need to use level shifters to avoid any damage to the Raspberry Pi and to get expected results. There is nice blog on arduino forms. If you are not aware the level shifters you can build one by looking at this blog. Once we have physical connection between the two, they can start talking with each other. On Pi side you can use any of your favorite programming language.

In this experiment I will use python with pyserial. I have built a simple library for arduino use which you can expose your variables to the serial interface. Here Raspberry takes the client role and Arduino takes the server role. From Pi you can set or get any Arduino variables that were exposed using the library. You can only expose float type variables using this library, of course with little modification you can use it for any type. To make it general I restricted the type to float.

You just need to know about the two methods , to be able to publish your variables to Raspberry Pi.

This method just works like a dictionary, which takes a key and associated value is address of the variable that you want publish. This method will returns negative value if it fails to publish the variable, zero or positive value for successful publish. We will use this key when sending query to Arduino from Pi. You might want keep this key as short as possible because we don’t have much RAM on Arduino.

The other method is sync()

This method serves the requests from Raspberry Pi. You need to call this method from your main loop as frequently as possible otherwise you may miss some requests.

The Python library provides two methods which you can use to get or set the variables of arduino program

Usage Instructions:

Download the libraries from here

Burn the example sketch to arduino

Python will be already available in Pi so no need to install, but you need to install pyserial and

Now your ready to use the setup, before that you need to connect the Pi UART port to the Arduino SoftSerial port which you have used in the your sketch.

You can use this on windows systems as well, download Python for windows python-2.7.9 or higher

You may need to add the python installation directory to PATH, the library usage is same on Pi and windows. Once python is installed you need to install pyserial. You need to provide the Serial port when initializing the library.

You can use the following screenshot as reference to use the library. Arduino was connected to the system through USB-UART(COM3) , with heaterControl.ino loaded into it at the time of running these commands. SerialPublish.py should be present in your working directory.

You can find a Temperature controller designed using this driver here.

Code

Code snippet #1Plain text
int publish(String key,float *ptr)
Code snippet #2Plain text
void sync()
Code snippet #3Plain text
setResource(key,value)//both key and value should be strings,like setResource("T","98.0")

getResource(key) //key is  a string
Code snippet #4Plain text
cd ~
sudo pip install pyserial
wget https://raw.githubusercontent.com/sankarcheppali/PiAndArduino/master/SerialPublishPythonClient/SerialPublish.py
Github
https://github.com/sankarcheppali/PiAndArduino

Comments

Similar projects you might like

Pac-Man LED Pixel Panel Costume

Project tutorial by Ben Muller

  • 5,076 views
  • 4 comments
  • 87 respects

LoRa Gateway for DeviceHive

Project tutorial by DeviceHive IoT team

  • 1,341 views
  • 2 comments
  • 17 respects

IoT Bird Feeder with Sigfox and Tweeter

Project showcase by Gaël Porté

  • 404 views
  • 0 comments
  • 7 respects

Raspberry Pi and Arduino Laptop

Project tutorial by Dante Roumega

  • 17,894 views
  • 6 comments
  • 45 respects

SmartWay

Project tutorial by Universum

  • 286 views
  • 0 comments
  • 5 respects

Arduino-Based Automatic Guitar Tuner

Project tutorial by Ben Overzat

  • 3,684 views
  • 0 comments
  • 12 respects
Add projectSign up / Login