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显示屏文字取模和图片取模教程