Detecting Colors with the Nano 33 BLE Sense Board

Learn how to visualize the dominant primary colour of an object using the built in RGB sensor on the Nano 33 BLE Sense

In this tutorial we will use an Arduino Nano 33 BLE Sense board to measure the main component of the light projected from an item in our environment, using the on-board APDS9960 sensor. This will turn on the red, green or blue on-board LEDs depending on what color the item close to the sensor is..

Goals

The goals of this project are:

  • Learn how the APDS9960 light sensor works.
  • Learn how to use the APDS9960 library.
  • Learn how to read specific colors and simulate them with the RGB LED embedded on the Nano 33 BLE Sense board.
  • Learn how to control the on-board RGB LED by using the data collected from a sensor.

Hardware & Software Needed

  • Arduino Nano 33 BLE Sense.
  • This project uses no external sensors or components.
  • In this tutorial we will use the Arduino Web Editor to program the board.

The APDS9960 Sensor

The APDS9960 sensor.
The APDS9960 sensor.

The APDS9960 sensor is a multipurpose device that features advanced Gesture detection, Proximity detection, Digital Ambient Light Sense (ALS) and Color Sense (RGBC). A vastly popular application of the APDS9960 sensor is in smartphones, where it is used to disable the screen when the user places the phone on their ear.

If you want to read more about the APDS9960 sensor module see here.

Color Sensor

How color sensing works.
How color sensing works.

White light is actually made of all of the colours of the rainbow because it contains all wavelengths, and it is described as polychromatic light. The color sensor is separated in four different channels (Red, Green, Blue and Clear light intensity). Each of them have a UV and IR blocking filter and a dedicated data converter to read the data simultaneously.

It features:

  • Four different channels to different color data
  • Ability to produce 16-bit data simultaneously
  • Accurate measurement of the ambient light and sense color

Creating the Program

1. Setting up

Let's start by opening the Arduino Web Editor, click on the Libraries tab, search for the APDS9960 library, then in Examples, open the ColorSensor example. Once the sketch is open, rename it as Light_composition.

Finding the library in the Web Editor.
Finding the library in the Web Editor.

2. Connecting the board

Now, connect the Arduino Nano 33 BLE Sense to the computer to check that the Web Editor recognizes it, if so, the board and port should appear as shown in the image. If they don't appear, follow the instructions to install the plugin that will allow the Editor to recognize your board.

Selecting the board.
Selecting the board.

3. Code walkthrough

First, we need to include the APDS9960 library that will allow us to control the sensor. To do so, we need to add the following portion of code before the

setup()
function.

1#include <Arduino_APDS9960.h>

We will keep the

setup()
section as it is, on it we have the
ADPS.begin()
within an
if
statement. This initializes the color sensor and will print a message in the Serial Monitor in case the sensor has not been successfully initialized. This string can be any message of your choice.

1void setup() {
2 Serial.begin(9600);
3 while (!Serial);
4
5 if (!APDS.begin()) {
6 Serial.println("Error initializing APDS9960 sensor.");
7 }
8}

In the

loop()
, we use the
colorAvailable()
function that checks if the sensor has detected any color data to read. Then, we will store the color data in the
r
,
g
and
b
variables using the
APDS.readColor()
function. After the
ADPS.readColor()
function, we need to add some
if...else
statements to know what is the dominant primary color and
digitalWrite()
functions to turn on the RGB LED based on it.

1if (r > g & r > b)
2 {
3 digitalWrite(LEDR, LOW);
4 digitalWrite(LEDG, HIGH);
5 digitalWrite(LEDB, HIGH);
6 }
7 else if (g > r & g > b)
8 {
9 digitalWrite(LEDG, LOW);
10 digitalWrite(LEDR, HIGH);
11 digitalWrite(LEDB, HIGH);
12 }
13 else if (b > g & b > r)
14 {
15 digitalWrite(LEDB, LOW);
16 digitalWrite(LEDR, HIGH);
17 digitalWrite(LEDG, HIGH);
18 }
19 else
20 {
21 digitalWrite(LEDR, HIGH);
22 digitalWrite(LEDG, HIGH);
23 digitalWrite(LEDB, HIGH);
24 }

Lastly, we use the

Serial.print()
and
Serial.println
functions to print out the color measured to the Serial Monitor.

4. Complete code

If you choose to skip the code building section, the complete code can be found below:

1/*
2 APDS9960 - Color Sensor
3
4 This example reads Color data from the on-board APDS9960 sensor of the
5 Nano 33 BLE Sense and prints the color RGB (red, green, blue) values
6 to the Serial Monitor once a second.
7
8 The circuit:
9 - Arduino Nano 33 BLE Sense
10
11 This example code is in the public domain.
12*/
13
14#include <Arduino_APDS9960.h>
15
16void setup() {
17 Serial.begin(9600);
18 while (!Serial);
19
20 if (!APDS.begin()) {
21 Serial.println("Error initializing APDS9960 sensor.");
22 }
23}
24
25void loop() {
26
27 // check if a color reading is available
28 while (! APDS.colorAvailable()) {
29 delay(5);
30 }
31 int r, g, b;
32
33 // read the color
34 APDS.readColor(r, g, b);
35
36 if (r > g & r > b)
37 {
38 digitalWrite(LEDR, LOW);
39 digitalWrite(LEDG, HIGH);
40 digitalWrite(LEDB, HIGH);
41 }
42 else if (g > r & g > b)
43 {
44 digitalWrite(LEDG, LOW);
45 digitalWrite(LEDR, HIGH);
46 digitalWrite(LEDB, HIGH);
47 }
48 else if (b > g & b > r)
49 {
50 digitalWrite(LEDB, LOW);
51 digitalWrite(LEDR, HIGH);
52 digitalWrite(LEDG, HIGH);
53 }
54 else
55 {
56 digitalWrite(LEDR, HIGH);
57 digitalWrite(LEDG, HIGH);
58 digitalWrite(LEDB, HIGH);
59 }
60
61
62 // print the values
63 Serial.print("Red light = ");
64 Serial.println(r);
65 Serial.print("Green light = ");
66 Serial.println(g);
67 Serial.print("Blue light = ");
68 Serial.println(b);
69 Serial.println();
70
71 // wait a bit before reading again
72 delay(500);
73}

Testing It Out

After you have successfully verified and uploaded the sketch to the board, open the Serial Monitor from the menu on the left. You will now see the new values printed as shown in the image below.

Serial Monitor output.
Serial Monitor output.

If you want to test out whether it is working, you could point the board to different objects of different colors to see the variables increase or decrease and the color of the RGB LED changing.

RGB LED matches the item's color.
RGB LED matches the item's color.

Troubleshoot

Sometimes errors occur, if the project is not working as intended there are some common issues we can troubleshoot:

  • Missing a bracket or a semicolon.
  • If your Arduino board is not recognized, check that the Create plugin is running properly on your computer.
  • The room is too dark

Conclusion

In this simple tutorial we learned how to read color values from the APDS9960 sensor using the APDS9960 library, and how to use the sensor embedded in the Arduino Nano 33 BLE Sense board, to measure and print out color values from the environment to the Serial Monitor and transfer those values to the RGB LED to display the dominant primary color.

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.