A few weeks ago I bought an AS7262 breakout board from SparkFun just for kicks. I don’t have much experience with spectroscopy but I decided to get one because it seems fairly novel and it has a serial command set. This page documents my attempt to use the sensor to identify the color of gummy bears.
It isn’t necessary to use SparkFun’s breakout board. From a quick Mouser search there are at least five AS7262 development boards, including one from AMS.
Once you’re able to talk to the sensor you’ll need some way to test it. I listed the items I used for testing under “Optional” since there are many ways to test a sensor of this type.
- SparkFun Spectral Sensor Breakout – AS7262 Visible
- 3.3V Serial UART to USB Converter
- Laptop or PC (I used Windows 7)
- Arduino software or other program capable of serial communication (PuTTY, Tera Term, HyperTerminal, etc.)
Optional testing material
- PVC pipe – 1 1/4 inch, Sch 80, white (a few inches)
- PVC coupling – 1 1/4 inch diameter, Sch 80, white (2)
- PVC plug – 1 1/4 inch, white (2)
- Gummy bears – green, yellow, pineapple, orange, red
To use serial UART the first thing you have to do is configure the solder jumpers on the breakout board (JP1 and JP2). The state of these two jumpers determines whether the board talks I2C or serial UART. SparkFun’s breakout board ships already configured for I2C.
JP1 is on the top side of the board. To remove the solder, gently slide a hot soldering iron over the pads 1-3 times. The three jumper pads have solder mask between them to prevent shorting, so it will not be necessary to remove all of the solder – just most of it.
Removing the solder from the three JP1 pads disconnects the I2C pullup resistors from the circuit.
Next you need to add solder to JP2 (short the two pads together).
This will pull pin 8 of the AS7262 low to select serial UART.
Pin 8 is a selector pin that lets the chip know whether you want I2C or serial UART.
Solder a header to J2
You will also need to solder a header to J2 (the serial interface).
You can solder the other header too but it will not be needed for serial UART. Now plug the UART to USB converter into a USB port on your computer and allow the drivers to install. Then connect the other end of the cable to the AS7262 breakout board, making sure the black wire aligns with the GND pin on the breakout board.
The UART to USB converter supplies power to the board, so no other wiring is required. On my board the blue LED turns on for about 1 second as the AS7262 powers up.
Follow these steps to start sending AT commands:
- To open the Windows device manager, hit the Windows key, type “device manager,” and then press Enter
- Expand the list named “Ports (COM & LPT)”
- Unplug the UART to USB converter from the USB port, wait 5 seconds, and then plug it back in.
- Note the name of the COM port that pops up in the list. For example, my UART to USB converter is on COM6.
- Launch the Arduino software
- Under Tools > Port, select your UART to USB converter’s COM port
- For example, mine is COM6
- Click the “Serial Monitor” button
- Ensure “Carriage return” and “115200 baud” are selected at the bottom of the serial monitor
- Type ATDATA into the serial monitor and press Enter.
- The sensor should reply with six comma-separated 16-bit integers, a space, and the word “OK”
- The other AT commands can be found on page 32 of the AT7262 datasheet.
- For example, to turn on the blue LED (LED0), send ATLED0=100. To turn it off send ATLED0=0.
- To turn on the white LED, send ATLED1=100. To turn it off, send ATLED1=0.
- To read the temperature of the chip in degrees Celsius, send ATTEMP.
- Here is a scope trace of the ATDATA command:
- The cluster of red bits on the left is the Arduino serial monitor sending the command ATDATA. The cluster of blue data on the right represents the first several bits of sensor’s reply. The vertical dashed lines are the scope’s cursors. You can see that about 2.8 mS passes between when the serial monitor sends the last bit of the ATDATA command and when it receives the first bit of the sensor reply (the red channel is on Tx, the blue channel is on Rx).
Basic visible spectroscopy
White light is a combination of lights of different wavelengths from the visible portion of the electromagnetic spectrum (400 nm to around 700 nm). Passing a beam of white light through a prism separates it into several colors of light.
Humans perceive light to be white when all visible wavelengths of light are present. When white light illuminates a room it interacts with the various objects in the room. Some of the light is reflected by the objects and some of the light is absorbed by the objects. A red object, for example, tends to reflect red light and tends to absorb light of other wavelengths. Only the reflected red light enters our eyes – and that is why we perceive the object as red. White objects appear white because they reflect all wavelengths of light, and black objects appear black because they absorb all wavelengths of light.
The datasheet doesn’t provide much detail about the internal operation of the sensor, but if you’re interested in my take please continue reading. The AS7262 appears to be intended primarily for applications involving color identification. It has an aperture or small hole to control the amount of light entering the sensor array.
Light reflects off the surface of some object, enters the chip through the aperture, and then interacts with the sensor array.
The sensor then performs a spectral analysis on the light, not unlike a prism. The sensor array is an array of six photo diodes.
Just for fun I decapsulated an extra AS7262 and took a picture of it under magnification. It might be my imagination, but I believe each photo diode is the expected color!
To me a photo diode is just a light transducer – a device that converts light into an electric current – where the output current is proportional to the amount of light the photo diode absorbs. Any light that enters the aperture is absorbed or not absorbed by the six photo diodes – and the sensor then has six currents that it can analyze accordingly.
The diode array is made of standard CMOS silicon, which puzzled me at first because according to this Wikipedia article, silicon normally absorbs electromagnetic energy with wavelengths between 190 nm and 1100 nm. If this is true, how does the sensor distinguish between, for example, red light and green light? Well, it might have something to do with the “nano-optic deposited interference filter technology” that is integrated onto each of the six diodes. What this means to me is that each photo diode in the array probably has what is essentially an optical band pass filter sitting on top of it. (Perhaps the colored squares on the decapsulated AS7262?) The red photo diode, for example, has to have an optical filter that allows red light to pass and that blocks other types of light. That way when the red photo diode converts light into current, the sensor can assume that this current corresponds to some amount of red light on the surface of the red photo diode. The current is then mathematically integrated to obtain charge, from which surface energy can be estimated.
The sensor has a 6-channel output, where each channel corresponds to some wavelength of light, and each channel has a 40 nm full-width half-max. The datatype of each channel is 16 bit unsigned integer, so the minimum value of any given channel is 0 and the maximum value is 216 – 1 = 65535. Units are in counts/(μW/cm2).
From page 13 the aperture limits the field of view to ±20 degrees (to me this means the chip designers want the angle of incidence of light entering the chip to be less than 20 degrees – otherwise the stated accuracy in the datasheet will change).
I admit I struggled to find a way to test this sensor. An Internet search turned up this Instructables page from about a year ago, but I could find no other content besides sales and marketing fluff.
At first I thought about trying to pass a beam of white light through a cuvette filled with water and food coloring. You could do a baseline reading on a cuvette filled with clear water, and then take a bunch of readings with colored water for comparison. But I decided against this since it would require mechanical fixturing that I didn’t want to deal with.
I also considered using a visible emitter or LED. The advantage to this approach is that I would be able to choose from a number of light sources that can output an exact peak wavelength. For example, this light source outputs light with a peak wavelength of 650 nm, which would be useful for testing the 650 nm channel. However when I tried to implement this I realized it would be a lot of work to test multiple channels this way and some of the emitters are fairly expensive. I may still test one or two channels this way. Do off-the-shelf variable wavelength light sources exist?
Then one day I started messing around with some extra PVC pipe I had lying around. I realized that if I cut the end off of a 1 1/4 inch pipe plug and inserted it into a 1 1/4 inch coupler that the resulting thing could hold the SparkFun breakout board.
So I hot glued the back of the board to the pipe to hold it in place and routed the UART to USB converter through what was left of the end cap.
Then I plugged the cable back into the board and plugged one end of the coupler with the end cap.
I also made an end cap for the other end using a second coupler and plug. This end cap is what I used to hold the test samples.
After I plugged the end cap in I took a baseline reading with the white LED on and no test sample in the end cap. My plan was to then add a single test sample (a red gummy bear) so that I could attribute any change in sensor reading to the addition of the test sample. However, when I took a reading with the red gummy bear in the end cap, the change I noticed seemed too drastic to be attributed to one gummy bear. I then decided to remove the gummy bear from the end cap so I could take a second baseline reading – again with the white LED on and no test sample in the end cap – and I noticed that the second baseline was much different than the first! My confidence in this approach was pretty shaken and I almost abandoned the idea. Why would the second baseline reading be different than the first? I was sure the position of the sensor had not shifted and I was using the same end cap. It turns out the second reading was different because, even though I used the same end cap for both readings, the end cap was not in the same position for both readings. It must be that the position of the end cap matters because the interior surface is not perfectly smooth.
I fixed this by marking the position of the end cap with a Sharpie. I found that as long as I always rotated the end cap to the same position, my baseline reading was repeatable.
Sanity testing the sensor
To get a sense of how I might be able to use the sensor to identify the color of a small object, I started by getting a baseline reading with the white LED on and no test sample in the end cap. The sensor settings were as follows:
- Mode: 2
- Gain: 3
- Integration time: 255
I took 30 samples (at 4 samples/sec) and then calculated a mean value for each channel.
|VIO (450 nm)||BLU (500 nm)||GRN (550 nm)||YEL (570 nm)||ORG (600 nm)||RED (650 nm)|
Baseline mean values:
|VIO (450 nm)||BLU (500 nm)||GRN (550 nm)||YEL (570 nm)||ORG (600 nm)||RED (650 nm)|
So the spectral signature of my empty test chamber looks something like this:
Next I repeated this test with one difference – I put a red gummy bear in the end cap.
I then subtracted the mean baseline values from the mean red gummy bear values so that I could see what the relative change in counts was for each channel.
You can see the 650 nm channel shows the smallest decrease, which makes sense since a red gummy bear would be expected to absorb less red light and more light of other wavelengths. Compare this to the orange gummy bear’s change in counts:
The red and orange gummy bears absorb about the same amount of violet (450 nm) and red (650 nm) light. However the orange gummy bear absorbs much less orange light (600 nm) than the red gummy. The results were similar when I tested additional red and orange gummy bears.
The yellow and pineapple gummy bears were also similar to each other.
However the yellow gummy bear absorbs much less green (550 nm), yellow (570 nm), and orange (600 nm) light.
The green gummy bear does not seem similar to the other gummy bears to me, and I noticed that it absorbs the most red light of all the gummy bears.
I was surprised by how repeatably I was able to produce the same spectral pattern for each color of gummy bear. Even with this crude setup it may be possible to develop a simple algorithm that could reliably identify the color of a gummy bear. (And you could use this to make… an automatic gummy bear sorter?)
I also wondered whether the SparkFun board’s red silkscreen affected the measurements. Given this board’s intended purpose, would it make more sense for the solder mask to be white?