Arduinoに照度センサをつないでみる [Arduino]
手元のジャンクにI2C接続の照度センサがあったのでArduinoにつないでみた.
照度センサは,ROHMのBH1751FVI.I2Cで制御するタイプだ.
で,ArduinoとはIOの電圧が違うので直結できない.なので,秋月のI2Cバス用双方向電圧レベル変換モジュール(PCA9306)を経由して接続してやる.
こんな感じ.
間にI2Cのレベル変換を挟んでいるだけで,基本的には素直に接続なので悩むことはないと思う.
で,Arduino側のスケッチはこんな感じ.
簡単すぎる.これでシリアルに照度値が出力されるようになる.
Arduinoってなにかちょっと試すのにホント便利だなぁ.
照度センサは,ROHMのBH1751FVI.I2Cで制御するタイプだ.
で,ArduinoとはIOの電圧が違うので直結できない.なので,秋月のI2Cバス用双方向電圧レベル変換モジュール(PCA9306)を経由して接続してやる.
こんな感じ.
間にI2Cのレベル変換を挟んでいるだけで,基本的には素直に接続なので悩むことはないと思う.
で,Arduino側のスケッチはこんな感じ.
#include <Wire.h> const int SensorAddress = 0x23; const int CommandHResolutionSingle = 0x20; const int ReadDataNum = 2; void setup() { Wire.begin(); Serial.begin(9600); } void loop() { Wire.beginTransmission(SensorAddress); Wire.write(CommandHResolutionSingle); Wire.endTransmission(); Wire.requestFrom(SensorAddress, ReadDataNum); while (Wire.available() < ReadDataNum); unsigned int h = Wire.read(); unsigned int l = Wire.read(); double lx = convertSensorDataToLx(h, l); Serial.println(lx); delay(500); } double convertSensorDataToLx(unsigned int high, unsigned int lo) { double result = (high * 256u + lo) / 1.2; return result; }
簡単すぎる.これでシリアルに照度値が出力されるようになる.
Arduinoってなにかちょっと試すのにホント便利だなぁ.