Skip to content

Instantly share code, notes, and snippets.

@adrianjost
Created August 14, 2021 09:35
Show Gist options
  • Save adrianjost/0cae90a3f984ba0ba1101877cdb1c7fc to your computer and use it in GitHub Desktop.
Save adrianjost/0cae90a3f984ba0ba1101877cdb1c7fc to your computer and use it in GitHub Desktop.
Linear Potentiometer to quadratic CCT Light - Arduino Controlled
// Uses a linear potentiometer to adjust the brightness of an CCT light connected to the arduino.
// the darker the light, the warmer the color temperature gets.
// The brightness get's adjusted quadratically to the potentiometer value.
// This is ideal for a night lamp with fine control in the lower spectrum but the ability to use full brightness at day.
#define PIN_WW 5
#define PIN_CW 6
#define PIN_IN A0
#define MAX_BRIGHTNESS 255
#define threshold 10
void setup() {
pinMode(PIN_IN, INPUT);
pinMode(PIN_WW, OUTPUT);
pinMode(PIN_CW, OUTPUT);
}
void loop() {
int sensorValue = analogRead(PIN_IN);
if(sensorValue <= threshold){
analogWrite(PIN_WW, 0);
analogWrite(PIN_CW, 0);
}else{
float x = map(sensorValue - threshold, threshold, 1024, 0, 1024);
float brightness = pow(float(x) * 0.016, 2);
float ratio = pow(brightness / MAX_BRIGHTNESS, 2);
analogWrite(PIN_WW, min(int(brightness), MAX_BRIGHTNESS));
analogWrite(PIN_CW, min(int(ratio * brightness), MAX_BRIGHTNESS));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment