README.md
1# BearPi-HM_Nano开发板传感器驱动开发——E53_SC1读取光照强度
2本示例将演示如何在BearPi-HM_Nano开发板上使用E53_SC1读取温度 、湿度、光照强度,当光照强度过低时,开启补光灯补光,设备安装如下图所示。
3
4
5## E53_SC1 API分析
6本案例主要使用了以下API完成温度 、湿度、光照强度读取。
7### E53SC1Init()
8```C
9int E53SC1Init(void);
10```
11 **描述:**
12
13初始化E53_SC1。
14
15### E53SC1ReadData()
16```C
17int E53SC1ReadData(float *data);
18```
19 **描述:**
20
21读取光照强度。
22
23**参数:**
24
25|参数名|描述|
26|:--|:------|
27| data | data,光照强度数据指针。 |
28
29### LightStatusSet()
30```C
31void LightStatusSet(E53SC1Status status);
32```
33 **描述:**
34
35控制补光灯开关
36
37**参数:**
38
39|参数名|描述|
40|:--|:------|
41| status | ON开,OFF关闭。 |
42
43
44
45## 硬件设计
46本案例将用到 E53_SC1 智慧路灯扩展板与 BearPi-HM_Nano 开发板,其中E53_SC1扩展板原理图如下,光照强度传感器BH1750是通过I2C来驱动,灯是通过GPIO_7来控制。
47
48
49
50
51
52E53_SC1 智慧路灯扩展板与 BearPi-HM_Nano 开发板安装如下图所示。
53
54
55## 软件设计
56
57**主要代码分析**
58
59
60首先调用 `E53SC1Init()` 函数初始化E53_SC1所接的引脚的功能,然后循环调用 `E53SC1ReadData()` 函数读取光照强度并通过串口打印出来,当光照强度过低时,开启灯。
61
62```C
63static void ExampleTask(void)
64{
65 int ret;
66 float Lux;
67
68 ret = E53SC1Init();
69 if (ret != 0) {
70 printf("E53_SC1 Init failed!\r\n");
71 return;
72 }
73
74 while (1) {
75 printf("=======================================\r\n");
76 printf("*************E53_SC1_example***********\r\n");
77 printf("=======================================\r\n");
78 ret = E53SC1ReadData(&Lux);
79 if (ret != 0) {
80 printf("E53_SC1 Read Data failed!\r\n");
81 return;
82 }
83 printf("Lux data:%.2f\r\n", Lux);
84 if (Lux < MIN_LUX) {
85 LightStatusSet(ON);
86 } else {
87 LightStatusSet(OFF);
88 }
89 usleep(TASK_DELAY_1S);
90 }
91}
92```
93
94
95
96## 编译调试
97
98### 修改 BUILD.gn 文件
99修改`device\bearpi\bearpi_hm_nano\app`路径下 BUILD.gn 文件,指定 `e53_sc1_example` 参与编译。
100```r
101#"C1_e53_sf1_mq2:e53_sf1_example",
102#"C2_e53_ia1_temp_humi_pls:e53_ia1_example",
103"C3_e53_sc1_pls:e53_sc1_example",
104#"C4_e53_sc2_axis:e53_sc2_example",
105#"C5_e53_is1_infrared:e53_is1_example",
106```
107
108
109
110
111### 运行结果
112
113示例代码编译烧录代码后,按下开发板的RESET按键,通过串口助手查看日志,会打印光照强度信息。用手遮住扩展板,补光灯会自动开启。
114```c
115=======================================
116*************E53_SC1_example***********
117=======================================
118Lux data:53.33
119=======================================
120*************E53_SC1_example***********
121=======================================
122Lux data:53.33
123```
124
125