초기엔 인터넷 예제를 그대로 따라해 보았으나 라이브러리를 제대로 불러오지 못하는 문제가 발생해서 adafruit홈페이지를 방문해서 문제점을 알게됬고 아예 파이 재설치하고 최신버전인 adafruit_circuitpython-dht라이브러리를 깔앗습니다. 결선도 똑바로 했고 코드는 example폴더에 있는 그대로 돌렸는데 계속 핀에서 센서를 찾을 수 없다고만 나옵니다.... dht22말고 다른 세서들은 제대로 잘 동작하는데 뭐가 문제인지 잘 모르겠습니다. 다른 센서들처럼 adc에 연결해서 값을 읽어야 할까요?... 아래는 예제코드입니다.
| import time | |
| import board | |
| import adafruit_dht | |
| # Initial the dht device, with data pin connected to: | |
| dhtDevice = adafruit_dht.DHT22(board.D18) | |
| # you can pass DHT22 use_pulseio=False if you wouldn't like to use pulseio. | |
| # This may be necessary on a Linux single board computer like the Raspberry Pi, | |
| # but it will not work in CircuitPython. | |
| # dhtDevice = adafruit_dht.DHT22(board.D18, use_pulseio=False) | |
| while True: | |
| try: | |
| # Print the values to the serial port | |
| temperature_c = dhtDevice.temperature | |
| temperature_f = temperature_c * (9 / 5) + 32 | |
| humidity = dhtDevice.humidity | |
| print( | |
| "Temp: {:.1f} F / {:.1f} C Humidity: {}% ".format( | |
| temperature_f, temperature_c, humidity | |
| ) | |
| ) | |
| except RuntimeError as error: | |
| # Errors happen fairly often, DHT's are hard to read, just keep going | |
| print(error.args[0]) | |
| time.sleep(2.0) | |
| continue | |
| except Exception as error: | |
| dhtDevice.exit() | |
| raise error | |
| time.sleep(2.0) |
오류메시지를 보여줘야지
센서를 연결해도 안해도 위짤처럼 나오고 컨씨 눌러서 프로그램 멈추면 아래짤처럼 나옴
while이랑 try except문 다 빼면 어떤 예외가 발생하는지 볼 수 있을거같은데.