Hi Everyone,
Im trying to read out a 100kOhm NTC-Sensor by Steinhart-Hart-Equatation and using the 3.3V voltage pin as an analog reference. The MEGA2560-R3 board is in use and i
m trying to display the values of the sensor to the Arduino TFT LCD Screen.
I finally got acceptable values from the sensor to the Serial Monitor, but have no clue how to give that values to the TFT.
So could Anyone please have a look at my code, for telling me if i need to change more than the underlined Codeline?
Thanks for your Help!
/*
NTC 100kohm, 160x128pixel LCD-Display,
LCD-Anschlüsse;
*LCD MISO-PIN an Arduino digital-Pin50
*LCD SCK-PIN an Arduino digital-Pin52
*LCD MOSI-PIN an Arduino digital-Pin51
*LCD CS-PIN an Arduino PMW-Pin10
*LCD D/C-PIN an Arduino PMW-Pin9
*LCD RESET-PIN an Arduino PMW-Pin8
*LCD BL-PIN an +5V
*/
// which analog pin to connect
#define THERMISTORPIN A0
// resistance at 25 degrees C
#define THERMISTORNOMINAL 100000
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25
// how many samples to take and average, more takes longer
// but is more 'smooth'
#define NUMSAMPLES 10
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 4092
// the value of the 'other' resistor
#define SERIESRESISTOR 1000
#include <TFT.h> // Arduino LCD library
#include <SPI.h>
// pin definition for the Mega
#define cs 10
#define dc 9
#define rst 8
// create an instance of the library
TFT TFTscreen = TFT(cs, dc, rst);
// char array to print to the screen
char sensorPrintout[4];
int samples[NUMSAMPLES];
void setup(void) {
Serial.begin(9600);
analogReference(EXTERNAL);
TFTscreen.begin(); // Put this line at the beginning of every sketch that uses the GLCD:
TFTscreen.background(0, 0, 0); // clear the screen with a black background
// write the static text to the screen
TFTscreen.stroke(255,255,255); // set the font color to white
TFTscreen.setTextSize(2); // set the font size
TFTscreen.text("Sensor Value :\n ",0,0); // write the text to the top left corner of the screen
TFTscreen.setTextSize(5); // ste the font size very large for the loop
}
void loop(void) {
uint8_t i;
float average;
// take N samples in a row, with a slight delay
for (i=0; i< NUMSAMPLES; i++) {
samples[i] = analogRead(THERMISTORPIN);
delay(10);
}
// average all the samples out
average = 0;
for (i=0; i< NUMSAMPLES; i++) {
average += samples[i];
}
average /= NUMSAMPLES;
Serial.print("Average analog reading ");
Serial.println(average);
// convert the value to resistance
average = 1023 / average - 1;
average = SERIESRESISTOR / average;
Serial.print("Thermistor resistance ");
Serial.println(average);
float steinhart;
steinhart = average / THERMISTORNOMINAL; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro)
steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // convert to C
Serial.print("Temperature ");
Serial.print(steinhart);
Serial.println(" *C");
delay(1000);
[u] // Read the value of the sensor on A0
String sensorVal = String(analogRead(A0));[/u]
// convert the reading to a char array
sensorVal.toCharArray(sensorPrintout, 4);
// set the font color
TFTscreen.stroke(255,255,255);
// print the sensor value
TFTscreen.text(sensorPrintout, 0, 20);
// wait for a moment
delay(250);
// erase the text you just wrote
TFTscreen.stroke(0,0,0);
TFTscreen.text(sensorPrintout, 0, 20);
}