Error in reading thermocouple when I turn on/off a relay

In the following code, I will read A1 which is an analog input from a thermocouple, A2 another input from a displacement sensor and then I will control a relay connected to pin 6 to switch it on/off.

The problem is that when I read zero C temperature it shows 14 C but when I remove "pinMode(6, OUTPUT);" then the temp. reads correctly (zero C) but the relay does not work anymore??!!

Anybody know what is happening? What should I do?

int sensePin1 = A1;  
int sensepin2=A2;
int sensorInput;    
int sensorInput2;    
double temp;        
double tempvolt;
double dis;         
double dis0; 
int in1 =7;
int i=0;
unsigned long int milli_time;    //variable to hold the time



void setup() {
 
Serial.begin(9600); //Start the Serial Port at 9600 baud (default)
Serial.println("CLEARDATA");       
Serial.println("LABEL,Position,Time (Milli Sec.),i,Temp");  
                            

}
void loop() {
 
 sensorInput = analogRead(A1);    //read the analog sensor and store it
 sensorInput2 = analogRead(A2);    //read the analog sensor and store it


  tempvolt=sensorInput;
  temp = 0.5452*tempvolt - 1.1888;


 sensorInput2 = analogRead(A2);    
 dis0=sensorInput2;
 dis=0.0671*dis0+33.389; 


 pinMode(6, OUTPUT);
 
 
 if (temp <10) {
   digitalWrite(6,LOW);
   
     
 }
else if (temp>20){
    
  digitalWrite(6,HIGH);
  
  delay(600);
  i=i+1;
 
   
 }

 milli_time = millis();

 Serial.print("DATA,TIME");
 Serial.print(",");
 Serial.print(dis);
 Serial.print(",");
 Serial.print(i);
 Serial.print(",");
 Serial.println(temp);
Serial.print(",");

   

delay(600);
}

First thing you need to do is read the guides at the start of the forum as to how to use the forum ad specifically how to post code. It makes it that much easier for the people who would like to help you, otherwise in frustration they might just turn away.

Thanks for your point, I did that. Would you please help me with this issue?

Hi, in this code I am controlling a switch base on temperature which is read by A1.

When I use " pinMode(6, OUTPUT);" to turn on/off the switch the temperature is not correct( it reads 14 C instead of 0 C for instance). But when I remove this line, the temperature reading is fine but the switch does not work anymore. Does anybody know the solution?
Thanks in advance,

int sensePin1 = A1;  
int sensepin2=A2;
int sensorInput;    
int sensorInput2;    
double temp;        
double tempvolt;
double dis;         
double dis0; 
int in1 =7;
int i=0;
unsigned long int milli_time;    //variable to hold the time



void setup() {
 
Serial.begin(9600); //Start the Serial Port at 9600 baud (default)
Serial.println("CLEARDATA");       
Serial.println("LABEL,Position,Time (Milli Sec.),i,Temp");  
                            

}
void loop() {
 
 sensorInput = analogRead(A1);    //read the analog sensor and store it
 sensorInput2 = analogRead(A2);    //read the analog sensor and store it


  tempvolt=sensorInput;
  temp = 0.5452*tempvolt - 1.1888;


 sensorInput2 = analogRead(A2);    
 dis0=sensorInput2;
 dis=0.0671*dis0+33.389; 


 pinMode(6, OUTPUT);
 
 
 if (temp <10) {
   digitalWrite(6,LOW);
   
     
 }
else if (temp>20){
    
  digitalWrite(6,HIGH);
  
  delay(600);
  i=i+1;
 
   
 }

 milli_time = millis();

 Serial.print("DATA,TIME");
 Serial.print(",");
 Serial.print(dis);
 Serial.print(",");
 Serial.print(i);
 Serial.print(",");
 Serial.println(temp);
Serial.print(",");

   

delay(600);
}

in this code I am controlling a switch base on temperature which is read by A1.

Could we get that in English please?

  1. Temperature is read by "analog input A1".
  2. Based on this temperature, I will command "output 6" to be "LOW" or "High" so as to switch on/off a relay.

Hope that helps!

Move pinMode(6, OUTPUT); to setup().

How often do you need to set the pin mode? Most people do it once in setup(). Have you tried moving that line.

Why set pinMode() every time round loop()? It's only needs to be done once in setup().

And reading two analog pins rapidly can cause problems because the ADC doesn't have time to settle. Putting a delay(1) or maybe delay(2) between them usually solves that.

Steve

I thought the standard way to read two analog pins back to back was to add a throw away read before each actual read.

adwsystems:
I thought the standard way to read two analog pins back to back was to add a throw away read before each actual read.

I'm pretty sure there are no standards but I've heard of that trick too. I've just never needed it cus a short delay has worked for me so far.

Steve

There's a very similar issue here

@m25269, please do not cross-post. Threads merged.

Where does the relay coil power come from? If from the 5V pin, it may be causing a small voltage dip that could throw your AREF voltage off. Please post a wiring diagram.

Thank you all.

The relay power comes from the 5v pin.

I moved the "pinMode" to setup(), it did not help.

I used the GND beside the 5-volt pin for the relay, later I used the GND pin next to digital pins and it seems the thermocouple and relay are both working properly.

I thought all of the GND pins on the board are connected, is not it true?