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?
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.
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.