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. To see the effect on the output you indicate that you can use: "Green LED (or any other colour, even a small light bulb, motor, etc...)".
    Leaving LEDs aside, isn't a light bulb or an engine a very high load without the help of a driver?

    1. Hi. Well, for a motor it would be convenient to use a driver as you indicate. But when I say small bulb I mean small bulbs that usually come with some toys or kits... They are not 220v bulbs.
      Greetings and apologies if this is not clear.

  2. tonixiclanaTony

    Hello Isaac.

    Forgive my ignorance, it's not python, it's processing, isn't it? Based on C and C++. I have an arduino UNO and when I send the serial message from the Raspberry to the arduino, it restarts, from what I have read this happens so that when from the IDE we upload the sketch, we do not have to press the reset, I have also read that it is possible to disable it by making a bridge from the reset to the 5v pin, but it has not been clear, have you ever done any of this?

    Nothing more to say than thank you very much for the attention and the very elaborate answer you have given.


    1. Hi Toni, I don't have to forgive anything. We all have mistakes, I just pointed out that it's not Python to avoid confusion.

      As for what you say about the reset, I have never done anything like that. If you are so kind, tell me where you have seen the bridge to the 5v pin and I'll take a look so I can help you.

      Greetings and thanks to you.

    2. Sorry, I just saw what you said about the reset jumper and the 5v output to disable the autoreset. For Arduino UNO it is very simple, you just have to bridge with the help of a 10 μF capacitor between the RESET pin and the ground pin (GND).

      For other Arduino boards (Arduino UNO doesn't seem to work too well) you need a 120 ohm resistor between the RESET pin and the 5v pin. One pin of the resistor should be connected to the RESET pin of the Arduino and the other to 5V. As shown in this picture:

      One note, as 120 ohms are hard to find, you can use a similar one, but never one lower than 110 ohms or higher than 124 ohms because it can damage the board.

      You don't have to modify anything else, neither in the sketch nor in the circuit, just make that bridge. The rest is the same...

      Greetings and I hope I have helped you.

      1. tonixiclanaTony

        Thank you Isaac, you've been very helpful, I tried to do it with a 640 ohm resistor but it didn't work. I have asked for help in several forums including the official arduino forum and nobody has helped me, thank goodness that every now and then you find people who help you in those moments when nothing works! haha. In case you are interested, I'm doing a final year project in which I raise a wifi network with the raspberry, and through a web platform in apache to control the devices, simulate a home automation installation, in which lights are turned on, a fan and opening and closing of a garage door built in a model, programmable events through crontab, I also want to include a motion sensor that will play a sound through omxplayer ... What I have time xD . For the garage motor control I have used a L293D integrated chip. My higher education is in network administration, so I have little idea about electronics xD, but I'm still working on it! Thanks again, if I can help you in any way (which I doubt xD), you already have my email.

  3. tonixiclanaTony

    The python code for the arduino does not work :/.

    sketch_may20a.ino: In function 'void loop()':
    sketch_may20a:9: error: argument of type 'int (HardwareSerial::)()' does not match 'int'.
    sketch_may20a:9: error: expected `;' before ')' token
    sketch_may20a.ino: In function 'void luz(int)':
    sketch_may20a:15: error: 'i' was not declared in this scope

    1. Hello Toni.

      Let's take it one step at a time. First of all, the code you are referring to is not Python, it is the Arduino language that you have inserted in the Arduino IDE. Python is used in previous steps from the terminal...

      Secondly, the error you report is very common. It is telling you that Arduino IDE when trying to compile the source code of the sketch has detected syntax errors. Whenever this happens to you, check that you haven't forgotten any ; at the end of the instructions, that the variables are well declared and that there is nothing wrongly written...

      In this case it has been our problem, that when passing the code and inserting it with the plugin that we use to insert code in the articles, it has changed some things. But it is already solved. Copy and paste the code and you shouldn't have any more problems.

      If you look at this image, you can see that my Arduino IDE has compiled the code correctly:

      Greetings. If you have any further questions, please do not hesitate to contact us.

Leave a Comment

Your email address will not be published.

Scroll to Top