Home > Blogosphere > Counting Vehicle Using Arduino and Ultrasonic Sensor – Bagian 2

Counting Vehicle Using Arduino and Ultrasonic Sensor – Bagian 2


Posting ini adalah bagian ke 2, lanjutan dari posting https://gigihfordanama.wordpress.com/2018/05/24/counting-vehicle-using-arduino-and-ultrasonic-sensor-bagian-1/ , pada bagian kedua ini akan membahas tentang bagaimana informasi deteksi object bisa dimunculkan menggunakan display berupa LED P10 (harga berkisar 120-150 ribu), dengan penampakan sebagai berikut

Setelah perangkat keras P10 ini sudah tersedia, maka langsung saja dihubungkan ke perangkat Arduino, perlu diingat bahwa agar nyala lampu LED bisa lebih terang, maka board P10 perlu dihubungkan ke catudaya eksternal 5 Volt (bisa juga menggunakan output 5 Volt dari Arduino).

Wiring diagram antara konektor P10 dan  Arduino adalah sebagai berikut;

Penjelasan mengenai pin pada konektor adalah SBB

  • OE: Output Enable untuk on/off semua LED
  • A dan B: memilih kolom yg aktif.
  • CLK: SPI clock
  • SCLK: Latch Data Register
  • Data: SERIAL DATA SPI

Terakhir gunakan IDE (sebelumnya download library DMD2 dan taruh difolder instalasi Arduino) untuk membuat program deteksi sensor Ultrasonic dan menampilan hasil countingnya pada P10 LED seperti gambar berikut;

//LED
#include <SPI.h>
#include <DMD2.h>
#include <fonts/Arial_Black_16.h>
SoftDMD dmd(1,1); // DMD controls the entire display
//DMD_TextBox box(dmd, 20, 4);
DMD_TextBox box(dmd, 2, 1, 32, 16);

#include <Wire.h> // Comes with Arduino IDE
#define echoPin 3 // Echo Pin
#define trigPin 2 // Trigger Pin
#define ledPin 4

long duration, distance; // Duration used to calculate distance
int sensorCounter = 0; // counter for the number of button presses
int lastsensorDistance = 0;
int setCounter = 20;
int incomingByte;

void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ledPin, OUTPUT);

//P10 LED
dmd.setBrightness(255);
dmd.selectFont(Arial_Black_16);
dmd.begin();

}

void loop() {

if (Serial.available() > 0) { // see if there's incoming serial data:
incomingByte = Serial.read(); // read the oldest byte in the serial buffer:
if (incomingByte == 'R') { // if it's a capital R, reset the counter
Serial.println("Reset");
sensorCounter = 20;

}
}

/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);

digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);

//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;

if (distance <= 300 && lastsensorDistance >= 310){
sensorCounter++;

Serial.print("Object Yang LEWAT: ");
Serial.println(sensorCounter);
box.print(sensorCounter);
box.println(F(""));
digitalWrite(ledPin, HIGH);
}

else {

digitalWrite(ledPin, LOW);
}

lastsensorDistance = distance;
delay(100);

// turns on the LED when counter is at setCounter
if (sensorCounter >= setCounter) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}

}

Demikianlah tulisan untuk memunculkan hasil counting ke P10 LED

Leave a comment