Sensors super inconsistent

Hello,

I'm super new to arduino, having just bought the starter kit. While going through the beginner projects, I'm having a bunch of issues with inconsistent sensors.

The temperature sensor will often jump like 10 degrees out of nowhere, making the temperature project pretty unusable, and having just built the "color mixing lamp" project (three phototransistors with colored plastic over them controlling a tri-color LED), I'm sitting here watching it and it will have a consistent color for like a quarter second before turning completely to one of the three colors or white, then back again. This happens super inconsistently (when I pull the plug then put it back in, theres like a 10% chance there won't be an issue until I cover one of the sensors, then it will start again).

I've double checked my construction and code, and as far as I can tell they are both correct. Did I get bum sensors, or am I doing something wrong? I'm powering it using the usb chord into a usb 2.0 slot, if that matters.

materials I'm using can be found in tech specs here:

https://store.arduino.cc/usa/arduino-starter-kit

Temperature sensor is TMP36

My code:

const int sensorpin = A0;
float baselinetemp = 20;

void setup() {
 //delay(1000);
 //const float baselineval = analogRead(sensorpin);
 //const float baselinevoltage = (baselineval/1024.0)*5.0;
 //baselinetemp = (baselinevoltage -.5)*100;
  Serial.begin(9600);
  Serial.print("Beginning Temp");
  Serial.println(baselinetemp);

  for(int pinnumber = 2; pinnumber<5; pinnumber++){
  pinMode(pinnumber,OUTPUT);
  digitalWrite(pinnumber,LOW)  ;
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  int sensorval = analogRead(sensorpin);
  Serial.print("sensor value: ");
  Serial.print(sensorval);

  float voltage = (sensorval/1024.0)*5.0;
  Serial.print(", Volts: ");
  Serial.print(voltage);
  Serial.print(", degrees C: ");
  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(1);
}

For informed help, please read and follow the directions in the "How to use this forum" post.

Not to be rude, but could you explain what I should add? I didn't put any code in due to the problems seeming to not be project specific.

Luxxum:
I've double checked my construction and code, and as far as I can tell they are both correct. Did I get bum sensors, or am I doing something wrong? I'm powering it using the usb chord into a usb 2.0 slot, if that matters.

Ultra simple troubleshooting idea;

Reduce your code so that it just repeatadly prints the temperature to serial console, remove all other code.

Watch the temperature for a while, does it suddenly jump ?

I'm super new to arduino ...............I've double checked my construction and code, and as far as I can tell they are both correct.

If your 'super new to arduino' is it possible your understanding of correct code is mistaken ?

Luxxum:
Not to be rude, but could you explain what I should add? I didn't put any code in due to the problems seeming to not be project specific.

Post your circuit and your code, we cannot possible guess these, and solving any problems requires
them. Also detail exactly which sensor you are using, this information is not available from the link
you give.

Its very common for faults to be in the parts you think are working, not where you think they are (otherwise
you'd be able to fix them for yourself?)

I'm also one of those who can't be bothered doing your homework - in this case digging through the details of a starter kit trying to guess what you have done. You have to help us help you.
Which temperature sensor? (type number).
What's YOUR circuit look like? (hand drawn diagram is fine).
What code are you actually running? (copy/paste between code tags - as described in the sticky).

srnet:
Ultra simple troubleshooting idea;

Reduce your code so that it just repeatadly prints the temperature to serial console, remove all other code.

Watch the temperature for a while, does it suddenly jump ?

Did what you said, and there isn't a jump. However, the temperature is now ~10 C rather than the ~50 C it was listed as before. I uploaded my circuit and code at the top.

I probably didn't explain it well before, but basically, no matter how much delay you put from turning the circuit on to having the component read, it starts out saying the temperature is mid 20's, before jumping up to 50's. right now the "20's" section is only like a half second long, but the first time I built the circuit, it would last for more like 5 second before jumping.

srnet:
If your 'super new to arduino' is it possible your understanding of correct code is mistaken ?

Very likely. The issue is that I'm copying the code given in the manual word-for-word (other than the commented out bits, where I was trying to give the component a chance to warm up before reading). However there are an insane amount of typos in the manual, so I guess anything is possible.