Cov txheej txheem:

Kev Pom Kev Nyob Nrog Raspberry Pi thiab HC-SR04: 3 Cov Kauj Ruam
Kev Pom Kev Nyob Nrog Raspberry Pi thiab HC-SR04: 3 Cov Kauj Ruam

Video: Kev Pom Kev Nyob Nrog Raspberry Pi thiab HC-SR04: 3 Cov Kauj Ruam

Video: Kev Pom Kev Nyob Nrog Raspberry Pi thiab HC-SR04: 3 Cov Kauj Ruam
Video: Saib Mis paub Poj niam (hluas nkauj) tus yam ntxwv lub siab 2024, Lub Xya hli ntuj
Anonim
Kev Txheeb Ze Nrog Raspberry Pi thiab HC-SR04
Kev Txheeb Ze Nrog Raspberry Pi thiab HC-SR04

HC-SR04 Ultrasonic Distance Sensor siv cov tsis sib cuag ultrasound sonar los ntsuas qhov nrug mus rau ib qho khoom. Nws muaj ob lub transmitters, tus txais thiab tus tswj Circuit Court. Cov neeg xa xov xa tawm lub suab nrov ultrasonic ntau, uas thaws tawm ib qho khoom nyob ze, thiab tus txais tau mloog rau txhua qhov rov qab ncha. Qhov ncha ntawd tau ua tiav los ntawm kev tswj hwm lub zog txhawm rau xam lub sijhawm sib txawv ntawm lub teeb liab uas tau xa thiab tau txais. Lub sijhawm no tuaj yeem siv tom qab, nrog rau qee qhov kev ua lej ntse, los xam qhov nrug nruab nrab ntawm lub ntsuas cua thiab qhov khoom xav tau!

Khoom siv

Koj yuav xav tau dab tsi:

  • Raspberry Pi 2/3/4
  • Micro SD Card thauj khoom nrog Raspbian
  • 5.1V USB Fais fab mov
  • HC-SR04 (pom tseeb)
  • Cov qhob cij
  • 4 Txiv neej rau Poj Niam Cables
  • Saib xyuas thiab keyboard rau Raspberry Pi

Kauj Ruam 1: Teeb Raspberry Pi

Teem Raspberry Pi
Teem Raspberry Pi
  1. Ntxig daim npav SD uas koj tau teeb tsa nrog Raspbian (ntawm NOOBS) rau hauv daim npav microSD ntawm sab hauv qab ntawm koj li Raspberry Pi.
  2. Nrhiav tus USB txuas kawg ntawm koj cov keyboard's cable, thiab txuas cov keyboard rau USB chaw nres nkoj ntawm Raspberry Pi (nws tsis muaj teeb meem qhov chaw nres nkoj twg koj siv).
  3. Nco ntsoov tias koj lub vijtsam raug ntsaws rau hauv lub qhov (socket) phab ntsa thiab hloov mus. Saib ntawm HDMI chaw nres nkoj (s) ntawm Raspberry Pi - ceeb toom tias lawv muaj lub tiaj tiaj nyob saum. Siv kab txuas txuas lub vijtsam mus rau Raspberry Pi's HDMI chaw nres nkoj - siv tus adapter yog tias tsim nyog.

  4. Txuas lub USB fais fab mov rau hauv lub qhov (socket) thiab txuas nws mus rau koj lub Raspberry Pi lub zog nres nkoj.
  5. Koj Raspberry Pi yuav pib khau raj tom qab ntawd koj yuav npaj mus.

Kauj ruam 2: Teeb Kho Vajtse

Teeb Kho Vajtse
Teeb Kho Vajtse

Kev teeb tsa lub ntsuas nrug deb ntawm ultrasonic yog yooj yim yooj yim, tsis muaj lwm qhov nyuaj xav tau, tsuas yog lub ntsuas cua, 4 kab thiab Raspberry Pi. Nws tsuas muaj plaub tus pin:

  • VCC rau Pin 2 (5V)
  • TRIG rau Pin 12 (GPIO 18)
  • ECHO rau tus Pin 18 (GPIO 24)
  • GND to Pin 6 (GND)

Kauj ruam 3: Python Script

Python Tsab Ntawv
Python Tsab Ntawv

Ua ntej tshaj plaws peb yuav tsum muaj lub tsev qiv ntawv sej gpiozero teeb tsa thiab siv peb yuav tsim tsab ntawv tshiab

sudo nano nrug_sensor.py

nrog cov hauv qab no:

# Tau txais cov tsev qiv ntawv uas peb xav tau

los ntawm gpiozero ntshuam DistanceSensor los ntawm lub sijhawm ntshuam pw tsaug zog # Pib ua haujlwm ultrasonic sensor sensor = DistanceSensor (ua rau = 18, ncha = 24) thaum Tseeb: # Tos 2 vib nas this pw (2) # Tau txais qhov nrug deb li deb li cas = sensor.distance # Tab sis peb xav tau nws hauv centimeters nrug = sensor.distance * 100 # Peb yuav tau txais tus lej zauv loj yog li peb yuav muab nws faib rau 2 qhov chaw nrug = puag ncig (sensor.distance, 2) # Luam tawm cov ntaub ntawv mus rau lub vijtsam luam tawm ("nrug: {} cm ".format (sensor.distance))

Pom zoo: