Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana membuat sebuah alat yang berfungsi untuk mendeteksi warna kemudian akan ditampilkan dilayar LCD 16x2 berupa tulisan terkait warna yang dideteksi, alat ini sebelumnya harus dilakukan kalibrasi terlebih dahulu sebelum digunakan untuk mendeteksi warna. sensor warna yang dipakai yaitu TCS3200. untuk lebih jelasnya berikut adalah komponen dan programnya.
a. Arduino Uno
b. Sensor Warna TCS3200
c. LCD + I2C
d. Program Arduino IDE
#include <Wire.h> // i2C Conection Library
#include <LiquidCrystal_I2C.h> //i2C LCD Library
#include <Servo.h> //i2C LCD Library
//LiquidCrystal_I2C lcd(0x27, 16, 2);
LiquidCrystal_I2C lcd(0x3F, 16, 2);
Servo myservo;
#define S0 2
#define S1 3
#define S2 4
#define S3 5
#define sensorOut 7
int frequencyr = 0;
int frequencyg = 0;
int frequencyb = 0;
int jumlah;
void setup() {
myservo.attach(9);
myservo.write(70);
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(sensorOut, INPUT);
// Setting frequency-scaling to 20%
digitalWrite(S0,HIGH);
digitalWrite(S1,LOW);
Serial.begin(9600);
lcd.begin();
lcd.clear();
lcd.noCursor();
}
void loop() {
lcd.setCursor(0, 0);
// Setting red filtered photodiodes to be read
digitalWrite(S2,LOW);
digitalWrite(S3,LOW);
// Reading the output frequency
frequencyr = pulseIn(sensorOut, LOW);
// Printing the value on the serial monitor
lcd.print(frequencyr);//printing RED color frequency
lcd.print(" ");
delay(100);
// Setting Green filtered photodiodes to be read
digitalWrite(S2,HIGH);
digitalWrite(S3,HIGH);
// Reading the output frequency
frequencyg = pulseIn(sensorOut, LOW);
// Printing the value on the serial monitor
lcd.print(frequencyg);//printing RED color frequency
lcd.print(" ");
delay(100);
// Setting Blue filtered photodiodes to be read
digitalWrite(S2,LOW);
digitalWrite(S3,HIGH);
// Reading the output frequency
frequencyb = pulseIn(sensorOut, LOW);
// Printing the value on the serial monitor
lcd.print(frequencyb);//printing RED color frequency
lcd.println(" ");
delay(100);
if((frequencyr > 10)&&(frequencyr < 50)&&(frequencyg > 100)&&(frequencyg < 170)&&(frequencyb > 70)&&(frequencyb < 130))
{
lcd.setCursor(0, 1);
lcd.println("MERAH ");
myservo.write(100);
jumlah++;
delay(1000);
}
else if((frequencyr > 50)&&(frequencyr < 100)&&(frequencyg > 20)&&(frequencyg < 70)&&(frequencyb > 10)&&(frequencyb < 50))
{
lcd.setCursor(0, 1);
lcd.println("BIRU ");
myservo.write(150);
jumlah++;
delay(1000);
}
else if((frequencyr > 100)&&(frequencyr < 150)&&(frequencyg > 100)&&(frequencyg < 150)&&(frequencyb > 80)&&(frequencyb < 130))
{
lcd.setCursor(0, 1);
lcd.println("HIJAU ");
myservo.write(170);
jumlah++;
delay(1000);
}
else
{
lcd.setCursor(0, 1);
lcd.println(" ");
}
lcd.setCursor(12, 1);
lcd.print(jumlah);
lcd.print(" ");
}
#include <LiquidCrystal_I2C.h> //i2C LCD Library
#include <Servo.h> //i2C LCD Library
//LiquidCrystal_I2C lcd(0x27, 16, 2);
LiquidCrystal_I2C lcd(0x3F, 16, 2);
Servo myservo;
#define S0 2
#define S1 3
#define S2 4
#define S3 5
#define sensorOut 7
int frequencyr = 0;
int frequencyg = 0;
int frequencyb = 0;
int jumlah;
void setup() {
myservo.attach(9);
myservo.write(70);
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(sensorOut, INPUT);
// Setting frequency-scaling to 20%
digitalWrite(S0,HIGH);
digitalWrite(S1,LOW);
Serial.begin(9600);
lcd.begin();
lcd.clear();
lcd.noCursor();
}
void loop() {
lcd.setCursor(0, 0);
// Setting red filtered photodiodes to be read
digitalWrite(S2,LOW);
digitalWrite(S3,LOW);
// Reading the output frequency
frequencyr = pulseIn(sensorOut, LOW);
// Printing the value on the serial monitor
lcd.print(frequencyr);//printing RED color frequency
lcd.print(" ");
delay(100);
// Setting Green filtered photodiodes to be read
digitalWrite(S2,HIGH);
digitalWrite(S3,HIGH);
// Reading the output frequency
frequencyg = pulseIn(sensorOut, LOW);
// Printing the value on the serial monitor
lcd.print(frequencyg);//printing RED color frequency
lcd.print(" ");
delay(100);
// Setting Blue filtered photodiodes to be read
digitalWrite(S2,LOW);
digitalWrite(S3,HIGH);
// Reading the output frequency
frequencyb = pulseIn(sensorOut, LOW);
// Printing the value on the serial monitor
lcd.print(frequencyb);//printing RED color frequency
lcd.println(" ");
delay(100);
if((frequencyr > 10)&&(frequencyr < 50)&&(frequencyg > 100)&&(frequencyg < 170)&&(frequencyb > 70)&&(frequencyb < 130))
{
lcd.setCursor(0, 1);
lcd.println("MERAH ");
myservo.write(100);
jumlah++;
delay(1000);
}
else if((frequencyr > 50)&&(frequencyr < 100)&&(frequencyg > 20)&&(frequencyg < 70)&&(frequencyb > 10)&&(frequencyb < 50))
{
lcd.setCursor(0, 1);
lcd.println("BIRU ");
myservo.write(150);
jumlah++;
delay(1000);
}
else if((frequencyr > 100)&&(frequencyr < 150)&&(frequencyg > 100)&&(frequencyg < 150)&&(frequencyb > 80)&&(frequencyb < 130))
{
lcd.setCursor(0, 1);
lcd.println("HIJAU ");
myservo.write(170);
jumlah++;
delay(1000);
}
else
{
lcd.setCursor(0, 1);
lcd.println(" ");
}
lcd.setCursor(12, 1);
lcd.print(jumlah);
lcd.print(" ");
}
e. VIDEO HASILNYA
No comments:
Post a Comment