Ok here it comes. CO2 only for now, If you like my approach I’ll do the same thing for PM.
I started with defining led colors for CO2 values. I also tried to use values matching up with the GO IAQS values:
400 is perfect, blue
800 is fine, green
1000 is questionable, yellow
1400 is bad, red.
2000 is really bad, magenta
To build a color function I used the HSL color space where H is the hue, this makes math with colors a lot easier.
If you put the color values and the ppm in a 2d chart it looks like this:
With a bit of help from matplotlib curve fitting I arrived at this function:
Which gives me this color gradient for the LED, what do you think?
Code for this would look like this (I use python, but transferring it to a different language like c should be easy)
import colorsys
# python library for conversion of color models
def CO2toLED(ppm: float, brightness: float) -> None:
'''
function to calculate and set LED colors from CO2 ppm value
input: CO2 value in ppm, LED brightness scale 0-1, saturation set to 1 for best color
'''
# limit values to 400-2000, below 400 always blue, above 20ö0 always magenta
if ppm < 400:
ppm = 400
if ppm > 2000:
ppm = 2000
h = -2.5e-08 * pow(ppm, 3) + 0.0001871 * pow(ppm, 2) - 0.513 * ppm + 417.7
# normalize H from angle in degrees to scale of 0..1 including shift for negative values from curve fitting
h_norm = ((h + 360) % 360) / 360
# convert HSV to RGB
rgb = colorsys.hsv_to_rgb(h_norm,1,brightness)
# LED code here
print(f"LED set to R:{rgb[0]}, G:{rgb[1]}, B:{rgb[2]}")