mattcurrentjr
Published © GPL3+

Color-changing Lamp Via Temperature Input

This project detects changes in temperature based on your specifications, and then changes the color of a LED to the color of your choosing.

BeginnerProtip8,576
Color-changing Lamp Via Temperature Input

Things used in this project

Story

Read more

Schematics

Temperature Sensing LED

The idea is to have the temperature sensor outside (wherever you want to detect changes in temperature) and then have an indicator (LED) in a comfortable location

Code

Temperature Incremental Changes

Arduino
Change the if statements to change the color and what temperatures your light will change out.
const int greenLEDPin = 9;    // LED connected to digital pin 9
const int redLEDPin = 10;     // LED connected to digital pin 10
const int blueLEDPin = 11;    // LED connected to digital pin 11

// room temperature in Celcius
const float baselineTemp = 20.0;

const float baselineSens = 144;
const int TempSensor = A0;  // pin with the temp sensor

int redValue = 0; // value to write to the red LED
int greenValue = 0; // value to write to the green LED
int blueValue = 0; // value to write to the blue LED

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);

  // set the digital pins as outputs
  pinMode(greenLEDPin, OUTPUT);
  pinMode(redLEDPin, OUTPUT);
  pinMode(blueLEDPin, OUTPUT);
}

void loop() {

  // read the value on AnalogIn pin 0
  // and store it in a variable
  int sensorVal = analogRead(TempSensor);
  delay(5);
  //Serial.println(sensorVal);
   // convert the ADC reading to voltage
   // convert temp sensor reading from 0-1024 to analogWrite value from 0-255 
  float voltage = (sensorVal / 1024.0) * 5.0;
 
  
    // Send the voltage level out the Serial port
  Serial.print("Volts: ");
  Serial.print(voltage);
  
  // convert the voltage to temperature in degrees C
  // the sensor changes 10 mV per degree
  // the datasheet says there's a 500 mV offset
  // ((volatge - 500mV) times 100)
  Serial.print(", degrees C: ");
  float temperature = (voltage - .5) * 100;
  Serial.println(temperature);
  if (temperature < baselineTemp) {
  analogWrite(redLEDPin, 0);
  analogWrite(greenLEDPin, 127);
  analogWrite(blueLEDPin, 0);
  }else if (temperature >= baselineTemp && temperature < baselineTemp + 2) {
  analogWrite(redLEDPin, 0);
  analogWrite(greenLEDPin, 255);
  analogWrite(blueLEDPin, 0);
  } // if the temperature rises 2-4 degrees, turn an LED on
  else if (temperature >= baselineTemp + 2 && temperature < baselineTemp + 4) {
  analogWrite(redLEDPin, 0);
  analogWrite(greenLEDPin, 0);
  analogWrite(blueLEDPin, 255);
  } // if the temperature rises 4-6 degrees, turn a second LED on
  else if (temperature >= baselineTemp + 4 && temperature < baselineTemp + 6) {
  analogWrite(redLEDPin, 127);
  analogWrite(greenLEDPin, 0);
  analogWrite(blueLEDPin, 127);
  } // if the temperature rises more than 6 degrees, turn all LEDs on
  else if (temperature >= baselineTemp + 6) {
  analogWrite(redLEDPin, 255);
  analogWrite(greenLEDPin, 0);
  analogWrite(blueLEDPin, 0);
  }
  delay(1);


}

Credits

mattcurrentjr

mattcurrentjr

0 projects • 2 followers

Comments