Week 9 : Assignment

For this assignment i attempted to make  temperature sensor readings using the TMP 36 , and controlling a set of LED’s with a potentiometer.

Temperature sensor components :

  • Four LED’s : blue, green, yellow, and red.
  • Temperature sensor TMP 36
  • Ten jumper wires
  • Four 330 ohm resistors

concept:
The TMP 36 is at first measuring the temperature of the room at the moment, the LED lights light up according to how warm or cool the environment is, as you can see from the video below, before placing my finger on the TMP 36 the temperature was ranging from 23 degrees to 24 degrees (yellow LED), after placing my finger on the sensor the temperature was reading a range of 26 degrees to 27 degrees Celsius (red LED).

Code :

const int sensorPin = A0;
const float baselineTemp = 18.5;


void setup() {
  // put your setup code here, to run once:

Serial.begin(9600);
for ( int pinNumber = 2; pinNumber < 6; pinNumber++)
{
  pinMode(pinNumber, OUTPUT);
  digitalWrite(pinNumber, LOW);
}


}

void loop() {
  // put your main code here, to run repeatedly:

int sensorVal = analogRead(sensorPin);

Serial.print("Sensor Value: ");
Serial.println(sensorVal);


float voltage = (sensorVal / 1024.0) * 5.0;

Serial.print(", Volts: ");
Serial.print(voltage);

Serial.print("' degrees C: ");
float temperature = (voltage - 0.5) * 100;
Serial.println(temperature);


if (temperature < baselineTemp + 1.5)
{
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  
}


 else if (temperature >= baselineTemp + 1.5 && temperature < baselineTemp + 3)
 {
  digitalWrite(2, HIGH);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
 }


 else if (temperature >= baselineTemp + 3 && temperature < baselineTemp + 4.5)
 {
  digitalWrite(2, LOW);
  digitalWrite(3, HIGH);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
 }


 else if (temperature >= baselineTemp + 4.5 && temperature < baselineTemp + 6)
 {
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, HIGH);
  digitalWrite(5, LOW);
 }

 else if (temperature >= baselineTemp + 6)
 {
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, HIGH);
 }


delay(1);




}

Explanation :

As you can see above , i set the baseline temperature at 18.5 degrees . Every time the temperature increases by 1.5 degrees that determines which color amongst the four LED’s lights up, 2 (blue) the coolest, 3(green), 4(yellow), and 5 (red) the warmest.

 

Difficulties :

I had some issues with setting up the baseline temperature, where at first it did not work, i kept on changing the LOW’s and HIGH’s until it worked how it supposed to.

 

Potentiometer sensor components :

  • potentiometer sensor
  • Four LED’s
  • eleven jumper wires
  • Four 330 ohm resistors

 

Concept:
Using the potentiometer, it controlled how many LED’s light up starting from the first blue LED until the last red LED , using voltage as the A5 pin im using reads the voltage   from 0 to 1023 volts.

Readings:

Code :

int potPin = A5 ;
int led1Pin = 2 ;
int led2Pin = 3 ;
int led3Pin = 4 ;
int led4Pin = 5 ;





void setup() {
  // put your setup code here, to run once:

pinMode(potPin, INPUT);
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
pinMode(led4Pin, OUTPUT);
Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:

int potMeasure = analogRead(A5);
Serial.println(potMeasure);

if (potMeasure < 256 )
{
  digitalWrite(led1Pin, HIGH);
  digitalWrite(led2Pin, LOW);
  digitalWrite(led3Pin, LOW);
  digitalWrite(led4Pin, LOW);
}

else if (potMeasure < 512)
{
 digitalWrite(led1Pin, HIGH);
  digitalWrite(led2Pin, HIGH);
  digitalWrite(led3Pin, LOW);
  digitalWrite(led4Pin, LOW); 
}


else if (potMeasure < 768)
{
 digitalWrite(led1Pin, HIGH);
  digitalWrite(led2Pin, HIGH);
  digitalWrite(led3Pin, HIGH);
  digitalWrite(led4Pin, LOW); 
}



else if (potMeasure < 1024)
{
 digitalWrite(led1Pin, HIGH);
  digitalWrite(led2Pin, HIGH);
  digitalWrite(led3Pin, HIGH);
  digitalWrite(led4Pin, HIGH); 
}
}

Explanation :

Starting at a low range of 256 volts, if pot measure is less than this measure LED 1 pin will turn on therefore, every time this range increases by 256 the other LED’s will turn on respectively until the last LED which stops at a range of 1024 volts.

 

Thoughts :

I enjoyed creating this assignment, but i would like to create something more creative and complex in the coming up assignments.

 

 

 

 

Leave a Reply