【树莓派Pico测评】四,Pico连接温度传感器DS18B20读取温度
<div class='showpostmsg'><p> </p><p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman""><span style="font-family:宋体">一,</span>DS18B20 <span style="font-family:宋体">温度传感器介绍</span></span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman"">1<span style="font-family:宋体">,采用单总线的接口方式</span> <span style="font-family:宋体">与微处理器连接时仅需要一条口线即可实现微处理器与</span> DS18B20 <span style="font-family:宋体">的双向通讯。单总线具有经济性好,抗干扰能力强,适合于恶劣环境的现场温度测量。</span></span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman"">2<span style="font-family:宋体">,测量温度范围宽,测量精度高</span> DS18B20 <span style="font-family:宋体">的测量范围为</span> <st1:chmetcnv hasspace="True" negative="True" numbertype="1" sourcevalue="55" tcsc="0" unitname="℃" w:st="on">-55 <span lang="EN-US" style="font-family:宋体">℃</span></st1:chmetcnv> ~+ <st1:chmetcnv hasspace="True" negative="False" numbertype="1" sourcevalue="125" tcsc="0" unitname="℃" w:st="on">125 <span lang="EN-US" style="font-family:宋体">℃</span></st1:chmetcnv> ; <span style="font-family:宋体">在</span> -10~+ <st1:chmetcnv hasspace="False" negative="False" numbertype="1" sourcevalue="85" tcsc="0" unitname="ᄚC" w:st="on">85<span lang="EN-US" style="font-family:宋体">°</span>C</st1:chmetcnv><span style="font-family:宋体">范围内,精度为</span> <span style="font-family:宋体">±</span> <st1:chmetcnv hasspace="False" negative="False" numbertype="1" sourcevalue="0.5" tcsc="0" unitname="ᄚC" w:st="on">0.5<span lang="EN-US" style="font-family:宋体">°</span>C</st1:chmetcnv> <span style="font-family:宋体">。</span></span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman"">3<span style="font-family:宋体">,在使用中不需要任何外围元件。</span></span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman"">4<span style="font-family:宋体">,持多点组网功能</span> <span style="font-family:宋体">多个</span> DS18B20 <span style="font-family:宋体">可以并联在唯一的单线上,实现多点测温。</span></span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman"">5<span style="font-family:宋体">,工作电源</span>: 3.0~5.5V<span style="font-family:宋体">。</span>DS18B20 <span style="font-family:宋体">也可以通过内部寄生电路从数据线上获取电源。因此,当数据线上的时序满足一定的要求时,可以不接外部电源,从而使系统结构更趋简单,可靠性更高。</span></span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman"">6<span style="font-family:宋体">,测量参数可配置</span> DS18B20 <span style="font-family:宋体">的测量分辨率可通过程序设定</span> 9~12 <span style="font-family:宋体">位。</span></span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman"">7<span style="font-family:宋体">,负压特性电源极性接反时,温度计不会因发热而烧毁,但不能正常工作。</span></span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman"">8<span style="font-family:宋体">,掉电保护功能</span> DS18B20 <span style="font-family:宋体">内部含有</span> EEPROM <span style="font-family:宋体">,在系统掉电以后,它仍可保存分辨率及报警温度的设定值。</span></span></span></p>
<p style="text-align:justify"> </p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman""><span style="font-family:宋体">二,硬件连接</span></span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman""><span style="font-family:宋体">因为</span>Pico<span style="font-family:宋体">的</span>IO<span style="font-family:宋体">是</span>3.3V<span style="font-family:宋体">的,所以这次</span>DS18B20<span style="font-family:宋体">的工作电源接</span>3.3V<span style="font-family:宋体">。</span></span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman""><span style="font-family:宋体">硬件连接:</span></span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman"">DS18B20<span style="font-family:宋体">的</span>1<span style="font-family:宋体">脚(</span>GND<span style="font-family:宋体">)</span>---Pico<span style="font-family:宋体">的</span>38<span style="font-family:宋体">脚(</span>GND<span style="font-family:宋体">)</span></span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman"">DS18B20<span style="font-family:宋体">的</span>2<span style="font-family:宋体">脚(</span>DQ<span style="font-family:宋体">)</span> ---Pico<span style="font-family:宋体">的</span>6<span style="font-family:宋体">脚(</span>GP4<span style="font-family:宋体">)</span></span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman"">DS18B20<span style="font-family:宋体">的</span>3<span style="font-family:宋体">脚(</span>VDD<span style="font-family:宋体">)</span>---Pico<span style="font-family:宋体">的</span>36<span style="font-family:宋体">脚(</span>3V3 OUT<span style="font-family:宋体">)</span></span></span></p>
<p style="text-align:justify"></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman""><span style="font-family:宋体">三,程序</span></span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman""><span style="font-family:宋体">系统是带相关的库的,所以直接使用就可以了。</span></span></span></p>
<p style="text-align:justify"> </p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman"">import machine, onewire, ds18x20, time</span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman"">ds_pin=machine.Pin(4)</span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman"">ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))</span></span></p>
<p style="text-align:justify"> </p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman"">roms = ds_sensor.scan()</span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman"">print('Found DS devices: ', roms)</span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman""> </span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman"">while True:</span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman""> ds_sensor.convert_temp()</span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman""> time.sleep_ms(750)</span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman""> for rom in roms:</span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman""> print(rom)</span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman""> print(ds_sensor.read_temp(rom))</span></span></p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman""> time.sleep(2)</span></span></p>
<p style="text-align:justify"> </p>
<p style="text-align:justify"><span style="font-size:10.5pt"><span style="font-family:"Times New Roman""><span style="font-family:宋体">下面是输出结果:</span></span></span></p>
<p style="text-align:justify"></p>
<p style="text-align:justify"> </p>
<p> </p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> <p>谢谢分享~~期待后续呀</p>
<p>有库了之后,编码变得好简单啊,就这么几句话!</p>
<p>期待楼主后续,有机会自己买一个玩玩,比较感兴趣。</p>
<p>不错,有参考价值!</p>
页:
[1]