Adds

24 June 2015

Arduino with a thermistor, LDR and RGB LED that changes colour with temperature

Code gets:

- Temperature from a thermistor

- Light value from an LDR

and

RGB LED changes colour depending on the Temperature

Prints the data to the Serial Monitor


Please let me know if you experience any problems with this code. Be sure to check back for more Arduino code.

Schematic:




Code:


 /*  
   
 Oliver Holden  
 Created 28/10/2014  
   
 Components:  
 * Arduino UNO  
 * Thermistor  
 * 10K resistor  
 * LDR  
 * 10K resistor  
 * RGB LED  
 * 3x 220 ohm resistors  
   
 - Gets Temperature form a Thermistor  
   
 - Gets an analogue value of Light from an LDR  
   
 - RGB LED changes colour depending on the Temperature   
   
 Prints the data to the Serial Monitor  
   
 Schematic for this project is on  
 http://breadboarder.blogspot.co.uk/  
   
 */  
   
 int redPin = 11;  // Red LED,  connected to digital pin 11  
 int grnPin = 10; // Green LED, connected to digital pin 10  
 int bluPin = 9; // Blue LED, connected to digital pin 9  
   
 int ThermistorPin = A0;  // Thermistor Reading pin  
 int LDRPin= A2;   // LDR Reading pin  
   
 int LDRReading = analogRead(LDRPin);   // Reading LDR analogue value  
   
 void setup()  
 {  
   Serial.begin(9600);   // Opening serial connection at 9600bps  
  Serial.println("Thermistor temperature and LDR light measurement:");   // Printing Serial Monitor header  
  Serial.println(" T (C), LDR");   // Setting headers of colums in PLX-DAQ spreadsheet  
  Serial.println("\n-----------------------------");  
   
  pinMode(redPin, OUTPUT);  // sets the LED pins as output  
  pinMode(grnPin, OUTPUT);  
  pinMode(bluPin, OUTPUT);  
    
          
 }  
   
 void loop() {  
    
 int Vo;   // Integer value of voltage reading  
   
 float R = 9870.0;   // Fixed resistance in the voltage divider  
 float logRt,Rt,T;  
 float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;  
   
 Vo = analogRead(ThermistorPin);   // Reading voltage Thermistor Reading pin  
   
 Rt = R*( 1023.0 / (float)Vo - 1.0 );  
   
 logRt = log(Rt);  
   
 T = ( 1.0 / (c1 + c2*logRt + c3*logRt*logRt*logRt ) ) - 273.15;   // Converting voltage to temperature  
   
 Serial.print("     "); Serial.print(T);  // Printing data to Serial Monitor  
   
   
 Serial.print("     "); Serial.print(LDRReading);  
   
   
 // Setting the colours of the RGB LED based on temperature,  
 // Colour is shown for 0.1s before checking temperature again.  
   
 if(T >= 26.2)  
  {  
   digitalWrite(redPin, HIGH);  // red  
   delay(100);  
     
   digitalWrite(grnPin, LOW);  
   digitalWrite(bluPin, LOW);  
   
  }  
   
 if((T < 26) && (T >= 23.2))  
  {  
   digitalWrite(redPin, HIGH);  // yellow  
   digitalWrite(grnPin, HIGH);  
   delay(100);  
     
   digitalWrite(bluPin, LOW);  
   
  }  
    
  if((T < 23) && (T > 20.2))  
  {  
   digitalWrite(grnPin, HIGH);  // green  
   delay(100);  
     
   digitalWrite(redPin, LOW);  
   digitalWrite(bluPin, LOW);  
    
  }  
   
 if((T < 20) && (T > 17.2))  
  {  
   digitalWrite(grnPin, HIGH);  // aqua  
   digitalWrite(bluPin, HIGH);  
   delay(100);  
     
   digitalWrite(redPin, LOW);  
     
   
  }  
   
  if(T <= 17)  
  {  
   digitalWrite(bluPin, HIGH);  // blue  
   delay(100);  
     
   digitalWrite(grnPin, LOW);  
   digitalWrite(redPin, LOW);  
    
  }   
    
  delay(1000);   // Printing data once a second  
   
 }  

No comments:

Post a Comment