Hello everyone.
I have a trouble with my LM35 thermometer project and can't figure out it by myself.
A0 - get LM35 signal;
A1 - get LDR with (internal pull-up);
Everything is working very good until that times when I enable LDR sensor on A1 in the code. Temperature readings begin chaotic dance from 22 to 27 C, when normal temerature in my room is about 25 C. Replacing sensors on the others pins, for example A3, A4 doesn't help.
I spent all night on searching to find out where is problem and nothing... Where is the bug? If you have comments about "how to make my code better", please share.
On this video reading LDR is closed in code, so LDR handling is not enabled to executing.
// Reading LDR (This is a trouble place)
// LDR_Bar = (analogRead(LDR_PIN) / BAR_SCALE);
// LDR_Block = analogRead(LDR_PIN);
and now you can see behavior when these lines are uncommented
Project code:
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
#include <SPI.h>
// pin 3 - Serial clock out (SCLK)
// pin 4 - Serial data out (DIN)
// pin 5 - Data/Command select (D/C)
// pin 6 - LCD chip select (CS)
// pin 7 - LCD reset (RST)
Adafruit_PCD8544 display = Adafruit_PCD8544(3, 4, 5, 6, 7);
float tempc, voltage;
int LDR_Bar, LDR_Block, LM35_Bar, LM35_Block;
long prev_millis;
#define LDR_PIN A1 // pin for LDR
#define LM35_PIN A0 // pin for LM35
#define UPDATE_TIME 3000
#define BAR_SCALE 12.19047619
static const unsigned char PROGMEM thermometer [] =
{
B00100000, // #
B01010000, // # #
B01010000, // # #
B01010000, // # #
B01010000, // # #
B01010000, // # #
B01010000, // # #
B01110000, // ###
B01110000, // ###
B01110000, // ###
B11111000, // #####
B11111000, // #####
B11111000, // #####
B01110000, // ###
};
void setup() {
pinMode(LDR_PIN,INPUT);
pinMode(LM35_PIN,INPUT);
// Enable pull-up resistor on the Analog Pin A1
digitalWrite(LDR_PIN, HIGH);
Serial.begin(9600);
// Init
display.begin();
// Show Adafruit splashscreen
display.display();
delay(1000);
// Clear the screen and buffer
display.clearDisplay();
// Contrast for the display
display.setContrast(60);
delay(500);
}
void ClearDisplay() {
// Clear display
display.display();
delay(300);
display.clearDisplay();
}
void DisplayTemperature() {
display.setCursor(0, 0);
display.setTextColor(BLACK);
display.setTextSize(1);
display.print("Temperature:");
// Draw bitmap here
display.drawBitmap(0, 10, thermometer, 5, 14, BLACK);
display.setCursor(8, 10);
display.setTextSize(2);
display.print(tempc);
display.println("C");
}
void DrawGraphicBar() {
// Graphic bar is awesome!
display.drawRect(0, 25, 84, 6, BLACK);
// 2px Bar for LM35
display.drawFastHLine(0, 26, LM35_Bar, BLACK);
display.drawFastHLine(0, 27, LM35_Bar, BLACK);
// 2px Bar for LDR
display.drawFastHLine(0, 28, LDR_Bar, BLACK);
display.drawFastHLine(0, 29, LDR_Bar, BLACK);
}
void DrawBlockTemperatureUnit() {
display.setTextSize(1);
display.setCursor(0,32);
display.setTextColor(WHITE, BLACK);
display.print("T/U");
display.setTextColor(BLACK);
display.print(":");
display.println(LM35_Block);
}
void DrawBlockTemperatureVoltage() {
display.setTextSize(1);
display.setCursor(0,41);
display.setTextColor(WHITE, BLACK);
display.print("T/V");
display.setTextColor(BLACK);
display.print(":");
display.print(voltage);
}
void DrawBlockLDRUnit() {
display.setTextSize(1);
display.setCursor(42,32);
display.setTextColor(WHITE, BLACK);
display.print("LDR");
display.setTextColor(BLACK);
display.print(":");
display.println(LDR_Block);
}
void loop() {
if (millis() - prev_millis >= UPDATE_TIME) {
prev_millis = millis();
// Let's read and calculate temperature
voltage = (4.65 * analogRead(LM35_PIN)) / 1024;
tempc = (voltage * 1000) / 10;
}
// Reading LM35
LM35_Bar = (analogRead(LM35_PIN) / BAR_SCALE);
LM35_Block = analogRead(LM35_PIN);
delay (50);
// Reading LDR (This is a trouble place)
//LDR_Bar = (analogRead(LDR_PIN) / BAR_SCALE);
//LDR_Block = analogRead(LDR_PIN);
delay (50);
// Serial.print ("LM35:");
// Serial.println(LM35_Block);
// Serial.print ("LDR:");
// Serial.println(LDR_Block);
// delay(200);
DisplayTemperature();
DrawGraphicBar();
DrawBlockTemperatureUnit();
DrawBlockTemperatureVoltage();
DrawBlockLDRUnit();
ClearDisplay();
}
Thanks for help.