Python serial arduino raspberry

Содержание

Управление Arduino с помощью Raspberry Pi и pyFirmata

Хотя Raspberry Pi и Arduino являются, по своей сути, разными аппаратными платформами по своей структуре, тем не менее, в современном мире они являются конкурирующими платформами с открытым исходным кодом. Обе эти платформы имеют достаточно большое сообщество во всем мире и хорошую поддержку. В этой статье мы постараемся показать как можно совместно использовать преимущества обеих этих платформ – мы будем с помощью платы Raspberry Pi и языка программирования Python управлять платой Arduino.

Мы будем использовать программное обеспечение PyFirmata чтобы передавать команды плате Arduino с помощью платы Raspberry Pi и скрипта на python. PyFirmata представляет собой пакет библиотек на языке python, который можно установить в Arduino чтобы осуществлять последовательную связь между скриптом на python на любом компьютере и платой Arduino. С помощью этого пакета библиотек можно считывать и записывать информацию на любые контакты платы Arduino. В данном случае мы будем запускать программу на python в Arduino используя Raspberry Pi.

Необходимые компоненты

  1. Плата Arduino Uno (или любая другая) (купить на AliExpress).
  2. Плата Raspberry Pi с установленной операционной системой Raspbian OS.
  3. USB кабель для Arduino.
  4. Светодиод.

В этом проекте мы использовали внешний монитор с HDMI кабелем чтобы соединиться с Raspberry Pi. Если у вас нет лишнего монитора для этих целей вы можете использовать SSH client (Putty) или VNC server чтобы соединиться с Raspberry Pi используя компьютер.

Установка PyFirmata в Arduino используя Raspberry Pi

Чтобы загрузить программное обеспечение PyFirmata в Arduino мы сначала должны установить Arduino IDE в Raspberry Pi. Необходимо выполнить следующую последовательность шагов:

Шаг 1. Соедините Raspberry Pi с сетью интернет. Откройте терминал команд, напечатайте в нем следующую команду и нажмите ввод (enter):

Источник

araffin/python-arduino-serial

Use Git or checkout with SVN using the web URL.

Work fast with our official CLI. Learn more.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Robust Arduino Serial Protocol in Python

Robust Arduino Serial is a simple and robust serial communication protocol. It was designed to make two arduinos communicate, but can also be useful when you want a computer (e.g. a Raspberry Pi) to communicate with an Arduino.

It supports both Python 2 and 3.

This repository is part of the Robust Arduino Serial project, main repository: https://github.com/araffin/arduino-robust-serial

Please read the Medium Article to have an overview of this protocol.

Implementations are available in various programming languages:

Run the tests (require pytest):

Read write in a file (WARNING: the file will be deleted when the script exits)

Serial communication with an Arduino: Arduino Source Code

Example: Communication with Sockets

It can be useful when you want two programs to communicate over a network (e.g. using wifi) or even locally on the same computer (e.g. when you want a python2 script that communicates with a python3 script).

Bluetooth Example with Two Computers

You need to change the server mac address SERVER_ADDR , you can use hciconfig to know the mac address of your computer.

Источник

Raspberry Pi — Arduino Serial Communication

Introduction: Raspberry Pi — Arduino Serial Communication

In this project, we will be making a communication between Arduino and Raspberry Pi which transfer vital information by sending data one bit at a time.

Serial communications are essential for every Micro-controllers to communicate between Micro-controllers and another device. The Micro-controller sends these 1 and 0 (bits) that contain necessary information one by one, or Serially. These bits form together and turn into bytes (composed of 8 bits). For further information, check it out sparkfun binary tutorials.

It’s a fairly easy project and can be used either on its own or part of something bigger (Check it Out >> Integrated Weather Station).

Step 1: List of Material

For this project, we will be using:

— Arduino boards (mine Arduino Uno Rev3)

Step 2: Hardware Connection

In common, the connections are fairly easy. Just connect Arduino USB Plug to Raspberry PI with USB cable and check the connection between Arduino and Raspberry pi by type «ls /dev/tty*» in Raspberry Pi terminal, the result should be content «/dev/ttyACM0» and you are good to go.

Step 3: Raspberry Pi Programming

Below is the Raspberry Pi Serial Communication code. Upload serial_test.ino code to your Arduino (Mine Arduino Uno Rev3) , Run serial_test.py Python code in Raspberry PI, Connect Arduino to Raspberry Pi through USB cable, and you should have no problems.

Attachments

Step 4: Enjoy!

After making sure that everything works smoothly, you can take this project into bigger project (Check it Out >> Integrated Weather Station)!

Power through batteries or a plug and you’re good to go!

8 People Made This Project!

Did you make this project? Share it with us!

Источник

Подключение Raspberry Pi к Arduino через UART интерфейс

В этой статье мы покажем вам, как обмениваться данными между Raspberry Pi и платой Arduino через последовательный интерфейс.

Для начала, кратко объясним, что такое последовательный интерфейс. Затем посмотрим, как настроить оборудование и программное обеспечение, а затем углубимся в код Python (Cpp для Arduino). Разберем простой пример, ведущий к более совершенному использованию Raspberry Pi, Arduino и других электронных устройств.

Это руководство подходит для плат Raspberry Pi 4 (и ранних: 3B, 3B +) и любой платы Arduino. Мы будем использовать в основном Arduino Uno, но также более подробно расскажем о различиях между платами Arduino, когда дело касается последовательной передачи данных.

При работе с электронными устройствами передача данных является ключевым моментом. Каждое устройство – помимо своей основной функции – должно четко обмениваться данными с другими устройствами. Это одна из самых важных вещей, с которыми нужно уметь работать, чтобы перейти от очень простых приложений к более сложным.

Что такое последовательный интерфейс (UART)?

Последовательная передача данных – это просто способ их передачи. Данные отправляются последовательно, по одному биту (1 байт = 8 бит), в отличие от параллельной связи, когда одновременно отправляется много битов.

Что такое протокол UART?

Говоря более конкретно, когда вы используете последовательный порт с Arduino и Raspberry Pi, вы используете протокол UART. UART означает «Универсальный асинхронный приёмопередатчик».

По сути, это асинхронный multi-master протокол, основанный на последовательном интерфейсе, который позволяет обмениваться данными между двумя платами. Будьте уверены, есть библиотеки, которые справятся со всеми низкими уровнями за вас.

Multi-master означает, что все подключенные устройства смогут отправлять данные, когда требуется. Это одно из основных отличий от протоколов «master-slaves», где только главное устройство может инициировать обмен данными.

Обычно используются другие протоколы, такие как I2C и SPI, когда вам нужны конфигурации ведущий-ведомый: например, когда у вас есть одна плата Arduino и несколько датчиков или исполнительных механизмов.

На плате Arduino Uno есть один UART, который можно использовать либо с USB-кабелем, либо с контактами RX / TX (не используйте его с обоими одновременно). Некоторые платы имеют больше доступных UART.

Например, Arduino Mega имеет разные последовательные интерфейсы (Serial, Serial1, Serial2, Serial3), а Arduino Zero имеет только порт USB (используйте SerialUSB вместо Serial).

На Raspberry Pi возможно подключить множество последовательных устройств к портам USB. У каждого устройства будет свое имя (посмотрим как их найти позже в этой статье). Вы также можете использовать GPIO (RX0 / TX0) для дополнительного UART.

Вы, наверное, уже знакомы с библиотекой Serial для Arduino

Когда вы используете монитор последовательного порта, в общем случае ваша среда разработки Arduino запускает последовательный интерфейс с вашим Arduino. Вы можете получать и отправлять данные прямо с монитора последовательного порта.

То, что мы сделаем здесь, почти то же самое, за исключением того, что вместо вашей среды разработки Arduino другой стороной последовательного интерфейса будет плата Raspberry Pi. Нам нужно будет произвести некоторые настройки и написать код, чтобы он заработал. Тогда и Raspberry Pi, и Arduino смогут отправлять сообщения друг другу.

Давайте теперь посмотрим, как физически соединить две платы вместе.

Настройка оборудования для последовательного интерфейса

Есть 2 способа подключить Raspberry Pi и Arduino для последовательного интерфейса.

1. С помощью USB

Самый простой способ – соединить обе платы кабелем USB.

Со стороны Raspberry Pi USB-разъем – это все, что вам нужно. Вы можете выбрать любой из 4 портов USB, имеющихся на плате.

Для Arduino вы будете использовать USB-порт, который вы используете для загрузки кода с вашего компьютера (со среды разработки Arduino) на вашу плату. Выбор USB-разъема зависит от того, какая у вас версия. Для таких плат, как Arduino Uno и Mega, разъем будет отличаться от Arduino Nano и от Arduino Zero.

В этом примере мы использовали плату Arduino Uno.

Как видите, это простейшее аппаратное соединение, которое вы можете установить между Raspberry Pi и Arduino.

Примечание: сначала вам нужно подключить Arduino к компьютеру, чтобы вы могли загрузить код в плату. После этого подключите USB-кабель к Raspberry Pi. Raspberry Pi будет питать Arduino через этот кабель

2. С помощью интерфейса ввода/вывода общего назначения (GPIO)

Чтобы установить последовательное соединение, вы также можете использовать простые провода между интерфейсом ввода/вывода Raspberry Pi и контактами Arduino

В зависимости от типа вашей платы Arduino вам может потребоваться переключатель уровня напряжения. Raspberry Pi работает при напряжении 3,3 В. Для плат Arduino, таких как Due, 101, это тоже подойдет, так как они также используют 3,3 В.

Но для многих Arduino, таких как Uno, Mega, Leonardo, Nano и многих других, плата работает при напряжении 5 В. Таким образом, вам понадобится переключатель напряжения 3,3 В / 5 В для защиты Raspberry Pi при подключении RX и TX.

В оставшейся части этого руководства мы будем использовать настройку с помощью кабеля USB. С ним будет проще работать, поэтому советуем использовать USB-кабель вместо GPIO для последовательного интерфейса.

Как настроить программное обеспечение Raspberry Pi?

Для Arduino не требуется никаких специальных настроек. Просто убедитесь, что вы скачали и установили среду разработки Arduino.

Однако, на вашем Raspberry Pi требуется несколько вещей, чтобы интерфейс заработал.

Подключитесь к Raspberry Pi через протокол SSH или с помощью экрана, мыши и клавиатуры, и откройте консоль. Если вы еще не установили ОС на плату, вы можете очень легко установить Raspbian или Ubuntu на SD-карту.

1. Найдите плату Arduino

При подключении к Arduino через USB, вы должны обнаружить ее по адресу /dev/ttyACM0 или /dev/ttyUSB0 (иногда цифра в адресе может отличаться, к примеру /dev/ttyACM1).

Источник

Raspberry Pi Arduino Serial Communication – Everything You Need To Know

In this tutorial I’ll show you how to communicate between a Raspberry Pi and an Arduino board via Serial communication.

First, I’ll quickly explain what is Serial communication. Then you’ll see how to setup your hardware and software, and we’ll dive into the Python code (Cpp for Arduino). A basic example at first, leading to a more complete application using a Raspberry Pi, an Arduino, and other electronic devices.

This tutorial will work with a Raspberry Pi 4 (and earlier: 3B, 3B+) board, and any Arduino board. I’ll use mostly Arduino Uno but will give more details about the differences between Arduino boards when it comes to Serial communication.

When working with electronic devices, communication is key. Each device – on top of doing well what it’s supposed to do – must be able to clearly communicate with other devices. That’s one of the most important thing to work on in order to switch from a very basic application to more complex ones.

Table of Contents

What is Serial communication (with UART)

Serial communication is simply a way to transfer data. The data will be sent sequentially, one bit at a time (1 byte = 8 bits), contrary to parallel communication, where many bits are sent at the same time.

UART protocol

More specifically, when you use Serial with Arduino and Raspberry Pi, you’re using the UART protocol. UART means “Universal Asynchronous Reception and Transmission”.

You are learning how to use the combo Raspberry Pi + Arduino to build your own projects?

Check out Raspberry Pi and Arduino and learn step by step.

Basically it’s an asynchronous multi-master protocol based on the Serial communication, which will allow you to communicate between the 2 boards. Be reassured, there are libraries that will handle all the low layers for you.

Multi-master means that all connected devices will be free to send data when they want. This is one of the main difference with master-slaves protocols, where only the master device can initiate a communication. Usually you’ll use other protocols such as I2C and SPI when you need master-slaves configurations: for example when you have one Arduino board and multiple sensors or actuators.

The Arduino Uno board has one UART that you can use either with a USB cable or from the RX/TX pins (don’t use it with both at the same time). Some boards have more available UARTs. For example the Arduino Mega has different Serials (Serial, Serial1, Serial2, Serial3) and the Arduino Zero has a native USB port only (use SerialUSB instead of Serial).

On the Raspberry Pi, you can connect many Serial devices on the USB ports. Each will have a different device name (we’ll see how to find them later in this tutorial). You can also use the GPIOs (RX0/TX0) for an additional UART.

You probably already know Serial communication

You’ve certainly already used Serial communication many times. I’m sure you already know the Arduino Serial library, which allows you to log what’s happening in your code and get user input. When you use the Serial monitor, well, basically your Arduino IDE initiates a Serial communication with your Arduino. You can receive and send data directly from the Serial monitor.

What we’ll do here is almost the same, except that instead of your Arduino IDE, the other side of the Serial communication will be a Raspberry Pi board. We’ll have to do some setup and write some code to make it work. Then, both Raspberry Pi and Arduino will be able to send messages to each other.

Let’s now see how to physically connect the 2 boards together.

Hardware setup for Serial communication

There are 2 ways to connect your Raspberry Pi and Arduino for Serial communication.

Serial via USB

The easiest way is to use a USB cable between both board.

On the Raspberry Pi side, a simple USB connector is all you need. You can choose any of the 4 USB ports available on the board.

For Arduino, you will use the USB port that you use to upload code from your computer (with the Arduino IDE) to your board. Here the USB connector will depend on which version you have. For boards like Arduino Uno and Mega, the connector will be different from Arduino Nano, and from Arduino Zero.

For this example I’ve used an Arduino Uno board.

As you can see, it’s the simplest hardware connection you can make between Raspberry Pi and Arduino.

Note: you will first need to connect your Arduino to your computer, so you can upload the code into the board. After that, connect the USB cable to the Raspberry Pi. The Raspberry Pi will power the Arduino via this cable.

Serial via GPIOs

To make a Serial connection you can also use plain wires between the Raspberry Pi GPIOs and the Arduino pins.

Depending on your Arduino board you might need to use a voltage level-shifter. The Raspberry Pi is operating at 3.3V. For Arduino boards like Due, 101, it will be fine because they also use 3.3V.

But, for many Arduino, such as Uno, Mega, Leonardo, Nano, and many more, the board is operating at 5V. Thus, you need a 3.3V/5V level-shifter to protect the Raspberry Pi when connecting RX and TX (more info on Raspberry Pi pins and Arduino Uno pins).

For the rest of this tutorial we’ll use the setup with the USB cable. This will be easier to manage and unless you have a good reason not too, you should prefer using the USB cable instead of plain gpios for Serial communication.

Raspberry Pi Software setup

You don’t need any special setup for Arduino. Just make sure you have downloaded and installed the Arduino IDE.

Now, on your Raspberry Pi, a few things are required to make the communication work.

Connect to your Raspberry Pi, either via ssh, or by plugin a screen+mouse+keyboard, and open a terminal. If you haven’t installed an OS on your Pi yet, you can install Raspbian or Ubuntu very easily on a SD card.

Detect the Arduino board

When connecting the Arduino with a USB cable, you should see it appear as /dev/ttyACM0, or /dev/ttyUSB0 (sometimes the number can be different, for example /dev/ttyACM1).

Simply run ls /dev/tty* and you should see it. At this point if you’re not sure which device is the Arduino board, simply disconnect the board (remove the USB cable), and run ls /dev/tty* again. This way you will easily spot the serial device name of your Arduino.

Hardware permissions for Serial

Also, you may want to add your user to the dialout group, to avoid errors such as: serial.serialutil.SerialException: [Errno 13] could not open port /dev/ttyACM0: [Errno 13] Permission denied: ‘/dev/ttyACM0’.

This will make sure you have access to Serial devices (/dev/ttyACMx, /dev/ttyUSBx, …).

After you’ve added yourself to the dialout group, you need to reboot your Pi (or at least logout/login) to apply the change – learn more about Raspberry Pi hardware permissions.

Install Python Serial library on Raspberry Pi

You need to install a library to be able to use the Serial interface with Python.

For this tutorial we’ll use the pySerial library (documentation for Python 3). To install it:

This Python library is well-known and used in a lot of applications.

When installing, if you get an error such as “/usr/bin/python3: No module named pip”, then you need to install pip first with sudo apt install python3-pip .

Simple Serial communication from Arduino to Raspberry Pi

Let’s start with a very simple program.

Arduino code

Upload this code from your computer to your Arduino, using the Arduino IDE.

First, we initialize the Serial communication, and choose a baud rate, here 9600. 9600 is a commonly used baud rate, and also a pretty low one. In your future applications you can use a baud rate of 57600 or even 115200 without any problem. For this tutorial we’ll continue with 9600.

Every second we make the Arduino send a string with Serial.println(). the println() function will add a newline character ‘\n’ at the end of the string.

You can open the Serial monitor to see the string on your computer.

Raspberry Pi Python code

So, what does this code do?

First, we import the serial library which we previously installed.

The Serial communication is initialized by calling serial.Serial() along with a few parameters:

  • Serial device name for the Arduino: usually ‘/dev/ttyACM0’, ‘/dev/ttyUSB0’, or similar.
  • Baud rate: this parameter is very important. You need to use the same baud rate as the one you used on Arduino, or else everything you’ll read and write will be garbage. So, we choose 9600 as we did in the Arduino sketch.
  • timeout: this is a timeout for read operations. Here we set it to 1 second. It means that when we read from Serial, the program won’t be stuck forever if the data is not coming. After 1 second or reading, if not all bytes are received, the function will return the already received bytes.

So, serial.Serial() returns an object that you can use for all your Serial operations. We store it in the ‘ser’ variable.

Then we use the reset_input_buffer() function. This will flush any byte that could already be in the input buffer at that point, so it will avoid receiving weird/not useful/not complete data at the beginning of the communication.

In an infinite loop (think of the loop() function in the Arduino sketch), we check if some data is available with the in_waiting attribute (don’t put parenthesis, this is not a function). If yes, we can read the data.

The readline() function will read all bytes until a newline character is detected.

If we just printed what we received we would see b’Hello from Arduino!\r\n’. You receive bytes when you read from Serial, and you have to convert (decode) those bytes into the appropriate data type. So, we use decode(‘utf-8’) – you can also use decode(‘ascii’) – to decode the received data to a string.

Finally, the rstrip() function is specific to strings, it allows you to remove any trailing characters (newline, carriage return). This way we can remove the ‘\r’ and ‘\n’ and get a proper string.

Note: here we read a complete line every time. If you want to read a certain amount of bytes – one or more – use the read(size=1) function.

Testing Serial communication

Now, unplug your Arduino board from your computer and connect it to your Raspberry Pi board.

The Arduino code is already running, as soon as it’s powered up.

On the Raspberry Pi, make the Python file executable and launch it.

It works! The string sent by Arduino every second is displayed on the Raspberry Pi terminal.

Bidirectional Serial communication between Raspberry Pi and Arduino

Let’s make things a little bit more interesting. You’ve seen how to send data from Arduino to Raspberry Pi. In this part you’ll see how to talk from Raspberry Pi to Arduino. Thus you’ll have both sides of the communication and you’ll be able to create programs that wait for each other’s input.

Arduino code

Here we check if the Arduino has received data with Serial.available(). This will give you the number of bytes already arrived and stored in the receive buffer.

If some data has arrived, we use Serial.readStringUntil() with a newline character ‘\n’ to get the next line. This is similar to the readline() function. All the bytes received until ‘\n’ are automatically converted and added in an Arduino String object.

Then, we just print back the data we received, with an additional piece of text.

Note: if you want to read bytes one by one, you can do so with the Serial.read() function. Then you’ll have to convert this(those) byte(s) if needed: int, String, etc.

Raspberry Pi Python code

Use the pySerial function write() to send data to the Arduino. Here you can see that I’ve added a ‘b’ before the string to send. This will encode the string to bytes, because you can only send bytes through Serial. Any data which is not a byte or byte array must be converted before being sent. If you just try to send the string like that, you’ll get this error ” TypeError: unicode strings are not supported, please encode to bytes: ‘Hello from Raspberry Pi!’ ”

Note: instead of ser.write(b»Hello from Raspberry Pi!\n») you could’ve written ser.write(«Hello from Raspberry Pi!\n».encode(‘utf-8’)) or ser.write(«Hello from Raspberry Pi!\n».encode(‘ascii’)) . This will do the same thing. The encode() function will take the string and encode it for Serial.

Also, we add a newline character ‘\n’ because that’s what the Arduino is expected to end its reading with Serial.readStringUntil(‘\n’).

Then, we do the same thing as we did before: we read a line, decode it to string, and remove any trailing character. We’re not using the ser.in_waiting here, because for this specific application we know that the Arduino will send back some data just after it has received the string. Also, we have a 1 second timeout to avoid being stuck on this line.

Finally, we print the received string and wait for 1 second with time.sleep() before sending the next string over Serial.

Testing bidirectional Serial communication

On your Raspberry Pi, make your Python file executable and launch it.

Success! The Arduino received the string, and sent it back with a few more words. Then the Raspberry Pi received and printed the final string.

Raspberry Pi Arduino Serial communication: Application example

Now that you know how to setup your Arduino and Raspberry Pi for Serial communication, and write programs to talk between each board, let’s create a more complex application.

In this application, 4 LEDs and one push button will be connected to the Arduino board.

The Raspberry Pi and Arduino will be connected with a USB cable and use Serial communication to talk to each other.

Here’s the result we want to get:

  • When the push button is pressed, the Arduino will send a notification to the Raspberry Pi (a simple byte).
  • The Raspberry Pi will then compute a random integer number between 1 and 4, and send it to the Arduino.
  • The Arduino will power on the LED related to this number, and power off the other LEDs.

This will make you practice on the communication and synchronization of tasks between Arduino and Raspberry Pi.

If you arrived at this point of the tutorial (congrats!) I encourage you to try to write the code by yourself before you read the code I’ve provided below. Also, there is not only one possible code to meet those requirements, so your code might be different from mine, it’s totally OK. You can get help from the Arduino Serial reference and the pySerial API reference.

Schematics

To make this circuit:

  • Connect 4 LEDs to pins 9-12. Add a 220 Ohm resistor for each LED between the longer leg and the digital pin. The shorter leg is connected to the ground.
  • Add a push button with a 10kOhm resistor connected to the ground (pull-down resistor). One side of the button is connected to 5V, the other to digital pin 5 for reading.
  • Don’t forget to make a common ground between all components: The Arduino board, the 4 LEDs and the push button. Don’t add any power supply if you haven’t at least correctly connected the grounds.
  • Connect the Arduino and Raspberry Pi via a USB cable, for the Serial communication (the USB cable already manages the ground connection between your 2 boards).

Arduino code

In the setup() function we initialize the pin modes for all 4 LEDs and the push button. We also make sure all LEDs are powered off.

Then in the loop() we do 2 things: handling the push button and managing the 4 LEDs. Both actions are done one by one, but they are handled very fast so it’s just as if they were happening at the same time. That’s a case of multitasking an Arduino program.

First action: handling the push button.

We need to debounce the button to avoid unwanted values. When we find out that the button is pressed, we can send some data to the Raspberry Pi via Serial.

Here I’ve used the Serial.write() function: it sends a byte or series of bytes. This is different from Serial.print() which will make the output nice to see on the Serial monitor. As we’re talking to another machine and not a human, no problem with that.

The number we send here is 18. This is just a random number that we’ll associate with the state “button has been pushed”, so when the Raspberry Pi will receive a number, it will check if it’s equal to 18. You could imagine sending different actions or messages via Serial, using different numbers.

Note: usually you can also use Arduino interrupts to know when a button has been pushed. If you do so, don’t use the Serial library in the interrupt. If you absolutely have to use interrupts, then set a flag inside the interrupt (a simple boolean variable), and use Serial inside your loop() function.

And the second action: managing the 4 LEDs.

First we check if the Arduino has received some data from the Raspberry Pi over the Serial communication.

If yes, we read the next byte with Serial.read(). Here you can notice a small trick, consisting of subtracting with ‘0’, which will convert the character to the number it represents – in this case an integer.

After that it’s quite simple. We power off all LEDs, and then power on only the LED associated with the number: 1, 2, 3 or 4. In case a wrong value is sent, no LED will be powered on.

Raspberry Pi Python code

First we add one import line: the Python random library.

In the infinite loop (“while True”), we first read one byte from Serial. The timeout is set at 1 second. If nothing is received after one second, the read() function will return b”.

So we check if what we received is simply empty (b”) or if there was really something. In this latter case, we convert the data we received to an integer so we can use in the code. For that we use the function int.from_bytes(), which will decode b’0x12′ (the hexadecimal representation of 18 is 0x12) to 18.

As you can see we check that the number is 18 to continue the action. If you have different commands to handle, using a different number for each command is a good practice.

We choose a random number between 1 and 4 to determine which LED to power on. Then we send it with the pySerial write() function. To send an int via Serial, we first convert it as a string with str(), and then encode it with encode(‘utf-8’).

Testing the application

Now you can connect the Arduino to the Raspberry Pi using the USB cable. The Raspberry Pi will then supply the power for both the Arduino and the 4 LEDs.

If you have many things connected to your Arduino, you might want to add an external power supply on the Arduino power supply connector. For example, if you want to use a stepper motor, the Raspberry Pi will never have enough current for itself + the Arduino + the motor. In this case, with 4 LEDs that’s OK.

Run your Python script and press the push button. You will see a log and one of the LED on the Arduino will be powered on.

Every time you press the button:

  • The Arduino will detect it and send 18 to the Raspberry Pi.
  • The Raspberry Pi will receive some data from Serial. It will check if the number equal to 18. If yes a random number (between 1-4) is chosen. The Raspberry Pi will send this number to the Arduino.
  • The Arduino will receive this number and power on the associated LED.

Note: to do this application faster and with less code, you can also choose to use the Firmata protocol, with for example the pyFirmata library on Raspberry and StandarfFirmata library on Arduino. Check out those tutorials to learn more.

Conclusion

In this tutorial you have learnt how to communicate between your Raspberry Pi and Arduino board via Serial.

Now, in real life, will this be useful to you? The answer is yes, and pretty often.

The Raspberry Pi is great for having a lot of computation power embedded in your application. You can run complete softwares in many languages, web servers, robotics applications, etc. The Raspberry Pi certainly has some GPIOs which you may use, but they are not as good as the Arduino ones. Also, you have more chances to burn your Pi if you do something wrong with hardware.

The Arduino is more appropriate to directly handle hardware sensors and actuators: IMU, servo motors, stepper motors, anything which requires more power, etc. – more info on When to use Arduino vs Raspberry Pi.

All in all, the 2 boards can work together perfectly: The Raspberry Pi as the “brain” of the application, and the Arduino as the “muscles”, with a Serial communication to make them talk to each other.

Did you find this tutorial useful?

Do you want to learn how to create projects with both Raspberry Pi and Arduino?

If yes, this course is for you:

Did you find this tutorial useful?

Do you want to become better at programming robots, with Arduino, Raspberry Pi, or ROS2?

If yes, subscribe to receive exclusive content and special offers!

Источник

Adblock
detector