python +arduino UNO+lcd 交互

硬件:

  1. Arduino UNO 板+USB数据线;

  2. Adafruit_RGBLCDShield;
    3.PC.
    软件:
    1.Arduino IDE+安装LCD第三方模块Adafruit_RGBLCDShield;

    Github RGB LCD library page (http://adafru.it/aMi).
    Download the Adafruit RGB LCD Shield Library from :

    https://github.com/adafruit/Adafruit-RGB-LCD-Shield- Library/archive/master.zip,

    To install it, download the compressed ZIP file then install it. This guide (http://adafru.it/aYM) will help you.

2.Python 2.7.12+安装pyserial模块;

操作:

  1. 将lcd显示屏与Arduino板接好线路。电源+GND+读数据。
  1. Arduino UNO 连接电脑。
  2. 将下列程序烧进板里,不要打开串口监视器。

---------------------------------------------------------------------------------------------------------

include

include

include

include

Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();

char line[50] = ""; // 传入的串行数据
void setup() {
Serial.begin(9600); // 打开串口,设置数据传输速率9600
lcd.begin(16, 2);
}

void loop() {

// 串口可用时操作
if (Serial.available() > 0) {
// 读取传入的数据: 读到 \n 为止,或者最多 500 个字符
Serial.readBytesUntil('\n', line, 50);
//打印你得到的:
Serial.println(line);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(line);
}
// 每1秒做一个输出
delay(1000);
Serial.println("Arduino: Hello, python!");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Hello, Python!");
}
-----------------------------------------------------------------------------------------------------------#

  1. 等烧完程序,运行下列python程序:

---------------------------------------------------------------------------------------------

!/usr/bin/env python

-- coding: UTF-8 --

import time
import serial

ser = serial.Serial('COM3',9600)
line = ser.readline()
while line:
print(line.strip())
line = ser.readline()
# 每 3 秒向窗口写当前计算机时间
sep = int(time.strftime("%S")) % 3
if sep == 0:
ser.write("Hi, Arduino")

ser.close()

---------------------------------------------------------------------------------------#

运行结果:

图片无法添加。damn。。

你可能感兴趣的:(python +arduino UNO+lcd 交互)