Opensourcetechブログ

OpensourcetechによるNGINX/Kubernetes/Zabbix/Neo4j/Linuxなどオープンソース技術に関するブログです。

光センサーBH1750FVI(GY-30)で明るさを測定する

こんにちは、LinuCエバンジェリストこと、鯨井貴博@opensourcetechです。

 

今回は、光センサーBH1750FVI(GY-30)を使って、明るさを測定してみます。

 

 

 BH1750FVI(GY-30)

f:id:opensourcetech:20190324234415p:plain

データシートは、以下にあります」。

https://www.mouser.com/ds/2/348/bh1750fvi-e-186247.pdf

 

なお、光センサーBH1750FVI(GY-30)は本体とピンの半田付けを行ってから使います。

f:id:opensourcetech:20190324154804j:plain

f:id:opensourcetech:20190324155501j:plain

f:id:opensourcetech:20190324161352j:plain


 

 明るさについて

調べてみると、単純に明るさと言ってもいくつか種類がありました。

詳細は、こちらで紹介されています。

今回は、ルクス(lx)という単位で表される照度と、輝度を測定します。

参考

照度の基準(https://エネプラ.com/hikari_led/%E7%85%A7%E5%BA%A6%E3%81%A8%E3%81%AF%EF%BC%9F

照度(http://www.hikariiku.com/think_of_as/1116/

輝度(http://www.hikariiku.com/think_of_as/1138/

 

 

 配線図

光センサーBH1750FVI(GY-30)とRaspberry PIを接続します。

f:id:opensourcetech:20190325004401p:plain

 

こんな感じで接続してます。

f:id:opensourcetech:20190324172550j:plain

 

 

 Raspberry Piの準備

 /boot/config.txt の「dtparam=i2c_arm=on」の先頭にある「#」を削除します。

pi@raspberrypi:~ $ sudo vi /boot/config.txt
pi@raspberrypi:~ $ cat /boot/config.txt
# For more options and information see
# http://rpf.io/configtxtreadme
# Some settings may impact device functionality. See link above for details

# uncomment if you get no picture on HDMI for a default "safe" mode
#hdmi_safe=1

# uncomment this if your display has a black border of unused pixels visible
# and your display can output without overscan
#disable_overscan=1

# uncomment the following to adjust overscan. Use positive numbers if console
# goes off screen, and negative if there is too much border
#overscan_left=16
#overscan_right=16
#overscan_top=16
#overscan_bottom=16

# uncomment to force a console size. By default it will be display's size minus
# overscan.
#framebuffer_width=1280
#framebuffer_height=720

# uncomment if hdmi display is not detected and composite is being output
#hdmi_force_hotplug=1

# uncomment to force a specific HDMI mode (this will force VGA)
#hdmi_group=1
#hdmi_mode=1

# uncomment to force a HDMI mode rather than DVI. This can make audio work in
# DMT (computer monitor) modes
#hdmi_drive=2

# uncomment to increase signal to HDMI, if you have interference, blanking, or
# no display
#config_hdmi_boost=4

# uncomment for composite PAL
#sdtv_mode=2

#uncomment to overclock the arm. 700 MHz is the default.
#arm_freq=800

# Uncomment some or all of these to enable the optional hardware interfaces
dtparam=i2c_arm=on <--先頭の「#」を削除
#dtparam=i2s=on
dtparam=spi=on

# Uncomment this to enable the lirc-rpi module
#dtoverlay=lirc-rpi

# Additional overlays and parameters are documented /boot/overlays/README

# Enable audio (loads snd_bcm2835)
dtparam=audio=on

# NOOBS Auto-generated Settings:
hdmi_force_hotplug=1

# for W1-GPIO
dtoverlay=w1-gpio-pullup,gpiopin=17

# for DHG11
dtoverlay=dht11

 

 また、光センサーBH1750FVI(GY-30)ではI2C通信を使うので、

/etc/modulesに「i2c-bcm2708」と「i2c-dev」の2つを追加しRasbianを再起動しておきます。

pi@raspberrypi:~ $ sudo vi /etc/modules
pi@raspberrypi:~ $ cat /etc/modules
# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.

i2c-bcm2708 <----追記
i2c-dev <----追記
w1-gpio
w1-therm
pi@raspberrypi:~ $ sudo reboot

 

 再起動後、使用する「i2c-tools」・「python-smbus」がインストールされているか確認します。

※インストールされていないようなら、apt・apt-getなどでインストールします。

※その他、pythonやpipなど必要なものはインストールします。

pi@raspberrypi:~ $ dpkg -l | grep i2c-tools
ii i2c-tools 3.1.1+svn-2 armhf heterogeneous set of I2C tools for Linux
pi@raspberrypi:~ $ dpkg -l | grep python-smbus
ii python-smbus 3.1.1+svn-2 armhf Python bindings for Linux SMBus access through i2c-dev

 

 続いて、接続されている光センサーBH1750FVI(GY-30)が使用されているI2Cのアドレスを調べます。

以下では、0x23となっていることがわかります。

※「-y」で指定している値は今回使用しているRaspberry PI zero wだと1でしたが、他のものでは0とするなど違いがあるようです。

pi@raspberrypi:~ $ sudo i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- 23 -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

 

 

 測定用のPythonプログラム

 BH1750FVI(GY-30)は、以下のデータシートにあるようにいくつかのモードがあるので、今回は0x10/0x11の2つを使っています。

https://www.mouser.com/ds/2/348/bh1750fvi-e-186247.pdf

pi@raspberrypi:~ $ cat /home/pi/bh1750fvi.py
#!/usr/bin/python3

import smbus

Bus = smbus.SMBus(1)
Addr = 0x23
LxRead = Bus.read_i2c_block_data(Addr,0x11)
print("照度: "+str(LxRead[1]* 10)+" ルクス")
LxRead2 = Bus.read_i2c_block_data(Addr,0x10)
print("輝度: " + str*1

以下にもアップしてあります。

RaspberryPi/bh1750fvi.py at master · kujiraitakahiro/RaspberryPi · GitHub

 

プログラム実行してみると、計測ができます。

部屋の電気を付けたり消したりすることで、測定値が変化します。

pi@raspberrypi:~ $ /home/pi/bh1750fvi.py
照度: 10 ルクス <--薄暗い部屋の中
輝度: 0.8333333333333334

pi@raspberrypi:~ $ /home/pi/bh1750fvi.py
照度: 970 ルクス <--デスクライト(蛍光灯)を付けた
輝度: 80.83333333333334

pi@raspberrypi:~ $ /home/pi/bh1750fvi.py
照度: 2180 ルクス <--部屋の電気を付けた
輝度: 181.66666666666669

pi@raspberrypi:~ $ /home/pi/bh1750fvi.py
照度: 0 ルクス <--部屋を真っ暗にした
輝度: 0.0

 

 

 おまけ

しかし、太陽の光って明るいっすねw

f:id:opensourcetech:20190402075001j:plain

pi@raspberrypi:~ $ /home/pi/bh1750fvi.py
照度: 2450 ルクス
輝度: 17270.833333333336

 

 


 

 

 

 

www.slideshare.net

github.com

www.facebook.com

twitter.com

www.instagram.com

 

 

にほんブログ村 IT技術ブログ Linuxへ
Linux

にほんブログ村 IT技術ブログ オープンソースへ
オープンソース

 

 

*1:LxRead2[0] * 256 + LxRead2[1]) / 1.2

Opensourcetech by Takahiro Kujirai