Sunday, May 19, 2024

Monitor the temperature of your Raspberry Pi using an I2C LCD without an external temperature sensor

Untuk memantau suhu Raspberry Pi menggunakan LCD I2C tanpa sensor suhu, 

anda boleh melihat suhu CPU terus dari Raspberry Pi dan memaparkannya pada LCD. Berikut ialah panduan langkah demi langkah:

Peralatan yang diperlukan:

1. Raspberry Pi 
2. I2C LCD Display 
3. Jumper wires (female to female)

----------------------------------------------------------------------------------------------------

 Step 1: Sambung I2C LCD ke Raspberry Pi

Power Off Raspberry Pi anda, Connect  I2C LCD ke Raspberry Pi ikut pin ini:

VCC ke 5V (Pin 2 atau 4 Raspberry Pi)
GND ke GND (Pin 6  Raspberry Pi)
SDA ke SDA (Pin 3 Raspberry Pi)
SCL ke SCL (Pin 5 Raspberry Pi)



----------------------------------------------------------------------------------------------

 Step 2: Enable I2C pada Raspberry Pi

1. Power on Raspberry Pi.
2. buka terminal.
3. Run `sudo raspi-config`.
4. Pergi ke Interfacing Options > I2C > Enable.
5. Reboot Raspberry Pi 

-----------------------------------------------------------------------------------------------
Step 3: Install Libraries

sudo apt update
sudo apt install -y i2c-tools
sudo apt install -y python3-smbus
sudo pip3 install RPLCD

----------------------------------------------------------------------------------------------
Step 4: Cari address I2C LCD 

sudo i2cdetect -y 1
>>> 0x27<<<<

---------------------------------------------------------------------------------------------
Step 5: Python Script untuk baca Temperature dan Display pada LCD

 nano cpu_temp_lcd.py 
(paste semua code dibawah)

 import os
   import time
   from RPLCD.i2c import CharLCD

   # Initialize the LCD with the correct address
   lcd = CharLCD('PCF8574', 0x27)

   def get_cpu_temp():
       res = os.popen('vcgencmd measure_temp').readline()
       return res.replace("temp=","").replace("'C\n","")

   try:
       while True:
           temp = get_cpu_temp()
           lcd.clear()
           lcd.write_string("CPU Temp: {}C".format(temp))
           time.sleep(1)
   except KeyboardInterrupt:
       lcd.clear()
       lcd.write_string("Goodbye!")
       time.sleep(2)
       lcd.clear()

save dan exit editor

-------------------------------------------------------------------------------------------------
Step 6: Run  Python Script

python3 cpu_temp_lcd.py

Paparan seperti gambar di bawah akan muncul, maka sukses.


-----------------------------------------------------------------------------------------------
auto run temp lcd selepas reboot

sudo nano /etc/rc.local

tambah line dibawah sebelum line exit 0, contoh seperti di bawah:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

# Run the Python script
sudo python3 /home/pi/cpu_temp_lcd.py &

exit 0

save dan exit editor

-----------------------------------------------------------------------------------------------

sudo chmod +x /etc/rc.local

-----------------------------------------------------------------------------------------------

sudo reboot

--------------------------------------------------------------------------------------------

selepas reboot dan raspi om, paparan suhu akan trus muncul pada lcd, jika gagal sila ulang semula step by step. semoga bermanfaat, 73


Connect a GPS module to a Raspberry Pi 4

  To connect a GPS module with four wires (RST, TX, VCC, and GND) to a Raspberry Pi 4, follow these steps:  Materials Needed: 1. GPS module ...