آموزش راه اندازی سنسور تشخیص شدت نور BH1750 با آردوینو

فهرست مطالب

ویژگی های سنسور تشخیص شدت نور BH1750

ماژول BH1750 یک برد مجهز به سنسور حساس به شدت نور است که دارای یک مبدل آنالوگ به دیجیتال شانزده بیتی می باشد. این ماژول می تواند مستقیما سیگنال دیجیتال در خروجی ایجاد کند. پروتکل ارتباطی این ماژول رابط I2C می باشد. این ماژول برای تشخیص میزان نور محیط با دقت و رزولوشن بالا مناسب بوده و داده های خروجی آن بصورت lx (لوکس متر) می باشد. همچنین این ماژول به راحتی به وسیله آردوینو قابل راه اندازی است.

دیتاشیت سنسور تشخیص شدت نور BH1750 را می توانید از اینجا دانلود کنید.

معرفی پایه ها (Pinout) سنسور نور BH1750

سنسور BH1750 دارای 5 پایه به شرح زیر است:

  •  VCC: تغذیه ماژول – 3.3 ولت
  •  GND: زمین
  •  SLC: همزمان سازی برای پروتکل I2C
  •  SDA: اطلاعات برای پروتکل I2C
  •  ADDR: تعیین آدرس برای پروتکل I2C

پین اوت (Pinout) این ماژول را می توانید در تصویر زیر مشاهده کنید.

لوازمی که به آن احتیاج دارید

قطعات مورد نیاز

آردوینو UNO R3 × 1
سنسور تشخیص شدت نور BH1750 × 1
سیم جامپر × 1
مبدل منطقی ( لاجیک کانورتر ) دو طرفه × 1

نرم افزارهای مورد نیاز

آردوینو IDE

راه اندازی ماژول تشخیص شدت نور BH1750 با آردوینو

گام اول: سیم بندی

مطابق مدار زیر، ماژول را به آردوینو وصل کنید.

گام دوم: کد

ابتدا کتابخانه زیر را بر آردوینوی خود نصب کنید.

https://github.com/claws/BH1750.git

توجه

 اگر نیاز به راهنمایی بیشتر برای نصب کتابخانه بر روی آردوینو دارید، می توانید به آموزش نصب کتابخانه بر آردوینو مراجعه کنید.

کد زیر را روی برد آردوینوی خود آپلود کنید.

/*
  Advanced BH1750 library usage example
  This example has some comments about advanced usage features.

  Connection:

    VCC -> 3V3 or 5V
    GND -> GND
    SCL -> SCL (A5 on Arduino Uno, Leonardo, etc or 21 on Mega and Due, on esp8266 free selectable)
    SDA -> SDA (A4 on Arduino Uno, Leonardo, etc or 20 on Mega and Due, on esp8266 free selectable)
    ADD -> (not connected) or GND

  ADD pin is used to set sensor I2C address. If it has voltage greater or equal to
  0.7VCC voltage (e.g. you've connected it to VCC) the sensor address will be
  0x5C. In other case (if ADD voltage less than 0.7 * VCC) the sensor address will
  be 0x23 (by default).

*/

#include <Wire.h>
#include <BH1750.h>

/*
  BH1750 can be physically configured to use two I2C addresses:
    - 0x23 (most common) (if ADD pin had < 0.7VCC voltage)
    - 0x5C (if ADD pin had > 0.7VCC voltage)

  Library uses 0x23 address as default, but you can define any other address.
  If you had troubles with default value - try to change it to 0x5C.

*/
BH1750 lightMeter(0x23);

void setup(){

  Serial.begin(9600);

  // Initialize the I2C bus (BH1750 library doesn't do this automatically)
  Wire.begin();
  // On esp8266 you can select SCL and SDA pins using Wire.begin(D4, D3);

  /*

    BH1750 has six different measurement modes. They are divided in two groups;
    continuous and one-time measurements. In continuous mode, sensor continuously
    measures lightness value. In one-time mode the sensor makes only one
    measurement and then goes into Power Down mode.

    Each mode, has three different precisions:

      - Low Resolution Mode - (4 lx precision, 16ms measurement time)
      - High Resolution Mode - (1 lx precision, 120ms measurement time)
      - High Resolution Mode 2 - (0.5 lx precision, 120ms measurement time)

    By default, the library uses Continuous High Resolution Mode, but you can
    set any other mode, by passing it to BH1750.begin() or BH1750.configure()
    functions.

    [!] Remember, if you use One-Time mode, your sensor will go to Power Down
    mode each time, when it completes a measurement and you've read it.

    Full mode list:

      BH1750_CONTINUOUS_LOW_RES_MODE
      BH1750_CONTINUOUS_HIGH_RES_MODE (default)
      BH1750_CONTINUOUS_HIGH_RES_MODE_2

      BH1750_ONE_TIME_LOW_RES_MODE
      BH1750_ONE_TIME_HIGH_RES_MODE
      BH1750_ONE_TIME_HIGH_RES_MODE_2

  */

  // begin returns a boolean that can be used to detect setup problems.
  if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
    Serial.println(F("BH1750 Advanced begin"));
  }
  else {
    Serial.println(F("Error initialising BH1750"));
  }

}


void loop() {

  float lux = lightMeter.readLightLevel();
  Serial.print("Light: ");
  Serial.print(lux);
  Serial.println(" lx");
  delay(1000);

}

پس از اجرای کد، تصویر زیر را در خروجی سریال مشاهده می‌کنید.

آموزش های مشابه

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد.