So, how to go about sending sensor data from Arduino to Raspberry Pi?
There are so many reasons why one would like to send sensor data from Arduino to Raspberry Pi.
One application that I can think of is creating some kind of monitoring project or an Arduino-Raspberry Pi data logger, where you would want all parameters to be saved at say a server from where it can be accessed later by an individual or a group of individuals.
Another very important reason why you would want an Arduino Raspberry Pi cross over project is because raspberry pi isn’t particularly designed to receive analog inputs as an Arduino is.
Yes, of course you can use an intermediary device like the MCP3008, but again let’s be honest that’s an extra interfacing step.
On the other hand, a microcontroller setup like an Arduino already has a lot of ADC’s that are ready to be deployed.
Additionally, retrieving parameters from a physical system is much more easy, apt and effective when you use a simple microcontroller setup rather than using a high-speed computing setup like a Raspberry Pi.
Therefore, the use case is demanding and the duo looks attractive from the context of practical deployment but the question is,
Can Raspberry Pi and Arduino Work Together?
Yes, absolutely they can. It’s very easy to setup a two-way communication between Raspberry Pi and Arduino using a serial communication protocol like the I2C. Arduino’s strength in reading inputs and Raspberry Pi as a Linux compute unit presents a potent combination that opens a sky of possibilities.
Here are the steps at a glance you need to follow in order to send sensor data from Arduino to Raspberry Pi.
- Initiate serial communication on your microcontroller setup (or Arduino).
- Physically connect Arduino and Raspberry Pi using a USB cable.
- Initiate I2C on your Raspberry Pi.
- Create a Python code base to receive sensor data coming from Arduino and a way to display the input.
- Run the code base and you are good.
Now that you have a mental map of the different steps that you will have to follow, its time to put all of these steps into action.
But before that I think these articles would do you a world of good, in context to the topic under discussion.
- Do Arduino Sensors Work With Raspberry Pi?
- Can Raspberry Pi be used as a general-purpose computer?
- How To Properly Setup A Raspberry Pi?
- How To Format SD card for Raspberry Pi?
- How To Install Raspbian On Raspberry Pi?
- What Are Embedded Systems?
Setting Up Serial Communication On Arduino
Now because there are so many different types of sensors you can interface with Raspberry Pi, I think the best way to approach this particular post is by teaching you the core concept.
What I will also do is maybe pick up a project where I will put all these learnings into action.
Picking up a project directly I feel will pave way for more confusion rather than providing clarity.
So, let’s quickly complete the core concepts first and then follow it up with an example later so that the understanding on sending sensor data from Arduino to raspberry pi is much mor comprehensive.
And in that regard the first thing that you need to initialize serial communication on Arduino.
Through this serial communication channel, you will be able to relay sensor data from Arduino to Raspberry Pi connected via USB cable.
Here is the code that you need to upload to your Arduino in order to start or initialize its serial communication channel.
void setup(){
Serial.begin(9600);
}
void loop(){
Serial.println("Serial Communication Established");
delay(1000);
}
In the setup() function we are first establishing the serial communication followed by the loop() snippet where we are sending a simple print statement serially at a delay of 1 second.
In addition to the serial communication code, you will also have to upload your project specific code (sensor initialization, reception, constant declarations) to your Arduino.
Interfacing Arduino With Raspberry Pi
This is a very simple step to follow.
All that you really need to do here is connect your Arduino and Raspberry Pi using a USB cable. This will be the physical communication channel over which the sensor data is going to be transmitted.
Just so you know you can transfer sensor data wirelessly as well, but that is a topic for another day and another separate article.
For now, let’s learn how serial communication happens between Arduino and Raspberry Pi.
Around you, you will find many gadgets that are basically a cluster of small electronic units or circuitry that work in tandem to generate the usability that you want out of your gadget.
The electronic circuitries are able to operate in tandem because of a communication channel between them.
That communication channel in our case where we are learning how to send sensor data from Arduino to Raspberry Pi is a USB cable via which serial communication happens.
Serial communication in simple terms is nothing but a way to transfer data bits from one device to another. In this mode of communication, data bits are sent serially from one device to another sequentially or serially or to put it even more simply, one bit at a time.
This type of communication is in contrast to aptly named parallel communication where all data bits are transferred a once via parallel communication lines or wires.
The simplest serial communication that you can establish between an Arduino and Raspberry Pi is using a USB cable.
Note that you can use any of the available USB ports on Raspberry Pi.
You can use the same USB cable to connect the two units that you use with your Arduino which you use to upload code on the Arduino setup.
Note that the serial communication can also be established by connecting the GPIO and RX/TX pins together.
However, if you choose to do that you will need a voltage level shifter as an intermediate IC to protect the Raspberry Pi.
This is because Raspberry Pi operates at 3.3V and Arduino (except for 101) operates at 5V. And therefore, in absence of an appropriate level shifter the arrangement won’t work.
Initiating I2C Communication Protocol On Raspberry Pi
For successful transmission of data bits, you now need to enable I2C serial communication protocol on the Raspberry Pi.
Doing this is really simple.
Open the terminal and run the following command
$ sudo raspi-config
You should see the following configuration screen.
From the available list select ‘5 Interfacing Options‘. In the interfacing options, you will once again see a list out of which you need to select P5 I2C and enable it as shown.
The core communication protocols that you will have to eventually work with if you are actively working on microcontroller systems like Arduino are UART, SPI and of course I2C.
I think given the context of the post I should briefly touch upon them so as to make your understanding comprehensive.
- UART
UART is short for Universal Asynchronous Reception And Transmission and is one of the simpler serial communication protocol that happens over two lines or digital pins which are RX (pin 0) and TX (pin 1).
The Arduino board does contain an onboard USB-to-Serial converter which allows the microcontroller subsystem to directly communicate to a computer.
- I2C
I2C or Inter-Integrated-Circuit is also a serial communication protocol, which is commonly deployed with sensor-based projects or similar where multiple modules are required to operate together.
Commonly the core circuit or the driver circuit is referred to as the Master and the receiving devices like the sensors, motors etc. are referred to as slaves.
I2C allows for the connection of multiple masters and slaves while at the same time maintaining a clear communication pathway among them.
- SPI
SPI stands for serial peripheral interface which is commonly deployed when you want two microcontrollers to talk to each other.
One key difference between I2C and SPI is that, while I2C allows for multiple masters and slaves to form a communication network, in SPI one master can be connected to a maximum of four slaves.
The common deployment scenarios include situations where speed is important like writing on SD cards, display devices or situations where the physical parameters change quickly and they need to be quickly logged to create a graph or similar.
Coding On Raspberry Pi To Receive Sensor Data From Arduino
This is penultimate step to sending sensor data from Arduino to Raspberry Pi and is also probably the most important one.
I am saying this because if you have played with Arduino, even a little bit, everything that we have discussed thus far should feel familiar.
In this step let’s put in some code on our Raspberry Pi which will enable it to receive the sensor data serially and display it on the screen.
In order to do this first create a file SerialSensor.py on your desktop. You can name it any which way you want.
Now open the file and write the following code on it.
import serial
import string
import time
#opening serial port
ser=serial.Serial('/dev/ttyUSB2', 9600)
#Here /dev/ttyUSB2 is used
#It can be different in your case like /dev/ttyUSB0, /dev/ttyUSB1 etc.
while True:
serialdata=ser.readline()
print(serialdata)
#read serial data and print it on screen
The code is pretty self-explanatory. All we are doing here is reading sensor data that we are receiving serially and printing it on screen.
In order to open a serial connection, the command that we used was
ser=serial.Serial('/dev/ttyUSB2', 9600)
As I said the ttyUSB2 in the code snippet is subjective and can differ in your case.
In order for you to find the right identifier for the USB interface you need to type in this command into the terminal.
$ ls /dev/tty*
You will see a string of data on the screen, find the one containing ACM or USB, and use it in your code.
Executing The Code On Raspberry Pi
And that’s pretty much it. Check that everything is connected and run the SerialSensor.py on the Raspberry Pi.
If everything is in its place you should be able to see the message ‘Serial Communication Established’ printed onto the screen every second.
It really is a great feeling when a small but significant setup like this comes to fruition.
However, if it doesn’t happen don’t get disheartened debugging and troubleshooting is an inherent aspect of the whole practical electronics domain.
- If you don’t see any output on the screen just double check the connections.
- If connections are okay see if serial data is transmitted from Arduino or not by connecting it to your PC and opening the serial monitor.
- The sensor data or string that you have added in your code should show up here, if it doesn’t check the code once again.
- If the code indeed shows up on serial monitor but you don’t see it on Raspberry Pi, try restarting your Raspberry Pi to reset any and all connections.
- This should troubleshoot and help you pinpoint the core issue. Try and see if you can find the solution on google, if you can’t comment below or shoot me an email and maybe we can solve the problem together.
FAQs
I hope you enjoyed the discussion so far because I think you have all the basics in place to send sensor data from Arduino to Raspberry Pi.
When we discuss Arduino and raspberry Pi in tandem, I know for a fact that the questions to which we have received answers to till now won’t cut it.
Because this is how it is with practical electronics, one question getting answered paves way for more questions, and those questions in turn produce more questions.
It’s like a pandora box really.
Keeping that understanding in mind let me answer a few more queries that I think you must be having as far as these two electronic subsystems are concerned.
Can Arduino IDE Run On Raspberry Pi?
Yes, Arduino IDE can run on Raspberry Pi. In order to run Arduino IDE on Raspberry Pi, you need to install it first. After successful installation all you need to do is go to Menu > Programming and you will find Arduino IDE. Open it up and start coding.
Here are the steps that you need to follow in order to install and run Arduino IDE on Raspberry Pi.
- Install Arduino IDE on Raspberry Pi
You can install the latest version of Arduino on your Raspberry Pi by using the following apt commands.
sudo apt-get update && sudo apt-get upgrade
sudo apt-get install Arduino
Or you can also go to the official download link and out of the many versions available click on the “Linux ARM 32 Bits“.
- Extract and Install Arduino IDE
If you have chosen to download the package directly from the website, you need to extract it into the /opt directory.
Once you have done that open your terminal and run the following script to install Arduino IDE on your Raspberry Pi.
cd Downloads/
tar -xf arduino-1.8.3-linuxarm.tar.xz
sudo mv arduino-1.8.3 /opt
sudo /opt/arduino-1.8.3/install.sh
- Run Arduino IDE
That’s pretty much it.
In order to open and start coding on the Arduino IDE, go to Menu > Programming, find Arduino IDE click on it and start your Mojo.
Can Raspberry Pi Run Arduino Code?
Yes, you can run codes or sketches written for Arduino on Raspberry Pi by using what is known as the RasPiArduino framework. What this framework does is it breaks down Arduino code into binaries which can then be simplified to run on a Linux system like the Raspberry Pi.
I think this article relays down the entire process pretty neatly. Check it out to understand the entire process.
Can You Program An Arduino With A Raspberry Pi?
Yes, you can most definitely program your Arduino using Raspberry Pi. All you need to do is install Arduino IDE into your Raspberry Pi system, connect your Arduino board to Raspberry Pi, select the right port and run a test program to check if everything is working.
Linux has always attracted coders and hobbyists into its domain for the freedom and a space for innovation that it offers them. There is just something raw about it working in a Linux environment and to top it off if you are also capable of interfacing devices like Arduino into its environment, the possibilities just open up for so many kinds of innovative developments.
Alright guys with that we are at the end of this elaborate article on how to send sensor data from Arduino to Raspberry Pi.
To really tell you the truth, my mind is teeming with potential projects that can be done when you combine these two potent forces together.
And very soon I will pick up a project to deploy everything that we have learnt here on it.
The thing is I will have to choose one that feels relatable to experts and beginners alike, so I may take some time on this one.
But hey, if you want to know when I have launched that project just subscribe to the email list so that I can keep you updated.
And while you are doing that also subscribe to my YouTube channel where I create awesome content like this.
Comment below if you have any queries associated with the article or otherwise.
Take great care of yourselves and I will see you in the next one!
Tada!
Hey ,Thanks for the info., but could u pls tell me whether images also shared from Arduino Uno to raspberry pi over serial communication? If any other components needed pls let me know ASAP !!
Thank you:)