PySerial This module encapsulates the access for the serial port.
Development Software
- Install Arduino Software (IDE) on Windows
- Install Arduino Software (IDE) on Linux
- Install Arduino Software (IDE) on OS X
Python 2.7 or Python 3.4 and newer If running on Windows: Windows 7 or newer If running on Jython: “Java Communications” (JavaComm) or compatible extension for Java
From PyPI
- Consider upgrading pip to pip version 18.1 is available:
python -m pip install --upgrade pip
- Install PySerial to use Python with Arduino
python -m pip install PySerial
From Conda
- pySerial can be installed from Conda:
conda install pyserial
or
conda install -c conda-forge pyserial
Route in Windows:
c:\users\name-user\appdata\local\programas\python\python36-32\lib\site-packages
-Import serial
import serial
-Establish the port in which the arduino is connected
PuertoSerie =serial.Serial('COM4', 9600)
-In a bucle wait for the response (data) sent from the arduino
while True:
sArduino = PuertoSerie.readline()
print (sArduino)
-Script .py
:
import serial
PuertoSerie =serial.Serial('COM4', 9600)
while True:
sArduino = PuertoSerie.readline()
print (sArduino)
-Establish a variable that is sending the data:
int sensorValue = 0;
-In the setup () the serial port is initialized in this case in 9600 (same as in the Python script)
void setup(){
Serial.begin(9600);
}
-In loop () the analog input is received (in this example) by port A0.
-The value of the sensor that will be sent to Python is written to the serial port
void loop(){
sensorValue = analogRead(A0);
Serial.println(sensorValue);
}
- Script
.ino
:
int sensorValue = 0;
void setup(){
Serial.begin(9600);
}
void loop(){
sensorValue = analogRead(A0);
Serial.println(sensorValue);
}
- Make the connection of the sensors on the arduino board
- Compile and upload arduino script to the Arduino card
- Running the Python script in console
rute\python name-project.py
In the next video show the implementig the example: https://youtu.be/0e9yuELxgFU.
Thank you