0% found this document useful (0 votes)
25 views

5 Project Arduino (Arduino Book Versi Ketiga)

The document discusses several projects using an LCD display to show text and sensor data. It includes code examples to display blinking, scrolling and running text on the LCD. It also presents examples of using sensors like light, temperature and ultrasonic distance to display corresponding data on the LCD or serial monitor.

Uploaded by

Nur Ikhwan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

5 Project Arduino (Arduino Book Versi Ketiga)

The document discusses several projects using an LCD display to show text and sensor data. It includes code examples to display blinking, scrolling and running text on the LCD. It also presents examples of using sensors like light, temperature and ultrasonic distance to display corresponding data on the LCD or serial monitor.

Uploaded by

Nur Ikhwan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Project 8.

DISPLAY BANNER, BLINKING, SCROLLING & RUNNING TEXT DI LAYAR LCD

// Program menulis Banner text di layar LCD

// include the library code:


#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins


LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
// set up the LCD's number of rows and columns:
lcd.begin(16, 2);
lcd.clear(); // start with a blank screen
lcd.setCursor(0,0); // set cursor to column 0, row 0 (the first row)
lcd.print("BLUES LAND"); // change this text to whatever you like. keep it clean.
lcd.setCursor(0,1); // set cursor to column 0, row 1
lcd.print("Arifal Akmal");
}

void loop()
{

// Program Blinking Banner text

// include the library code:


#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins


LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
// set up the LCD's number of rows and columns:
lcd.begin(16, 2);
lcd.clear(); // start with a blank screen
lcd.setCursor(0,0); // set cursor to column 0, row 0 (the first row)
lcd.print("BLUES LAND"); // change this text to whatever you like. keep it clean.
lcd.setCursor(0,1); // set cursor to column 0, row 1
lcd.print("Arifal Akmal");
}

void loop() {
// Turn off the blinking cursor:
lcd.noDisplay();
delay(500);
// Turn on the display:
lcd.display();
delay(500);
}

// Program Banner scroll text 1234567890

// include the library code:


#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins


LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16,2);
}

void loop() {
// set the cursor to (0,0):
lcd.setCursor(0, 0);
// print from 0 to 9:
for (int thisChar = 0; thisChar < 10; thisChar++) {
lcd.print(thisChar);
delay(500);
}

// set the cursor to (16,1):


lcd.setCursor(16,1);
// set the display to automatically scroll:
lcd.autoscroll();
// print from 0 to 9:
for (int thisChar = 0; thisChar < 10; thisChar++) {
lcd.print(thisChar);
delay(500);
}
// turn off automatic scrolling
lcd.noAutoscroll();

// clear screen for the next loop:


lcd.clear();
}

// Program Baner running text

// include the library code:


#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins


LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
// set up the LCD's number of rows and columns:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("ROBOT KREATIF"); // 12 huruf
delay(1000);
}

void loop() {
// scroll 12 positions (string length) to the left
// to move it offscreen left:
for (int positionCounter = 0; positionCounter < 12; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
// wait a bit:
delay(300);
}

// scroll 28 positions (string length + display length) to the right


// to move it offscreen right: 12+16 = 28
for (int positionCounter = 0; positionCounter < 28; positionCounter++) {
// scroll one position right:
lcd.scrollDisplayRight();
// wait a bit:
delay(300);
}

// scroll 16 positions (display length + string length) to the left


// to move it back to center:
for (int positionCounter = 0; positionCounter < 16; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
// wait a bit:
delay(300);
}

// delay at the end of the full loop:


delay(1000);

}
Project 9. SENSOR CAHAYA PHOTOCELL / LDR

Dengan sensor cahaya LDR (Light Dependent Resistor), Lampu akan otomatis menyala (On) jika
sensor tidak terkena cahaya (gelap). Sebaliknya, lampu akan mati (Off ) apabila sensor terkena
cahaya terang.

Sketch:

// Program Lampu otomatis gelap/terang

Int sensorPin = 0; // pin signal LDR dihubungkan ke Port Analog 0 Arduino


Int ledPin = 13; // pin untuk LED
int sensorValue = 0; // variable nilai yg dihasilkan sensor

void setup()
{
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // untuk membaca data pada serial port di layar monitor
}
void loop()
{
sensorValue = analogRead(sensorPin); // membaca nilai dari sensor:

Serial.println(sensorValue); // menulis nilai sensor di layar monitor

if (sensorValue <= 500 ) // tentukan batas intensitas cahaya 0 - 1024


{
digitalWrite(ledPin, HIGH); // menyalakan lampu LED (on)
}
else
{
digitalWrite(ledPin, LOW); // mematikan lampu LED (off)
}
}

Catatan: Pin signal modul LDR (juga keluarga resistor yg lain, misalnya Potensiometer,
Thermistor) dihubungkan dengan port Analog Arduino, bukan port digital
Project 10. TERMOMETER DIGITAL

Untuk mengukur suhu, caranya cukup mudah. IC jenis LM 35 ini cukup praktis, bentuknya kecil
dan akurasinya tinggi. Cukup dengan menghubungkan kakinya ke kutub + , A0 dan – seperti
pada gambar, nilai tegangan listrik yang didapat akan dikonversi menjadi nilai suhu dalam
satuan derajat Celsius/Rheamur/Fahrenheit.

Sketch:

a. Nilai temperatur ditampilkan di layar komputer

int potPin = 0; // select the input pin for the LM35


float temperature = 0; // type float -> 2 angka di blkng
koma
long val = 0; // tyle long = int, hanya range nilainya lebih besar

void setup()
{
Serial.begin(9600);
}
void loop()
{
if (Serial.available()) // if monitor screen opened
{
val = analogRead(potPin); // read the value from the sensor
temperature = (5.0 * val * 100.0)/1024.0; // convertion formula to Celcius
Serial.println(temperature); // write temperature to notebook
monitor
// Serial.println((long)temperature); // jika nilainya ingin dibulatkan
}
delay(1000);
}

b. Nilai temperatur ditampilkan di layar LCD

#include <LiquidCrystal.h> // include library for LCD

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // pin to LCD

int potPin = 0; // select the input pin for the LM35


float temperature = 0;
long val = 0;

void setup()
{
lcd.begin(16, 2); // set up the LCD's number of rows and columns
}

void loop()
{
val = analogRead(potPin); // read the value from the sensor
temperature = (5.0 * val * 100.0)/1024.0; // convert to Celcius

lcd.clear(); // clear LCD screen


lcd.setCursor(0,0); // set text to LCD row 1
lcd.print("current temp. "); // some text to add meaning to the numbers
lcd.setCursor(0,1); // set text to LCD row 2
lcd.print((long)temperature); // writing temperature value
lcd.print(" deg.C");

delay(1000);
}
c. Thermometer digital dengan fitur lampu indikator

Tambahkan pengukur suhu ini dengan fitur lampu indikator (rangkaian traffict light) yang
menyala bergantian pada suhu tertentu. Gunakan logika IF-ELSE atau SWITCH-CASE.

int potPin = 0; // select the input pin for the LM35


float temperature = 0;
int suhu;
long val = 0;
int redLight = 7;
int yellowLight = 6;
int greenLight = 4;

void setup()
{
Serial.begin(9600);

pinMode(redLight, OUTPUT);
pinMode(yellowLight, OUTPUT);
pinMode(greenLight, OUTPUT);
}

void loop()
{
if (Serial.available()) // if monitor screen opened
{
val = analogRead(potPin); // read the value from the sensor
temperature = (5.0 * val * 100.0)/1024.0;
suhu = temperature;
Serial.println("Suhu sekarang adalah : ");
Serial.println((long)temperature);

if (suhu > 30) { // Jika lebih besar dari 30 derajat


digitalWrite(redLight,HIGH);
digitalWrite(yellowLight,LOW);
digitalWrite(greenLight,LOW);
}

if (suhu >=27 || suhu <= 30) { // Jika suhunya antara 27-30 derajat
digitalWrite(yellowLight,HIGH);
digitalWrite(greenLight,LOW);
digitalWrite(redLight,LOW);
}
if (suhu < 27) { // Jika suhunya kurang dari 27 derajat
digitalWrite(greenLight,HIGH);
digitalWrite(redLight,LOW);
digitalWrite(yellowLight,LOW);
}

}
delay(5000);
}

Catatan:

---------------------------- Jika menggunakan IF - ELSE

if (suhu > 24) //


{
}
if (suhu == 26) // ‘sama dengan’ ditulis dengan simbul == . Tidak sama dengan, simbulnya !=
{
}
if (suhu != 24 || suhu !=26) // simbul || artinya OR, sedangkan simbul && artinya AND
{
}

-------------------------- Jika menggunakan SWICH - CASE

switch (suhu)
{
case 24 : // Jika suhunya 24 derajat
……….
break;

case 26 : // Jika suhunya 23 derajat


……….
break;

default : // jika suhunya selain 23 dan 24 derajat


……….
}
Project 11. PENGUKUR JARAK

Untuk mengukur jarak digunakan Sensor Ultrasonic yang cara kerjanya adalah memancarkan
gelombang ultrasonic dan menangkap pantulannya jika mengenai benda di depannya. Waktu
pantul itulah yang akan di konversi kedalam satuan jarak.

Sketch:

const int pingPin = 7;

void setup() {
Serial.begin(9600);
}
void loop() {

long duration, cm;

pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

cm = microsecondsToCentimeters(duration);

Serial.print(cm);
Serial.print("cm");
Serial.println();

delay(1000);
}

long microsecondsToCentimeters(long microseconds)


{
return microseconds / 29 / 2;
}

#include <LiquidCrystal.h> // include library for LCD

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // pin to LCD


int pingPin = 7;

void setup()
{
lcd.begin(16, 2); // set up the LCD's number of rows and columns
}

void loop() {

long duration, cm;


pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

cm = microsecondsToCentimeters(duration);

lcd.clear();
lcd.setCursor(0, 0);
lcd.print(cm);
lcd.print("cm");

delay(1000);
}

long microsecondsToCentimeters(long microseconds)


{
return microseconds / 29 / 2;
}

Catatan:

Sensor jarak ini banyak diimplementasikan untuk berbagai macam proyek robotik, misalnya:
- Penaksir jarak
- Robot obstacle avoider (Penghindar halangan)
- Radar pendeteksi benda asing
- Pengukur tinggi badan
- Pengukur ketinggian air
Project 12. PENDETEKSI GERAK

Untuk mendeteksi gerakan, diperlukan sensor gerak yang biasa disebut Passive Infra Red (PIR),
yang cara kerjanya adalah mendeteksi adanya perbedaan/perubahan suhu sekarang dan
sebelumnya.

/*
Motion detector
*/

const byte ledPin = 13; // internal LED pada pin 13


const byte motionPin = 2; // pin Signal sensor gerak dihubungkan dengan port
2 byte senseMotion = 0; // variable deteksi gerak

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(motionPin, INPUT);
}

void loop()
{
senseMotion = digitalRead(motionPin); // membaca nilai sensor gerak
if (senseMotion == HIGH) { // jika terdeteksi ada gerakan
digitalWrite(ledPin, HIGH);
delay(2000);
} else { // jika tidak ada gerakan
digitalWrite(ledPin, LOW);
}
}
Project 13. PENGENDALI PERALATAN LISTRIK DENGAN REMOTE CONTROL TV

Sensor yang dapat menangkap signal dari remote control TV disebut Infra Red Decoder (IR
Decoder). Setiap tombol remote control memiliki nilai yang berbeda. Kita bisa menangkap nilai
tombol yang diinginkan dengan menampilkannya pada layar monitor dan memberikan perintah
tertentu untuk tombol tersebut.

Catatan:
- Gunakan rangkaian lampu traffict light sebagai
output
- Sinyal remote control yg ditangkap IR
Decoder dan nilainya ditampilkan dilayar
monitor.
- Selanjutnya, tombol yang diinginkan (misal:
1,2,3) dapat diberi perintah untuk
menyalakan LED 1, 2 atau 3

// Apabila menggunakan Remote control merek Sonny

#include <IRremote.h>

int RECV_PIN = 11;


IRrecv irrecv(RECV_PIN); // Output signal sensor remote dihubungkan ke pin 11
decode_results results;
int key;
int led1 = 5; // lampu1 pada pin 5
int led2 = 6; // lampu2 pada pin 6
int led3 = 7; // lampu3 pada pin 7

void setup()
{
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}
unsigned long last = millis();

void loop() {
if (irrecv.decode(&results)) {
// If it's been at least 1/4 second since the last
if (millis() - last > 250) {
key = results.value, HEX;
Serial.println(key); // Mengetahui nilai tombol remote

if(key == 16) {
Serial.println("tombol 1"); // jika ditekan tombol 1
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
}
if(key == 2064) {
Serial.println("tombol 2"); // jika ditekan tombol 2
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
}
if(key == 1040) {
Serial.println("tombol 3"); // jika ditekan tombol 3
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
}
}
last = millis();
irrecv.resume(); // Receive the next value
}
}

Catatan: Gunakan statement SWITCH-CASE sebagai pengganti IF – ELSE,


agar proses berjalan lebih cepat !

You might also like