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

电子说

1.2w人已加入

描述

步骤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

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分