Wrong value of temperature reader when switching relay? (Beginner)

I have a a problem. i am currently reading 3 analog values on my arduino.
but i also just connected a relay and when im switching that on the value of my temperature will increase, and when i switch it off it will decrease again.
The diffrence is about 10 celcius.

Its look pretty mutch like
20
20
20
20 (Switching relay on)
32
32
32 (Switching relay off)
20
20

Please notice, the relay is not connected to the temperature sensor and its only the temperature value thats affected by the relay the other ones acts as they supposed to do.
I have really no clue what im doing wrong… But i assume its something very stupid ::slight_smile:

Also im really new to this, the code is most likley written in a very bad way.
So please excuse that !

#include <Servo.h>
const int temperaturePin = 0;
int lightPin = A1; // select the input pin for ldr
int lightValue = 0; // variable to store the value coming from the sensor
int moistPin = A2; // select the input pin for ldr
int moistValue = 0; // variable to store the value coming from the sensor
int message = 0; // This will hold one byte of the serial message
int LEDPin = 13; // This is the pin that the led is conected to
int relay = 2; // 
char incomingOptionServo; //Servo
int servoPin = 9;  //Servo
Servo servo;

void setup()
{
pinMode(2, OUTPUT);
Serial.begin(9600);
servo.attach(servoPin);
}

void loop()
{
/* CALCULATES AND PRINT OUT ANALOG VALUES, MOIST, TEMP, LIGHT */
lightValue = analogRead(lightPin); // read the value from the sensor
moistValue = analogRead(moistPin); // read the value from the sensor
  float voltage, degreesC, degreesF;
  voltage = getVoltage(temperaturePin);   //Calculates Temperature
  degreesC = (voltage - 0.5) * 100.0;   //Calculates Temperature
  degreesF = degreesC * (9.0/5.0) + 32.0;   //Calculates Temperature

Serial.print(degreesC); //prints the values coming from TEMP sensor on the screen
  Serial.print(",");    "," = SPLIT
  Serial.print(moistValue); //prints the values coming from MOIST sensor on the screen
  Serial.print(",");      // "," = SPLIT
  Serial.println(lightValue); //prints the values coming from LIGHT sensor on the screen
   delay(1000);

/* CONTROLL RELAY & SERVO MOTOR */
  if (Serial.available() > 0) { // Check to see if there is a new message
message = Serial.read(); // Put the serial input into the message
if (message == '1'){ // IF "1" Turns on RELAY
digitalWrite(2, HIGH); // Turns on RELAY
}
if (message == '2'){ // IF "2" 
digitalWrite(2, LOW); // Turns off RELAY
}
if (message == '3'){ // IF "3" 
servo.write(20); // SET ANGEL TO 20
}
if (message == '4'){ // IF "1" 
servo.write(100); // SET ANGEL TO 100
}
}
}
float getVoltage(int pin)
{
  return (analogRead(pin) * 0.004882814);   //Calculates Temperature
}

Change what is connected to each analog pin. Does the problem follow the temperature sensor?

Most likely a hardware error. Probably a ground loop. Does the temperature sensor and relay share a common ground? Sensors should have their own ground, anyway.

PaulS: Change what is connected to each analog pin. Does the problem follow the temperature sensor?

No matter what pin im using for what, the problem is still on the temperature

KeithRB: Most likely a hardware error. Probably a ground loop. Does the temperature sensor and relay share a common ground? Sensors should have their own ground, anyway.

The problem might aswell be the hardware as you say. I have connected it like this. (I see i forgot some cables to the light sensor and maybe something else, also the relay being used is abit diffrent then mine)

The both grounds are connected so i assume that might be the problem?

|500x264

Bigger image: http://sv.tinypic.com/view.php?pic=10wl5q9&s=8#.VglbtfQaxiY

What kind of relay is it? Most likely, you should be having the Arduino toggle the gate on a transistor, and have it toggle the relay.

PaulS: What kind of relay is it? Most likely, you should be having the Arduino toggle the gate on a transistor, and have it toggle the relay.

Relay SPDT Sealed

https://www.sparkfun.com/products/100

Just left above the relay i have a NPN transistor.

Id like to say that i connected it similar to what they've done in sparkfuns tutorial https://learn.sparkfun.com/tutorials/sik-experiment-guide-for-arduino---v32/experiment-13-using-relays

The only diffrence that i have 6volt connected to one of the sides on my board, however even if i dont have that one plugged in, the error is the same. Also, if i pull out the red cable that goes between arduino and relay it keeps doing the same, if i pull out the black cable going from transistor to ground the problem stops

PP3 battery powering a relay and servo sounds like a recipe for power problems.

Thanks for all your answers!

UKHeliBob: PP3 battery powering a relay and servo sounds like a recipe for power problems.

Really? Why is that? i am not trying to argue with you, im just trying to understand what im doing.. The servo is running on 4-6volt if i am connecting a 5v fan or something equal to the relay i should be ok?

I am actually using a 12v DC outlet. The battery was as close as i could get!

I am actually using a 12v DC outlet.

So you are not using a PP3 battery after all. If you were then it is unlikely that it would provide sufficient current for very long.

In the circuit the servo appears to be connected to the battery which you say is really a 12V supply. If that is the case then how is the servo running on 4-6 V ?