Temperature sensor projects

Hello! I have been trying to create a project for school that has a temperature sensor and it takes the temperature, then lights up LEDs according to how hot something is according to the sensor. But when I plug in the sensor and run everything, nothing happens. Then when I plug the sensor back in all the lights blink then turn on and hold. Its like the not only the sensor is taking a reading but when the sensor is even connected to the breadboard, it kills everything. This is also known as the 4th Arduino starter kit project, but anyways here is my code, thanks all in advance.

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

void setup()
{
Serial.begin(9600); // opens a serial port and sets speed to 9600 bits per second

for(int pinNumber = 2; pinNumber<5; pinNumber++) // creates local variable, sets it to 2, tells it to add one each time and stop at 5
{
pinMode(pinNumber, OUTPUT);
digitalWrite(pinNumber, LOW);
}

}

void loop()
{
// creates local variable, and calls analogRead() to read the pin AO (line 1) and stores that in variable

int sensorVal = analogRead(sensorPin);
Serial.print("Sensor Value: "); // Prints “Sensor Value”
Serial.print(sensorVal); // and puts in the sensorVal value in

// Converts sensor reading and converts it into voltage

float voltage = (sensorVal/ 1024.0) * 5.0;

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

// Converts the voltage to temp, and sends that to the computer

Serial.print(" , Degrees C: ");

float temperature = (voltage - .5) * 100;// convert the voltage to a temperature in degrees C

Serial.print(temperature);

if(temperature < baselineTemp)
{
digitalWrite(2, LOW);
digitalWrite(3, LOW); // Turns LEDs off for a low temp
digitalWrite(4, LOW);

}

else if(temperature >= baselineTemp+2 && temperature <= baselineTemp+4)
{
digitalWrite(2, HIGH);
digitalWrite(3, LOW); // Turns on 1 LED for a lowish temp
digitalWrite(4, LOW);
}

else if(temperature >= baselineTemp+4 && temperature <= baselineTemp+6)
{
digitalWrite(2, HIGH);
digitalWrite(3, HIGH); // Turns on 2 LEDs for a meduim temp
digitalWrite(4, LOW);
}

else if(temperature >= baselineTemp+6)
{
digitalWrite(2, HIGH);
digitalWrite(3, HIGH); // Turns on all LEDs for a high temp
digitalWrite(4, HIGH);
}
delay(1);
}

If you open Tools > Serial Monitor while the sketch is running, what do you see printed in the Serial Monitor window?

Welcome to the list. Please read HOW TO USE THIS FORUM

pay attention to #7 Come back and edit your post. Bottom right of your post is the modify button

   float voltage = (sensorVal/ 1024.0) * 5.0;
   
   Serial.print(" , Volts: ");
   Serial.print(voltage);
   
   // Converts the voltage to temp, and sends that to the computer
   
   Serial.print(" , Degrees C: ");
   
   float temperature = (voltage - .5) * 100;// convert the  voltage to a temperature in degrees C
   
   Serial.print(temperature);

just so I understand.

analog in is 0-1023
you read the output of the temperature sensor.

put that into voltage.

assume you have room temperate at mid scale, your analog value would be mid-scale of 1024
so 512

so the first calculation is (512 / 1024) * 5.0 =
512/1024 = 0.5
0.5 * 5 = 2.5

so, the first reading is 0.5 * 5 = 2.5 // meaning 2.5 volts

the second math is

float temperature = (voltage - .5) * 100;// convert the voltage to a temperature in degrees C

so, ( 2.5 -0.5 ) = 2.0
2.0 * 100 = 200

=============

if you follow the sugestion of Pert, you should see:

, Volts: 2.5 , Degrees C: temperature 200 , Volts: 2.5 , Degrees C: temperature 200 , Volts: …

is that about right ?

======

at this early stage of your learning, you might want to set the delay to 250 or 500
that is 1/4 or 1/2 second to read and print the data to your monitor.
delay(1) going to have your monitor fill up quickly.

delay() is a poor choice when you want to do things, but for this stage of your learning it will be helpful.

====================

one question is that of plugging in and un-plugging the sensor into the Arduino.
do you do that with power off ?