Any algorithm for temperature to color (NeoPixel or any 3-color LED)

Has anyone developed any algorithm for generating RGB based on temperature:

Blue = Cold (say... 24 deg)
Green = maybe 30 deg?
Yellow = 40 deg
Orange = 50 deg
Red = Hot 65+ deg

And all shades in between?

Someone has to have developed a nice formula.

Ideas?

Thanks

1 Like

Has been done. you should use HSL colour format (check wikipedia for the details) and convert it to RGB.

HSL is easier to make a rainbow.

void setup()
{
}

void loop()
{
  f loat R,G,B;
  float t = getTemperature();

  t = t/ 100;  // need to be adapted
  HSL(t, 1, 0.5, R, G, B);
  // use R G B values here
   
}


void HSL(float H, float S, float L, float& Rval, float& Gval, float& Bval)
{
  float var_1;
  float var_2;
  float Hu=H+.33;
  float Hd=H-.33;
  if ( S == 0 )                       //HSL from 0 to 1
  {
    Rval = L * 255;                      //RGB results from 0 to 255
    Gval = L * 255;
    Bval = L * 255;
  }
  else
  {
    if ( L < 0.5 ) 
      var_2 = L * ( 1 + S );
    else           
      var_2 = ( L + S ) - ( S * L );

    var_1 = 2 * L - var_2;

    Rval = round(255 * Hue_2_RGB( var_1, var_2, Hu ));
    Gval = round(255 * Hue_2_RGB( var_1, var_2, H ));
    Bval = round(255 * Hue_2_RGB( var_1, var_2, Hd ));
  }
}

float Hue_2_RGB( float v1, float v2, float vH )             //Function Hue_2_RGB
{
  if ( vH < 0 ) 
    vH += 1;
  if ( vH > 1 ) 
    vH -= 1;
  if ( ( 6 * vH ) < 1 ) 
    return ( v1 + ( v2 - v1 ) * 6 * vH );
  if ( ( 2 * vH ) < 1 ) 
    return ( v2 );
  if ( ( 3 * vH ) < 2 ) 
    return ( v1 + ( v2 - v1 ) * (.66-vH) * 6 );
  return ( v1 );
}

should get you started

Something a little simpler:

 int temp;
  byte red, green, blue;

  temp = constrain(temp, 24, 65);
  if (temp > 30) {
    red = map(temp, 30, 65, 0, 255);
    green = 255 - red;
    blue = 0;
  }
  else {
    red = 0;
    green = map(temp, 24, 30, 0, 255);
    blue = 255 - green;
  }

PS. 24 is nice and warm, 50+ scorches human skin!

PaulRB:
PS. 24 is nice and warm, 50+ scorches human skin!

Some locations have hit that ambient temperature in recent times!

Paul__B:
Some locations have hit that ambient temperature in recent times!

Yes, we've been hearing about the fires on the news. Between news items about which parts of northern England have been flooded out for the third or fourth time this winter...