Menu
Cart 0

Designing a Simple Light Spectrum Analyser

Posted by Matthew Little on

This project was to create a nice interface and display for the data from the Osram AS7341 11-channel visible light spectral sensor. This is an 11 channel spectral sensor with a response range of 380nm to 1000nm. It has 8 channels centred in the visible spectrum (VIS), plus one near-infrared (NIR) and a clear channel. Data is transferred via an I2C bus. This sensor also comes in a 14 channel version (the AS7343) which I am also getting samples of, but it is quite a big step up in price.

Display Unit

I obtained some ESP32 boards with a 320 x 240 pixel colour LCD display, SD card slot and resistive touch screen. These are pretty low cost items from Chinese manufacturers, which have been called 'CYD' units, or 'Cheap Yellow Display' units, as they are usually on a yellow PCB. Some great work has been done by the hacker community to get more information in English available and to simplify getting started with these boards. The ones I obtained use an ESP32-WROOM-32 processor with reference name "CYD_2.4inch_ESP32-2432S024". If you search for that name then there is a GitHub repository of lots of information and downloaded examples.  These are pretty low cost but have a nice colour display with (resistive) touch screen. I wanted to show the spectral data as a bar graph, along with showing the peaks with a little bar.

Here is the end result, read on for more info about developing this:

Firstly I wanted to be able to upload simple code to the CYD display board. Following the instructions supplied by the manufacturer, I ensured that the ESP32 board manager was installed (already used for other projects). I made sure that the upload settings were correct (the board seems to need DIO, rather than QIO) as shown here:

I did a simple serial.println("Hello World") and uploaded. This worked all fine!

The next step was to get something to display on the LCD. I followed the instructions for installing the TFT_eSPI library (which is needed to drive the LCD touch screen). Within the TFT_eSPI library folder location there is a User_Setup.h file. This is where you configure which TFT you are using and all the associated pins. From the demo examples in the GitHub repository for the display board there is an example User_Setup.h file which should allow the examples to run. I replace the original User_Setup.h file in the TFT_eSPI folder with the new User_Setup.h file which should be aligned with my CYD board. I uploaded code to do a Rainbow example on the display, but nothing appeared! It turns out I needed to do some more updates of the pin definitions. It took a little while but I got the User_Setup.h to work with the CYD unit I have. Here are the changes:

Firstly we need to change to alternative driver:

The I needed to check the backlight control pin:

Then I needed to ensure the pins to control the TFT are correctly set:

These setting worked for me and I used the PCB schematic to ensure this was correct - there are different versions of the CYD board with subtle changes so please double check your version before using these pin settings. I have included this User_Setup.h within the GitHub repository for this code.

Now I have the TFT working I can work on the rest of the code.

Connection to the sensor

The I2C is available via a 1.25mm pitch JST 4 pin connector. I needed to order some tiny connectors for this, as its smaller pitch than I usually work with. My board schematic has GPIO on port CN1 with the following GPIO conenctions:

// Hardware Pin Assignments
#define EXPANSION_SDA_PIN 22
#define EXPANSION_SCL_PIN 21

I connected the sensor to this connector and used the DFRobot "DFRobot_AS7341.h" library to talk to the AS7341 sensor. This gave me data back, all great. I rigged up some code to display the spectral data as a bar graph, but I realised that the colour mapping was incorrect (red came out as green). It turns out that the colour inversion and ordering also needed to be changed within the User_setup.h file. I uncommented the following to eventually get it to display correctly. First the colour order to get RGB colours correct:

Next was inverting the TFT so that we get a black background, rather than white:

This was saved and the updated User_setup.h was installed within the TFT_eSPI folder. Hopefully all working correctly now!

Getting Touch Screen to work

Next I wanted to add some touch screen control for the options. The changes I needed to make to the User_Setup.h were:

I was still getting slightly error-prone readings from the touch screen, so I added code to read the value three times and give an averaged value back. This greatly solved the touch errors I was getting. I could now add some touch screen buttons to the device to control different things.

I ended up with three touch screen buttons: one to adjust the scale (from auto to fixed), one to adjust the sensor LED brightness (and switch it off) and one to reset the 'peak' maximum lines on the bar graph. I also added a little flash of the RGB LED on the CYD board so that you know you have pressed the button.

Increasing display update speed

With the original code I was using the DFRobot AS7341 library. This had some blocking and wait features which meant I was only getting updates every 1 second or so. I re-wrote the code to use Wire.h directly and remove the blocking DFRobot code. I used AI to help guide me through this process. Eventually I got the speed of updates down to 73mS for a display update. I adjusted the code to read the sensor every 100mS but give updated to the LCD screen every 250mS which looked smooth and seemed to work well.

Normalising Values

The AS7431 IC does not have a normalised output. This means that not all spectrum channels respond the same - the lower 4 bands will read lower as the IC is not as sensitive to those bands. In order to correct for this I added some additional normalising gains to boost the lower 4 bands, as per the AS7431 datasheet. The gains I used are (the other bands are left at 1.0 x gain): 

  •  Gain 3.20x for F1: 415nm (Violet - lowest responsivity / narrowest FWHM)
  •  Gain 2.10x for F2: 445nm (Blue)
  •  Gain 2.40x for F3: 480nm (Cyan)
  •  Gain 1.45x for F4: 515nm (Green)

This made the response more obvious and was much better are picking up lower frequency bands.

Video of unit in action

Power Supply Options

I wanted the unit to be portable and battery powered. The CYD board has a 2-pin 1.25mm JST battery connection port designed for a single-cell 3.7V LiPo / Li-ion battery (4.2V max). I have been using 18650 cells in some projects and so decided to add a holder for an 18650 cell to be added, but also have access to the USB C port which can be used for charging (or fully powering) the unit. I also needed to add an on/off switch on the battery connector.

Hardware and enclosure

I designed a little laser cut enclosure with M3 hex stand-offs to hold the sides together. It was a first attempt, but has come together quite nicely. 

   

Design Files and GitHub Repository

The firmware code I used and the enclosure design files is available in my GitHub repository for this project here:

https://github.com/curiouselectric/spectralSensor



Share this post



← Older Post


Leave a comment

Please note, comments must be approved before they are published.