Pages: [1] 2   Go Down
Author Topic: Lcd vs serial monitor  (Read 1898 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a arduino UNO and a dfrobot keypad LCD shield.  When I tested my lm35 temp sensor whit serialread I get good results. When I change de code  to lcd.print everyting goes well until I reach 31 degrees C then I get strange things on the LCD. If i use lcd.print(A0) he gives me the same Numbers as serial monitor. It's like the translation of the equation to the LCD goes wrong
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Strange problem,

Please post your code so we can check it
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

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

Code:
/*-----( Import needed libraries )-----*/
#include <LiquidCrystal.h>

/*-----( Declare objects )-----*/
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); //These are the pins used on this shield

int up = 11;
int down = 12;
const int NO = 13;

int plus = 0;// variable for reading the pin status
int minus = 0;
int counter = 20;
int currentState = 0;
int previousState = 0;

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  lcd.begin(16, 2);              // start the lcd object

  pinMode(up, INPUT); // declare pushbutton as input
  pinMode(down, INPUT);
  pinMode(NO, OUTPUT);


}/*--(end setup )---*/

void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
  lcd.setCursor(0,1);
  lcd.print(counter);

  // read the input on analog pin 0:
  int Tval = analogRead(A1);


  plus = digitalRead(up); // read input value
  minus = digitalRead(down);

  if (plus == HIGH) {
    currentState = 1;
  }  // check if the plus is HIGH (button released)

  else {
    currentState = 0;
  }               //  plus
  if(currentState != previousState){
    if(currentState == 1){
      counter = counter + 1;

      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print(counter);



    }
  }

  if (minus == HIGH) {
    currentState = 1;
  }  // check if the minus is HIGH (button released)

  else {
    currentState = 0;
  }                //minus
  if(currentState != previousState){
    if(currentState == 1){
      counter = counter -1;

      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print(counter);
    }
  }
  previousState = currentState;


  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float temp = (5 * Tval * 100) / 1024;

  // print out the value you read:
  lcd.setCursor(0,0);
  lcd.print(temp);
  lcd.setCursor(2,0);
  lcd.print("C  ");

  float contact = (counter - temp);

  if (contact >=1)
  { 
    digitalWrite(NO, HIGH);
  }
  else   { 
    digitalWrite(NO, LOW); 
  }


  delay(1000);





}


when i try the thermometer code separate everything works fine. when i put it in my thermostat code i get strange readings at 32° C it becomes unreadable on the lcd. With the serial monitor it shows minus degrees. Can anyone explain what is wrong with the code.
I have every function of the code working fine but when i combine everything it goes wrong

thx
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the formula (5 * Tval * 100)  are all ints and the math will overflow (and becomes negative or so) when Tval > 65.
When tVal ==65 the formula resolves to 32 ...


so please change
Code:
float temp = (5 * Tval * 100) / 1024;

to
Code:
float temp = Tval * 0.48828125;  // do all the math in one step

tip:
lcd.print("°C  ");  // ALT-0176 special char


tip2:
please press CTRL-T befor posting code, remove unneeded comments (only explain WHY is enough)
makes the code better readable.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Texas
Offline Offline
Edison Member
*
Karma: 25
Posts: 1298
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If i use lcd.print(A0) he gives me the same Numbers as serial monitor

Are you reading A0 or A1 ?

Quote
  // read the input on analog pin 0:
  int Tval = analogRead(A1);
Logged

Good luck, Jack

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

Thx
 
the reading is correct now, but the °C thing did not work i get a other symbol

the sensor is A1 because its a keypad lcd shield that uses the A0 for the keypad. Almost al my code is copy/pasted so there can be some errors copied in to.

My next step is to change the digital buttons whit the buttons on the keypad. If anyone has tips please let me know.

thanks
Logged

Israel
Offline Offline
Sr. Member
****
Karma: 5
Posts: 281
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Try lcd.write(0xDF); for the ° sign. Or create your own custom character.
Logged


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

that worked, everything works now. I just found a bug. when the relay switches on, the voltage drops and the reading drops also, the result is that the relay goes on an of until the differential is more than de voltage drop.
« Last Edit: August 06, 2013, 05:35:55 pm by Fossaert » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Give the relay a power supply of its own.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Texas
Offline Offline
Edison Member
*
Karma: 25
Posts: 1298
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can do several things.
Do what robtillaart said.
after the relay is turned on, delay in the code, before reading the input again.
Put more capacitors in.

I don't think you have shown us the schematic for the relay. Maybe a bit of reworking it may help. What is the amps needed to run the relay, etc.
Logged

Good luck, Jack

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

according to the datasheet its 90mA so that is not to much. the voltage drop is 0,2V so that has an effect on the lm35 I presume, c have to create a potential free contact so it kan turn on my heating system. perhaps I can use somting else?


* thermostaat.jpg (995.98 KB, 2355x1710 - viewed 41 times.)
Logged

Offline Offline
God Member
*****
Karma: 16
Posts: 592
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

90mA is more than 2X the maximum amp. an arduino output can source, you need a transistor to drive the relay
Logged

Texas
Offline Offline
Edison Member
*
Karma: 25
Posts: 1298
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You ask:
Quote
My next step is to change the digital buttons whit the buttons on the keypad. If anyone has tips please let me know.
I don't know for sure which "LCD keypad shield" you have, but I think the most common one is like mine. If you give us the url for yours we can be more accurate.
On mine, I use this sketch to read the keys.
Code:
void keypressed(){
  analogRead(A0);
  int value = analogRead(A0)/12;
  if(value<70){keyvalue=60;}else{keyvalue=80; return;}
  if(value<50)keyvalue=39;
  if(value<31)keyvalue=25;
  if(value<16)keyvalue=10;
  if(value<5)keyvalue=0;
  lcd.setCursor(15,1); // set cusor position to row 1, col 1
  //lcd.print(keyvalue);   // value on A0
 
  if(keyvalue==85) return;
  if(keyvalue==10) {
    lcd.print("^");
  } 
  if(keyvalue==25) {
    lcd.print("V");
  } 
  if(keyvalue==39) {
    lcd.print("<");
    barChar--;
  } 
  if(keyvalue==0) {
    lcd.print(">");
  } 
  if(keyvalue==60) {
    lcd.print("*");
  } 
  if(keyvalue==25)   {  adjustTime(-62); delay(100);}
  if(keyvalue==10)   {  adjustTime(1); delay(100);}
  } 
Logged

Good luck, Jack

United Kingdom
Offline Offline
Tesla Member
***
Karma: 224
Posts: 6593
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If you're trying to do precise measurements on the analog inputs, don't use the 5V supply as the reference voltage. It's nowhere near stable enough if you are powering the Arduino from USB, or if you have heavy switched loads (such as your relay) powered from 5V. Instead, connect the AREF pin to the 3.3V pin and put an analogReference(EXTERNAL) call in setup, before you do any analogRead calls. The maths will have to change because the 5 in your original formula now needs to be 3.3. A side-effect is that your temperature resolution will be improved from about 0.5C to about 0.32C.

btw I recommend using the LM34 sensor rather than the LM35, because it gives you more than twice the resolution and can measure temperatures below freezing. You can easily convert its output to Celsuis in the Arduino software.

Since the LM35 produces an output of 1V or less for temperatures below 100C, another possibility is to use the internal voltage reference for the ADC. However this varies between chips (it can be anywhere between 1.0 and 1.2V), so you would have to calibrate it.

And as alnath says, you need to drive your relay through a transistor.
« Last Edit: August 11, 2013, 04:43:02 pm by dc42 » Logged

Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

Texas
Offline Offline
Edison Member
*
Karma: 25
Posts: 1298
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe a SSR would be better than a relay.
Logged

Good luck, Jack

Pages: [1] 2   Go Up
Jump to: