HomeHome  [日本語 / English]

Change the color of RGB LEDs according to the CPU temperature of a PC

Introduction

The content is as shown in the title. I found a HP PC with such a function and wanted to reproduce it on my own PC. As a result of my research, I was able to realize it using some open source software and my own C# application.

緑 赤

Environment

The environment in which I tried it is as follows

ARGB LED

The motherboard used, TUF GAMING B550-PLUS, supports both regular RGB LED and addressable RGB (ARGB) LED connections. In this case, we used the following ARGB LED strips.

AINEX 2Way ARGB LED Strip Light 30cm RLD-STRDM30B

System Configuration

The overall picture of the system I have created is shown in the figure below.

System Configuration

It consists of the following components.

Hardware
Hardware to be monitored and controlled.
LibreHardwareMonitorLib
This is a library for acquiring hardware information. In this case, I am using it to obtain the CPU temperature, but it can also be used to obtain a variety of other information. https://github.com/LibreHardwareMonitor/LibreHardwareMonitor
OpenRGB
This software is designed to control LEDs. In addition to operating as a stand-alone application to control LEDs, it also has a server function and can be used by clients through a TCP/IP connection. https://openrgb.org/
OpenRGB.Net
This library is a client for connecting to the OpenRGB server functionality described above from C#.https://github.com/diogotr7/OpenRGB.NET
Application
This is a self-made application program that controls the entire system. It is created in C#.

Preparations

  1. Connect the ARGB LED strip to the motherboard.
  2. Download OpenRGB from its website.
  3. Create a C# project and install LibreHardwareMonitorLib and OpenRGB.Net using the Nuget package manager.

Obtain CPU temperature

The program to get the CPU temperature using LibreHardwareMonitorLib is shown below." The "/amdcpu/0/temperature/2" part may need to be changed depending on the CPU.

As a precaution, the temperature could not be obtained without "Run as administrator" when executing this program.

            
Computer computer = new Computer();
computer.IsCpuEnabled = true;
computer.Open();
computer.Accept(new UpdateVisitor());

double temperature = 0;
foreach (IHardware hardware in computer.Hardware)
{
    foreach (ISensor sensor in hardware.Sensors)
    {
        if (sensor.Identifier.ToString() == "/amdcpu/0/temperature/2")
        {
            temperature = sensor.Value.GetValueOrDefault();
        }
    }
}

UpdateVisitor is defined as follows;

            
public class UpdateVisitor : IVisitor
{
    public void VisitComputer(IComputer computer)
    {
        computer.Traverse(this);
    }
    public void VisitHardware(IHardware hardware)
    {
        hardware.Update();
        foreach (IHardware subHardware in hardware.SubHardware) subHardware.Accept(this);
    }
    public void VisitSensor(ISensor sensor) { }
    public void VisitParameter(IParameter parameter) { }
}

LED Control

When you run OpenRGB, you will see a screen like the one below, which also allows you to control LEDs with OpenRGB alone.

If you want to control it from the client application, as in this case, you should set "Mode" to "Direct".

OpenRGB

To run as a server, select the "SDK Server" tab and press the "Start Server" button. Then connect to the server from the client application.

OpenRGB Server

The program to connect to the OpenRGB server using OpenRGB.NET and set the LED colors is shown below. The variables r, g, and b contain the brightness of each RGB color.

            
var openRgbClient = new OpenRGBClient(name: "My OpenRGB Client", autoconnect: true, timeout: 1000);
var devices = openRgbClient.GetAllControllerData();

for (int i = 0; i < devices.Length; i++)
{
    var leds = Enumerable.Range(0, devices[i].Colors.Length)
        .Select(_ => new OpenRGB.NET.Models.Color((byte)r, (byte)g, (byte)b))
        .ToArray();
    openRgbClient.UpdateLeds(i, leds);
}

Temperature to RGB Conversion

When I actually measured the CPU temperature, it was about 40°C at no load and about 90°C at 100% load. Therefore, we decided to change the LED color from green to yellow to red as the CPU temperature changes from 40°C to 65°C to 90°C.

In this case, the temperature to RGB conversion program is as follows

            
int temp = (int)temperature;    // Temperature
int r, g, b;    // Brightness of each color

if (temp < 40)
{
    r = 0;
    g = 255;
    b = 0;
}
else if (temp < 65)
{
    r = 255 * (temp - 40) / 25;
    g = 255;
    b = 0;
}
else if (temp < 90)
{
    r = 255;
    g = 255 * (90 - temp) / 25;
    b = 0;
}
else
{
    r = 255;
    g = 0;
    b = 0;
}

In fact, when I use it, I can see the temperature of the CPU with my eyes, and I can feel how the CPU is working hard to perform calculations.

...and upwards.