This example shows how to configure I2C bus and read data from BH1750(light intensity sensor) by POSIX APIs, this decreases dependence on hardware and platform.
Before project configuration and build, be sure to set the correct chip target using idf.py set-target <chip_name>
.
Open the project configuration menu (idf.py menuconfig
).
In the Example Configuration
menu:
I2C Testing Count of Reading Sensor's Data
option.Run idf.py -p PORT flash monitor
to build, flash and monitor the project.
(To exit the serial monitor, type Ctrl-]
.)
As you run the example, you will see the light intensity value from following log:
Plaintext
I (0) cpu_start: Starting scheduler on APP CPU.
I (328) ext_vfs: Extended VFS version: 0.1.0
Opening device /dev/i2c/0 for writing OK, fd=3.
Sensor val: 190.00 [Lux].
Sensor val: 173.33 [Lux].
Sensor val: 173.33 [Lux].
Sensor val: 173.33 [Lux].
Sensor val: 173.33 [Lux].
Sensor val: 176.67 [Lux].
Sensor val: 180.00 [Lux].
Sensor val: 186.67 [Lux].
Sensor val: 190.00 [Lux].
Sensor val: 193.33 [Lux].
Close device OK.
To create a project from this example, run:
idf.py create-project-from-example "espressif/extended_vfs=0.3.0:i2c_bh1750"