Weekend ini saya terinspirasi oleh anak kedua saya yang meminta tangan saya untuk menjadi portal buka-tutup untuk mobil mainannya. Terbesit dipikiran untuk membuat sesuatu, yaitu portal otomatis. Setelah mengecek toolbox, ternyata ada satu lagi unit ESP8266, satu hobby servo 1Kg, satu unit sensor ultrasonic HC-SR04 dan passive buzzer yang nganggur.
Prinsip kerjanya sangat sederhana, servo akan bergerak jika sensor ultrasonic mendeteksi benda dalam jangkauan yang ditentukan. Bahan lengkap yang diperlukan dapat dibeli di www.tokopedia.com, antara lain:
Bahan:
1x ESP8266 NodeMCU
1x Mini Servo Tower Pro 1Kg
1x Ultrasonic Sensor
1x Passive buzzer
1x resistor 680 ohm
1x resistor 1K ohm
1x protoboard
kabel secukupnya
Box bekas
Stik es krim
Lem / glue gun
Waktu pengerjaan: 120 menit
Hasil akhirnya bisa diliihat di posting instagram berikut ini:
Instruksi rangkaian:

Instruksi script dan penjelasannya:
#Simple gate system with ESP8266 and Micropython #When object is at ultrasonic sensor defined range, then servo motor will be activated #While the servo is moving, beeping sound is added at every single movement of the motor #Script by Andi Dinata #1 April 2017 from machine import Pin, PWM import time #Define the buzzer. Pin = D6 (GPIO12) buzz=Pin(12,Pin.OUT) #Define the onboard led. Pin = D4 (GPIO02). #For onboard led, led.high() means off led=Pin(2, Pin.OUT) led.high() #Define servo. Pin = D5 (GPIO14) servo=PWM(Pin(14), freq=50) #Setup Servo duty cycle data for each position. min=35 max=110 range=max-min step=180/range #Setup and initiate the ultrasonic sensor Trigger Pin=D1 (GPIO05) #Echo Pin D2 (GPIO04) trig=Pin(5,Pin.OUT) trig.low() time.sleep_ms(2) trig.high() time.sleep_ms(10) trig.low() echo=Pin(4,Pin.IN) #Set function for the beeping sound def beep(s): buzz(1) time.sleep(s) buzz(0) time.sleep(s) #Set function to move and close the portal. a for angle, s for speed of portal #opening. For this example, the gate closes in 2 seconds after the servo reach #its full movement. def move(a,s): for i in range(min,(min+(int(a/step)))): servo.duty(i) time.sleep(s) beep(s) time.sleep(2) servo.duty(min) #Main prograam loop. The ultrasonic is triggered continuously. When the calculated #distance value is below the given threshold, then turn on the led and activate #the servo. while True: trig.high() time.sleep_ms(10) trig.low() while echo.value() == 0: pass start = time.ticks_us() while echo.value() == 1: pass stop = time.ticks_us() d = (stop - start) / 58 print(d) if d < 6: led.low() move(80,0.05) led.high() time.sleep(3)
Full script bisa didownload di Download page
Selamat mencoba.
Leave a Reply