This week, I continued to learn something about the Raspberry Pi sensor, as well as about the Laser Radar, Temperature Sensor, and Touch Bottom.
A laser Radar is a tool that uses a Laser to measure the distance. And it is the most popular way to measure something.
Here is the code to use Laser Radar on the Raspberry Pi👇
import time
import board
import busio
import adafruit_vl53l0x
pii2c = busio.I2C(board.SCL, board.SDA)
makerobo_vl53 = adafruit_vl53l0x.VL53L0X(pii2c)
# 程序入口
if __name__ == '__main__':
try:
# 循环持续输出距离值
while True:
print("Range: {0}mm".format(makerobo_vl53.range)) # 打印出距离值
time.sleep(1.0) # 延时1s
except KeyboardInterrupt:
print("Exit") # Exit on CTRL+C
This is its wiring diagram👇
And here is the result👇
https://www.youtube.com/shorts/rYRvamweVUg
The Temperature Sensor is a sensor that can let us measure the temperature in real-time.
Here is the code👇
import os
import time
makerobo_ds18b20 = '28-46e7d4454fbc' # DS18B20 设备地址
# 加载内核模块
#os.system('modprobe w1-gpio')
#os.system('modprobe w1-therm')
# 初始化函数
def makerobo_setup():
global makerobo_ds18b20
for i in os.listdir('/sys/bus/w1/devices'):
if i != 'w1_bus_master1':
makerobo_ds18b20 = i # 获取 DS18B20 的设备地址
# 读取温度数据
def makerobo_read():
try:
makerobo_location = '/sys/bus/w1/devices/' + makerobo_ds18b20 + '/w1_slave'
with open(makerobo_location) as makerobo_tfile:
makerobo_text = makerobo_tfile.read()
secondline = makerobo_text.split("\\n")[1]
temperaturedata = secondline.split(" ")[9]
temperature = float(temperaturedata[2:]) / 1000
return temperature
except (IndexError, FileNotFoundError) as e:
return None # 返回 None 表示读取失败
# 循环函数
def makerobo_loop():
while True:
temp = makerobo_read()
if temp is not None:
print("Current temperature : %0.3f C" % temp)
time.sleep(1) # 每秒读取一次
# 释放资源
def destroy():
pass
# 主程序入口
if __name__ == '__main__':
try:
makerobo_setup()
makerobo_loop()
except KeyboardInterrupt:
destroy()
This is its wiring diagram👇
And here is the result👇