基于树莓派SHT20温湿度采样

本文详细介绍了如何在树莓派上通过I2C协议与SHT20温湿度传感器进行连接和通信。首先启用内核I2C驱动,然后安装并使用i2c-tools来检测设备。接着展示了一个C语言程序,用于初始化传感器、执行软复位、获取序列号以及读取温度和湿度数据。通过这个程序,成功获取到了SHT20的序列号和温湿度样本数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上一篇介绍了i2c协议,SHT20是使用标准的i2c接口的温湿度传感器,我们首先要完成树莓派与SHT20传感器的连接:
在这里插入图片描述
使能内核I2C驱动模块:

liruiyan@cloud-ubuntu18:~$  sudo raspi-config

在这里插入图片描述
重启树莓派,系统启动之后会自动安装i2c的相关驱动:

liruiyan@cloud-ubuntu18:~$  sudo reboot
liruiyan@cloud-ubuntu18:~$  sudo apt-get install i2c-tools
liruiyan@cloud-ubuntu18:~$  lsmod | grep i2c
i2c_bcm2835         16384 0
i2c_dev             16384 0
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e f
00: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- -- 

使用i2cdetect命令可以查看到SHT21温湿度传感器设备地址0x40。
i2c设备驱动有两种模式:一种是用户模式设备驱动,这种驱动依赖于i2c子系统中i2c-dev驱动,这种驱动对应用程序员的要求很高,要
求应用程序员了解硬件的一些东西,了解时序、地址等;另一种是普通的设备驱动,应用程序员在使用的时候就像读写文件
一样,不过这里的read()和write()函数只能对应一条消息。
liruiyan@cloud-ubuntu18:~$ vim sht20.c

  1 #include <stdio.h>
  2 #include <fcntl.h>
  3 #include <unistd.h>
  4 #include <sys/ioctl.h>
  5 #include <linux/types.h>
  6 #include <sys/stat.h>
  7 #include <linux/i2c.h>
  8 #include <linux/i2c-dev.h>
  9 #include <stdio.h>
 10 #include <stdlib.h>
 11 #include <sys/types.h>
 12 #include <string.h>
 13 #include <stdint.h>
 14 #include <time.h>
 15 #include <errno.h>
 16 #include <string.h>
 17 #define SOFTRESET                        0xFE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mierya0707

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值