Termostat problems

Hi!

Im new to Arduino and got my starter kit yesterday. Im trying to build a termostat with a LM35 temperature sensor, a pot meter and a led. The temperature works perfect, until i connect the pot meter to the 5V pin. When i turn up the pot meter i get wierd reading from my temperatur sensor on the serial port (I use it for debuging). I get both higher and lower values from the temperatur sensor. Why is it like this? What can i do to get it working?

we need a diagram of how you have wired things up

jackrae:
we need a diagram of how you have wired things up

Or a photo.
And a link to the starterkit.

Here is how i have connected it up. Sorry for bad wiring, i know its a mess, but you can se how i have done it.

Here is the code, and it works, but i get strange readings from the temp sensor when i have connected the pot.

int tempPin = 0;
int ledPin = 12;
int potPin = 1;
int pot;
int termostat;
float tempC;

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

void loop() {
  tempC = analogRead(tempPin);
  tempC = (5.0 * tempC * 100.0)/1024.0;
  Serial.print("Temperatur: ");
  Serial.print(tempC);
  Serial.println(" grader");
  pot = analogRead(potPin);
  termostat = map(pot, 0, 1023, 20, 30);
  Serial.print("Termostat: ");
  Serial.print(termostat);
  Serial.println(" grader.");
  if (tempC > termostat) {
     digitalWrite(ledPin, HIGH);
  } else {
     digitalWrite(ledPin, LOW);
  }
  delay(1000);
}

Thanks for the picture and code.
Your connections look okay to me.
But in your code you mix analog with digital.

pinMode(1) sets the digital pin '1'. That's mixing digital with analog.
For an analog input, using analogRead(), you don't need to set it with pinMode().
The led is the only digital output, so use pinMode() only for the led.
Remove the two other pinMode() and see what it does.

I removed pinMode for everyting but the LED. It didnt help. Here is my reading from serial monitor:

Temperatur: 21.48 grader
Termostat: 30 grader.
Temperatur: 42.97 grader
Termostat: 30 grader.
Temperatur: 31.74 grader
Termostat: 30 grader.
Temperatur: 27.83 grader
Termostat: 30 grader.
Temperatur: 24.90 grader
Termostat: 30 grader.
Temperatur: 43.46 grader
Termostat: 30 grader.
Temperatur: 40.04 grader
Termostat: 30 grader.
Temperatur: 33.20 grader
Termostat: 30 grader.
Temperatur: 36.62 grader
Termostat: 30 grader.

Its normaly reads 22 when the pot is not connected. Btw, the pot works, but the temperature is wierd.

There appears nothing obviously wrong with your diagram - are you 100% certain your wiring is connected as shown.

Yes, i am. Everything works, the pot, the led and the temperature sensor. Can it be too little power or anyting? Noise from the pot? I dont know.

Is the pot 100 Ohms ?
Do you use the USB to supply the 5V.

A pot of 10k would be normal.
You could try a adapter of 6...12V as power supply.

THe pot is a 100k from the fritzing starter kit.

I use 5V from the USB port.

I can try using a other power source of you think that will help, but i dont have any here now.

when you remove code with // about the pot, do the temp values go back to normal?

No, it doesnt work if i dont remove the + wire from the pot.

Try making 2 analogRead calls in succession from the temp sensor. Throw away the first reading and use the second.

Also check that your Arduino is not resting on a conductive surface, and you don't have a short between the 2 analog inputs you are using.

You don't show how you are powering the system. USB, battery and if the latter what voltage, type and size.

There is a possibility (remote ?) that the pot wires are inducing noise into your temperature sensor input circuit. Perhaps placing 0.1 capacitors between ground and each of your 2 inputs will dampen any noise.

I have tried both with USB power and a 9 V battery with the same result.

But now i made a new setup, just swaped the pot meter with a LDR. I have the same led, and temp. sensor. I have the same problem with the temp sensor when i have the LDR connected. Can it be a bug in Arduino when i use 2 analog ports?

Yes, there is a bug in this area. The readings you get from calls to analogRead() from 2 different pins interfere with each other when the source resistance driving one or both pins is more than a few Kohms, because the authors of the Arduino analogRead() code chose not to put even the smallest delay between selecting the correct input and starting the ADC conversion. Calling analogRead() twice in succession for the same pin and discarding the first reading is a partial workaround - see my earlier reply.

The readings you get from analogRead() can also be affected by turning output devices on and off if you use a common ground connection. To avoid this, dedicate one of the Arduino ground pins as sensor ground, and connect the ground sides of your analog sensors and any associated filter capacitors to that pin. Use a different Arduino ground pin to connect power and the ground sides of any output devices such as LEDs.