首页 > 技术文章 > Arduino+0.96OLED+GY30(BH1750)光照强度采集光照传感器 光控灯实验

icloudelectron 2020-06-18 14:35 原文

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

推荐阅读