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);
}