Sarful Hassan
Published © Apache-2.0

LCD Temperature Display- Arduino Workshop

LCD Temperature Display- Arduino Workshop

BeginnerFull instructions provided1 hour15,113
LCD Temperature Display- Arduino Workshop

Things used in this project

Story

Read more

Schematics

LCD Temperature Display- Arduino Workshop

Code

Code

Arduino
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // LCD object with pin assignments

int maxC = 0, minC = 100, maxF = 0, minF = 212;
int scale = 1;
int buttonPin = 8;

void setup() {
  lcd.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows
  analogReference(INTERNAL); // Set internal reference voltage
  pinMode(buttonPin, INPUT); // Set button pin as input
  lcd.clear(); // Clear LCD screen
}

void loop() {
  lcd.setCursor(0, 0); // Set cursor to home position
  int sensorValue = analogRead(A0); // Read temperature sensor
  int buttonState = digitalRead(buttonPin); // Read button state
  
  if (buttonState == HIGH) {
    scale = -scale; // Toggle between Celsius and Fahrenheit
    lcd.clear(); // Clear LCD screen
  }
  
  if (scale == 1) {
    displayTemperatureC(sensorValue);
  } else {
    displayTemperatureF(sensorValue);
  }
  
  delay(250); // Delay for stability
}

void displayTemperatureC(int sensorValue) {
  lcd.setCursor(0, 0);
  int temperatureC = sensorValue * 0.1074188; // Convert sensor value to Celsius
  lcd.print(temperatureC);
  lcd.write(0xDF); // Degree symbol for Celsius
  lcd.print("C ");
  
  if (temperatureC > maxC) { maxC = temperatureC; }
  if (temperatureC < minC) { minC = temperatureC; }
  
  lcd.setCursor(0, 1);
  lcd.print("H=");
  lcd.print(maxC);
  lcd.write(0xDF);
  lcd.print("C L=");
  lcd.print(minC);
  lcd.write(0xDF);
  lcd.print("C ");
}

void displayTemperatureF(int sensorValue) {
  lcd.setCursor(0, 0);
  float temperatureF = (sensorValue * 0.1074188 * 1.8) + 32; // Convert to Fahrenheit
  lcd.print(int(temperatureF));
  lcd.write(0xDF); // Degree symbol for Fahrenheit
  lcd.print("F ");
  
  if (temperatureF > maxF) { maxF = temperatureF; }
  if (temperatureF < minF) { minF = temperatureF; }
  
  lcd.setCursor(0, 1);
  lcd.print("H=");
  lcd.print(maxF);
  lcd.write(0xDF);
  lcd.print("F L=");
  lcd.print(minF);
  lcd.write(0xDF);
  lcd.print("F ");
}

void setup() {
  lcd.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows
  analogReference(INTERNAL); // Set internal reference voltage
  pinMode(buttonPin, INPUT); // Set button pin as input
  lcd.clear(); // Clear LCD screen
}

Code

Python
from machine import Pin, ADC
from time import sleep_ms
import lcd

# Define LCD pins
lcd_rs = Pin(12)
lcd_en = Pin(11)
lcd_d4 = Pin(5)
lcd_d5 = Pin(4)
lcd_d6 = Pin(3)
lcd_d7 = Pin(2)

# Initialize LCD
lcd.init(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, 16, 2)

# Initialize LM35 temperature sensor
sensor_pin = ADC(0)

maxC = 0
minC = 100
maxF = 0
minF = 212
scale = 1
button_pin = Pin(8, Pin.IN)

def setup():
    lcd.clear()
    lcd.move_to(0, 0)
    lcd.putstr("Temperature:")
    lcd.move_to(0, 1)
    lcd.putstr("Max:     Min:   ")

def loop():
    global scale
    lcd.move_to(0, 0)
    sensor_value = sensor_pin.read()
    button_state = button_pin.value()
    
    if button_state == 1:
        scale = -scale
        lcd.clear()
    
    if scale == 1:
        display_temperature_C(sensor_value)
    else:
        display_temperature_F(sensor_value)
    
    sleep_ms(250)

def display_temperature_C(sensor_value):
    global maxC, minC
    temperature_C = int(sensor_value * 0.1074188)
    lcd.putstr("{:2}C ".format(temperature_C))
    
    if temperature_C > maxC:
        maxC = temperature_C
    if temperature_C < minC:
        minC = temperature_C
    
    lcd.move_to(4, 1)
    lcd.putstr("{:2}   {:2}".format(maxC, minC))

def display_temperature_F(sensor_value):
    global maxF, minF
    temperature_F = int((sensor_value * 0.1074188 * 1.8) + 32)
    lcd.putstr("{:2}F ".format(temperature_F))
    
    if temperature_F > maxF:
        maxF = temperature_F
    if temperature_F < minF:
        minF = temperature_F
    
    lcd.move_to(4, 1)
    lcd.putstr("{:2}   {:2}".format(maxF, minF))

setup()

while True:
    loop()

Credits

Sarful Hassan

Sarful Hassan

9 projects • 1 follower
I AM NOT US” Our aim is to present technology to the world in a simple and beautiful way , Our main focus will be on Mechatronics

Comments