Arduino Lesson 11: LCD Display Part II


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.




LCD Display (16×2 characters)



10 kΩ variable resistor (pot)



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



Photocell (Light Dependent Resistor)



TMP36 temperature sensor



Half-size Breadboard



Arduino Uno R3



Jumper wire pack


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.

  1. /*
  2. Adafruit Arduino – Lesson 12. Light and Temperature
  3. */
  5. #include
  7. int tempPin =0;
  8. int lightPin =1;
  10. // BS E D4 D5 D6 D7
  11. LiquidCrystal lcd(7,8,9,10,11,12);
  13. void setup()
  14. {
  15. lcd.begin(16,2);
  16. }
  18. void loop()
  19. {
  20. // Display Temperature in C
  21. int tempReading = analogRead(tempPin);
  22. float tempVolts = tempReading *5.0/1024.0;
  23. float tempC =(tempVolts 0.5)*100.0;
  24. float tempF = tempC *9.0/5.0+32.0;
  25. // —————-
  26. lcd.print(“Temp F “);
  27. lcd.setCursor(6,0);
  28. lcd.print(tempF);
  29. // Display Light on second row
  30. int lightReading = analogRead(lightPin);
  31. lcd.setCursor(0,1);
  32. // —————-
  33. lcd.print(“Light “);
  34. lcd.setCursor(6,1);
  35. lcd.print(lightReading);
  36. delay(500);
  37. }

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

  1. // BS E D4 D5 D6 D7
  2. 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.

  1. int tempReading = analogRead(tempPin);
  2. float tempVolts = tempReading *5.0/1024.0;
  3. float tempC =(tempVolts 0.5)*100.0;
  4. 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.

  1. // —————-
  2. lcd.print(“Temp F “);
  3. lcd.setCursor(6,0);
  4. 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.

Με ετικέτα: , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *