Science Oxford Logo

Format the LCD Screen


Improve your LCD display information

You have a limited number of characters on the screen, so you will need to get creative to display your information.


Round the numbers down

For some of the sensors, you get a number with lots of decimal places, which takes up more space than you need.

Python (including MicroPython) has some built-in maths functions you can use, for example:

temp = values.get('Temp')
display_temp = round(temp)
lcd[0] = str(display_temp)

This will round the number to a whole number. If you would like, for example, one decimal place, you can add another argument to the code, like this:

round(temp, 1)

Challenge: Once you have this code working in your own program, see if you can reduce the number of lines of code needed to get the same result.


Add units

To get a number showing on the display, we have converted it into a string, using this code:

lcd[0] = str(temp)

Strings can be merged with other strings - we call this concatenation.

Look at this example:

lcd[0] = str(temp) + "C"

This will stick the character 'C' directly after the number, with no space between them. You can add information before the number too:

lcd[0] = "Temperature: " + str(temp) + "C"

Challenge: Can you combine the string concatenation code with the number rounding code described above?