Serial communication between Raspberry pi --Arduino Uno (Python)

Raspberry pi --An object-oriented version of what I was able to find when I wanted to exchange data between Arduino and so on.

Source on the Arduino side

Source

Serial.ino


int n = 12;

void setup(){
  Serial.begin(9600);
}

void loop(){
  Serial.println(n);
  Serial.write("Hello World!\n");
  delay(1000);
}

--For the time being, check if communication is possible with the serial monitor. --Close the serial monitor when you can confirm the communication on the serial monitor. (If you don't close it, you will get a SerialException error.) --Communication speed is set to 9600 --Use the Serial.print function to send numbers and the Serial.write function to send strings. (Can't we make one?)

Raspberry pi side (Python) source

pip installation of pyserial

Install the library required for serial communication with Python.

pip install pyserial

Source

Arduino_USB.py


import serial
import time

class Arduino_USB:
    data = ""

    #constructor
    def __init__(self, dev, bps):
        #Communication settings Device name Communication speed
        self.ser = serial.Serial(dev, bps)
        time.sleep(2)
    
    #Start serial communication
    def startUSB(self):
        #Start command. Byte character"a"Send
        self.ser.write(b"a")

    #Serial communication disconnection
    def closeUSB(self):
        self.ser.close()

    #Read data from Arduino
    def getUSB(self):
        self.data = self.ser.readline()
        return str(self.data, encoding = "utf-8")

a = Arduino_USB("/dev/ttyACM0", 9600)
a.startUSB()
print(a.getUSB())
print(a.getUSB())
a.closeUSB()

--Connect Arduino to Raspberry pi via USB. --The device name is basically recognized by / dev / ttyACM0. --Set the same communication speed as the Arduino side. --Wait for lag on the Arduino side with time.sleep. -Maybe you are reading up to the line feed code?

Execution result

12

Hello

Summary

――I'm not sure about the synchronization of data reading timing and the reading range. (It was written as 1Byte, but why can I read Japanese?) ――Isn't it possible to use the Serial.print function to send numbers and the Serial.write function to send strings? ――It's a mystery, but it's a personal play, so if you move, Yoshi!

Recommended Posts

Serial communication between Raspberry pi --Arduino Uno (Python)
Serial communication with Raspberry Pi + PySerial
Serial communication with python
Introduction to serial communication [Python]
Raspberry Pi + Python + OpenGL memo
raspberry pi 1 model b, python
2020 Arduino / Raspberry Pi / Python / Microcomputer C language learning ~ Recommended book ~
Translate I2C device driver for Arduino to Python for Raspberry pi
Testing uart communication on Raspberry Pi
Use vl53l0x with Raspberry Pi (python)
Power on / off Raspberry pi on Arduino
[Raspberry Pi] Changed Python default to Python3
Detect "brightness" using python on Raspberry Pi 3!
Raspberry Pi Security Infrared Camera (Python Edition)
Adafruit Python BluefruitLE works on Raspberry Pi.
[Python] [Windows] Serial communication in Python using DLL
Run servomotor on Raspberry Pi 3 using python
Detect temperature using python on Raspberry Pi 3!
Working with GPS on Raspberry Pi 3 Python
Detect slide switches using python on Raspberry Pi 3!
[Amateur remarks] Raspberry Pi 3, Wordpress vs Raspberry Pi 3, python, Django
I tried L-Chika with Raspberry Pi 4 (Python edition)
Detect magnet switches using python on Raspberry Pi 3!
Get CPU information of Raspberry Pi with Python
Easy connection between Raspberry Pi and AWS IoT
Make DHT11 available on Raspberry Pi + python (memo)
Connect to MySQL with Python on Raspberry Pi
Build a Python development environment on Raspberry Pi
GPS tracking with Raspberry Pi 4B + BU-353S4 (Python)
Measure CPU temperature of Raspberry Pi with Python
Raspberry Pi backup
From setting up Raspberry Pi to installing Python environment
How to use the Raspberry Pi relay module Python
Control other programs from Python (communication between Python and exe)
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Interprocess communication between Ruby and Python (POSIX message queue)
Install pyenv on Raspberry Pi and version control Python
Output to "7-segment LED" using python on Raspberry Pi 3!
Display USB camera video with Python OpenCV with Raspberry Pi
Let's operate GPIO of Raspberry Pi with Python CGI
Raspberry Pi with Elixir, which is cooler than Python