Processor

【라즈베리파이】 wiringPi Python : I2C 가변저항과 조이스틱

작성자 임베디드코리아 작성일26-02-20 00:58 조회76회 댓글0건
■  I2C 가변저항
--->>>  예제  :  I2C_Potentiometer.py  <<<-------------------------------
import smbus
import time
address = 0x48 #I2C 버스 주소 즉, ADC 모듈의 주소
A0 = 0x40 #AIN0의 주소
bus = smbus.SMBus(1) #Raspberry Pi에서 버스 1번을 사용

while True:
bus.write_byte(address, A0) # write -> read
    value = bus.read_byte(address)
    print(value)
    time.sleep(0.1)
---------------------------------------------------------

--->>>  예제 : I2c_joystick.py    <<<-----------------------------
import smbus
import time

address = 0x48
bus = smbus.SMBus(1)

a0 = 0x40
a1 = 0x41
a2 = 0x42
x_value = 0
y_value = 0
pulse_value = 0

while True:
    bus.write_byte(address, a0)
    x_value = bus.read_byte(address)
    bus.write_byte(address, a1)
    y_value = bus.read_byte(address)
    bus.write_byte(address, a2)
    pulse_value = bus.read_byte(address)
    print(x_value, y_value, pulse_value)
    time.sleep(0.01)
-----------------------------------------------------------
◈ x축과 y축 그리고 Pulse값이 있다.
◈ x_value가 0에 가까워질 수록 저항이 약해진다는 뜻으로, 전압이 약해진다. 즉, 조이스틱이 왼쪽을 향하고 있다.
◈ x_value가 255에 가까워질 수록 저항을 지나 커진다는 뜻으로, 전압이 커진다. 즉, 조이스틱이 오른쪽을 향하고 있다.
    (반대로 설정한 경우 반대로 동작)
◈ y_value가 0에 가까워질 수록 저항이 약해진다는 뜻으로, 전압이 약해진다. 즉, 조이스틱이 위쪽을 향하고 있다.
◈ y_value가 255에 가까워질 수록 저항을 지나 커진다는 뜻으로, 전압이 커진다. 즉, 조이스틱이 아래쪽을 향하고 있다.
    (반대로 설정한 경우 반대로 동작)