Current Display Temperature

Jesus Avila
Strategio
Published in
6 min readFeb 13, 2023

Displays real-time temperature with visual and audio cues.

Introduction

During my Computer Engineering studies, one of the classes I took was Microcomputers I, a class in the embedded systems concentration at my university. Here I worked on a fairly simple solo project where I’d use a microcontroller and make a real-time temperature display that includes an alarm that plays different audio cues depending on the temperature.

Current Display Temperature displaying Caution as temps get toasty

Materials

  • LPC1768 microcontroller
  • HD44780 1602 LCD Display
  • LM35 sensor
  • 8Ω Speaker
  • Breadboard
  • Jumper wires
  • micro USB cable

Flowchart

When first tackling these kinds of projects it’s good to create a flowchart before coding to get a visual representation of what you want your code to accomplish.

In the flowchart above a rectangle represents an event that is controlled within the process and is waiting for an action to occur, by the user or not. The diamonds represent a decision being made that can usually be answered by true or false. These also simplify our idea of where the code will probably have an if-statement. There would also be a different shape for manual inputs by a user, but this project does not have one.

Microcontroller pinout and Schematic

https://os.mbed.com/platforms/mbed-LPC1768/

Above is a picture of the general microcontroller I was working with and its pinouts. As you can see it has many interfaces and communication protocols, such as I2C, SPI, CAN, USB, and Ethernet, which each have their own pros and cons to accommodate many different types of projects. It is a great prototyping board for those getting into embedded systems and who want to learn how to program them. In this simple project, I’m only using the Analog, PwmOut, GND, and VU pins.

Below I’ve included a very rough schematic, excuse my terrible art skills, to follow if you want to create a similar project yourself.

Project Schematic displaying cable connections

Here is also a picture of the real-life setup to see a bit of what it should look like. Ideally, you’d choose different cable length wires or shorten them yourselves, but I was more concerned with getting it to work at this stage than optimizing aesthetics.

Code (C++)

Now for the code, this is done online through os.mbed IDE, I have several comments below which describe each step and what it does, so most of it is self-explanatory. Coding an embedded device can involve using constraints and declaring our pins variables and whether they’re being used as input or output. You can see at the top what libraries I’ve included and what variable types, variable names, and pins are held in each variable depending on what hardware we’re connected to. I’ve imported the main library used for embed programming on this microcontroller, mbed.h, as it holds the necessary drivers, but also imported a library, TextLCD.h, which is used to simplify our coding process for the external LCD. Without this library I’d have to use modular coding to interface the LCD, an ASCII table to output the right characters, and define custom LCD functions with binary shift registers, bitmasks, and more making the code long and cumbersome to read. I’ve dabbled a bit in doing that in this class, but for this project, I just saved myself and you the headache by using the library. You can learn more about this library, it’s functions, and its author here: https://os.mbed.com/users/simon/code/TextLCD/docs/tip/classTextLCD.html

C++ code for this project

Most of this code should be pretty easy to follow, but you might be wondering about these huge arrays containing float values. Well, each float value represents a note and an octave that is necessary to produce the sound that you want. There is also a beat that decides how long the note is held. You can see for the first song the beat is consistent throughout the whole jingle, but for the second song, I needed to have the beat held at different lengths of time depending on which note was being played. I used the chart below to help me decide on the needed float values to get the right song to play.

https://studioguru.co/producer-tools/note-frequency-chart/

Now inside the main function, I have a loop variable used for counting and a while(1) to create an infinite loop and have my program run indefinitely. Inside this while-loop, you can see at the top where I convert analog data, that I’ve received from the microcontroller’s p15 Analog in port that has the LM35 connected to it, and convert it using a formula to Fahrenheit. This formula was made by using the LM35’s documentation which states that its output is 10mV/°C (10 millivolts per celsius) and combining it with the celsius to Fahrenheit formula we can store the needed output to variable ADCdata. Essentially we’re turning this analog data and converting it to digital for our program to use (ADC). This is why looking at the documentation and documenting things on your own is so important. In this segment you can also see functions being used like lcd.printf and lcd.cls(). I’m able to use these thanks to the TextLCD.h library I talked about earlier. It simplifies using the LCD a lot and is another great lesson in not reinventing the wheel when someone already has a great implementation and documentation of what you need.

Analog to temp conversion

The rest of the code is if-statements with conditions based on the temperature input and inside each statement are for-loops that iterate through the correct array depending on the temperature reached. You can also see where I play with the PWM (Pulse Width Modular) output by using the .period() function and feeding it data based on the frequency of the array. The duty cycle is also played with as these functions are part of the LPC1768 controller and allow us to use it as a DAC (Digital to Analog Converter). This of course is needed as speakers need an analog signal to play sound.

If statements, frequencies, DAC

You may also notice a loop counter that keeps track of what if-statements have entered and are part of the if-statement conditions themselves. This is to prevent songs from playing over and over again constantly if temperatures are unstable and that is headache-inducing.

And finally for those that made it this far here is a short video of me displaying the project and its function. I use my finger to get the LM35 to reach a temperature above 80°F to display the blinking caution, then above 85°F to play the first warning song (Dr. Wily Castle theme from Mega Man 2), and then finally I use a hair dryer to get the temperature above 95°F to play the 2nd warning which appropriately plays a segment of Through the Fire and Flames by Dragonforce.

https://www.youtube.com/watch?v=ZU0uRfcyXBs

Conclusion

There are many possible modifications I could have done to improve this project like using interrupt routines instead polling since polling is wasteful of CPU resources and not very energy efficient. Ultimately, I hope this small tidbit into the world of embedded engineering through this simple project explanation piqued your interest in programming embedded systems just a tiny bit. Feel free to comment below and tell me what you think. Thanks for reading!

--

--