Hardware Reference
In-Depth Information
43
44 float UVindex = uv.readUV();
45 // the index is multiplied by 100 so to get the
46 // integer index, divide by 100!
47 UVindex /= 100.0;
48 Serial.print("UV: "); Serial.println(UVindex);
49
50 delay(1000);
51 }
Now, it's time to have a closer look at that sketch. On lines 1 to 16, the author
begins with a comment. This is a general explanation of the example, what
component it is used for, and some licensing information for the software. The
BSD license allows you to use the source code for your projects. You can use
this library as long as you credit the original author and agree not to take legal
action against them if it does not work as expected.
On line 18, the Wire library is imported. This is used to communicate through
the I 2 C protocol, and this is how the Si1145 communicates. On line 19, the Adafruit
SI1145 library is imported.
On line 21, an Adafruit_SI1145 object is created called uv . This is the object
that will be used to access the sensor's information.
On line 23, setup() is declared. Like most test sketches, it opens up the serial
port to allow for simple debugging. On line 28, begin() is called to the uv object.
Ty p i c a l ly, begin() functions are called to initialize hardware pins, to set voltages
to a required state, or to send coni guration data to microchips. The Si1145 is an
I 2 C device, so there is no need to coni gure the I 2 C bus; it is done via the Wire
library. It has a i xed address, so there is no coni guration required. It does not
require any external pins so that isn't done either. What it does require is a lot
of parameters to be sent to the device for it to function correctly. This is what
begin() does. For this library, it also detects if the device is present, a nice addi-
tion. It is all too easy to incorrectly connect a device. The function returns true
if the sensor is present, making sure that you have set everything up correctly
before proceeding with the rest of the sketch.
On line 36, loop() is declared, and this is where the fun begins. Several func-
tions are called: readVisible() on line 38, readIR() on line 39, and readUV()
on line 44. The readVisible() function returns the current ambient light level,
and readIR() returns the current infrared light level. Adafruit's Si1145 breakout
board does not come with an IR LED, but it has a connector if you want to use
one. For those who do, another function is available (but commented out in the
example): readProx() on line 42.
This is an example of a well-designed library; one that is easy to import includes
board detection in the begin() function, and works well with a fantastic piece
of hardware. The Si1145 is an excellent sensor, and Adafruit has worked hard
to create a good breakout board and a great library to go with it.
Search WWH ::




Custom Search