树莓派开发界面显示温度_树莓派4B+ubuntu20.04读取ds18b20温度传感器数据
测试环境树莓派4B 8G + Ubuntu20.04 64位1. 断电取下内存卡插入到win10电脑上,修改内存卡下的usercfg.txt文件2. 在usercfg.txt下添加如下内容:#ds18b20dtoverlay=w1-gpio-pullup,gpiopin=4修改如下图所示:3. 接线将传感器DQ引脚接GPIO.7引脚上、VCC接3.3V、GND接GND4. 连接树莓派①挂...
·

测试环境
树莓派4B 8G + Ubuntu20.04 64位
1. 断电取下内存卡插入到win10电脑上,修改内存卡下的usercfg.txt文件

2. 在usercfg.txt下添加如下内容:
#ds18b20dtoverlay=w1-gpio-pullup,gpiopin=4
修改如下图所示:

3. 接线
将传感器DQ引脚接GPIO.7引脚上、VCC接3.3V、GND接GND


4. 连接树莓派
①挂载设备驱动
sudo modprobe w1-gpiosudo modprobe w1-therm
② 确认设备是否生效
cd /sys/bus/w1/devices/
ls

28-011939632f5b就是外接的温度传感器设备,但并不是每个客户端都显示一样的,这个是传感器的序列号。
④编写c代码读取传感器数据
#include #include #include #include #include #include #include #include int Open_send(char *base){//打开发送数据 int fd, size; char buffer[1024]; fd = open(base,O_RDONLY); lseek(fd,69,SEEK_SET); size = read(fd,buffer,sizeof(buffer)); close(fd); printf("temp ℃ = %f\n",(float)atoi(buffer)/1000.0); return 0;}int readFileList(char *basePath){//文件查找 DIR *dir; struct dirent *ptr; char base[1024]; if ((dir=opendir(basePath)) == NULL){ perror("Open dir error..."); exit(1); } while ((ptr=readdir(dir)) != NULL) { if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0){//current dir OR parrent dir continue; } else if(ptr->d_type == 10){ memset(base,'\0',sizeof(base)); sprintf(base,"%s",ptr->d_name); if((strcmp("27",base)<0)&&(strcmp("29",base)>0)){ sprintf(base,"%s/%s/w1_slave",basePath,ptr->d_name); //printf("%s\n",base); while(1) Open_send(base); } } } closedir(dir); return 1;}int main(void){ DIR *dir; char basePath[1024]; memset(basePath,'\0',sizeof(basePath)); strcpy(basePath,"/sys/bus/w1/devices"); readFileList(basePath); return 0;}
⑤编译后运行
gcc -o ds18b20 ds18b20.c./ds18b20

—— END ——

关注微信公众号:物联网智能传感器
了解更多知识
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐




所有评论(0)