For my first project I wanted to create a temperature reader that made LEDs turn on depending on the temperature read by the sensor.
Step 1: The Materials
- Arduino Uno
- Breadboard
- 3 red LEDs
- Jumper wires
- Temperature Sensor
- To start I connected the breadboard to GND (ground) and 5V (voltage)
- Next I inserted the LEDs, 2 220 resistors for each and 3 yellow jumpers to attach them to the Arduino
- Then I inserted the TMP (temperature) sensor and the appropriate jumpers for that as well.
const int sensorPin = A0;
const float baselineTemp = 20.0;
void setup(){
Serial.begin(9600); //open a serial port
for(int pinNumber = 2; pinNumber<5; pinNumber++){
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, LOW);
}
}
void loop(){
int sensorVal = analogRead(sensorPin);
Serial.print("Sensor Value: ");
Serial.print(sensorVal);
//convert sensor reading to voltage
float voltage = (sensorVal/1024.0) * 5.0;
Serial.print(", Volts: ");
Serial.print(voltage);
Serial.print(", degrees: ");
//convert voltage to temp
float temperature = (voltage - .5) * 100;
Serial.println(temperature);
if(temperature < baselineTemp){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}else if(temperature >= baselineTemp+2 && temperature < baselineTemp+4){
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}else if(temperature >= baselineTemp+4 && temperature < baselineTemp+6){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
}else if(temperature >= baselineTemp+6){
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
}
delay(3);
}
Reflection:
This was day 1 of the Arduino Project. I did not focus on one project and expand because I wanted to see how many different programs I could discover. I had some difficulty with the temperature sensor and I had to work on it outside of class. There was a point when the temperature sensor actually became burning hot and actually burned me...so I figured something wasn't right. After a few adjustments in my code I was able to get my "Love-o-meter" working.
No comments:
Post a Comment