const int sensorPin = A0;
const float baseLineTemp = 20.0; //float values can store decimals.
void setup() {
Serial.begin(9600); //open a serial port
for(int pinNumber = 2; pinNumber<6; pinNumber++){
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, LOW);
}
}
void loop() {
int sensorVal = analogRead(sensorPin);
Serial.print(“Sensor Value: “);
Serial.print(sensorVal);
//convert the ADC reading to voltage
float voltage = (sensorVal/1024.0) * 5.0;
Serial.print(“, Volts: “);
Serial.print(voltage);
Serial.print(“, degrees C:”);
//convert the voltage to temperature in degrees
float temperature = (voltage – .5) * 100;
Serial.println(temperature);
if(temperature <= baseLineTemp){
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
}else if(temperature >= baseLineTemp+2 && temperature <baseLineTemp+4){
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
}else if(temperature >= baseLineTemp+4 && temperature < baseLineTemp+8){
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
}else if(temperature >= baseLineTemp+8){
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
}
delay(1);
}
|
Naughty! :-). If they say to use a resistor there’s a good reason for that! Switch it off, NOW!
The resistor is there to limit the LED’s current. If you omit it the current limiting has to come from the Arduino’s output, and it will not like it. How do you find out what the resistor needs to be? You do know Ohm’s Law? If you don’t, write it down in big letters: V=I⋅R Voltage equals current times resistance. Or you could say R=VI It’s the same thing. The voltage you know: Arduino runs at 5V. But not all that will go over the resistor. The LED also has a voltage drop, typically around 2V for a red LED. So there remains 3V for the resistor. A typical indicator LED will have a nominal current of 20mA, then R=5V−2V20mA=150Ω The Arduino Uno uses the ATmega328 microcontroller. The datasheet says that the current for any I/O pin shouldn’t exceed 40mA, what’s commonly known as Absolute Maximum Ratings. Since you don’t have anything to limit the current there’s only the (low!) resistance of the output transistor. The current may so well be higher than 40mA, and your microcontroller will suffer damage. edit The following graph from the ATmega’s datasheet shows what will happen if you drive the LED without current limiting resistor: Without load the output voltage is 5V as expected. But the higher the current drawn the lower that output voltage will be, it will drop about 100mV for every extra 4mA load. That’s an internal resistance of 25Ω. Then I=5V−2V25Ω=120mA The graph doesn’t go that far, the resistance will rise with temperature, but the current will remain very high. Remember that the datasheet gave 40mA as Absolute Maximum Rating. You have three times that. This will definitely damage the I/O port if you do this for a long time. And probably the LED as well. A 20mA indicator LED will often have 30mA as Absolute Maximum Rating. |