In this tutorial, we will create an LCD temperature display using both Arduino and MicroPython. We'll interface an LM35DT analog temperature sensor and a push button to toggle between Celsius and Fahrenheit temperature scales. The LCD will show real-time temperature readings along with maximum and minimum temperature records.
What We Will Learn in This Section- Interfacing an LM35DT analog temperature sensor with Arduino and MicroPython
- Programming an LCD display to show temperature in Celsius and Fahrenheit
- Implementing user interaction with a push button for scale switching in both Arduino and MicroPython environments
This lesson is essential for electronics enthusiasts and IoT hobbyists interested in sensor integration and programming with both Arduino and MicroPython. Understanding temperature monitoring with these platforms enables you to build various environmental monitoring and automation projects.
Components List (Amazon Affiliate Links)- Arduino UNO
- Resistors
- 6×2 LCD Display Module
- Potentiometer
- Connecting wires
- Breadboard
- Mini Push Button Switch
- LM35DT Analog Temperature Sensor
- Arduino Analog Pin 0 to LM35DT Sensor Output
- Arduino Digital Pin 8 to Push Button
- LCD Module connected to appropriate Arduino pins (as per code)
#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 Explanation (Arduino)- LiquidCrystal lcd(12, 11, 5, 4, 3, 2): Initializes the LCD object with pin assignments.
- buttonPin = 8: Connects the push button to Arduino digital pin 8 for scale toggling.
- analogRead(A0): Reads analog input from the LM35DT temperature sensor connected to Arduino analog pin 0.
- displayTemperatureC and displayTemperatureF: Functions to display temperature in Celsius and Fahrenheit, respectively, on the LCD.
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()
Code Explanation (MicroPython)- Import Statements: Import necessary modules from MicroPython (
machine
,time
,lcd
). - LCD Initialization: Define and initialize the LCD pins (
lcd_rs
,lcd_en
,lcd_d4
tolcd_d7
) usinglcd.init()
function. - LM35 Temperature Sensor: Set up an analog-to-digital converter (ADC) on pin 0 (
sensor_pin
) to read temperature values. - Global Variables: Initialize global variables (
maxC
,minC
,maxF
,minF
,scale
) to store temperature records and scale state. - Setup Function: Clears the LCD screen and initializes the initial display format.
- Loop Function: Continuously reads temperature from the sensor, checks the state of the button (
button_pin
), and updates the LCD display accordingly. - Display Functions:
display_temperature_C()
anddisplay_temperature_F()
formats and displays the temperature in Celsius and Fahrenheit, respectively, on the LCD. - Main Program: Executes the
setup()
function once and then runs theloop()
function continuously to update the temperature display.
This project demonstrates how to create an LCD temperature display using both Arduino and MicroPython. It provides a foundation for further exploration into sensor integration, user interaction, and application development in both environments. Experiment with different sensors and expand upon this project to suit your specific needs and interests in electronics and IoT.
Comments