From e28181dfd00da42a95d82e0a31abdcdf571dd507 Mon Sep 17 00:00:00 2001 From: westnife3 Date: Wed, 27 Aug 2025 17:21:18 +0900 Subject: [PATCH] =?UTF-8?q?refs=20#2=20PCF8591=20A/D=20=EC=BB=A8=EB=B2=84?= =?UTF-8?q?=ED=84=B0=20=EB=AA=A8=EB=93=88=20=EC=86=8C=EC=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- requirements.txt | 24 ++++++++++++ tutorial/1.dht11_sensor.py | 1 - tutorial/2.rotary_potentio_meter_module.py | 45 ++++++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 requirements.txt create mode 100644 tutorial/2.rotary_potentio_meter_module.py diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..0a2e1fa --- /dev/null +++ b/requirements.txt @@ -0,0 +1,24 @@ +Adafruit-Blinka==8.62.0 +adafruit-circuitpython-busdevice==5.2.13 +adafruit-circuitpython-connectionmanager==3.1.5 +adafruit-circuitpython-dht==4.0.9 +adafruit-circuitpython-framebuf==1.6.9 +adafruit-circuitpython-requests==4.1.13 +adafruit-circuitpython-ssd1306==2.12.21 +adafruit-circuitpython-typing==1.12.1 +Adafruit-PlatformDetect==3.81.0 +Adafruit-PureIO==1.1.11 +binho-host-adapter==0.1.6 +cbor2==5.6.5 +luma.core==2.5.1 +luma.oled==3.14.0 +pillow==11.3.0 +pyftdi==0.56.0 +pyserial==3.5 +pyusb==1.3.1 +RPi.GPIO==0.7.1 +rpi_ws281x==5.0.0 +smbus==1.1.post2 +smbus2==0.5.0 +sysv_ipc==1.1.0 +typing_extensions==4.14.1 diff --git a/tutorial/1.dht11_sensor.py b/tutorial/1.dht11_sensor.py index 94bd3fb..49d66ec 100644 --- a/tutorial/1.dht11_sensor.py +++ b/tutorial/1.dht11_sensor.py @@ -35,4 +35,3 @@ while True: raise error time.sleep(2.0) # 2초마다 센서 값을 읽습니다. - diff --git a/tutorial/2.rotary_potentio_meter_module.py b/tutorial/2.rotary_potentio_meter_module.py new file mode 100644 index 0000000..cb79533 --- /dev/null +++ b/tutorial/2.rotary_potentio_meter_module.py @@ -0,0 +1,45 @@ +import smbus +import time + +# PCF8591의 I2C 주소 (i2cdetect -y 1 명령으로 확인 가능) +# 대부분의 PCF8591 모듈은 0x48을 기본 주소로 사용합니다. +PCF8591_ADDRESS = 0x48 + +# I2C 버스 번호 (라즈베리 파이 2, 3, 4는 1, 초기 모델은 0일 수 있음) +I2C_BUS = 1 + +# smbus 객체 생성 +bus = smbus.SMBus(I2C_BUS) + +# PCF8591 제어 바이트 (CONTROLL_BYTE) 설정 +# 비트 7: 1 (DAC 활성화 - 여기서는 사용 안 함) +# 비트 6: 0 (AIN 활성화 - 여기서는 사용 안 함) +# 비트 5: 0 (AIN0 활성화 - 여기서는 사용 안 함) +# 비트 4: 0 (AIN1 활성화 - 여기서는 사용 안 함) +# 비트 3-2: AIN 입력 모드 (00: 4단일 입력, 01: 차동 입력, 10: 2차동 입력) +# 여기서는 4개의 단일 입력 모드 (AIN0-AIN3)를 사용합니다. +# 비트 1-0: 사용할 아날로그 입력 채널 선택 (00: AIN0, 01: AIN1, 10: AIN2, 11: AIN3) +# 따라서 AIN0을 읽으려면 0x40 (1000000) 또는 0x00 (00000000)과 같이 설정할 수 있습니다. +# 일반적으로 0x40은 4채널 단일 입력 모드에서 AIN0을 나타냅니다. +# 하지만 라이브러리에서 채널 번호를 직접 지정하기 때문에 간단하게 0x40으로 설정합니다. +# 0x40 = 0100 0000b (단일 입력 모드 설정) +CONTROL_BYTE = 0x40 + +print(f"PCF8591 A/D 컨버터 테스트 시작 (주소: 0x{PCF8591_ADDRESS:X})") +print("로터리 엔코더를 돌려 값을 확인하세요. Ctrl+C로 종료.") + +try: + while True: + # PCF8591에서 AIN0 채널의 값을 읽습니다. + # read_byte_data(주소, 제어_바이트 + 채널_번호) + # 제어 바이트에 채널 번호를 더하여 특정 채널을 선택합니다. + # 예를 들어 AIN0은 채널 0이므로 CONTROL_BYTE + 0 입니다. + analog_value = bus.read_byte_data(PCF8591_ADDRESS, CONTROL_BYTE + 0) + + # 읽은 아날로그 값 (0-255) 출력 + print(f"아날로그 값 (AIN0): {analog_value}") + + time.sleep(0.1) # 0.1초마다 값 읽기 + +except KeyboardInterrupt: + print("\n프로그램 종료.")