arduino 4脚/7脚 OLED显示光照传感器

arduino 4脚/7脚 OLED显示光照强度传感器BH1750



前言

使用arduino实现光照传感器的数值显示在OLED显示屏上
光照强度传感器采用BH1750


一、4脚OLED和光照接线

在这里插入图片描述
在这里插入图片描述
光照强度传感器BH1750接线方式:
VCC <-----> 5V
GND <-----> GND
SCL <-----> A5
SDA <-----> A4
ADD <-----> NC
OLED接线方式:
VCC<————>3.3V
GND<————>GND
SCL<————>SCL
SDA<————>SDA

二、使用步骤

1.arduino OLED4脚示例代码

代码如下(示例):

/*
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
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


U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);    
byte highByte = 0;
byte lowByte = 0;
unsigned int sensorOut = 0;
unsigned int illuminance = 0;
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");

} 
void setup() {
    Serial.begin(9600);
    Wire.begin();
}
 
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之间的数据
        
      }else{
        unsigned char i;
        i = map(illuminance, 0, 200, 200, 100); //将400到800之间的数据映射成200到100之间的数据
      }
    }
  Serial.println("b," + String(Warm_Empty));
  Serial.println("c," + String(Cold_Empty));
  u8g.firstPage();  
  do {
    draw();
  } while( u8g.nextPage() );

  delay(200);
}

三.7脚OLED和光照接线

在这里插入图片描述
在这里插入图片描述
光照强度传感器BH1750接线方式:
VCC <-----> 5V
GND <-----> GND
SCL <-----> A5
SDA <-----> A4
ADD <-----> NC
OLED接线方式:
VCC<————>3.3V
GND<————>GND
SCL<————>10
SDA<————>9
CS<————>12
DC<————>11


四、使用步骤

2.arduino OLED7脚示例代码

代码如下(示例):

/*
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<————>10
SDA<————>9
CS<————>12
DC<————>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(10,9,12,11);    // I2C 


byte highByte = 0;
byte lowByte = 0;
unsigned int sensorOut = 0;
unsigned int illuminance = 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");

} 
void setup() {
    Serial.begin(9600);
    Wire.begin();

}
 
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之间的数据
        
      }else{
        unsigned char i;
        i = map(illuminance, 0, 200, 200, 100); //将400到800之间的数据映射成200到100之间的数据

      }
    }
  u8g.firstPage();  
  do {
    draw();
  } while( u8g.nextPage() );

  delay(200);
}


总结

根据自己需求修改文字和OLED用法等方式方法放在下面。
实际OLED使用方法可以参考Arduino - 使用u8glib库操作OLED屏
更改文字可参考取模软件0.96寸OLED显示屏文字取模和图片取模教程

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

♡巴雷特-安琪拉♡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值