#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
// Pin definitions for Nokia 5110 display
#define PIN_RST 3
#define PIN_CE 4
#define PIN_DC 5
#define PIN_DIN 6
#define PIN_CLK 7
// Temperature sensor pin
#define TEMP_PIN A0
// Initialize display
Adafruit_PCD8544 display = Adafruit_PCD8544(PIN_CLK, PIN_DIN, PIN_DC, PIN_CE, PIN_RST);
// NTC thermistor parameters
const float BETA = 3950; // Beta coefficient (adjust based on your sensor)
const float SERIES_RESISTOR = 10000; // 10k ohm series resistor
const float THERMISTOR_NOMINAL = 10000; // 10k ohm at 25°C
const float NOMINAL_TEMP = 25; // Nominal temperature in Celsius
// Variables for temperature reading
float temperature = 0;
float previousTemp = 0;
unsigned long lastReadTime = 0;
const unsigned long readInterval = 1000; // Read temperature every 1 second
void setup() {
Serial.begin(9600);
// Initialize display
display.begin();
// Set contrast (adjust as needed for best visibility)
display.setContrast(50);
// Clear the buffer
display.clearDisplay();
// Display startup message
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(0, 0);
display.println("Nokia 5110");
display.println("Temperature");
display.println("Monitor");
display.println("");
display.println("Initializing...");
display.display();
delay(2000);
// Draw initial interface
updateDisplay();
}
void loop() {
// Read temperature every second
if (millis() - lastReadTime >= readInterval) {
lastReadTime = millis();
readTemperature();
updateDisplay();
// Print to serial monitor for debugging
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
}
// Small delay to prevent overwhelming the display
delay(50);
}
void readTemperature() {
// Read analog value (0-1023)
int analogValue = analogRead(TEMP_PIN);
// Convert analog reading to resistance
float resistance = SERIES_RESISTOR / ((1023.0 / analogValue) - 1.0);
// Calculate temperature using the Beta equation
float steinhart;
steinhart = resistance / THERMISTOR_NOMINAL; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= BETA; // 1/B * ln(R/Ro)
steinhart += 1.0 / (NOMINAL_TEMP + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
temperature = steinhart - 273.15; // Convert to Celsius
// Optional: Convert to Fahrenheit
// float temperatureF = (temperature * 9.0/5.0) + 32.0;
}
void updateDisplay() {
display.clearDisplay();
// Draw border
display.drawRect(0, 0, 84, 48, BLACK);
// Title
display.setTextSize(1);
display.setCursor(2, 2);
display.println("Temperature:");
// Temperature value (large font)
display.setTextSize(2);
display.setCursor(2, 16);
// Format temperature with 1 decimal place
char tempStr[10];
dtostrf(temperature, 4, 1, tempStr);
display.print(tempStr);
display.print(" C");
// Add a simple bar graph to show temperature visually
int barHeight = map(constrain(temperature, 0, 40), 0, 40, 0, 30);
display.fillRect(60, 35 - barHeight, 20, barHeight, BLACK);
display.drawRect(60, 5, 20, 30, BLACK);
// Draw temperature icon (thermometer)
display.drawRect(72, 38, 4, 8, BLACK);
display.fillRect(73, 40, 2, 4, BLACK);
// Add a simple indicator for temperature trend
if (temperature > previousTemp + 0.2) {
display.fillTriangle(78, 42, 82, 42, 80, 38, BLACK);
} else if (temperature < previousTemp - 0.2) {
display.fillTriangle(78, 44, 82, 44, 80, 48, BLACK);
}
previousTemp = temperature;
// Show reading indicator
display.setTextSize(1);
display.setCursor(68, 1);
display.print("*");
// Display last update time
display.setCursor(2, 42);
display.print("Updated");
display.display();
}
// Optional: Add a simple menu system or button handling
// You can expand this to show min/max temperatures, etc.Loading
nokia-5110
nokia-5110