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
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.