Cov txheej txheem:

Ultrasonic Sensor txhawm rau txhawm rau Hloov Txoj Haujlwm ntawm Cov Hom Phiaj: 3 Kauj Ruam
Ultrasonic Sensor txhawm rau txhawm rau Hloov Txoj Haujlwm ntawm Cov Hom Phiaj: 3 Kauj Ruam

Video: Ultrasonic Sensor txhawm rau txhawm rau Hloov Txoj Haujlwm ntawm Cov Hom Phiaj: 3 Kauj Ruam

Video: Ultrasonic Sensor txhawm rau txhawm rau Hloov Txoj Haujlwm ntawm Cov Hom Phiaj: 3 Kauj Ruam
Video: Ultrasonic sensor working principle | How does HC-SR04 Work 2024, Hlis ntuj nqeg
Anonim
Ultrasonic Sensor txhawm rau txhawm rau hloov pauv ntawm cov khoom
Ultrasonic Sensor txhawm rau txhawm rau hloov pauv ntawm cov khoom

Nws yog ib qho tseem ceeb kom muaj koj cov khoom muaj txiaj ntsig muaj kev nyab xeeb, nws yuav tsis muaj zog yog tias koj ua tib zoo saib xyuas koj lub tsev fuabtais tas hnub. Siv lub koob yees duab raspberry pi koj tuaj yeem siv lub snaps ntawm lub sijhawm. Phau ntawv qhia no yuav pab koj tua cov vis dis aus lossis thaij duab thaum qhov kev hloov pauv tau nkag siab hauv thaj tsam ciam teb.

Kho vajtse:

  1. Raspberry Pi 2/3/4
  2. Ultrasonic sensor
  3. Pi lub koob yees duab
  4. Cov neeg dhia

Kauj ruam 1: Kev sib txuas

Kev sib txuas
Kev sib txuas
  • TXI to RPI4B 17
  • VCC to RPI4B 5V
  • GND to RPI4B GND
  • Ncha mus rau 470-ohm resistor rau txuas-1
  • GND rau 1K ohm resistor rau txuas-1
  • txuas-1 rau RPI4B 4

Cov txheej txheem hluav taws xob tau tsim los siv circuito.io, nws muaj txhua qhov nrov tshaj plaws microcontrollers, sensors, thiab lwm yam thiab lub platform yooj yim siv rau cov pib tshiab.

Kauj ruam 2: Upload Code

Rub tawm Txoj Cai
Rub tawm Txoj Cai
Rub tawm Txoj Cai
Rub tawm Txoj Cai

Ua ntej koj khiav tsab ntawv, tsim cov ntawv tais ceev tseg los ntawm cov lus txib hauv qab no qhib lub davhlau ya nyob twg thiab tom qab ntawd hloov cov ntawv sau.

pi@raaspberrypi: mkdir media

pi@raaspberrypi: nano measure.py

Txoj cai siv lub koob yees duab thiab tsev qiv ntawv GPIO. Hla-kos GPIO_TRIGGER & GPIO_ECHO pins tau txuas nrog kom raug rau 17th & 4th pins ntawm Raspberry Pi sab nraud.

Luam thiab paste cov cai hauv qab no lossis ntaus rau hauv cov ntaub ntawv nab thiab sau nws li 'measure.py'

#Librariesimport RPi. GPIO li GPIO ntshuam lub sijhawm ntshuam os los ntawm picamera ntshuam PiCamera # Lub koob yees duab hom lub koob yees duab = PiCamera () camera.rotation = 180 # Qhia kab ntawv no yog cov duab zoo kawg nkaus #GPIO Hom GPIO.setmode (GPIO. BCM) GPIO.setwarnings (Cuav) #set GPIO Pins GPIO_TRIGGER = 17 GPIO_ECHO = 4 #set GPIO kev taw qhia (IN / OUT) GPIO.setup (GPIO_TRIGGER, GPIO. OUT) GPIO.setup (GPIO_ECHO, GPIO. IN) def nrug (): # teeb Trigger rau HIGH GPIO.output (GPIO_TRIGGER, True) # teeb tsa tom qab 0.01ms rau LOW time.sleep (0.00001) GPIO.output (GPIO_TRIGGER, False) StartTime = time.time () StopTime = time.time () # txuag Pib Sijhawm thaum GPIO.input (GPIO_ECHO) == 0: PibTime = sijhawm.time () # txuag lub sijhawm tuaj txog thaum GPIO.input (GPIO_ECHO) == 1: StopTime = time.time () # lub sijhawm sib txawv ntawm pib thiab tuaj txog TimeElapsed = StopTime - StartTime # sib npaug nrog lub suab sonic (34300 cm / s) # thiab faib ua 2, vim tias muaj thiab nrug nrug = (TimeElapsed * 34300) / 2 rov qab nrug yog _name_ == '_main_': camera.start_preview (alpha = 200) sim: thaum Tseeb: deb = nrug () luam tawm ("Ntsuas nrug = %.1f cm" % deb) yog qhov deb <= 20: # hloov tus nqi no raws li koj teeb tsa tam sim no = sijhawm.ctime (). hloov ("", "-") camera.capture ("media/image % s.jpg" % tam sim no) luam tawm ("Duab khaws tseg ntawm media/image- % s.jpg" % tam sim no) # lub koob yees duab.start_recording ("media/video- % s.h264" % tam sim no) # Tsis pom zoo qhov no txhawm rau thaij video # luam tawm ("Video tau khaws tseg ntawm media/duab- % s.jpg" % tam sim no) # tsaug zog (5) # Tsis pom zoo qhov no siv sijhawm yees duab 5 vib nas this.sleep (3) camera.stop_preview () # camera.stop_recording () # Tsis pom zoo qhov no txhawm rau thaij video # Rov pib dua los ntawm nias CTRL + C tshwj tsis yog KeyboardInterrupt: luam tawm ("Kev ntsuas ntsuas nres los ntawm Tus Neeg Siv") GPIO.cleanup ()

Kauj ruam 3: Khiav Code

Khiav Txoj Cai
Khiav Txoj Cai
Khiav Txoj Cai
Khiav Txoj Cai
Khiav Txoj Cai
Khiav Txoj Cai
Khiav Txoj Cai
Khiav Txoj Cai

Tam sim no khiav tsab ntawv li

pi@raspberrypi: nab hab sej ntsuas.py

Qhov kev ncua deb yog ntsuas rau txhua 3 vib nas this (koj tuaj yeem hloov tus nqi hauv tsab ntawv) thiab tau luam tawm rau ntawm lub vijtsam yog tias muaj cov khoom pom nyob hauv 20 centimeters, lub koob yees duab pi yuav yees duab thiab khaws cia hauv cov ntawv xov xwm.

Xwb, koj tuaj yeem tua cov vis dis aus los ntawm kev tsis txaus siab lossis tshem tawm cov hashtags (#) los ntawm kab ntawv kab lus uas tau hais raws li cov lus pom. Koj tseem tuaj yeem txuas cov vis dis aus ntev los ntawm kev yooj yim nce/txo tus nqi hauv "time.sleep (5)".

Zoo siab Circuiting!

Pom zoo: