Links

BH1721 with Arduino Uno

First, log in to your Simuli account and navigate to the Simuli Virtual Lab. Here we will create a new emulated instance of the Arduino Uno. Click on the Launch button under the Arduino to get started.
This will open the configuration menu. First, provide a name for the project. Then, we will add the BH1721 Ambient Light Sensor by selecting it from the list of available components. Finally, review that the name for the project and the selected sensors are correct and click on the Launch button.
Once we have clicked on the Launch button, a new instance of the Arduino Uno will be created. It can take a few minutes, so be patient.
If you have not set up your Arduino IDE, follow the guide linked below.
Once you have the Arduino IDE ready to go, copy the code given below and paste it in the IDE.
#include <Wire.h>
//Driver code for the BH1721
class BH1721 {
public:
enum measMode {
measModeAR,
measModeHR,
measModeLR,
};
bool begin() {
Wire.begin();
return true;
}
bool setMeasTime(int mt) {
if ((mt < MT_MIN) || (mt > MT_MAX)) {
return false;
}
return (sendCmd(CMD_MT_HIGH | (mt >> 5)) && sendCmd(CMD_MT_LOW | (mt & 0x10)));
}
bool startMeasure(enum measMode mode) {
byte cmd;
switch (mode) {
case measModeAR:
cmd = CMD_AR_MODE;
break;
case measModeHR:
cmd = CMD_HR_MODE;
break;
case measModeLR:
cmd = CMD_LR_MODE;
break;
default:
return false;
}
return sendCmd(cmd);
}
bool readLux(float *lux) {
if (Wire.requestFrom(I2C_ADDR, 2) != 2) {
return false;
}
byte msb = Wire.read();
byte lsb = Wire.read();
*lux = (msb * 0x100U | lsb) / 1.2;
return true;
}
private:
bool sendCmd(byte cmd) {
Wire.beginTransmission(I2C_ADDR);
int size = Wire.write(cmd);
if ((Wire.endTransmission() != 0) || (size != 1)) {
return false;
}
return true;
}
static const byte CMD_POWER_DOWN = 0x00;
static const byte CMD_POWER_ON = 0x01;
static const byte CMD_AR_MODE = 0x10;
static const byte CMD_HR_MODE = 0x12;
static const byte CMD_LR_MODE = 0x13;
static const byte CMD_MT_HIGH = 0x40;
static const byte CMD_MT_LOW = 0x60;
static const int MT_MIN = 140;
static const int MT_MAX = 1020;
static const int I2C_ADDR = 0x23;
};
BH1721 bh1721;
void setup() {
Serial.begin(115200);
bh1721.begin();
if (!bh1721.startMeasure(BH1721::measModeAR)) {
Serial.println("Cannot start measurements");
while (true);
}
}
void loop() {
float lux;
delay(1000);
if (bh1721.readLux(&lux)) {
Serial.println("Luminosity: " + String(lux) + " lux");
}
else {
Serial.println("Cannot measure luminosity!");
}
}
Now save the sketch in a folder and keep a note of that folder. We now need to get the compiled binaries for the sketch. Go to Sketch and find Export Compiled Binary. The program will take some time to compile. Once the compilation is complete, go back to IoTIFY Virtual Lab and open the instance we created.
A new tab will open and we can interact with our Arduino. First, we need to uploadour compiled binary. Click on the Arduino, this will open a file explorer, navigate to where you had stored the Arduino sketch. In the same folder, a file called sketch.ino.standard.hex will be present (where sketch is the name of the Arduino sketch). Select this file and click on open. Now the hex file will be uploaded. Finally, reset the Arduino so that it loads the new file. To reset, just click the red power button above the top left corner of the Arduino. Now you can see the reading of the BH1721 ambient light sensor in the terminal. If you change the luminosity value on the sensor, it will also be reflected in the terminal.
Now that you know how to get the readings from the BH1721 sensor, you could try to make an automatic LED, which would turn on when the Luminosity decreases below a certain threshold. You need an if statement to compare the current luminosity value with the threshold and then switch on the LED if the condition is satisfied. Keep building and have fun! 🎉