Anda di halaman 1dari 22

MODUL

KOMPUTER TERAPAN

XI TKJ
Anisa Rahmanti, S.Kom

1| M o d u l K o m p u t e r Te r a p a n
DAFTAR ISI

Daftar Isi...................................................................................................................................................... 2
Materi .........................................................................................................................................................3
Praktik 1. Sensor Ultrasonik ................................................................................................................... 3
Praktik 2. Keypad ...................................................................................................................................5
Praktik 3. Lampu Berjalan dengan Potensiometer.................................................................................... 7
Praktik 4. Button Switch .........................................................................................................................9
Praktik 5. PIR ......................................................................................................................................... 11
Praktik 6. Light Sensor ........................................................................................................................... 12
Praktik 7. Mini PIR ................................................................................................................................ 13
Praktik 8. Sensor Suhu ........................................................................................................................... 14
Praktik 9. Traffic Light ........................................................................................................................... 16
Praktik 10. Lampu Cerdas Cermat ..........................................................................................................18
Praktik 11. Penghitung Jumlah Tamu ..................................................................................................... 20
Praktik 12. Lampu Berjalan Bolak Balik ................................................................................................ 23

2| M o d u l K o m p u t e r Te r a p a n
Materi
Praktik 1. Sensor Ultrasonik
Alat : 1. Sensor Ultrasonik HC-SR04
2. Kabel Jumper
Hasil : Serial Monitor, akan berubah bila halangan di depan sensor berbeda jarak
Tugas : Tampilan di Serial Monitor menjadi jarak dengan satuan meter

/*
HC-SR04
Tutorial untuk mengukur jarak menggunakan sensor ultrasonic HC-SR04
Dibuat oleh Q-Electronics
6 November 2016
*/

/* Set pin trigger HC-SR04 ke GPIO nomor 10 pada Arduino, pin Echo ke GPIO nomor 9 */
const int trigPin = 10;
const int echoPin = 9;

long durasi; // waktu perjalanan sinyal ultrasonic dalam microseconds


int jarak; // jarak antar sensor dan benda terdekat yang ingin diukur

void setup() {

pinMode(trigPin, OUTPUT); // set trigPin sebagai Output


pinMode(echoPin, INPUT); // set echoPin sebagai Input
Serial.begin(9600);
}

void loop() {
// clear trigPin dengan memberikan sinyal LOW ke HC-SR04 selama 2 microseconds
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// mengaktifkan pin trigger dengan memberikan sinyal HIGH selama 10 microseconds

3| M o d u l K o m p u t e r Te r a p a n
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// membaca waktu yang dibutuhkan sinyal ultrasonic dari transmiter HC-SR04


// untuk kembali ke receiver pada sensor HC-SR04 dalam microseconds
durasi = pulseIn(echoPin, HIGH);

// menghitung jarak berdasarkan waktu perjalanan sinyal ultrasonic dalam satuan cm


jarak = durasi * 0.034 / 2;

// Menampilkan jarak antar sensor dan benda


Serial.print("Jarak: ");
Serial.print(jarak);
Serial.println(" cm");
delay(500);
}

Gambar 1.1

4| M o d u l K o m p u t e r Te r a p a n
Materi
Praktik 2. Keypad

Alat : 1. Keypad 4x4


2. Kabel Jumper
Hasil : Serial monitor, muncul sesuai tombol keypad yang dipencet
Tugas : Buat output di Serial Monitor ganti baris setiap ditekan
Perhatian : Input dulu library keypad.h

/*4x4 Matrix Keypad connected to Arduino


This code prints the key pressed on the keypad to the serial port*/

#include <Keypad.h>

const byte numRows= 4; //number of rows on the keypad


const byte numCols= 4; //number of columns on the keypad

//keymap defines the key pressed according to the row and columns just as appears on the keypad
char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};

//Code that shows the the keypad connections to the arduino terminals
byte rowPins[numRows] = {9,8,7,6}; //Rows 0 to 3
byte colPins[numCols]= {5,4,3,2}; //Columns 0 to 3

5| M o d u l K o m p u t e r Te r a p a n
//initializes an instance of the Keypad class
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

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

//If key is pressed, this key is stored in 'keypressed' variable


//If key is not equal to 'NO_KEY', then this key is printed out
//if count=17, then count is reset back to 0 (this means no key is pressed during the whole
keypad scan process
void loop()
{
char keypressed = myKeypad.getKey();
if (keypressed != NO_KEY)
{
Serial.print(keypressed);
}
}

Gambar 2.1

6| M o d u l K o m p u t e r Te r a p a n
Materi
Praktik 3. Lampu Berjalan dengan Potensiometer

Alat : 1. Potensiometer,
2. LED
3. Kabel jumper
Hasil : LED akan menyala sesuai putaran potensiometer
Tugas : Ubah LED menjadi 3 warna (merah 3, kuning 3, hijau 4)

const int analogPin = A0; // the pin that the potentiometer is attached to
const int ledCount = 10; // the number of LEDs in the bar graph

int ledPins[] = {
2, 3, 4, 5, 6, 7, 8, 9, 10, 11
}; // an array of pin numbers to which LEDs are attached

void setup() {
// loop over the pin array and set them all to output:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
}

void loop() {
// read the potentiometer:
int sensorReading = analogRead(analogPin);
// map the result to a range from 0 to the number of LEDs:
int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);

// loop over the LED array:


for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// if the array element's index is less than ledLevel,

7| M o d u l K o m p u t e r Te r a p a n
// turn the pin for this element on:
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH);
}
// turn off all pins higher than the ledLevel:
else {
digitalWrite(ledPins[thisLed], LOW);
}
}
}

Gambar 3.1

8| M o d u l K o m p u t e r Te r a p a n
Materi
Praktik 4. Button Switch
Alat : 1. Push button/Tactile Switch
2. LED
3. Kabel jumper
4. Resistor 10 K
Hasil : Saat ditekan, LED akan menyala
Tugas : Sediakan 2 LED, bila button ditekan, LED 1 nyala, LED 2 mati, dan sebaliknya

const int buttonPin = 2; // the number of the pushbutton pin


const int ledPin = 13; // the number of the LED pin

// variables will change:


int buttonState = 0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else { Gambar 4.1
// turn LED off:
digitalWrite(ledPin, LOW);
}}

9| M o d u l K o m p u t e r Te r a p a n
Materi
Praktik 5. PIR

Alat : 1. LED
2. Kabel Jumper
3. Sensor PIR
Hasil : Bila terdeteksi gerakan, maka LED akan menyala
Tugas : Bila tidak ada gerakan, LED hijau menyala, bila ada gerakan, LED merah menyala dan LED hijau mati

const int ledPin= 13;


const int inputPin= 2;

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

void loop(){
int value= digitalRead(inputPin);

if (value == HIGH)
{
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
} Gambar 5.1
else
{
digitalWrite(ledPin, LOW);
}
}

10| M o d u l K o m p u t e r Te r a p a n
Materi
Praktik 6. Light Sensor
Alat : 1. LED
2. Kabel Jumper
3. Light Sensor
Hasil : LED akan menyala apabila tingkat cahaya = 250, dipantau dari Serial Monitor
Tugas : LED merah akan menyala apabila tingkat cahaya >250, dan LED hijau akan menyala apabila tingkat cahaya <=250

int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor

void setup() {
// declare the ledPin as an OUTPUT:
Serial.begin(9600);
pinMode(ledPin, OUTPUT);

void loop()
{
sensorValue=analogRead(sensorPin);
if(sensorValue <= 250)
digitalWrite(ledPin,HIGH);
else
digitalWrite(ledPin,LOW);
Serial.println(sensorValue);
delay(1000);
}

Gambar 6.1

11| M o d u l K o m p u t e r Te r a p a n
Materi
Praktik 7. Mini PIR
Alat : 1. Mini PIR Sensor
2. Kabel Jumper
3. LED
4. Resistor 220 Ω
Hasil : Bila terdeteksi gerakan, akan muncul tulisan di serial monitor
Tugas : Tambahkan LED merah dan LED hijau. Saat ada gerakan, LED merah menyala. Saat tidak ada gerakan, LED hijau
menyala.

void setup() {
Serial.begin(9600);
pinMode(6,INPUT);
digitalWrite(6,LOW);
}
void loop() {
if(digitalRead(6)==HIGH) {
Serial.println("Awas, ada penyusup!");
}
else {
Serial.println("Aman");
}
delay(1000);
}

Gambar 7.1

12| M o d u l K o m p u t e r Te r a p a n
Materi
Praktik 8. Sensor Suhu

Alat : 1. LM 35/ sensor suhu


2. Kabel jumper.
3. Resistor 220 Ω
4. LED
Program ini akan menyalakan LED apabila suhu 20°C-32°C
Hasil : Serial monitor dan nyala LED
Tugas : bila suhu >20°C maka LED akan mati

/*
Sensing the Temperature with the LM35
This script will output the sensed temperature of the LM35 from 20 to 32°C by LEDs.
inspired by
ladyada http://www.ladyada.net/learn/sensors/tmp36.html
pscmpf http://pscmpf.blogspot.com/2008/12/arduino-lm35-sensor.html

*/

float temperature; // stores the temperature


int sensorPin = 0; // pin where the sensor is connected to
int startTemp=20; // the start temperature > at this temperature, no LED
will light up

void setup()
{
Serial.begin(9600); // initialisation of the serial connection
for (int i=2;i<8; i++){ // output channels from 2 to 7
pinMode(i,OUTPUT); // pin is a output
}
}

13| M o d u l K o m p u t e r Te r a p a n
void loop()
{
temperature = analogRead(sensorPin); // reading analog sensor value
temperature = temperature*0.488; // correcting to °C

for (int i=0;i<8; i++){


if (temperature>((i*2)+startTemp)){ // switch LED on, if temperature is higher than
starttemp + (LED number*2)
digitalWrite(i,HIGH);
}
else {
digitalWrite(i,LOW); // else, switch it off
}
}
Serial.print(temperature);Serial.println(" °C"); // send the temperature to the serial
monitor
delay(500); // just wait a little
}

Gambar 8.1

14| M o d u l K o m p u t e r Te r a p a n
Materi
Praktik 9. Traffic Light

Alat : 1. LED
2. Kabel Jumper
3. Resistor 220 Ω
Hasil : Lampu hijau no 1 menyala, lampu merah no 2 menyala. Lampu kuning keduanya akan menyala bila akan berganti ke lampu
hijau
Tugas : Membuat Traffic Light perempatan (4 set)

int g1 = 2;
int y1 = 3;
int r1 = 4;
int g2 = 5;
int y2 = 6;
int r2 = 7;

void setup() {
pinMode (r1, OUTPUT);
pinMode (y1, OUTPUT);
pinMode (g1, OUTPUT);

pinMode (r2, OUTPUT);


pinMode (y2, OUTPUT);
pinMode (g2, OUTPUT);

15| M o d u l K o m p u t e r Te r a p a n
void loop() {

digitalWrite(g1, HIGH);
digitalWrite(r2, HIGH);
digitalWrite(y1, LOW);
digitalWrite(y2, LOW);
delay(3000);
digitalWrite(g1, LOW);
digitalWrite(r2, LOW);
digitalWrite(y1, HIGH);
digitalWrite(y2, HIGH);
delay(1000);
digitalWrite(y1, LOW);
digitalWrite(y2, LOW);
digitalWrite(r1, HIGH);
digitalWrite(g2, HIGH);
delay(3000);
digitalWrite(g2, LOW);
digitalWrite(r1, LOW);
digitalWrite(y1, HIGH); Gambar 9.1
digitalWrite(y2, HIGH);
delay(1000);
}

16| M o d u l K o m p u t e r Te r a p a n
Materi
Praktik 10. Lampu Cerdas Cermat

Alat : 1. LED
2. Kabel Jumper
3. Resistor 220 Ω
Hasil : Bila dikirimkan karakter sesuai case dari Serial Monitor, LED akan menyala sesuai case nya
Tugas : Buat nyala lampu lebih lama untuk melihat grup mana yang memencet tombol

void setup() {
// initialize serial communication:
Serial.begin(9600);
// initialize the LED pins:
for (int thisPin = 2; thisPin < 5; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}

void loop() {
// read the sensor:
if (Serial.available() > 0) {
int inByte = Serial.read();
// do something different depending on the character received.
// The switch statement expects single number values for each case;
// in this exmaple, though, you're using single quotes to tell
// the controller to get the ASCII value for the character. For
// example 'a' = 97, 'b' = 98, and so forth:

switch (inByte) {
case 'a':
digitalWrite(2, HIGH);
break;
case 'b':

17| M o d u l K o m p u t e r Te r a p a n
digitalWrite(3, HIGH);
break;
case 'c':
digitalWrite(4, HIGH);
break;
default:
// turn all the LEDs off:
for (int thisPin = 2; thisPin < 5; thisPin++) {
digitalWrite(thisPin, LOW);
}
}

Gambar 10.1

18| M o d u l K o m p u t e r Te r a p a n
Materi
Praktik 11. Penghitung Jumlah Tamu
Alat : 1. pushbutton/tactile switch
2. LED
3. Kabel jumper
4. Resistor 10K
Hasil : Serial monitor akan menampilkan jumlah button ditekan
Tugas : membuat program untuk 2 button, button 1 : tamu datang, button 2 tamu pulang

const int buttonPin = 2; // pin yang terhubung pushbutton


const int ledPin = 13; // pin yang terhubung LED

// Variables will change:


int buttonTekan = 0; // pencacah saat button ditekan
int buttonState = 0; // status dari button
int lastButtonState = 0; // status button sebelumnya

void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}

void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);

// compare the buttonState to its previous state


if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:

19| M o d u l K o m p u t e r Te r a p a n
buttonTekan++;
Serial.println("siap");
Serial.print("Jumlah tamu yang datang : ");
Serial.println(buttonTekan);
} else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("selesai");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
if (buttonTekan % 4 == 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}

Gambar 11.1

20| M o d u l K o m p u t e r Te r a p a n
Materi
Praktik 12. Lampu Berjalan Bolak Balik

Alat : 1. LED
2. Resistor 220 Ω
3. Kabel Jumper
Hasil : Lampu menyala berjalan bolak balik
Tugas : Menambah menjadi 10 LED

const int pin_9 = 9;


const int pin_10 = 10;
const int pin_11 = 11;
const int pin_12 = 12;
const int pin_13 = 13;

int arah = 1;
int indeksled = 0;

void setup() {
pinMode(pin_9, OUTPUT);
pinMode(pin_10, OUTPUT);
pinMode(pin_11, OUTPUT);
pinMode(pin_12, OUTPUT);
pinMode(pin_13, OUTPUT);
}

void loop() {
int pin;
if (indeksled == 0)
pin = pin_13;
else
if (indeksled == 1)
pin = pin_12;

21| M o d u l K o m p u t e r Te r a p a n
else
if (indeksled == 2)
pin = pin_11;
else
if (indeksled == 3)
pin = pin_10;
else
if (indeksled == 4)
pin = pin_9;

digitalWrite (pin, HIGH);


delay (200);
digitalWrite (pin, LOW);

if (indeksled == 5)
arah = -1;
else
if (indeksled == 0)
arah = 1;
indeksled = indeksled + arah;
}

22| M o d u l K o m p u t e r Te r a p a n

Anda mungkin juga menyukai