ESP32-BMP180气压、气温传感器

  • BMP180传感器介绍
  • 一、连接引脚
  • 二、使用步骤
    • 1.创建代码
    • 2.保存运行
  • 总结

BMP180传感器介绍

BMP180可以实时的测量大气压力,还能测量实时温度。具有IIC总线的接口。
BMP180详解


一、连接引脚

示例:BMP180传感器使用I2C传输数据 。
4根引脚,名称与功能如下;
vcc 为外接供电电源输入端
GND 地线
SCL I2C通信模式时钟信号,连接ESP32的18引脚
SDA I2C通信模式数据信号,连接ESP32的19引脚

二、使用步骤

1.创建代码

代码如下(示例):

'''
bmp180 is a micropython module for the Bosch BMP180 sensor. It measures
temperature as well as pressure, with a high enough resolution to calculate
altitude.
Breakoutboard: http://www.adafruit.com/products/1603
data-sheet: http://ae-bst.resource.bosch.com/media/products/dokumente/
bmp180/BST-BMP180-DS000-09.pdf
The MIT License (MIT)
Copyright (c) 2014 Sebastian Plamauer, oeplse@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''from ustruct import unpack as unp
from machine import I2C, Pin
import math
import time# BMP180 class
class BMP180():'''Module for the BMP180 pressure sensor.'''_bmp_addr = 119             # adress of BMP180 is hardcoded on the sensor# initdef __init__(self, i2c_bus):# create i2c obect_bmp_addr = self._bmp_addrself._bmp_i2c = i2c_busself._bmp_i2c.start()self.chip_id = self._bmp_i2c.readfrom_mem(_bmp_addr, 0xD0, 2)# read calibration data from EEPROMself._AC1 = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xAA, 2))[0]self._AC2 = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xAC, 2))[0]self._AC3 = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xAE, 2))[0]self._AC4 = unp('>H', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xB0, 2))[0]self._AC5 = unp('>H', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xB2, 2))[0]self._AC6 = unp('>H', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xB4, 2))[0]self._B1 = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xB6, 2))[0]self._B2 = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xB8, 2))[0]self._MB = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xBA, 2))[0]self._MC = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xBC, 2))[0]self._MD = unp('>h', self._bmp_i2c.readfrom_mem(_bmp_addr, 0xBE, 2))[0]# settings to be adjusted by userself.oversample_setting = 3self.baseline = 101325.0# output rawself.UT_raw = Noneself.B5_raw = Noneself.MSB_raw = Noneself.LSB_raw = Noneself.XLSB_raw = Noneself.gauge = self.makegauge() # Generator instancefor _ in range(128):next(self.gauge)time.sleep_ms(1)def compvaldump(self):'''Returns a list of all compensation values'''return [self._AC1, self._AC2, self._AC3, self._AC4, self._AC5, self._AC6, self._B1, self._B2, self._MB, self._MC, self._MD, self.oversample_setting]# gauge rawdef makegauge(self):'''Generator refreshing the raw measurments.'''delays = (5, 8, 14, 25)while True:self._bmp_i2c.writeto_mem(self._bmp_addr, 0xF4, bytearray([0x2E]))t_start = time.ticks_ms()while (time.ticks_ms() - t_start) <= 5: # 5mS delayyield Nonetry:self.UT_raw = self._bmp_i2c.readfrom_mem(self._bmp_addr, 0xF6, 2)except:yield Noneself._bmp_i2c.writeto_mem(self._bmp_addr, 0xF4, bytearray([0x34+(self.oversample_setting << 6)]))t_pressure_ready = delays[self.oversample_setting]t_start = time.ticks_ms()while (time.ticks_ms() - t_start) <= t_pressure_ready:yield Nonetry:self.MSB_raw = self._bmp_i2c.readfrom_mem(self._bmp_addr, 0xF6, 1)self.LSB_raw = self._bmp_i2c.readfrom_mem(self._bmp_addr, 0xF7, 1)self.XLSB_raw = self._bmp_i2c.readfrom_mem(self._bmp_addr, 0xF8, 1)except:yield Noneyield Truedef blocking_read(self):if next(self.gauge) is not None: # Discard old datapasswhile next(self.gauge) is None:pass@propertydef oversample_sett(self):return self.oversample_setting@oversample_sett.setterdef oversample_sett(self, value):if value in range(4):self.oversample_setting = valueelse:print('oversample_sett can only be 0, 1, 2 or 3, using 3 instead')self.oversample_setting = 3@propertydef temperature(self):'''Temperature in degree C.温度以C度为单位。'''next(self.gauge)try:UT = unp('>H', self.UT_raw)[0]except:return 0.0X1 = (UT-self._AC6)*self._AC5/2**15X2 = self._MC*2**11/(X1+self._MD)self.B5_raw = X1+X2return (((X1+X2)+8)/2**4)/10@propertydef pressure(self):'''Pressure in mbar.压力(以毫巴为单位)'''next(self.gauge)self.temperature  # Populate self.B5_rawtry:MSB = unp('B', self.MSB_raw)[0]LSB = unp('B', self.LSB_raw)[0]XLSB = unp('B', self.XLSB_raw)[0]except:return 0.0UP = ((MSB << 16)+(LSB << 8)+XLSB) >> (8-self.oversample_setting)B6 = self.B5_raw-4000X1 = (self._B2*(B6**2/2**12))/2**11X2 = self._AC2*B6/2**11X3 = X1+X2B3 = ((int((self._AC1*4+X3)) << self.oversample_setting)+2)/4X1 = self._AC3*B6/2**13X2 = (self._B1*(B6**2/2**12))/2**16X3 = ((X1+X2)+2)/2**2B4 = abs(self._AC4)*(X3+32768)/2**15B7 = (abs(UP)-B3) * (50000 >> self.oversample_setting)if B7 < 0x80000000:pressure = (B7*2)/B4else:pressure = (B7/B4)*2X1 = (pressure/2**8)**2X1 = (X1*3038)/2**16X2 = (-7357*pressure)/2**16return pressure+(X1+X2+3791)/2**4@propertydef altitude(self):'''Altitude in m. 海拔高度(以米为单位)'''try:p = -7990.0*math.log(self.pressure/self.baseline)except:p = 0.0return pif __name__ == '__main__':import timefrom machine import Pin,I2C#气压传感器bus = I2C(scl=Pin(18),sda=Pin(19), freq=100000)bmp180 = BMP180(bus)bmp180.oversample_sett = 2bmp180.baseline = 101325# BMP180温度,以C度为单位w = bmp180.temperatureprint('BMP180温度:',w)# BMP180压力(以毫巴为单位)pressures = bmp180.pressureprint('BMP180压力:',pressures)# BMP180海拔高度(以米为单位)altitude  = -bmp180.altitudeprint('BMP180海拔高度:', altitude)

2.保存运行

运行结果如下(示例):

>>> %Run -c $EDITOR_CONTENT
Warning: I2C(-1, ...) is deprecated, use SoftI2C(...) instead
BMP180温度: 21.595
BMP180压力: 101694.2
BMP180海拔高度: 29.05686

总结

这个模块很小巧。注意不要连接错线了。海拨高度是通过气压推算出来的,气压又受到气象的影响,只能作为参考。

ESP32-BMP180气压、气温传感器相关推荐

  1. ESP32设备驱动-BMP180气压温度传感器驱动

    BMP180气压温度传感器驱动 1.BMP180介绍 BMP180 是Bosch Sensortec 新推出的数字气压传感器,性能非常高,可用于智能手机,平板电脑和运动设备等高级移动设备.它遵循BMP ...

  2. ESP8266-Arduino编程实例-BMP180气压温度传感器驱动

    BMP180气压温度传感器驱动 1.BMP180介绍 BMP180 是用于测量气压和温度的最佳低成本传感解决方案. 传感器焊接在带有 3.3V 稳压器.I2C 电平转换器和 I2C 引脚上的上拉电阻的 ...

  3. STM32F1与STM32CubeIDE编程实例-BMP180气压温度传感器驱动

    BMP180气压温度传感器驱动 1.BMP180介绍 BMP180 是用于测量气压和温度的最佳低成本传感解决方案. 传感器焊接在带有 3.3V 稳压器.I2C 电平转换器和 I2C 引脚上的上拉电阻的 ...

  4. 【Renesas RA6M4开发板之I2C读取BMP180气压温度】

    [Renesas RA6M4开发板之I2C读取BMP180气压温度] 1.0 BMP180 1.1 BMP180介绍 1.2 BMP180特点 1.3 产品应用 2. RT-theard配置 2.1 ...

  5. MicroPython ESP32 读取DHT11温湿度传感器数据

    MicroPython ESP32 读取DHT11温湿度传感器数据 DHT11温湿度传感器 接线说明 ESP32 ----- DHT11 3.3V ----- VCC GND ----- GND GP ...

  6. 从零开始的DIY智能家居 - 基于 ESP32 的智能光照传感器

    文章目录 前言 硬件选择 代码解析 获取代码 设备控制命令: 设备和协议初始化流程: 配置设备信息 回调函数注册 数据获取与发送流程 总结 前言 上周出差有点急,结果家里灯没关,开了整整一周的时间(T ...

  7. 【工程师有空了】安信可ESP32之TOUCH触摸传感器的花式应用--一个IO识别多个触摸按键

    文章目录 前言 一.TOUCHU 触摸传感器工作原理及硬件设计 1.触摸传感器的原理 2.多个触摸按键的识别方案 3.硬件连接 二.程序设计 1.传感器IO选择 2.驱动库的使用 3. 关于按键枚举 ...

  8. 快速接头 数字温度传感器 整体热电偶套管 气温传感器 测温传感器 温度传感器 温度传感器生产厂家 温度变送器 温度感应器 温度测量 热电偶 热电偶传感器 热电偶套管 热电偶温度传感器 热电偶温度计

    工业传感器和组件 100系列 传感器包括直接浸入式.可变浸入式和弹簧加载式热电偶套管设计.温度范围为-196°C至1260°C.所有热电偶都有特殊限制,以提高精度. 200系列 传感器包括直接浸入式. ...

  9. esp32外部中断_玩转 ESP32 + Arduino (四) 电容按键 霍尔传感器 外部中断 延时 脉冲检测...

    一. 电容输入 touchRead(pin) 及电容输入中断touchAttachInterrupt(pin, TSR , threshold) ESP32专门提供了电容触摸传感器的功能, 共有T0, ...

最新文章

  1. Oracle使用技巧----sqlplus Set常用设置
  2. python的类变量与实例变量以及__dict__属性
  3. 近世代数--置换群--置换permutation分解成什么?置换的级如何计算?
  4. python中head_Python pandas.DataFrame.head函数方法的使用
  5. const constexpr C++ 解释
  6. 81. 搜索旋转排序数组 II---Leecode----java
  7. hdu 5802——Windows 10
  8. Nginx安装手册(摘自入云龙老师教案,亲测可用)
  9. 利用openssl来计算sha256哈希值
  10. Oracle数据把持和控制言语详解-1
  11. Clover 驱动文件夹_四叶草Clover相关
  12. 扩展ExoPlayer实现多音轨同时播放
  13. 「c#」图片转换ico图标程序及源码
  14. python另存为excel_python 将数据保存为excel的xls格式(实例讲解)
  15. Android 三方数据库ObjectBox使用
  16. Zotero-word中引用跳转到参考文献/建立超链接-引用格式(Xie et al 2021, Achanta et al 2012)
  17. 蓝色经典钢琴-Cinesamples Piano In Blue v2.3b Kontakt
  18. TIOBE 11 月编程语言排行榜:Rust 在 Top 20 站住脚!
  19. matlab三大重要数组之胞元数组
  20. 基于jsp+ssm的爱康医院专家预约管理系统

热门文章

  1. 【Excel】Excel学习笔记 -- 通配符的使用与定位条件
  2. catchlog是什么软件_如何处理异常? catch Exception OR catch Throwable
  3. 【Gulimall+】第三方服务:对象存储OSS、短信验证、社交登录、支付宝支付
  4. 最全的厚黑学...教你怎样混社会(转...作者不是一般的城府,但这就是中国真实的社交关系,深的很)
  5. CentOS8 配置记录
  6. CC2530步进电机
  7. 用Scrapy爬取笔趣阁小说
  8. 小米路由器显示无法连接到服务器,小米路由无法连接WIFI的五种解决方法【图解】...
  9. 小白程序员的学习路线
  10. 3.OpenCV可视化(Viz)——单目相机标定模拟