树莓派5运行超声波传感器Python代码遇RuntimeError问题求助
树莓派5运行超声波传感器Python代码报错:RuntimeError: Cannot determine SOC peripheral base address
我在树莓派5上运行Python代码测试超声波传感器,代码如下:
import RPi.GPIO as GPIO import time #GPIO Mode (BOARD / BCM) GPIO.setmode(GPIO.BCM) #set GPIO Pins GPIO_TRIGGER = 5 GPIO_ECHO = 6 #set GPIO direction (IN / OUT) GPIO.setup(GPIO_TRIGGER, GPIO.OUT) GPIO.setup(GPIO_ECHO, GPIO.IN) def distance(): # set Trigger to HIGH GPIO.output(GPIO_TRIGGER, True) # set Trigger after 0.01ms to LOW time.sleep(0.00001) GPIO.output(GPIO_TRIGGER, False) StartTime = time.time() StopTime = time.time() # save StartTime while GPIO.input(GPIO_ECHO) == 0: StartTime = time.time() # save time of arrival while GPIO.input(GPIO_ECHO) == 1: StopTime = time.time() # time difference between start and arrival TimeElapsed = StopTime - StartTime # multiply with the sonic speed (34300 cm/s) # and divide by 2, because there and back distance = (TimeElapsed * 34300) / 2 return distance if __name__ == '__main__': try: while True: dist = distance() print ("Measured Distance = %.1f cm" % dist) time.sleep(1) # Reset by pressing CTRL + C except KeyboardInterrupt: print("Measurement stopped by User") GPIO.cleanup()
已尝试以下操作,但始终出现RuntimeError: Cannot determine SOC peripheral base address错误:
- 以root身份通过
sudo运行代码 - 重装RPi.GPIO库
- 在树莓派配置中启用GPIO接口
- 执行系统更新命令:
sudo rpi-update sudo reboot sudo apt-get update sudo apt-get upgrade sudo adduser pi gpio - 更新pip和setuptools至最新版本:
pip install --upgrade pip setuptools - 从源码安装python-gpiozero:
git clone https://github.com/RPi-Distro/python-gpiozero.git cd python-gpiozero sudo python setup.py install
请求解决该问题。
内容的提问来源于stack exchange,提问作者Lachheb Nesrine
相关产品推荐
相关产品推荐

