1. i2c 를 활성화한 상태에서 실행해야한다.
2. smbus 를 설치해야한다.
sudo apt-get update
sudo apt-get install python-smbus
핀의 연결은 아래와 같다.
아래 코드를 실행
#!/usr/bin/python
import smbus
import time
# Define some constants from the datasheet
DEVICE = 0x23 # Default device I2C address
POWER_DOWN = 0x00 # No active state
POWER_ON = 0x01 # Power on
RESET = 0x07 # Reset data register value
# Start measurement at 4lx resolution. Time typically 16ms.
CONTINUOUS_LOW_RES_MODE = 0x13
# Start measurement at 1lx resolution. Time typically 120ms
CONTINUOUS_HIGH_RES_MODE_1 = 0x10
# Start measurement at 0.5lx resolution. Time typically 120ms
CONTINUOUS_HIGH_RES_MODE_2 = 0x11
# Start measurement at 1lx resolution. Time typically 120ms
# Device is automatically set to Power Down after measurement.
ONE_TIME_HIGH_RES_MODE_1 = 0x20
# Start measurement at 0.5lx resolution. Time typically 120ms
# Device is automatically set to Power Down after measurement.
ONE_TIME_HIGH_RES_MODE_2 = 0x21
# Start measurement at 1lx resolution. Time typically 120ms
# Device is automatically set to Power Down after measurement.
ONE_TIME_LOW_RES_MODE = 0x23
#bus = smbus.SMBus(0) # Rev 1 Pi uses 0
bus = smbus.SMBus(1) # Rev 2 Pi uses 1
def convertToNumber(data):
# Simple function to convert 2 bytes of data
# into a decimal number
return ((data[1] + (256 * data[0])) / 1.2)
def readLight(addr=DEVICE):
data = bus.read_i2c_block_data(addr,ONE_TIME_HIGH_RES_MODE_1)
return convertToNumber(data)
def main():
while True:
print "Light Level : " + str(readLight()) + " lx"
time.sleep(0.5)
if __name__=="__main__":
main()
또는 웹사이트에서 바로 다운로드
wget https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/bh1750.py
설치 완료사진
테스트 화면
200ms 단위로 새로 측정하는데, Ctrl+C 키를 눌러서 강제 종료해야한다.
'Computer > Linux' 카테고리의 다른 글
아파치 서버 업로드 사이즈 한계 설정 (0) | 2019.02.07 |
---|---|
웹에서 SSH 실행 - WebsSSH shellinabox (1) | 2019.02.07 |
리눅스 패스워드 파일경로 및 숨김해제, 암호 확인 (0) | 2019.02.07 |
워드프레스 설치하기 (0) | 2019.02.07 |
라즈베리파이 i2c 활성화 (0) | 2019.02.07 |
라즈베리파이 온습도계 소스 (0) | 2019.02.07 |
crontab (1) | 2019.02.07 |
VirtualHost 서브도메인 특정폴더로 연결 (0) | 2019.02.07 |