0

I want to program a temperature sensor with a LED using Arduino. the goal is to make a sensor, when the temperature is over than Temperature max, the LED turns off and when the temperature is small than temperature max we turn on the LED.

my problem is when I programed the void setup() as followed, the LED doesn't work, and when I comment Serial.begin (9600) the LED work but I cannot diplay the value of the temperature.

void setup() {
   Serial.begin(9600);
   pinMode(tempPin, INPUT);
   pinMode(led, OUTPUT);
}

here you find my complet code:

    int tempPin = A0;   // the output pin of LM35
float Max_temp = 24.0;
int led = 1;

float  temp, cel;
void setup() {
   Serial.begin(9600);
   pinMode(tempPin, INPUT);
   pinMode(led, OUTPUT);
}
void loop() { 
      cel = readTemp(); 
  if(cel< Max_temp) {        // if temp is higher than tempMax
      digitalWrite(led, HIGH);  // turn on led
      delay(300);
     } 
     else {                    // else turn of led
       digitalWrite(led, LOW);  // turn on led
      delay(100);
  }
   Serial.print("TEMP: ");
   Serial.print(cel);      // display the temperature
   Serial.print("C ");
   Serial.println();
   delay(300);  
}

float readTemp() {  // get the temperature and convert it to celsius
  temp = analogRead(tempPin);
  return temp * 0.48828125;
}

thank you very much in advance

2
  • I think that the others have more than enough covered it, but next time please edit your question (link). If you have any questions please feel free to respond to me with @AnnonomusPenguin. Thanks! Commented Nov 5, 2015 at 23:19
  • there are 2 excellent answers below. it is only polite to extend the authors the courtesy of an upvote and/or comment and/or accept it. ;)
    – tony gil
    Commented Jun 25, 2019 at 21:51

2 Answers 2

4
int led = 1;

You do know that pins 0 and 1 are the serial port don't you?

Take your pick.... Either use pin 1 for the LED OR use pin 1 for the serial connection. You can't do both!

As it says in the manual:

In addition, some pins have specialized functions:

  • Serial: 0 (RX) and 1 (TX). Used to receive (RX) and transmit (TX) TTL serial data. These pins are connected to the corresponding pins of the ATmega8U2 USB-to-TTL Serial chip.
0
1

Your logic is backwards. Your program says if the temp is LESS THAN max then turn on the LED.

But worse than that, you should not use pin 1. Pick any other pin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.