Arduino goes nuts when doing this...

Hey guys!
Im new here, so please understand that im clueless :slight_smile:

Im working on a project to have my arduino control a small garden. Im using an LM35 temp sensor, a light sensor and a transistor to switch a reley for a 220v light.
So the problem is that my temp sensor goes a bit nuts when i start using anything else. The temperature goes up and down like crazy.

So my question is, how to connect multiple sensors and the relay properly so that i dont get this crazy behaviour?

im using a 2n2222 transistor and a 4007diode for the relay, all powered from the arduino. The temp sensor is also powered by the arduino and connected to an analog pin.
am i missing capacitors here?
im just lost here!

It could help if you paste here the code you are using.....

How do you power it? Show us how you wired everything (schematics, photo). What type of relay do you use?

hello,

im using a 5vDC/220V AC relay thats wired to emiter and collector of 2n2222 transistor, thats how its energized - i also put a diode to the reley - dont know why, but i read that it has to be there :slight_smile:

the base of the 2n2222 goes to a digital pin that is defined as output in the code.

the sensor in LM35, connected to 5v and ground on the arduino, with the output pin going to analog pin.

the code is:

temp = analogRead(tempPin);
temp = 5.0temp100)/1023
Serial.println((byte)temp);
delay(1000);

then i have an if statement, if temp is greater than 25 then digitalWrite(reley, HIGH) else LOW.

and thats it. :slight_smile:

ill paste pics and the full code later today, as i am now at work and dont have my project here with me.

Cheers!

temp = analogRead(tempPin);
temp = 5.0*temp*100)/1023
Serial.println((byte)temp);
delay(1000);

Please, post your code, not what you think you wrote.

Hello,

yes i was in a bit of a rush before:) so lets start at the beginning, i have an arduino rev3 with a motor shield.
What i want to accomplish in the end is an automated watering system - however, i've never dealt with micro-controllers before, so im testing stuff out as i go along.

So what i want to do here is connect an LM35 temperature sensor, a relay and 1 motor on channel A of the motor shield.
First off, i got a 9V power supply for the arduino, since it has to be autonomous.

ok, so here is my code:

float tempC;
int tempPin = 2;
int relay = 4;
int reading;
int mAdir = 12;
int mApwm = 3;
int mAbrake = 9;


void setup ()
{
  Serial.begin(9600);
  analogReference(INTERNAL);
  pinMode(relay, OUTPUT);
  pinMode(mAdir, OUTPUT);
  pinMode(mApwm, OUTPUT);
  pinMode(mAbrake, OUTPUT);  
}

void loop()
{
  reading = analogRead(tempPin);
  tempC = reading / 9.31;
  Serial.print(tempC);
  Serial.println("C  Temperature");
 
  if(tempC > 25.00)
  {
    digitalWrite(relay, HIGH);
    Serial.println("relay ON");
    digitalWrite(mAdir, LOW);
    digitalWrite(mAbrake,LOW);
    analogWrite(mApwm, 255);
    Serial.println("motor ON");
    
  }
  else
 {
   digitalWrite(relay, LOW);
   Serial.println("relay OFF");
   digitalWrite(mAbrake, HIGH);
 }
 delay(1000);
}

and this actually works fine! the problem begins when i try to add another sensor on the next analog pin!

float tempC;
int water;
int waterPin = 3;
int tempPin = 2;
int relay = 4;
int reading;
int mAdir = 12;
int mApwm = 3;
int mAbrake = 9;


void setup ()
{
  Serial.begin(9600);
  analogReference(INTERNAL);
  pinMode(relay, OUTPUT);
  pinMode(mAdir, OUTPUT);
  pinMode(mApwm, OUTPUT);
  pinMode(mAbrake, OUTPUT);  
}

void loop()
{
  water = analogRead(waterPin);
  reading = analogRead(tempPin);
  tempC = reading / 9.31;
  Serial.print(tempC);
  Serial.println("C  Temperature");
  Serial.println(water);
 
  if(tempC > 25.00)
  {
    digitalWrite(relay, HIGH);
    Serial.println("relay ON");
    digitalWrite(mAdir, LOW);
    digitalWrite(mAbrake,LOW);
    analogWrite(mApwm, 255);
    Serial.println("motor ON");
    
  }
  else
 {
   digitalWrite(relay, LOW);
   Serial.println("relay OFF");
   digitalWrite(mAbrake, HIGH);
 }
 delay(1000);
}

note that i havent connected any sensor to pin 3 yet, but as soon as i upload it i get a totally different reading on the temperature sensor, it just jumps around randomly, hovers at 9 degrees, then 30 etc...

any ideas?

Ok i figured it out!

Arduino cant switch between analog pins (they are multiplexed) as fast as it would want to read them!
the solution is first swithcing the pin (analogRead(pin)), then puting in a delay(10ms is fine) and then reading the analog again to get the real value!

It seemed to me that the analogs were somehow connecting, i had no idea that the Atmega only has 1 ADC for all analog pins...

Cheers!

Two consecutive reads, discarding the first value ought to be enough.