步骤1:在计算机上安装Python IDLE
如果您要已经在计算机中安装了Python IDLE。如果是,请转到步骤2,否则请按照以下说明进行操作:
1。转到python网站并下载(此处)。
2。完成后,继续进行安装,方法是保留默认情况下安装python的目录。
注意:即使您的计算机运行在64位系统上,由于与Arduino库的兼容性不足,您也可以使用32位Python本身。
步骤2:观看视频以了解更多详细信息
步骤3:安装PySerial
PySerial是一个Python API模块,用于向Arduino或任何其他微控制器读取和写入串行数据。要在Windows上安装,只需访问PySerial的下载页面,然后按照以下步骤操作:
1。从上面的链接下载PySerial。
2。通过将设置保留为默认值来安装它。您应该确保Pyserial可以正常运行;为此,请输入:
import serial
(如果没有)没有遇到任何错误,所以您的状态很好,否则我建议您检查安装和Python IDLE扩展。
步骤4:Python代码
首先,我们需要一个简单的程序来使Python通过串行端口发送数据。
import serial #Serial imported for Serial communication
import time #Required to use delay functions
ArduinoUnoSerial = serial.Serial(‘com15’,9600) #Create Serial port object called ArduinoUnoSerialData time.sleep(2) #wait for 2 secounds for the communication to get established
print ArduinoUnoSerial.readline() #read the serial data and print it as line
print (“You have new message from Arduino”)
while 1: #Do this forever
var = raw_input() #get input from user
if (var == ‘1’): #if the value is 1
ArduinoUnoSerial.write(‘1’) #send 1 to the arduino‘s Data code
print (“LED turned ON”)
time.sleep(1)
if (var == ’0‘): #if the value is 0
ArduinoUnoSerial.write(’0‘) #send 0 to the arduino’s Data code
print (“LED turned OFF”)
time.sleep(1)
if (var == ‘fine and you’): #if the answer is (fine and you)
ArduinoUnoSerial.write(‘0’) #send 0 to the arduino‘s Data code
print (“I’m fine too,Are you Ready to !!!”)
print (“Type 1 to turn ON LED and 0 to turn OFF LED”)
time.sleep(1)
步骤5:Arduino代码
要从Python启动与Arduino的连接,我们首先必须确定Arduino处于哪个COM端口。正如我在上图中通知的那样,此任务是由Ardunio编程环境简单完成的。
int data;
int LED=13;
void setup() {
Serial.begin(9600); //initialize serial COM at 9600 baudrate
pinMode(LED, OUTPUT); //declare the LED pin (13) as output
digitalWrite (LED, LOW); //Turn OFF the Led in the beginning
Serial.println(“Hello!,How are you Python ?”);
}
void loop() {
while (Serial.available()) //whatever the data that is coming in serially and assigning the value to the variable “data”
{
data = Serial.read();
}
if (data == ‘1’)
digitalWrite (LED, HIGH); //Turn On the Led
else if (data == ‘0’)
digitalWrite (LED, LOW); //Turn OFF the Led
}
步骤6:获得支持
责任编辑:wv
-
python
+关注
关注
57文章
4858浏览量
89600 -
Arduino
+关注
关注
190文章
6515浏览量
195994
发布评论请先 登录
贸泽电子开售全新Arduino UNO Q单板计算机
termux如何搭建python游戏
【VisionFive 2单板计算机试用体验】环境搭建与点灯实验
【VisionFive 2单板计算机试用体验】安装openplc
如何在树莓派上安装并运行 Arduino 集成开发环境!
如何在Android设备上安装Cyusb3014芯片驱动?
如何在计算机上安装麒麟操作系统Kylin V10 SP3
如何在Ubuntu 24.04上运行5.4.47版本?
告别复杂的终极指南:如何在树莓派上安装 Manjaro:2024
如何在虚拟环境中使用 Python,提升你的开发体验~
是否可以使用OpenVINO™部署管理器在部署机器上运行Python应用程序?
使用Python实现xgboost教程
Flexus 云服务器 X:Python 安装的极致便捷之旅

如何在计算机上安装python以及如何在Arduino上使用python
评论