-1

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 complete 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;
}
0

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.