Arduino control via Raspberry Pi

Raspberry Pi USB Arduino

Arduino and Raspberry Pi are not two unrelated products, together they can make quite an interesting symbiosis and allow you to expand the range of possibilities of an Arduino board alone. You can also learn how the communication between software and hardware works to capture the essence of what is going on in your computer every time you perform an operation.

That's the great thing about Arduino and Raspberry Pi, you don't just create, you learn. And that's why they are two very powerful friends for your projects. In this article we show you how to make the links between Arduino and Raspberry Pi in order to be able to control your project Arduino using the software we created on the Raspi.

What is certain is that the communication between the two devices can be done in many ways. As for the physical ways of doing it, there are: using an Ethernet module, through the USB cable itself, via Bluetooth, etc. But the simplest and cheapest way is to do it via the serial port (the USB port it comes with). Once the physical method has been chosen, there are also various communication methods: apps for control, using the Arduino IDE console or creating our own program written in any programming language that supports serial communication (Python, C, Java, Processing,...).

Material required

  • A ready-made Raspberry Pi board.
  • An Arduino UNO board.
  • Green LED (or any other colour, even a small light bulb, motor, etc... could be used).


We are going to use a fairly simple method, via USB cable with which we connect the Arduino to the PC and using Python. For our example we will use a simple circuit consisting of only one LED, so you can see the results in a simple way (but you can create anything else). The steps to follow are:

  • From our Raspberry Pi already configured we access the terminal (it must be connected to the Internet, if you do not have connection to the Raspi, you can download the package from another PC and pass it to the Raspberry Pi) and type the following:

sudo apt-get install python-serial

  • We now have the package installed Python which is needed for serial communication with the Arduino. We are now going to assemble the "circuit" for which we simply have to insert the long terminal (anode) of the LED in the digital pin 13 of the Arduino board and the short (cathode) in GND or ground. By the way, placing the LED directly on the Arduino board is not advisable in case of leaving the LED on for a long time, in that case we can implement a resistor (about 220 ohms is enough) in series with the long terminal.

 Arduino LED circuit

  • Then we will write the source code or sketch to program Arduino from Arduino IDE (if you want you can have it installed on your Raspberry Pi or even on your Android mobile phone thanks to the ArduinoDroid app). For our sketch we use the following code:

/*Program to make the Arduino wait for commands from the Raspberry Pi*//

const int led=13; //Declare digital pin of the LED

void setup(){
pinMode(led, OUTPUT); //Configure pin as output
Serial.begin(9600); //Start the serial transmission at 9600 baud.
void loop(){ //This is our execution block
if (Serial.available()) {
light( - '0'); //This takes the initial value 0

void light(int n){
for(int i=0; i<n; i++){
digitalWrite(led, HIGH); //LED is lit for 130ms
digitalWrite(led, LOW); //LED is turned off for 130ms

  • Once we have checked for syntactical errors or any other errors, we load it onto our Arduino board using the USB cable and the quick access button Cargar.
  • There is only one part left to do, write the line in the terminal and Python will do the rest. Take a look at the following line. If you write it in the Raspi terminal respecting the dot and the single quotes and substituting n for any number, the Arduino will flash the LED as many times as you have specified:


I will end by explaining a little what happens. By writing the Arduino code or sketch, we have managed to make the board "listen" to any entire quantity that arrives on the serial port. Thanks to the for that we have created, the code it contains will be repeated as many times as indicated by the number n. If you look at the code for (int i = 0; i < n; i++) what it does is evaluate the number i which starts by taking the value 0 and is increasing (i++). The loop is broken when i outperforms n (i < n) Now it's your turn, I hope you liked it!

More information - How to set up and configure Raspberry Pi step by step

12 thoughts on “Control de Arduino mediante la Raspberry Pi”

  1. Hello this code does not work, or I am doing something wrong.
    I have a Raspberry Pi 3b+ and an arduino UNO. but I try to compile it in the IDE and I get an error, both in the raspberry and in windows.
    EN WIN does not like this sentence
    light( - '0');

    in raspi
    IN Function 'void loop()':
    invalid use of non-static member function

  2. Hello, in this case we have used the serial port, could it be done via Bluetooth or a wireless connection?

    Thanks in advance, I'm just starting in this world and I'm getting an idea of a project I have in mind.

Leave a Comment

Your email address will not be published.

Scroll to Top