Light Projects

A collection LED control examples for Arduino

Low-voltage DC Lamps
LED Strip Control
Addressable LEDs
  NeoPixel Library Quickstart
  Making Electronic Candles
Color Spaces
Light Rendering Indices
Pattern Making

This project is maintained by tigoe

Building Your Own Spectrometer

It’s common in any lighting practice to need a spectrometer to measure the properties of light. Good spectrometers are expensive, though. For an example, see this page on the Sekonic C-800-U. Fortunately, there are some affordable spectral sensors that you can connect to a microcontroller to make your own spectrometer. This tutorial shows you how to connect the AMS AS7341 11-channel spectral sensor to an Arduino Nano 33 IoT. The AS7341 will work with any microcontroller that has an I2C interface, but the Nano 33 IoT has a Bluetooth and WiFi radio, making it easy to connect to via multiple means.

Below, you’ll see how to get output from your spectrometer via multiple means:

The AS7341 Sensor

The AS7341 is an 11-channel spectral sensor that can detect light levels in multiple frequencies from around 400nm to 900nm. It has 8 that sense light in visible spectrum; one channel in the near infrared spectrum; one clear channel without a filter; and one channel that detects 50Hz-60Hz light flicker.

Here is the product page and the datasheet.

The frequencies it can detect are as follows:

The following companies make breakout boards for it:

The following companies make Arduino libraries for it:

The Circuit

The circuit for all of the applications below is the same. These diagrams show Adafruit’s AS7341 breakout boards, but any breakout board for the AS7341 will work. The AS7341 connects to the Nano 33 IoT via I2C. For the Arduino Uno or Nano boards, the I2C pins are pins A4 (SDA) and A5 (SCL). The sensor’s SDA pin connects to the Arduino’s SDA pin and the SCL pin connects to the Arduino’s SCL pin. The sensor’s voltage and ground pins connect to the microcontroller’s power and ground pins as well. This is the same connection for almost any I2C sensor. Figures 1 and 2 below shows the connections.

Breadboard view of an AMS AS7341 sensor connected to a Nano 33 IoT

Figure 1. Breadboard view of an AMS AS7341 sensor connected to a Nano 33 IoT. The Nano’s SDA pin (physical pin 8) is connected to the sensor’s SDA pin (physical pin 4) and the Nano’s SCL pin (physical pin 9) is connected to the sensor’s SCL (physical pin 3). The sensor’s Vdd (physical pin 1) is connected to the Nano’s 3V3 pin (physical pin 2) and the sensor’s ground (physical pin 2) is connected to the Nano’s ground (physical pin 14). Image created using Fritzing.

Schematic view of an AMS AS7341 sensor connected to a Nano 33 IoT

Figure 1. Schematic view of an AMS AS7341 sensor connected to a Nano 33 IoT. The connections are as described above: SDA to SDA, SCL to SCL, voltage and ground to voltage and ground, respectively. Image created using Fritzing.

Serial to p5.js Client

The simplest way to see the readings from the AS7341 is by printing the values out from the Nano’s serial port (UART). This example does just that. It prints the sensor readings, converts them to basic counts, applies a daylight correction, and sends the values out to the serial monitor. For an explanation of the process, see the Spectral Sensor Calibration Methods application note, section 2.1 - 2.5.

TO DO: The correction math in these examples is crude, and simply applies the numbers in Fig. 10 of the application note with no further math. I haven’t yet verified its accuracy.

This browser-based client reads and visualizes the data from serial port in this repository. It uses p5.js, chart.js and the p5.serialport library and p5SerialControl app. Figure 3 shows a screenshot. This is similar to the appearance of the next few client apps below.

screenshot of the serial-to-chartjs client Figure 3. Screenshot of the Serial client in a browser. Ten colored bands representing the sensor’s frequencies are visible in a chart in the browser window.

WebSerial to p5.js Client

The WebSerial to p5.js Client uses the same chart.js display code as the previous example, but uses webSerial instead of p5.serialport. It does not require the p5.serialcontrol app. It will work in the Chrome and Edge and Opera browsers, but not Safari or Firefox as of this writing. It’s otherwise the same as the previous client, but useful to compare to see the difference between webSerial and p5.serialport.

WiFi and MQTT Client

If you’ve never connected to WiFi using the Nano 33 IoT, you should visit the WiFiNINA reference and this repository.

The Message Queueing Telemetry Transfer (MQTT) protocol was made for sensor monitoring over IP networks, so it’s a good choice for a WiFi-based system for the spectrometer. MQTT is a publish-and-subscribe or PubSub model, in which clients connect to a broker application and subscribe to different topics. When they have new data, they publish to the topics. Topics can be broken up into subtopics when useful as well. There is very little overhead to the protocol; you just subscribe, then either publish or wait for messages. For more on MQTT, see this repository, or this definition from IBM, who developed the protocol.

There are several free online brokers that you can use. The most popular is called mosquitto. You can run mosquitto on your own computer, or you can use as a test broker. The instructions and port numbers for the various ways of reaching it are listed on the site. is another MQTT test broker, with a graphic interface so you can see a graph of clients and topics. is another MQTT test broker with a fairly bare-bones set of documentation and no visualizer.

Once you have a broker set up, you enter your credentials, log in, subscribe, and start sending messages to publish. You can send messages in any format: plain text, CSV, JSON, or whatever suits your needs.

The criteria you need to log into any broker are:

Each broker will publish their details for login on their website.

This MQTT example for the Nano 33 IoT publishes to using the topic spectrometer. It uses MQTTS, the encrypted version of the protocol, on port 8883. You can modify it to communicate with other brokers by changing the credentials. It uses the same sensor process as the example above: it prints the sensor readings, converts them to basic counts, applies a daylight correction, and sends the values out via MQTT. It prints them serially as well.

This browser-based client uses the Eclipse PAHO library, p5.js, and chart.js like the serial client above.

Bluetooth LE Client using p5ble.js

The Arduino Nano 33 IOT, MKR WiFi 1010, and Nano 33 BLE and BLE Sense boards can all send data via Bluetooth LE, using the ArduinoBLE library. To complement this, p5ble.js is a great library for enabling BLE connections from the Chrome browser. The browser-based BLE client in this repo is based on the same chart.js code for visualizing the data as the previous two. It uses p5.js, chart.js and the p5.ble library.

Unfortunately this sketch does not work on every browser, particularly on mobile platforms. However, it has been tested successfully on Chrome on MacOS and Windows; Chrome on Android, and WebBLE on iOS.