Arduino+0.96OLED+GY30(BH1750)光照強度采集光照傳感器 光控燈實驗


Arduino+0.96OLED+GY30光照傳感器Module UNO
一、電路連接

VCC <-----> 5V
GND <-----> GND
SCL <-----> A5
SDA <-----> A4
ADD <-----> NC
OLED接線方式:
VCC<————>3.3V
GND<————>GND
SCL<————>SCL
SDA<————>SDA
LED1<————>10
LED2<————>11
二、實驗材料

Uno R3開發板
GY-30光照傳感器
面包板及配套連接線
2個LED燈
1個0.96OLED
三、功能

1、OLED實時顯示光照強度
2、當光照強度大於600LX時關燈
3、當光照強度大於200小於600時打開冷光燈,並隨着光強度變化而變化,強度越大燈光越暗(PWM調節),同時OLED顯示開關狀態;
4、當光照強度小於200時打開曖光燈關閉冷光,並隨着光強度變化而變化,強度越大燈光越暗(PWM調節),同時OLED顯示開關狀態;
5、使用u8glib庫操作OLED屏
6、串口實時發送相應標志數據,可通過ESP8266發送到APP端顯示參數
特點:無需人為干預的自動控制設備。
示例程序

 

/*
Measurement of illuminance using the BH1750FVI sensor module
Connection:
Module        UNO
VCC    <----->    5V
GND    <----->    GND
SCL    <----->    A5
SDA    <----->    A4
ADD    <----->    NC
OLED接線方式:
VCC<————>3.3V
GND<————>GND
SCL<————>SCL
SDA<————>SDA
LED1<————>10
LED2<————>11
LingShun LAB*/
#include "U8glib.h"
#include <Wire.h>
// OLED庫
#define ADDRESS_BH1750FVI 0x23    //ADDR="L" for this module
#define ONE_TIME_H_RESOLUTION_MODE 0x20
//One Time H-Resolution Mode:
//Resolution = 1 lux
//Measurement time (max.) = 180ms
//Power down after each measurement

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);    // I2C
const unsigned char pinMotorCW  = 10;   // 接控制電機順時針轉的 H 橋引腳
const unsigned char pinMotorCCW = 11;   // 接控制電機逆時針轉的 H 橋引腳

byte highByte = 0;
byte lowByte = 0;
unsigned int sensorOut = 0;
unsigned int illuminance = 0;
unsigned int Warm_Empty = 0; //曖光 變量
unsigned int Cold_Empty = 0;//冷光
int ledValue = 0;  //保存LED燈占空比

const   uint8_t bitmap_g []   U8G_PROGMEM  ={  
0x01,0x00,0x21,0x08,0x11,0x08,0x09,0x10,0x09,0x20,0x01,0x00,0xFF,0xFE,0x04,0x40,
0x04,0x40,0x04,0x40,0x04,0x40,0x08,0x42,0x08,0x42,0x10,0x42,0x20,0x3E,0xC0,0x00//"光",0
};   
const   uint8_t bitmap_q []   U8G_PROGMEM  ={  
0x00,0x00,0xF9,0xFC,0x09,0x04,0x09,0x04,0x09,0xFC,0x78,0x20,0x40,0x20,0x43,0xFE,
0x42,0x22,0x7A,0x22,0x0B,0xFE,0x08,0x20,0x08,0x24,0x08,0x22,0x57,0xFE,0x20,0x02//"強",1
};
const   uint8_t bitmap_d []   U8G_PROGMEM  ={  
0x01,0x00,0x00,0x80,0x3F,0xFE,0x22,0x20,0x22,0x20,0x3F,0xFC,0x22,0x20,0x22,0x20,
0x23,0xE0,0x20,0x00,0x2F,0xF0,0x24,0x10,0x42,0x20,0x41,0xC0,0x86,0x30,0x38,0x0E//"度",2
};
const   uint8_t bitmap_x []   U8G_PROGMEM  ={  
0x00,0x00,0x1F,0xF0,0x10,0x10,0x10,0x10,0x1F,0xF0,0x10,0x10,0x10,0x10,0x1F,0xF0,
0x04,0x40,0x44,0x44,0x24,0x44,0x14,0x48,0x14,0x50,0x04,0x40,0xFF,0xFE,0x00,0x00//"顯",3
};
const   uint8_t bitmap_s []   U8G_PROGMEM  ={  
0x00,0x00,0x3F,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFE,0x01,0x00,
0x01,0x00,0x11,0x10,0x11,0x08,0x21,0x04,0x41,0x02,0x81,0x02,0x05,0x00,0x02,0x00//"示",4
};

void draw(void) {
u8g.setColorIndex(1);
 
  u8g.drawBitmapP ( 25 , 0 , 2 , 16 , bitmap_g);
  u8g.drawBitmapP ( 42 , 0 , 2 , 16 , bitmap_q );
  u8g.drawBitmapP ( 59 , 0 , 2 , 16 , bitmap_d );
  u8g.drawBitmapP ( 76 , 0 , 2 , 16 , bitmap_x );
  u8g.drawBitmapP ( 93 , 0 , 2 , 16 , bitmap_s );
  u8g.setFont(u8g_font_8x13); //使用8x13大小的字符
  u8g.setPrintPos(0, 30);
  u8g.print("Beam :");
  u8g.setPrintPos(55, 30);
  u8g.print(illuminance);
  u8g.setPrintPos(90, 30);
  u8g.print("LX");
  //LED的狀態
  u8g.setPrintPos(0, 45);
  u8g.print("Cold :");
  u8g.setPrintPos(55, 45);
  u8g.print(Warm_Empty);
  u8g.setPrintPos(80, 45);
  u8g.print("OFF/ON");
  u8g.setPrintPos(0, 60);
  u8g.print("Warm :");
  u8g.setPrintPos(55, 60);  
  u8g.print(Cold_Empty);
  u8g.setPrintPos(80, 60);
  u8g.print("OFF/ON");
}
void setup() {
    Serial.begin(9600);
    Wire.begin();
    pinMode(pinMotorCW,OUTPUT);//冷燈
    pinMode(pinMotorCCW,OUTPUT);//曖燈

    digitalWrite(pinMotorCW,LOW);
    digitalWrite(pinMotorCCW,LOW);
}
 
void loop() {
    Wire.beginTransmission(ADDRESS_BH1750FVI); //"notify" the matching device
    Wire.write(ONE_TIME_H_RESOLUTION_MODE);     //set operation mode
    Wire.endTransmission();     
    delay(180);
    Wire.requestFrom(ADDRESS_BH1750FVI, 2); //ask Arduino to read back 2 bytes from the sensor
    highByte = Wire.read();  // get the high byte
    lowByte = Wire.read(); // get the low byte
    
    sensorOut = (highByte<<8)|lowByte;
    illuminance = sensorOut/1.2;
    Serial.println("a," + String(illuminance));
    delay(300);
    //Serial.print(illuminance);  
    //Serial.println(" lux");
    
    if(illuminance <= 600){
      if(illuminance >= 200){
        unsigned char i;
        i = map(illuminance, 200, 600, 200, 0); //將200到600之間的數據映射成200到0之間的數據
        motorCW(i);
        Warm_Empty=LOW;
        Cold_Empty=HIGH;
        
      }else{
        unsigned char i;
        i = map(illuminance, 0, 200, 200, 100); //將400到800之間的數據映射成200到100之間的數據
        motorCCW(i);
        Warm_Empty=HIGH;
        Cold_Empty=LOW;
      }
    }else{
      motorStop();
      Warm_Empty =LOW;
      Cold_Empty =LOW;
    }
  Serial.println("b," + String(Warm_Empty));
  Serial.println("c," + String(Cold_Empty));
  u8g.firstPage();  
  do {
    draw();
  } while( u8g.nextPage() );

  delay(200);
}

// 關閉冷燈和曖燈
void motorStop()
{
    digitalWrite(pinMotorCW, LOW);
    digitalWrite(pinMotorCCW, LOW);
}

// 冷燈以參數設定的 pwm 值光強度
void motorCW(unsigned char pwm)
{
    analogWrite(pinMotorCW,   pwm);
    digitalWrite(pinMotorCCW, LOW);
}

// 曖燈以參數設定的 pwm 值光強度
void motorCCW(unsigned char pwm)
{
    digitalWrite(pinMotorCW, LOW);
    analogWrite(pinMotorCCW, pwm);
}

 

https://blog.csdn.net/icloudelectron/article/details/106765637


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM