• sales

    +86-0755-88291180

Raspberry Pi 1.14inch 1.3inch LCD - Python Stats Example User Guide

  • If you have previously installed the Kernel Driver with the PiTFT Easy Setup, you will need to remove it first in order to run this example.

We can also display some stats about your Pi such as the IP address, resource usage, and even the CPU Temperature. Start by saving the code below as stats.py in your home directory on your Raspberry Pi.

  1. # -*- coding: utf-8 -*-
  2.  
  3. import time
  4. import subprocess
  5. import digitalio
  6. import board
  7. from PIL import Image, ImageDraw, ImageFont
  8. import adafruit_rgb_display.st7789 as st7789
  9.  
  10.  
  11. # Configuration for CS and DC pins (these are FeatherWing defaults on M0/M4):
  12. cs_pin = digitalio.DigitalInOut(board.CE0)
  13. dc_pin = digitalio.DigitalInOut(board.D25)
  14. reset_pin = None
  15.  
  16. # Config for display baudrate (default max is 24mhz):
  17. BAUDRATE = 64000000
  18.  
  19. # Setup SPI bus using hardware SPI:
  20. spi = board.SPI()
  21.  
  22. # Create the ST7789 display:
  23. disp = st7789.ST7789(
  24. spi,
  25. cs=cs_pin,
  26. dc=dc_pin,
  27. rst=reset_pin,
  28. baudrate=BAUDRATE,
  29. width=135,
  30. height=240,
  31. x_offset=53,
  32. y_offset=40,
  33. )
  34.  
  35. # Create blank image for drawing.
  36. # Make sure to create image with mode 'RGB' for full color.
  37. height = disp.width # we swap height/width to rotate it to landscape!
  38. width = disp.height
  39. image = Image.new("RGB", (width, height))
  40. rotation = 90
  41.  
  42. # Get drawing object to draw on image.
  43. draw = ImageDraw.Draw(image)
  44.  
  45. # Draw a black filled box to clear the image.
  46. draw.rectangle((0, 0, width, height), outline=0, fill=(0, 0, 0))
  47. disp.image(image, rotation)
  48. # Draw some shapes.
  49. # First define some constants to allow easy resizing of shapes.
  50. padding = -2
  51. top = padding
  52. bottom = height - padding
  53. # Move left to right keeping track of the current x position for drawing shapes.
  54. x = 0
  55.  
  56.  
  57. # Alternatively load a TTF font. Make sure the .ttf font file is in the
  58. # same directory as the python script!
  59. # Some other nice fonts to try: http://www.dafont.com/bitmap.php
  60. font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 24)
  61.  
  62. # Turn on the backlight
  63. backlight = digitalio.DigitalInOut(board.D22)
  64. backlight.switch_to_output()
  65. backlight.value = True
  66.  
  67. while True:
  68. # Draw a black filled box to clear the image.
  69. draw.rectangle((0, 0, width, height), outline=0, fill=0)
  70.  
  71. # Shell scripts for system monitoring from here:
  72. # https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load
  73. cmd = "hostname -I | cut -d' ' -f1"
  74. IP = "IP: " + subprocess.check_output(cmd, shell=True).decode("utf-8")
  75. cmd = "top -bn1 | grep load | awk '{printf \"CPU Load: %.2f\", $(NF-2)}'"
  76. CPU = subprocess.check_output(cmd, shell=True).decode("utf-8")
  77. cmd = "free -m | awk 'NR==2{printf \"Mem: %s/%s MB %.2f%%\", $3,$2,$3*100/$2 }'"
  78. MemUsage = subprocess.check_output(cmd, shell=True).decode("utf-8")
  79. cmd = 'df -h | awk \'$NF=="/"{printf "Disk: %d/%d GB %s", $3,$2,$5}\''
  80. Disk = subprocess.check_output(cmd, shell=True).decode("utf-8")
  81. cmd = "cat /sys/class/thermal/thermal_zone0/temp | awk '{printf \"CPU Temp: %.1f C\", $(NF-0) / 1000}'" # pylint: disable=line-too-long
  82. Temp = subprocess.check_output(cmd, shell=True).decode("utf-8")
  83.  
  84. # Write four lines of text.
  85. y = top
  86. draw.text((x, y), IP, font=font, fill="#FFFFFF")
  87. y += font.getsize(IP)[1]
  88. draw.text((x, y), CPU, font=font, fill="#FFFF00")
  89. y += font.getsize(CPU)[1]
  90. draw.text((x, y), MemUsage, font=font, fill="#00FF00")
  91. y += font.getsize(MemUsage)[1]
  92. draw.text((x, y), Disk, font=font, fill="#0000FF")
  93. y += font.getsize(Disk)[1]
  94. draw.text((x, y), Temp, font=font, fill="#FF00FF")
  95.  
  96. # Display image.
  97. disp.image(image, rotation)
  98. time.sleep(0.1)

Go ahead and run the script by typing:

python3 stats.py

It should display some system information.

adafruit_products_4393-06.jpg

Modifications for the 1.3" Display

To get the stats.py example to display properly on the 1.3" TFT Display, you will need to make some changes due to the different geometry of the display. The parameters you will need to adjust are the heightx_offsety_offset, and rotation.

The new values should be:

  • height 240
  • x_offset 0
  • y_offset 80
  • rotation = 180

The easiest way to replace them may be to copy the following code block and replace it in the above code.

 Download: file
  1. # Create the ST7789 display:
  2. disp = st7789.ST7789(
  3. spi,
  4. cs=cs_pin,
  5. dc=dc_pin,
  6. rst=reset_pin,
  7. baudrate=BAUDRATE,
  8. width=240,
  9. height=240,
  10. x_offset=0,
  11. y_offset=80,
  12. )
  13.  
  14. # Create blank image for drawing.
  15. # Make sure to create image with mode 'RGB' for full color.
  16. height = disp.width # we swap height/width to rotate it to landscape!
  17. width = disp.height
  18. image = Image.new("RGB", (width, height))
  19. rotation = 180

Running Stats on Boot

You can pretty easily make it so this handy program runs every time you boot your Pi.

The fastest/easiest way is to put it in /etc/rc.local

Run sudo nano /etc/rc.local and add the line

sudo python3 /home/pi/stats.py &

on its own line right before exit 0

Then save and exit. Reboot to verify that the screen comes up on boot!

Troubleshooting Stats on Boot

For the normal installation of Blinka on Raspberry Pi, we have you install stuff without the sudo keyword, which will install the libraries locally. However, to have the script run at boot, you will need to have the libraries available on a more system wide level. You can test this out by running the following command and see if the the stats come up:

sudo python3 /home/pi/stats.py

If you have any errors, most can be fixed by running the following command:

sudo pip3 install --upgrade adafruit-blinka adafruit-circuitpython-rgb-display spidev

Once you can get it to come up, go ahead and press Control+C and reboot the system. It should come up now.

Sometimes the Pi can boot too fast, so you may also need to add sleep 10 on the line before the command you added in /etc/rc.local.


TAG: ESP32 ST7789 Laser 450nm 5W high precision module Adjustable focal length For Laser Carving / Laser 3D Printing ADXL354C Jetson Nano Module NVIDIA XIAO-nRF52840 Plus 20GPIOs BLE Sense Tiny SuperMini Onboard Mic/Sensor For Arduino For SeeedStudio SIM8230G M2 3G/4G/5G/GNSS/GPS Sub-6G PCIe M.2 Moudle For LTE-A/NSA/SA For DFOTA /VoLTE For SIMCOM ESP32-S3 LCD raw display Raspberry Pi Pico 2 RP2350B 2.1 inch Round LCD Touch Display 480x480 HMI LVGL QMI8658 6-axis 65K Colors USB TO RS485 Jetson Nano Raspberry Pi 3.5 inch LCD G Display Resistive TouchScreen 60fps ST7796 320x480 Also For Arduino/Pico2/ESP32/RP2040/RP2350 Raspberry Pi Pico Raspberry Pi 0.85inch LCD i5-1135G7 Raspberry Pi 5 Heatsink Raspberry Pi IR Array Thermal Imaging Camera Far infrared 80×62 Pixels 45/90 Degree FOV Milk-V Duo S eMMC version firmware burning 1.54 inch Passive NFC e-Paper BW Display E-ink Screen No Need Battery Wireless Powered & Data Transfer