Pages: [1]   Go Down
Author Topic: Arduino goes nuts when doing this...  (Read 449 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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!
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 47
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Switzerland
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4702
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

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.0*temp*100)/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. smiley


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

Cheers!
« Last Edit: March 06, 2013, 10:52:02 am by Dulimir » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24321
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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!

Code:
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?


* photo.JPG (1891.47 KB, 2592x1936 - viewed 11 times.)
« Last Edit: March 06, 2013, 02:06:30 pm by Dulimir » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24321
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: