Cara setting ultrasonik di arduino menggunakan NodeMCU dan Blynk

STEP 1

Siapkan NodeMCU dan kabel jumper Female-Female. Sekarang, Colokan sensor ultrasonik ke NodeMCU dengan petunjuk berikut ini: VCC=>3V, Gnd=>G, D1=>ECHO, D2=>TRIG.

STEP 2

buka arduino klik new project hapus codingan yang ada di new project lalu paste codingan yang di bawah ini:

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#define trig D2
#define echo D1

long duration;
int distance;

// You should get Auth Token in the Blynk App.

char auth[] = “CocqqvQPtWE5OctdBW431MuKiu_k4_8U”; //your token
char ssid[] = “school”; // your ssid
char pass[] = “hakim1997”; // your pass

BlynkTimer timer;

void setup()
{
// Debug console
pinMode(trig, OUTPUT); // Sets the trigPin as an Output
pinMode(echo, INPUT); // Sets the echoPin as an Inpu
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);

// Setup a function to be called every second
timer.setInterval(1000L, sendSensor);
}

void loop()
{
Blynk.run();
timer.run();
}
void sendSensor()
{
digitalWrite(trig, LOW); // Makes trigPin low
delayMicroseconds(2); // 2 micro second delay

digitalWrite(trig, HIGH); // tigPin high
delayMicroseconds(10); // trigPin high for 10 micro seconds
digitalWrite(trig, LOW); // trigPin low

duration = pulseIn(echo, HIGH); //Read echo pin, time in microseconds
distance = duration * 0.034 / 2; //Calculating actual/real distance

Serial.print(“Distance = “); //Output distance on arduino serial monitor
Serial.println(distance);

if(distance <= 5)
{
Blynk.tweet(“My Arduino project is tweeting using @blynk_app and it’s awesome!\n #arduino #IoT #blynk”);
Blynk.notify(“Post has been twitted”);
}
Blynk.virtualWrite(V0, distance);
delay(1000); //Pause for 3 seconds and start measuring distance again
}

jangan lupa ssid,pass dan tokennya diganti. setelah itu di upload.

STEP 3

Sekarang buka aplikasi blynk di HP. klik tanda +, lalu klik Gauge, kemudian slide settingnya dari simple ke advanced. kemudian ubah inputnya jd V0. lalu limitnya ubah dari settingan awal ke 30. jika sukses, maka akan terlihat jaraknya di blynk kalian.

Terima kasih sudah membaca :)