Arduino 入门学习笔记7 I2C LCD1602液晶显示实验 及 超声波传感器距离检测
I2C 简介
I2C总线是由Philips公司开发的一种简单、双向二线制同步串行总线。它只需要两根线即可在连接于总线上的器件之间传送信息。
主器件用于启动总线传送数据,并产生时钟以开放传送的器件,此时任何被寻址的器件均被认为是从器件.在总线上主和从、发和收的关系不是恒定的,而取决于此时数据传送方向。如果主机要发送数据给从器件,则主机首先寻址从器件,然后主动发送数据至从器件,最后由主机终止数据传送;如果主机要接收从器件的数据,首先由主器件寻址从器件.然后主机接收从器件发送的数据,最后由主机终止接收过程。在这种情况下.主机负责产生定时时钟和终止数据传送。
——《百度百科》
I2C LCD1602的蓝色电位器用于调整背光以获得更好的显示效果。I2C仅使用两个双向漏极开路线,串行数据线(SDA)和串行时钟线(SCL),通过电阻上拉。使用的典型电压为+5V或3.3V,但允许使用其它电压的系统。
线路图
LiquidCrystal_I2C库
将 LiquidCrystal_I2C 库拷到arduino IDE 下的 libraries 目录下。
LCD显示程序demo
// 湖南创乐博智能科技有限公司
// include the library code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
/**********************************************************/
char array1[]=" Arduino "; //the string to print on the LCD
char array2[]="hello, world! "; //the string to print on the LCD
int tim = 500; //the value of delay time
// initialize the library with the numbers of the interface pins
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 0x3F for a 16 chars and 2 line display
/*********************************************************/
void setup()
{lcd.init(); //initialize the lcdlcd.backlight(); //open the backlight
}
/*********************************************************/
void loop()
{lcd.setCursor(15,0); // set the cursor to column 15, line 0for (int positionCounter1 = 0; positionCounter1 < 26; positionCounter1++){lcd.scrollDisplayLeft(); //Scrolls the contents of the display one space to the left.lcd.print(array1[positionCounter1]); // Print a message to the LCD.delay(tim); //wait for 250 microseconds}lcd.clear(); //Clears the LCD screen and positions the cursor in the upper-left corner.lcd.setCursor(15,1); // set the cursor to column 15, line 1for (int positionCounter = 0; positionCounter < 26; positionCounter++){lcd.scrollDisplayLeft(); //Scrolls the contents of the display one space to the left.lcd.print(array2[positionCounter]); // Print a message to the LCD.delay(tim); //wait for 250 microseconds}lcd.clear(); //Clears the LCD screen and positions the cursor in the upper-left corner.
}
/************************************************************/
运行:
超声波传感器距离检测demo
拷贝库 NewPing到libraries下
代码
// ---------------------------------------------------------------------------
// * 湖南创乐博智能科技有限公司
// Example NewPing library sketch that does a ping about 20 times per second.
// ---------------------------------------------------------------------------// include the library code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <NewPing.h>LiquidCrystal_I2C lcd(0x27,16,2);//0x27 0x3F#define TRIGGER_PIN 2 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 3 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.void setup() {Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.lcd.init(); lcd.backlight();
}void loop() {delay(100); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).Serial.print("Ping: ");Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)Serial.println("cm");lcd.setCursor(0, 0);lcd.print("Distance:");lcd.setCursor(0, 1);lcd.print(" ");lcd.setCursor(9, 1);lcd.print(uS / US_ROUNDTRIP_CM);lcd.setCursor(12, 1);lcd.print("cm");
}
接线图
运行效果
Arduino 入门学习笔记7 I2C LCD1602液晶显示实验 及 超声波传感器距离检测相关推荐
- Arduino 入门学习笔记2 三色LED实验
电路 程序: /*************************************************** name:RGB LED *************************** ...
- arduino第一次学习笔记
文章目录 简介 灯闪烁程序 一秒钟闪烁两次 闪烁两短一长 随机闪烁(亮度随机) 完整代码: 蓝牙通信/Debug应用 简介 arduino入门学习,第一次笔记.实现灯的三种闪烁模式和蓝牙交互通信(可以 ...
- dubbo入门学习笔记之入门demo(基于普通maven项目)
注:本笔记接dubbo入门学习笔记之环境准备继续记录; (四)开发服务提供者和消费者并让他们在启动时分别向注册中心注册和订阅服务 需求:订单服务中初始化订单功能需要调用用户服务的获取用户信息的接口(订 ...
- Crypto++入门学习笔记(DES、AES、RSA、SHA-256)
Crypto++入门学习笔记(DES.AES.RSA.SHA-256) 背景(只是个人感想,技术上不对后面的内容构成知识性障碍,可以skip): 最近,基于某些原因和需要,笔者需要去了解一下Crypt ...
- 机器学习入门学习笔记:(4.2)SVM的核函数和软间隔
前言 之前讲了有关基本的SVM的数学模型(机器学习入门学习笔记:(4.1)SVM算法).这次主要介绍介绍svm的核函数.软间隔等概念,并进行详细的数学推导.这里仅将自己的笔记记录下来,以便以后复习查看 ...
- 机器学习入门学习笔记:(3.2)ID3决策树程序实现
前言 之前的博客中介绍了决策树算法的原理并进行了数学推导(机器学习入门学习笔记:(3.1)决策树算法).决策树的原理相对简单,决策树算法有:ID3,C4.5,CART等算法.接下来将对ID3决策树算法 ...
- 机器学习入门学习笔记:(2.3)对数几率回归推导
理论推导 在以前的博客(机器学习入门学习笔记:(2.1)线性回归理论推导 )中推导了单元线性回归和多元线性回归的模型. 将线性回归模型简写为:y=ωTx+by = \omega^Tx+b: ...
- 机器学习入门学习笔记:(2.2)线性回归python程序实现
上一篇博客中,推导了线性回归的公式,这次试着编程来实现它.(机器学习入门学习笔记:(2.1)线性回归理论推导 ) 我们求解线性回归的思路有两个:一个是直接套用上一篇博客最后推导出来的公式:另一 ...
- 汇编入门学习笔记 (十二)—— int指令、port
疯狂的暑假学习之 汇编入门学习笔记 (十二)-- int指令.port 參考: <汇编语言> 王爽 第13.14章 一.int指令 1. int指令引发的中断 int n指令,相当于引 ...
最新文章
- ldd3笔记_3_编译模块
- wpa_supplicant drivers 查看跟踪
- python矩阵输入_Python基础之矩阵输入
- bal插口_播放器上的这个“昂贵”的插口,是噱头还是真有用
- python 循环控制语句结束_Python控制语句.while循环语句
- 文件正由另一进程使用,该进程无法访问该文件,解决方法
- 编程常见错误——循环中进行有符号数和无符号数的比较
- [Leetcode 18]四数之和 4 Sum
- 关于游戏中的材质系统
- IIS安装前已经安装了.NET Framework,安装后如何启用.NETFramework
- 浏览器Quirksmode(怪异模式)与CSScompat
- 数据-第8课-线性表的链式存储结构(未)
- [20180801]insert导致死锁.txt
- 如何解压 GZ 文件
- XXXX大学课程设计说明书格式规范
- 泰晤士计算机专业排行,泰晤士2021世界大学学科排名:世界大学计算机学科排名...
- C语言删除字符串的所有尾部空格
- 逆水寒捏脸服务器维护,《逆水寒》2019年3月28日更新公告
- 转载:rose软件下载(Rational Rose 2003 Enterprise Edition)
- 关于IE8浏览器JS导出excel,要使导出列宽度按自己控制。