嵌入式技术
下面介绍一下我的Seeeduino V3/Arduino UNO玩板经验,供大家学习参考,通过下面四个小项目从浅入深介绍Seeeduino V3在生活中的应用
使用LCD1602液晶屏显示文字
使用DHT11温湿度传感器测量温湿度并在LCD1602液晶屏显示环境温湿度
使用HC-SR04超声波模块测距并在LCD1602液晶屏显示物体间距
使用RTC模块DS1307和DHT11温湿度传感器在LCD1602液晶屏显示时间日期工作日和环境温湿度数据
开发环境搭建:
Seeeduino V3/Arduino UNO需要下载Arduino IDE来编程和烧写到开发板,到官网根据操作系统下载安装Arduino IDE即可,
安装好Arduino IDE后打开程序按下图选择开发板类型
然后将Seeeduino V4.2用手机USB线连接到电脑的USB口,安装下面驱动
然后会在设备管理器里面看出USB串行设备记下串口号
在Arduino IDE里面修改串口号为上面串口号
这样就完成了开发环境的配置
一、使用LCD1602液晶屏显示文字
下面从最简单的使用LCD1602液晶屏显示文字开始学习,为了减少连接线,使用I2C方式连接LCD1602
硬件连接方法:
LCD1602的VCC GND SDA SCL分别连接到seeed红版(Arduino uno r3)增强板的5V GND A4 A5,然后使用数据线连接seeed红版(Arduino uno r3)增强板和电脑
查看LCD1602的I2C地址:
首先打开Arduino IDE,输入下面sketch扫描i2c地址
#include void setup(){ Wire.begin(); Serial.begin(115200); Serial.println(" I2C Scanner");}void loop(){ byte error, address; int nDevices; Serial.println("Scanning..."); nDevices = 0; for(address = 1; address < 127; address++ ) { Wire.beginTransmission(address); error = Wire.endTransmission(); if (error == 0) { Serial.print("I2C device found at address 0x"); if (address<16) Serial.print("0"); Serial.print(address,HEX); Serial.println(" !"); nDevices++; } else if (error==4) { Serial.print("Unknow error at address 0x"); if (address<16) Serial.print("0"); Serial.println(address,HEX); } } if (nDevices == 0) Serial.println("No I2C devices found "); else Serial.println("done "); delay(5000);}
然后上传开发板运行
然后打开串口检测
查看串口信息如下
可知液晶屏i2c地址3f,然后打开库管理,搜索Liquid Crystal I2C库并安装
然后创建一个新sketch,输入下面
#include #include LiquidCrystal_I2C lcd(0x3f,16,2);void setup(){lcd.init();// Print a message to the LCD.lcd.backlight();lcd.print("www.ickey.cc");}void loop(){}上传后效果如下
二、使用DHT11温湿度传感器测量温湿度并在LCD1602液晶屏显示环境温湿度
硬件连接方法:
LCD1602连接方法和之前一样,使用I2C连接方法,将VCC GND SDA SCL分别连接开发板的5V GND A4 A5
DHT11的VCC GND DATA连接开发板的3V3 GND D7
软件配置方法:
需要安装的库LiquidCrystal_I2C、DHT11,dht11库可以按下图安装
然后加入选择下面文件即可安装DHT11库
然后创建一个sketch
然后上传到开发板运行效果如下
三、使用HC-SR04超声波模块测距并在LCD1602液晶屏显示物体间距
硬件连接方法:
将LCD1602使用I2C连接到开发板,SDA SCL分别连接开发板上SDA SCL,LCD1602的VCC GND连接到开发板JP3的VCC GND
将HC-SR04超声波传感器的VCC GND TRIG ECHO分别连接到开发板的5V GND D0 D1
软件配置方法:
打开Arduino IDE,确认安装LiquidCrystal_I2C库后新建sketch,输入下面代码
#include #include #define Trig 0#define Echo 1 float cm;float range; LiquidCrystal_I2C lcd(0x3F,16,2); void setup() { pinMode(Trig, OUTPUT); pinMode(Echo, INPUT); lcd.init(); lcd.backlight();} void loop() { digitalWrite(Trig, LOW); delayMicroseconds(2); digitalWrite(Trig,HIGH); delayMicroseconds(10); digitalWrite(Trig, LOW); range = float(pulseIn(Echo, HIGH)); cm = range*0.034/2; lcd.setCursor(0,0); lcd.print("D = "); lcd.print(cm); lcd.setCursor(0,1); lcd.print("Distance in CM"); delay(500); }
上传后运行如下图
四、使用RTC模块DS1307和DHT11温湿度传感器在LCD1602液晶屏显示时间日期工作日和环境温湿度数据
硬件连接方法:
LCD1602的SDA SCL分别连接到开发板A4 A5,LCD1602的VCC GND连接开发板JP3的VCC GND
DS1307模块的VCC GND SDA SCL连接开发板的5V GND SDA SCL
DHT11温湿度传感器的VCC GND DATA连接开发板的3V3 GND D7
软件配置方法:
打开Arduino IDE安装RTCLIB库、LiquidCrystal_I2C库、DHT Sensor Library by Adafruit库、Adafruit Unified Sensor库
新建sketch输入下面代码
#include #include #include "DHT.h"#include #define DHTPIN 7#define DHTTYPE DHT11DHT dht(DHTPIN, DHTTYPE);float hum;float temp;LiquidCrystal_I2C lcd(0x3F,16,2);RTC_DS1307 RTC;char daysOfTheWeek[7][12] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};void setup () { Serial.begin(9600); lcd.init(); lcd.backlight(); dht.begin(); Wire.begin(); RTC.begin(); RTC.adjust(DateTime(__DATE__, __TIME__)); }void loop () { int h = dht.readHumidity(); int t = dht.readTemperature(); DateTime now = RTC.now(); Serial.print(now.year(), DEC); Serial.print('-'); Serial.print(now.month(), DEC); Serial.print('-'); Serial.print(now.day(), DEC); Serial.print(' '); Serial.print(now.hour(), DEC); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.print(now.second(), DEC); Serial.print(' '); Serial.print(daysOfTheWeek[now.dayOfTheWeek()]); Serial.println(); lcd.setCursor(0,0); lcd.print(now.year(), DEC); lcd.print('-'); lcd.print(now.month(), DEC); lcd.print('-'); lcd.print(now.day(), DEC); lcd.print(' '); lcd.print(daysOfTheWeek[now.dayOfTheWeek()]); delay(1000); lcd.setCursor(0,1); lcd.print(now.hour(), DEC); lcd.print(':'); lcd.print(now.minute(), DEC); lcd.print(':'); lcd.print(now.second(), DEC); lcd.print(' '); lcd.setCursor(9,1); lcd.print(t); lcd.print("C"); lcd.print(" "); lcd.print(h); lcd.print("%");}效果如下
全部0条评论
快来发表一下你的评论吧 !