Potentiometer interfere with the temperature reading

I have a problem with my project, the potentiometer is changing the temperature of the LM35 sensor

I tried to delay, serial flush, change the ports, remove the relay and LCD display to prevent overcharging and nothing.

I made a video today to show the problem.

Can anyone help me please? The code is below.

Sorry, but have portuguese comments.

#include <LiquidCrystal.h>                 //Inclui o drive para o display LCF.
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);     //Define as portas digitais que o Display ira trabalhar.
int sensortemp = A0;                       //Define a porta analogica para captura da temperatura.
int valmed = 0;                            //Guarda a informação do potenciometro, definida como zero no inicio.
int pot = A1;                              //Define a porta analogica para conexão do potenciometro. 
int sensorvalor = 0;                       //Guarda a informação do sensor de temperatura, definida como zero no inicio.
float temperatura = 0;                     //Guarda a informação de temperatura calculada, definida como zero no inicio.
int rele = 8;                              //Define a porta digital 8 como sendo a do rele.         
int tempo = 200;                           //Define a variavel tempo para o funcionamento do ckt.

void setup() {
  
  pinMode(rele,OUTPUT);                   //Define o Rele como saida.
  lcd.begin(16, 2);                       // Definir o numero de colunas e linhas do display LCD
}

void funcao1(void){   
  
 valmed = map(analogRead(pot),0,1023,10,30);    //Faz a proporção do range do potenciometro para representar valores entre 10ºc e 30ºc.

delay(tempo); 
Serial.flush();

}

void funcao2(void){
  
  sensorvalor = analogRead(sensortemp);          //Salvar o valor do sensor de temperatura na variavel sensorvalor.
  temperatura =(500*sensorvalor)/1023;            //Multiplicação para transformar os valores convertidos de mili para escala real. 
Serial.flush();
delay(tempo);                                    //Define um atraso para economizar memoria e processamento. 
  
}

void funcao3(void){
  
  lcd.setCursor(0, 0);                           //Defini o inicio na primeira Linha
  lcd.print("Temp:");                            //Escreve a palavra entre "" no visor.
  lcd.print(temperatura,0);                      //Escreve a temperatura calculada no visor. ",0" define que o numero não apresentara fração. 
  lcd.print("c");                                //Escreve a palavra entre "" no visor.
  lcd.setCursor(0, 1);                           //Defini o inicio na primeira Linha
  lcd.print("Set :");                            //Escreve a palavra entre "" no visor.
  lcd.print(valmed);                             //Escreve a temperatura desejada no visor.
  lcd.print("c");                                //Escreve a palavra entre "" no visor.
  
  
  if (temperatura > (valmed+1)) {                //Define uma condição da temperatura ser maior que a desejada em 1ºC a mais. 
    digitalWrite(rele, HIGH);                    //Ação de ligar o rele.
}
if (temperatura < (valmed-1)) {                  //Define uma condição da temperatura ser menor que a desejada em 1ºc a menos.
  digitalWrite(rele, LOW);                       //Desliga o rele.
}
Serial.flush();
delay(tempo);                                    //Define um atraso para economizar memoria e processamento. 
}

void loop() { 
funcao1();
funcao2();
funcao3(); 

delay(tempo); 

}

Caixa_termica2.ino (3.06 KB)

Hi josepetri

Try putting two analogRead() statements in each function, throw away the first value you read and use the second value.

void funcao1(void)
{   
  int junk = analogRead(pot);  
  valmed = map(analogRead(pot),0,1023,10,30);    
  delay(tempo);   
  etc
void funcao2(void)
{
  int junk = analogRead(sensortemp);
  sensorvalor = analogRead(sensortemp); 
  etc

The Atmega chip in the Arduino has one ADC that is multiplexed between the different analog input pins. If the circuits you have connected to the input pins are of relatively high impedance, the voltage on one input pin can affect the reading on the other pins.

Regards

Ray

Hello and welcome :slight_smile:

I'm not an expert, but try to put 0.1uF capacitors, from A0 to GND and from A1 to GND, it might help.

Serial.flush() just waits until serial output is complete. So stop doing it.

 lcd.setCursor(0, 0);                           //Defini o inicio na primeira Linha
  lcd.print("Temp:");                            //Escreve a palavra entre "" no visor.
  lcd.print(temperatura,0);                      //Escreve a temperatura calculada no visor. ",0" define que o numero não apresentara fração. 
  lcd.print("c");                                //Escreve a palavra entre "" no visor.
  lcd.setCursor(0, 1);                           //Defini o inicio na primeira Linha
  lcd.print("Set :");                            //Escreve a palavra entre "" no visor.
  lcd.print(valmed);                             //Escreve a temperatura desejada no visor.
  lcd.print("c");                                //Escreve a palavra entre "" no visor.

you need to clear (write spaces to the whole of each line of the display) before writing the new data.

Instead of

void loop() { 
funcao1();
funcao2();
funcao3(); 

delay(tempo); 

}

Try

void loop() { 
funcao1();
funcao1();
funcao2();
funcao2();
funcao3(); 

delay(tempo); 

}

Get rid of all the delays except the one in loop, they are not needed.

Mark

Hi! Thank you very much for the help people, they work and my project is much better now, but I discovery another problem, this time is with the Rele.

When I turn on the rele, the temperature change 3 degrees, If I turn of the temperature become to the correct measure.

Any one have a glue about what is the problem?

I tried to link the ground, try to use another source of power for the rele, try to link the other source of power with the Arduino board, try to use a capacitor 0.1u and 0.01u with ground and power, try to change the ports, try to change the rele, try to change the sensor LM35 and the problem still there =/

here are the new video with the new problem LOL

Thank you for the help people =]

Hi.

You need to read the comments more carefully.
The capacitor wasn't meant to be put in the power lines.
It was meant to be put between the analog input and GND, which by the way is a real bad advice.
The idea must have been to stabilize the levels at that input, but it's more likely to screw up your reading.

Do you have a multimeter ?
If so, read the 5 volt power supply from your Arduino while the relay switches on and off.
Do you see the 5 volts dropping ?

Try to power the relay from an external power supply, and make sure the GND's (of Arduino and the external supply) are connected.
[edit]I'm assuming and hoping the relay board has it's own transistor[/edit]

By the way, did you solve the first problem, and how did you do that ?

The breadboard's power bus is separated.
It needs to be joined:

dlloyd:
The breadboard's power bus is separated.
It needs to be joined:

Yes, I have made this connection, this is not the problem, but thank you for comment =]

MAS3:
Hi.

You need to read the comments more carefully.
The capacitor wasn't meant to be put in the power lines.
It was meant to be put between the analog input and GND, which by the way is a real bad advice.
The idea must have been to stabilize the levels at that input, but it's more likely to screw up your reading.

Do you have a multimeter ?
If so, read the 5 volt power supply from your Arduino while the relay switches on and off.
Do you see the 5 volts dropping ?

Try to power the relay from an external power supply, and make sure the GND's (of Arduino and the external supply) are connected.
[edit]I'm assuming and hoping the relay board has it's own transistor[/edit]

By the way, did you solve the first problem, and how did you do that ?

Sorry, I made the right thing but speak the wrong LOL, I did it, put the capacitor in the port, but don't work neither.

I have tried with external source supply, with the link with 2 grounds and don't work, and the relay board have his own transistor, it is made for arduino =]

I solve the problem now, I change the relay for one transistor TIP 225, and use a photocoupler.

I solved the first problem with the tips sand in this post, I use double read functions and the problem disappeared like magic =D

Thank you for every one, now my circuit is ok =D