본문 바로가기

Linux/라즈베리파이

[RPI/Python] 라즈베리파이 PSD 센서 사용 하기 ― ADS1115로 아날로그 값 읽기

728x90
반응형

 

 

라즈베리파이를 사용하여 PSD센서값을 받아보겠습니다.

라즈베리파이는 라즈베리파이3 B를 사용하였고, PSD센서는 Sharp사의 것을 사용하였습니다.

또한, PSD의 센서값은 아날로그값인데, 라즈베리파이는 아두이노와 다르게 ADC(Analog-to-Digital Converter)가 내장되어 있지 않아 따로 ADC를 달아야 합니다.

그래서 저는 4채널 16bit ADC 모듈 ADS1115를 사용하였습니다.

 이제 각 부품의 datasheet를  참고하여, 핀을 연결합니다.

 

  • ADS1115

  • 라즈베리파이 3 B

  • PSD(2Y0A21)

 

  • Pin 연결

 

  • Python 라이브러리 설치

Adafruit ADS1x15 Python library를 설치합니다.

sudo apt-get update
sudo apt-get install build-essential python-dev python-smbus git
cd ~
git clone https://github.com/adafruit/Adafruit_Python_ADS1x15.git
cd Adafruit_Python_ADS1x15
sudo python setup.py install

 

  • Python 패키지 설치
sudo pip3 install adafruit-ads1x15

python3을 사용하므로 pip3으로 설치합니다.

 

  • I2C Enable

라즈베리파이 메뉴 기본설정>Raspberry Pi Configuration>Interfaces에서 I2C를 Enable해주어야 합니다.

설정을 바꾸었다면 OK를 누르고 재부팅 해줍니다.

 

  • Python Example Code

example Code는 ~/Adafruit_Python_ADS1x15/examples 에서 볼 수 있습니다.

sampletest.py를 실행해 보겠습니다.

# Simple demo of reading each analog input from the ADS1x15 and printing it to
# the screen.
# Author: Tony DiCola
# License: Public Domain
import time

# Import the ADS1x15 module.
import Adafruit_ADS1x15


# Create an ADS1115 ADC (16-bit) instance.
adc = Adafruit_ADS1x15.ADS1115()

# Or create an ADS1015 ADC (12-bit) instance.
#adc = Adafruit_ADS1x15.ADS1015()

# Note you can change the I2C address from its default (0x48), and/or the I2C
# bus by passing in these optional parameters:
#adc = Adafruit_ADS1x15.ADS1015(address=0x49, busnum=1)

# Choose a gain of 1 for reading voltages from 0 to 4.09V.
# Or pick a different gain to change the range of voltages that are read:
#  - 2/3 = +/-6.144V
#  -   1 = +/-4.096V
#  -   2 = +/-2.048V
#  -   4 = +/-1.024V
#  -   8 = +/-0.512V
#  -  16 = +/-0.256V
# See table 3 in the ADS1015/ADS1115 datasheet for more info on gain.
GAIN = 1

print('Reading ADS1x15 values, press Ctrl-C to quit...')
# Print nice channel column headers.
print('| {0:>6} | {1:>6} | {2:>6} | {3:>6} |'.format(*range(4)))
print('-' * 37)
# Main loop.
while True:
    # Read all the ADC channel values in a list.
    values = [0]*4
    for i in range(4):
        # Read the specified ADC channel using the previously set gain value.
        values[i] = adc.read_adc(i, gain=GAIN)
        # Note you can also pass in an optional data_rate parameter that controls
        # the ADC conversion time (in samples/second). Each chip has a different
        # set of allowed data rate values, see datasheet Table 9 config register
        # DR bit values.
        #values[i] = adc.read_adc(i, gain=GAIN, data_rate=128)
        # Each value will be a 12 or 16 bit signed integer value depending on the
        # ADC (ADS1015 = 12-bit, ADS1115 = 16-bit).
    # Print the ADC values.
    print('| {0:>6} | {1:>6} | {2:>6} | {3:>6} |'.format(*values))
    # Pause for half a second.
    time.sleep(0.1)

 

읽어오는 간격만 0.1s로 바꾸었습니다.

실행해보면, 연결한 0번에서 아날로그 값을 잘 읽는 것을 확인할 수 있습니다.

 

 

 

728x90
반응형