🔬
Wiki.Linhkientot
  • 👋Chào mừng tới Linhkientot's Wiki
  • Hướng dẫn phần mềm
    • Cài đặt Arduino IDE 2.0
    • Cài đặt phần mềm Mixly
    • Cài đặt phần mềm Mind+
    • Cài đặt phần mềm Thonny
  • Arduino Products
    • Hướng dẫn ban đầu Arduino IDE
      • Hướng dẫn nạp chương trình Arduino IDE
      • Thêm thư viện Arduino IDE
      • Cài đặt Driver CH340 trên MAC OS
      • Lập trình Board ESP32 trên Arduino IDE
    • Arduino Starter Kit
      • Lesson 0. Getting Started - install IDE
      • Lesson 2. LEDs
      • Lesson 3. RGB LEDs
      • Lesson 4. Eight LEDs and a Shift Register
      • Lesson 5. The Serial Monitor
      • Lesson 6. Digital Inputs
      • Lesson 7. Make an RGB LED Fader
      • Lesson 8. Analog Inputs
      • Lesson 9. Sensing Light
      • Lesson 10 Making Sounds
      • Lesson 11 LCD Displays Part 1
      • Lesson 12 LCD Displays Part 2
      • Lesson 13 DC Motors
      • Lesson 14 Servo Motors
      • Lesson 15 DC Motor Reversing
      • Lesson 16 Stepper Motors
      • Lesson 17 Email Sending Movement Detector
    • Arm Robot
      • Hướng dẫn lắp ráp Arm-4DoF
      • Hướng dẫn lắp ráp Arm-6DoF
      • Hướng dẫn lắp ráp Arm - 6DoF đế tròn quay
    • Car Robot
      • Hướng dẫn lắp ráp tank TS-100
    • Arm Car Robot
    • Smart home IoT
    • Lập trình cảm biến khí ga/ khói MQ-2 Arduino
  • Micro:bit Products
    • Micro:bit Setup
    • Micro:bit Starter Kit V1
      • Setting up the micro:bit with Makecode
      • Set up Arduino IDE for micro:bit
      • Using the Buttons and LED Matrix on micro:bit
      • Pushbutton with micro:bit
      • Tilt Sensor with micro:bit
      • Temperature Sensor with micro:bit
      • DC Motor with micro:bit
      • Sound Sensor with micro:bit
      • Raindrop Sensor with micro:bit
      • Make an RGB LED Blink with micro:bit
      • Ultrasonic Distance Sensor with micro:bit
      • Analog Inputs and micro:bit
      • Servo with micro:bit
      • Smoke sensor with micro:bit
      • Light-dependent Resistor with micro:bit
      • Infrared Obstacle Avoidance Sensor with micro:bit
      • Using LEDs with micro:bit
    • Micro:bit Starter Kit V2
      • Microbit Introduction
      • Microbit Basic Lessons
      • 1. Lesson: Đèn LED nhấp nháy
      • 2. Lesson: RGB Led
      • 3. Lesson: Đọc giá trị từ triết áp
      • 4. Lesson: Đọc nút nhấn
      • 5. Lesson: Servo Motor
      • 6. Lesson: Passive Buzzer-Còi thụ động
      • 7. Lesson: Active Buzzer-Còi chủ động
      • 8. Lesson: Compass-La bàn
      • 9. Lesson: Accelerometer-Gia tốc kế
      • 10. Lesson: Module phát hiện âm thanh
      • 11. Lesson: Cảm biến ánh sáng (quang trở)
      • 12. Lesson: Cảm biến ngọn lửa
      • 13. Lesson: Cảm biến khói
      • 14. Lesson: Cảm biến siêu âm
      • 15. Lesson: ModuleRelay
      • 16. Lesson: Cảm biến nhiệt độ (DHT11)
      • 17. Lesson: Bộ điều khiển từ xa (IR)
      • 18. Lesson: Hiển thị màn hình LCD i2c 1602
      • 19. Lesson: Hiển thị nhiệt độ, độ ẩm trên màn hình LCD
      • 20. Lesson: Điều khiển quạt bằng Relay
    • Micro:bit Advanced Kit V2
      • Giới thiệu BBC Sensor Shield V2
      • Danh sách linh kiện bộ Kit
    • Micro:bit Car
  • ESP32 PRODUCTS
    • Hướng dẫn lập trình ESP32
  • Raspberry Products
    • Raspberry Pico Kit
    • Raspberry Pico Car
  • Group Robots
    • 🤖Arduino Robots
    • 🐦Micro:bit Robots
    • 🍓Raspberry Robots
Powered by GitBook
On this page
  • Arduino Lesson 12. LCD Displays - Part 2
  • Overview
  • Parts
  • Breadboard Layout
  • Arduino Code
  • Other Things to Do
  1. Arduino Products
  2. Arduino Starter Kit

Lesson 12 LCD Displays Part 2

PreviousLesson 11 LCD Displays Part 1NextLesson 13 DC Motors

Last updated 1 year ago

Arduino Lesson 12. LCD Displays - Part 2

Created by Simon Monk

Last updated on 2022-12-01 01:53:02 PM EST

Table of Contents

Overview 3

Parts 4

  • Part

  • Qty

Breadboard Layout 9

Arduino Code 10

Other Things to Do 12

Overview

In this lesson, you will build on what we have learnt in lesson 11 and use a LCD display to show the temperature and light intensity.

Light intensity is measured using the same photocell that you used in lesson 9.

To measure the temperature, you will use a temperature measurement chip. This device has just three leads two for 5V and GND and the third lead is connected directly to an analog input on the Arduino.

Parts

LCD Display (16x2 characters) 1

1 kΩ Resistor (brown, black, red stripes) 1

1

Half-size Breadboard 1

Arduino Uno R3 1

Jumper wire pack 1

The TMP36 looks just like the PN2222 transistor, but if you look at the flat side of the package body, you should see it labelled as a TMP36.

Breadboard Layout

The breadboard layout is based on the layout from lesson 11, so if you still have this on the breadboard it will simplify things greatly.

There are a few jumper wires that have been moved slightly on this layout. In particular, those near the pot.

The photocell, 1 kΩ resistor and TMP36 are all new additions to the board. The TMP36 has its curved face towards the display.

Arduino Code

The sketch for this is based on that of lesson 11. Load it up onto your Arduino and you should find that warming the temperature sensor by putting your finger on it will increase the temperature reading.

Also if you wave your hand over the photocell blocking out some of the light, the reading will decrease.

/*

Adafruit Arduino - Lesson 12. Light and Temperature

*/

#include <LiquidCrystal.h> int tempPin = 0;

int lightPin = 1;

//

BS E D4 D5 D6 D7

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup()

{

lcd.begin(16, 2);

}

void loop()

{

// Display Temperature in C

int tempReading = analogRead(tempPin);

float tempVolts = tempReading * 5.0 / 1024.0; float tempC = (tempVolts - 0.5) * 100.0; float tempF = tempC * 9.0 / 5.0 + 32.0;

// lcd.print("Temp F "); lcd.setCursor(6, 0); lcd.print(tempF);

// Display Light on second row

int lightReading = analogRead(lightPin); lcd.setCursor(0, 1);

// lcd.print("Light "); lcd.setCursor(6, 1); lcd.print(lightReading); delay(500);

}

I find it useful to put a comment line above the 'lcd' command.

//

BS E D4 D5 D6 D7

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

This makes things easier if you decide to change which pins you use.

In the 'loop' function there are now two interesting things going on. Firstly we have to convert the analog from the temperature sensor into an actual temperature, and secondly we have to work out how to display them.

First of all, let's look at calculating the temperature.

int tempReading = analogRead(tempPin);

float tempVolts = tempReading * 5.0 / 1024.0; float tempC = (tempVolts - 0.5) * 100.0; float tempF = tempC * 9.0 / 5.0 + 32.0;

The raw reading from the temperature sensor is first multiplied by 5 and then divided by 1024 to give us the voltage (between 0 and 5) at the 'tempPin' analog input.

To convert the voltage coming from the TMP36 into a temperature in degrees C, you have to subtract 0.5V from the measurement and then multiply by 100.

To convert this into a temperature in Fahrenheit, you then have to multiply it by 9/5 and then add 32.

Displaying changing readings on an LCD display can be tricky. The main problem is that the reading may not always be the same number of digits. So, if the temperature changed from 101.50 to 99.00 then the extra digit from the old reading is in danger of being left on the display.

To avoid this, write the whole line of the LCD each time around the loop.

//

lcd.print("Temp F "); lcd.setCursor(6, 0); lcd.print(tempF);

The rather strange comment serves to remind you of the 16 columns of the display. You can then print a string of that length with spaces where the actual reading will go.

To fill in the blanks, set the cursor position for where the reading should appear and then print it.

Exactly the same approach is used for displaying the light level. There are no units for the light level, we just display the raw reading from the analog read.

Other Things to Do

Try changing the example so that it displays the temperature in degrees C rather than F.

Part Qty

10 kΩ variable resistor (pot) 1

Photocell (Light Dependent Resistor) 1

TMP36 temperature sensor