Cellphone Spectrophotometer

by aubreyb11 in Workshop > Science

302 Views, 4 Favorites, 0 Comments

Cellphone Spectrophotometer

introphoto3c2.png

Did you know your phone is good for more than doomscrolling? Why not use it to see the light?

I'm talking about spectrophotometery of course. Spectrophotometery is the practice of measuring the intensity of light by wavelength. It's an analytical tool used in astronomy, chemistry, materials science, and biochemistry. It is used to study the makeup of stars, determine olive oil vs extra virgin olive oil, analyze the effectiveness of sunscreen, detect and measure water pollution, and much much more.

Supplies

  1. Android cellphone (this was tested on a Pixel 7)
  2. I have made an app to record the results from the cell phone spectrometer, but it can't be directly installed on an iOS. You may have success with an Android emulator.
  3. 3D printer + filament (preferably black)
  4. Onshape account (it's free)
  5. DVD/CD
  6. Tape
  7. Light source with known wavelength peaks for calibration. (I used an LED, CFL is also very good)
  8. Ruler/ Caliper
  9. Google Sheets/ Excel

Understand Basic Principles and Uses of Spectrophotometry

spectexplain.png

While this little blurb is in no way a replacement for paying attention in physics class, I will explain the very basics of light and spectrophotometry.

As you may know, the visible light that we are able to see is only a small part of the spectrum of electromagnetic radiation, which is basically a series of electromagnetic waves organized by wavelength/frequency. This encompasses radio, microwaves, infrared, visible light, ultraviolet, x-rays, and gamma rays. As you can guess, visible light (~400nm-700nm) refers to what is visible to the human eye. Cell phone cameras can normally pick up both visible light and some infrared (~1100nm). However, cameras tend to have a filter over the camera to block out infrared light. You can test your cell phone's ability to capture infrared by pointing it at the emitting end of a remote and pressing a button. If the camera can pick up infrared, light should be visible on your screen when you press a button.

Spectrophotometers are tools used to measure the intensity of light by it's wavelength. The device works by sending a light beam through a material that bends light (refraction) in a way that separates the wavelengths (Spectral Dispersion). This material is usually a prism (I'm sure you've seen this on a t-shirt or poster) or some sort of diffraction grating. In this case, we will be building a device that takes a light beam, sends it through a sample, focuses that light through a slit, passes it through diffraction grating, then records the light as a camera image. This is what's known as an array spectrophotometer.

What makes spectrophotometry so useful is the fact that different materials absorb light differently. If you are not colorblind, you may have noticed this already. A green object absorbs all colored light except green, black objects absorb all colors, while white absorbs none. A material's chemistry decides how much light is absorbed by a certain wavelength. In fact, the amount of absorbance at a certain wavelength is inversely proportional to the concentration of a substance, a relationship known as Beer-Lambert's Law:

Absorbance = Extinction coefficient * light path length * concentration

The extinction coefficient varies by wavelength is determined by a substance's chemical makeup. This property allows for the measurement of concentration of substances, detecting chemical components, or evaluating the purity of a solution.

Measure Phone Specifications

thicknessCrop.png
cameraDimenstions.png

The spectrophotometer case should fit over your phone, with an opening right in front of the camera. I have made a 3D file with the case dimensions made to fit my Pixel 7. You will need to adjust the file to fit your phone's dimensions. First, measure the key dimensions with a ruler or caliper (for improved accuracy).

  1. Thickness
  2. Width
  3. Camera Height = distance from center of back camera to top of phone
  4. Camera Width = distance from center of back camera to LEFT edge of phone

If you live in the 21st century, you likely have multiple back cameras. Typically one of them is the "main" camera. Unfortunately at the present, Android doesn't allow you to manually choose which one to use. Instead decide the main camera by attempting to take a picture of an object a couple feet away in a normally lit room, and placing your finger over the cameras to see which one is in use.

Create Spectrophotometer Case

step1accountEdit.png
step2copyEdit.png
step2copy2Edit.png
step3variablesEdit.png
step4exportEdit.png
step4export2edit.png

I have made a 3D file that will allow users to customize the case according to their phone. No CAD experience needed. I have hosted this file on OnShape, a free online CAD software.

  1. Create a free account at https://www.onshape.com/en/
  2. Go to https://cad.onshape.com/documents/c077b06bfef90e1094eb37f4/w/8c5f1b4a06750f3c3b3f9c2a/e/7382cba452aac4862c433a96?renderMode=0&uiState=68cd579417efa7f5f877d093 Copy the document to your own workspace: click on the three hamburger lines on the top left → Copy Workspace..
  3. Change the variables to match your own dimensions (default units are inches)
  4. Export: right click on part and click on export. You should choose .STL as the download file.
  5. Print on your printer of choice. I used an Ender 3 V2 with black PLA with supports generated and infill 20%.

Attach Diffraction Gating

dvd.jpg
peelDVD.jpg
measureDVD.jpg
cutDVD.jpg
not assembled.jpg
assembled phone.jpg

We will use the plastic portion of a DVD or CD as the diffraction grating. DVDs and CDs are optical disks that use the same basic principles made of a plastic disk + reflective layer. DVD's tend to be double: plastic + reflective layer + plastic.

  1. Find a DVD or CD to sacrifice. Sorry Will Ferrell. If a DVD, peel apart the layers. A CD is only one layer.
  2. Cut a potion of disk, about 25mm square will fit.
  3. The reflective portion or any label will need to by scrapped off (preferably by a fingernail, a knife may scratch the plastic), leaving clear plastic
  4. The plastic can be taped over the circular opening, completing the case. Keep the plastic so the inner/outer edge of disk is top/bottom on the camera. The resulting "rainbow" should be vertical.

Install Spectrophotometer App

screenshot.png

First, test out your new spectrophotometer device. The case should fit over your phone, and when pointing at a light (flat end of the shaft perpendicular to the light source), a rainbow should appear. It might be a little messy depending on how bright the light source is or ambient light. The blue light should be either on the top or bottom (vertical rainbow). If not, rotate the disk/diffraction grating 90°. The rainbow should also be somewhat in the middle of the image.

Using pixel colors isn't good enough to actually measure light by wavelength. For starters, the way camera sensor records light and stores the data doesn't always match well with wavelengths. However with the diffraction grating, light is split by its wavelengths sequentially, so we can estimate wavelength by position on the image.

I have made an app that takes the image from the camera, and measures the brightness intensity of the pixels starting from the top of the image to the bottom. Specifically, the average brightness of the 25 middle pixels are recorded by row. This is an unpublished apk app made with flutter. Theoretically, the app should run on iOS devices, but apple doesn't allow direct installing from unknown locations. Furthermore, the camera permissions are set up for Android. Nonetheless, if you have an iPhone, you may be able to finagle it with an Android emulator + editing the code to allow for camera permissions.

For the Android users:

  1. On your phone, visit github repository: https://github.com/aubreyb11/CellPhoneSpectrophotometerProto
  2. Click on CellPhoneSpectrophotometer v3. (underneath Releases on the right-hand side).
  3. Click on spectrophotometerRelease.apk to download it.
  4. Go to your files where it's downloaded, tap on spectrophotometerRelease.apk and select install. You will need to give permission to your file system to install unknown apps.
  5. Run the app and allow camera access.
  6. You should see a camera preview on top, a share button, and a graph.

All the code is uploaded to github and open-source. If you are using a non-Pixel 7 Android and are having issues running the app, I have included a debugging version in the v2 release which also might work. You may be able to get it to work by downloading Flutter and all the code and running the program on your phone in debugging mode. That is beyond the scope of this instructables however. Feel free to leave a comment with your phone type and operating system and how it worked for you.

Taking Calibration + Readings

setup.jpg
contactlens3.jpg
eyeglasses.jpg
sunglasses.jpg

Now for the fun part: taking measurements. You will need to use a light source that has a known spectra (spectral power distribution), so you can compare your calibration readings to determine what pixel position correlates to what wavelength (more on that in the next step).

I am using the LUXEON 3014 white LED which is part of the SparkFun Triad Spectroscopy Sensor. A CFL bulb also has a well defined spectra (but you might need to visit a museum to find one). You might need to try googling the spectral power distribution for different light bulbs you have available to you.

It's important not to move or adjust the light source or phone between calibration readings and when measuring the light through a sample. To make this easier, I recommend increasing the screen timeout on your phone (warning, this will absolutely burn through your battery!). Click on the share button to send yourself an email with a .csv file that contains the pixel position (from top of image to bottom) and the relative intensity in two columns.

For an example, I recently bought some contacts that advertised themselves as being "blue light filtering". I decided to put that to the test, along with my eyeglasses and a pair of sunglasses.

Analyzing Your Data

step7 1.png
step7 2.png
step7 3.png
step7 4.png

From the emails I sent to myself, I copy + pasted the data in a google sheet. It might be helpful to delete portions if you know there was a glare at the top or bottom of the screen outside of the range of the rainbow.

Now we need to use linear regression to align our pixel values with the wavelength (no linear algebra knowledge required)

  1. Find at least two features on your spectra graph to compare to the reference. I chose three, the first peak, first trough, and the second peak. Because all I had was the image of the graph, I had to get a ruler out to measure the points on my screen. If you make a graph in google sheets with your data, you can hoover your mouse over points of interest to get x,y values.
  2. Visit an online linear regression calculator: https://www.statskingdom.com/linear-regression-calculator.html
  3. You will need to first correlate your pixel values to wavelength (nm). Use your measured pixel number as X and the reference wavelength as Y. The resulting equation can be used to transform your pixel numbers to wavelength
  4. Tip: The R2 value (in green writing) gives a measure of how closely the resulting equation fits the data, with 1 being the best and 0 being the worst.
  5. Use the equation you found in Google Sheets to convert your pixel position to wavelength.
  6. Compare your samples' spectra to your calibration. Remember a low intensity value the calibration means that wavelength is being absorbed by your sample. Looking at my own data, I can see there's a bigger gap between contact lens and calibration at the 400nm point than at the 550nm, meaning they are indeed absorbing higher-frequency light. The same doesn't seem to be true of the eyeglasses, however there's some distortion there. As expected the sunglasses abosrbs light at all wavelengths.


I'd like to give my thanks the PublicLabs.org (RIP). Unfortunatly the organization no longer exists, but their open-source DIY citizen science is what inspired this project in the first place, and it is where I learned about DIY spectrophotometry. In the future I'd like to explore improving the precision and accuracy enough to be able to measure chemical concentrations. Please let me know if you make your own and what you use it for!