In this guide I will show you how to get started with Arduino and an APDS-9930 ambient light and proximity sensor.
You need a few components to follow this tutorial (click the links to see the product pages and buy the parts).
- An APDS-9930 ambient light and proximity sensor breakout board. (The link is correct, don’t mind the wrong description; it is an APDS-9930, not an APDS-9960)
- An Arduino board. You can get the original one, or you can get cheap ones from China, like a Funduino Uno or a Funduino Mega.
- A 5V-3.3V bidirectional logic level shifter.
- A solderless breadboard.
- Some jumper wires.
A quick explaination of what the pins on the board are for.
|VL||Optional power to the IR LED if PS jumper is disconnected. Must be 3.0 – 4.5V|
|VCC||Used to power the sensor. Must be 2.4 – 3.6V.|
|INT||External interrupt. Pulled low by the sensor on interrupt event.|
Make sure you solder the headers to the breakout board and to the level shifter. Putting the headers on the breadboard while soldering might help, as long as you don’t heat them for too long.
Connecting the board
Now you need to make the connections. Plug the level shifter in the middle of the breadboard, and the module on some other lines. We need to make sure the level shifter acts as a medium between the 5V Arduino board and the 3.3V sensor, so that the Arduino can communicate with the sensor and the sensor does not blow up. 😉
Connect the 5V pin of the Arduino board to the 5V pin on the level shifter, and do the same for ground, then connect 3V and GND on the other side of the level shifter to VCC and GND on the sensor. Now use any of the pins labeled “bidirectional lines” and make the following connections, but make sure they go through the shifter!
Note: the SDA and SCL pins are only available on the r3 revision of the boards and are usually marked on the back. If they’re not available, use analog pins 4 and 5.
|Arduino pin||APDS-9930 pin|
|SDA (or A4)||SDA|
|SCL (or A5)||SCL|
Installing and using the library
Open the Arduino IDE, open the Sketch menu, Include library, and hit Add .ZIP library. Select the zip file of the library you just downloaded. The Arduino IDE will automatically install it.
Now open the File menu, go to Examples, APDS-9930 Ambient Light and Proximity Sensor and select the AmbientLightSensor example. Connect the board to the computer using the USB cable, then in the tools menu make sure you have selected the correct board model and serial port. Click Sketch then Upload. The code will be uploaded on the board and will start running, but you won’t notice anything. To get some feedback from the booard, open Tools then Serial Monitor. Make sure the baudrate is set to 9600 bps (the same value used in the sketch). You’ll start seeing some output coming from the sensor. Point a torch to it and see how the values change 🙂
There are more examples bundled with the library. I’ll explain quickly what they do.
- AmbientLightInterrupt – Set up and use an ambient light interrupt
- AmbientLightLED – Turn on and off an LED connected to a PWM pin based on how much light the sensor is reporting
- AmbientLightToneAC – Emit a tone through a piezo buzzer (using the toneAC library) based on how much light the sensor is reporting
- ProximityInterrupt – Set up and use a proximity interrupt
- ProximityLED – Turn on and off an LED connected to a PWM pin based on how far is an object from the sensor
- ProximitySensor – Prints out the values from the proximity sensor to the serial monitor
Check the the APDS9930.cpp to see all the available functions. They’re all well documented inline.
Thanks for reading, I hope you found it useful. Let me know your thoughts in the comments below! 🙂