DEV Community

Cover image for Raspberry PI with temperature and humidity sensor
Ayar Hlaine
Ayar Hlaine

Posted on

2

Raspberry PI with temperature and humidity sensor

ယခုပြောပြသွားမှာကတော့ Raspberry PI ကနေ DHT-11 (temperature and humidity sensor) ကို ဘယ်လိုအသုံးပြု့ရမလဲဆိုတာပြောပြပေးသွားမှာ ဖြစ်ပါတယ်။

DHT-11 sensor ကိုအသုံးပြု့ဖို့ဆိုရင် Raspberry PI အတွက်လုပ်ထားတဲ့
python libraries တွေ node js packages တွေအများကြီးရှိပါတယ်။

  1. DHT11 Python
  2. DHT11 Node JS

ယခု turorial မှာတော့ DHT11 Python ကိုအသုံးပြု့တဲ့နည်း ပြောပြသွားမှာဖြစ်ပါတယ်။

အသုံးပြု့ပုံ အဆင့်ဆင့်

  1. Run the standard updates
  2. Install DHT11 Python
  3. Connect DHT11 sensor with Raspberry Pi board
  4. Write python code to read data

1. Run the standard updates

ဒါကတော့ ထုံးစံအတိုင်း linux based OS တွေဖြစ်တဲ့အတွက် libraries တွေကို update/upgrade လုပ်ပေးရမယ်။ Raspberry PI OS က Debian-based ဖြစ်နေတာကြောင့်ပါ။

Raspberry PI ကို Terminal ကနေ
sudo apt-get update
sudo apt-get upgrade
တို့ကို run ပေးပါ။

2. Install DHT11 Python

ဒုတိယ အဆင့်အနေနဲ့ DHT11 Python library ကို install လုပ်ပေးပါမယ်။

python3 -m pip install dht11 ကို အသုံးပြု့ပြီး အလွယ်တကူ install လုပ်နိုင်ပါတယ်။

Screen Shot 2021-08-01 at 3.09.16 PM

3. Connect DHT11 sensor with Raspberry Pi board

တတိယ အဆင့်အနေနဲ့ကတော့ DHT11 sensor ကို Raspberry Pi board ရဲ့
pins တွေနဲ့ ဘယ်လိုချိတ်ဆက်ရမလဲသိ့ဖို့လိုပါတယ်။

အရင်ဆုံး DHT11 sensor မှာ pin ဘယ်နှချောင်းပါလဲ အရင်ကြည့်ရအောင်ပါ။

dht11 sensor

Symbol Description
G Ground Pin ကို ဆိုလိုပါတယ်
V Voltage Pin ကိုဆိုလိုပါတယ်
S Signal/Data ကိုဆိုလိုပါတယ်

နောက်ထပ်သိ့ရမှာက မိ့မိ PI ရဲ့ Pins တွေကို နားလည်ဖို့လိုပါမယ်။
ဒါကတော့ Raspberry PI ရဲ့ GPIO Pin dragram ပါ။

GPIO-Pinout-Diagram-2 (1)

ဒါကိုပဲ မိ့မိ့ PI ရဲ့ gpio pins တွေအသေးစိတ်သိ့ချင်တယ်ဆိုရင်
Terminal ကနေပဲ
pinout လို့ရိုက်ပြီး ကြည့်ရှုနိုင်ပါသေးတယ်။

Screen Shot 2021-08-01 at 2.28.48 PM

ကျနော့်ရဲ့ Raspberry PI 4 Model B မှာတော့

  1. 3 Voltage (2 pins)
  2. 5 Voltage (2 pins)
  3. Ground Pin (8 pins)
  4. GPIO Pin (28 pins) ပါပါတယ်.

အသေးစိတ်ထပ်ဖတ်ချင်တယ်ဆိုရင်တော့ GPIO Pin docs မှာဖတ်လို့ရပါတယ်။

အဲတော့ sensor ရဲ့
G pin ကို Ground pin
V pin ကို 3 V/ 5 V pin
S pin ကို any GPIO pin
တွေမှာချိတ်ဆက်ပေးရပါမယ်။

Screen Shot 2021-08-01 at 2.44.10 PM

အထက်ပါအတိုင်းချိတ်ဆက်လိုက်တဲ့ အတွက်
G pin က pin 6
V pin က pin 2
S pin က pin 10 (GPIO 15)
မှာ ရှိနေမှာပါ။

4. Write python code to read data

ဒါကတော့ အထက်အဆင်မှာ installed လုပ်ခဲ့တဲ့ dht11 library ကို အသုံးပြု့ထားတဲ့ code ဖြစ်ပါတယ်။

import RPi.GPIO as GPIO
import dht11

# initialize GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()

# read data using pin 15
instance = dht11.DHT11(pin = 15)
result = instance.read()

if result.is_valid():
    print("Temperature: %-3.1f C" % result.temperature)
    print("Humidity: %-3.1f %%" % result.humidity)
else:
    print("Error: %d" % result.error_code)
Enter fullscreen mode Exit fullscreen mode

Desktop ပေါမှာ dht11_example.py ဆိုတဲ့ဖိုင်လေးဆောက်ပါ။
ပြီးတော့ အထက်က code ကို ကူးထည့်လိုက်ပါ။

Screen Shot 2021-08-01 at 3.06.58 PM

ပြီးရင်တော့ Terminal ကနေ

pi@raspberrypi:~/Desktop phthon3 dht11_example.py
Enter fullscreen mode Exit fullscreen mode

လို့ run ပေးရပါမယ်။
သတိ့ထားရမှာ က code ထဲမှာ gpio pin no ကို မှန်မှန်ကန်ကန်ထည့်ပေးရပါမယ်။
ချိတ်ဆက်တုန်းက S pin က pin 10 (GPIO 15) gpio 15 pin ကို အသုံးပြု့ထားတာဖြစ်ထဲ့အတွက် 15 လို့ထည့်ထားတာပါ။

Screen Shot 2021-08-01 at 3.05.25 PM

အားလုံးပဲအဆင်ပြေကြပါစေ။


Buy Me a Coffee at ko-fi.com

အားလုံးကိုကျေးဇူးတင်ပါတယ်။
ဆက်လက်ကြိုးစားပါအုံးမည်။

Ayar Hlaine

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.

👋 Kindness is contagious

Value this insightful article and join the thriving DEV Community. Developers of every skill level are encouraged to contribute and expand our collective knowledge.

A simple “thank you” can uplift someone’s spirits. Leave your appreciation in the comments!

On DEV, exchanging expertise lightens our path and reinforces our bonds. Enjoyed the read? A quick note of thanks to the author means a lot.

Okay