Digital IO, LCD, Analog Input
Digital IO, LCD, Analog Input
void setup() {
// put your setup code here, to run once:
pinMode(pin_led,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(pin_led,HIGH);
delay(1000);
digitalWrite(pin_led,LOW);
delay(1000);
}
LED ON
void setup() {
// put your setup code here, to run once:
pinMode(13,OUTPUT);
}
void setup() {
// put your setup code here, to run once:
pinMode(13,OUTPUT);
pinMode(12,OUTPUT);
pinMode(11,INPUT_PULLUP);
pinMode(10,INPUT_PULLUP);
}
void loop() {
// put your main code here, to run repeatedly:
data1 = digitalRead(11);
data2 = digitalRead(10);
int data1, data2; if (data1==0) {
digitalWrite(13,HIGH);
void setup() { } else {
// put your setup code here, to run once: digitalWrite(13,LOW);
pinMode(13,OUTPUT); }
pinMode(12,OUTPUT); if (data2==0) {
pinMode(11,INPUT_PULLUP); digitalWrite(12,HIGH);
pinMode(10,INPUT_PULLUP); } else {
} digitalWrite(12,LOW);
}
void loop() { }
// put your main code here, to run repeatedly:
data1 = digitalRead(11);
data2 = digitalRead(10);
digitalWrite(13,data1); If Statement (Conditional Statement)
digitalWrite(12,data2); The if() statement is the most basic of all programming
} control structures. It allows you to make something
happen or not, depending on whether a given condition
is true or not. It looks like this:
LED-PB-IF
if (someCondition) {
// do stuff if the condition is true
}
There is a common variation called if-else that looks
like this:
if (someCondition) {
// do stuff if the condition is true
} else {
// do stuff if the condition is false
}
There's also the else-if, where you can check a second
condition if the first is false:
if (condition1) {
// do Thing A
}
else if (condition2) {
// do Thing B
}
else {
// do Thing C
}
LCD
LCD I2C
// include the library code:
#include <LiquidCrystal.h> #include <LiquidCrystal_I2C.h>
// Creates an LCD object. Parameters: (rs, en, d4, d5, d6, //(I2C_Address, Kolom, Baris)
d7) LiquidCrystal_I2C lcd(0x27, 16, 2);
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
void setup()
void setup() {
{ lcd.begin();
// set up the LCD's number of columns and rows: lcd.clear();
lcd.begin(16, 2); }
// Clears the LCD screen void loop()
lcd.clear(); {
} lcd.print("BELAJAR");
lcd.setCursor(0, 1);
void loop() lcd.print(" Sistem Embedded ");
{ }
// Print a message to the LCD.
lcd.print("BELAJAR");
Serial (RxTx)
// set the cursor to column 0, line 1 Fungsi : Komunikasi via Serial (RxTx)
// (note: counting begins with 0):
Lib -
lcd.setCursor(0, 1);
Var -
// Print a message to the LCD.
setup Serial.begin(9600); //baudrate
lcd.print(" Sistem Embedded ");
} loop Serial.print(" Hello world!");
Serial.println(" Hello world!");
LCD I2C-Serial
#include <LiquidCrystal_I2C.h>
void setup()
{
lcd.begin();
lcd.clear();
Serial.begin(9600);
}
void loop()
{
lcd.print("BELAJAR");
lcd.setCursor(0, 1); GND should be connected to the ground of Arduino.
lcd.print(" Sistem Embedded "); VCC is the power supply for the LCD which we connect
the 5 volts pin on the Arduino.
Serial.print("BELAJAR"); Vo (LCD Contrast) controls the contrast and brightness of
Serial.println(" Sistem Embedded "); the LCD. Using a simple voltage divider with a
} potentiometer, we can make fine adjustments to the
contrast.
LCD-PB-IF RS (Register Select) pin lets the Arduino tell the LCD
whether it is sending commands or the data. Basically this
#include <LiquidCrystal.h> pin is used to differentiate commands from the data.
For example, when RS pin is set to LOW, then we are
LiquidCrystal lcd(7, 6, 5, 4, 3, 2); sending commands to the LCD (like set the cursor to a
int data1; specific location, clear the display, scroll the display to the
right and so on). And when RS pin is set on HIGH we are
void setup() { sending data/characters to the LCD.
lcd.begin(16, 2); R/W (Read/Write) pin on the LCD is to control whether or
lcd.clear();
not you’re reading data from the LCD or writing data to
pinMode(13,INPUT_PULLUP);
} the LCD. Since we’re just using this LCD as an OUTPUT
device, we’re going to tie this pin LOW. This forces it into
void loop() { the WRITE mode.
if (data1==0) { E (Enable) pin is used to enable the display. Meaning, when
lcd.setCursor(0, 0); this pin is set to LOW, the LCD does not care what is
lcd.print(" Tombol Ditekan "); happening with R/W, RS, and the data bus lines; when this
pin is set to HIGH, the LCD is processing the incoming
} else { data.
lcd.setCursor(0, 0); D0-D7 (Data Bus) are the pins that carries the 8 bit data we
lcd.print(" Tombol Dilepas "); send to the display. For example, if we want to see the
}
uppercase ‘A’ character on the display we will set these
} pins to 0100 0001(according to the ASCII table) to the
LCD.
A-K (Anode & Cathode) pins are used to control the backlight
of the LCD.
Other useful functions in LiquidCrystal Library // include the library code:
There are a few useful functions you can use #include <LiquidCrystal.h>
with LiquidCrystal object. Few of them are listed below:
// initialize the library with the numbers of the interface
• If you just want to position the cursor in the upper-left of the pins
LCD without clearing the display, use home() LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
• There are many applications like turbo C++ compiler or
notepad++, in which pressing ‘insert’ key on the keyboard // make some custom characters:
byte Heart[8] = {
changes cursor. Just like that you can change the cursor on the
0b00000,
LCD using blink() or lcd.cursor() . 0b01010,
• blink() function displays the blinking block of 5×8 pixels, 0b11111,
while lcd.cursor() displays an underscore (line) at the position to 0b11111,
which the next character will be written. 0b01110,
0b00100,
• You can use the noBlink() function to turns off the blinking 0b00000,
LCD cursor and lcd.noCursor() to hide the LCD cursor. 0b00000
• You can scroll the contents of the display one space to the };
right using lcd.scrollDisplayRight() or one space left
byte Bell[8] = {
using lcd.scrollDisplayLeft() . If you want to scroll the text
0b00100,
continuously, you need to use these functions inside a ‘for 0b01110,
loop’. 0b01110,
0b01110,
0b11111,
Custom character generation for 16×2 character LCD
0b00000,
If you are finding characters on the display dull and 0b00100,
unexciting, you can create your own custom characters 0b00000
(glyph) and symbols for your LCD. They are extremely };
useful when you want to display a character that is not part
of the standard ASCII character set.
Creating custom character was not easy until now! We byte Alien[8] = {
have created a small application called Custom character 0b11111,
generator for character LCD. Can you see the blue grid 0b10101,
below? That’s it. That’s the application. You can click on 0b11111,
0b11111,
any of the 5×8 pixels to set/clear that particular pixel. And
0b01110,
as you click on pixels, the code for the character is 0b01010,
generated next to the grid. This code can directly be used 0b11011,
in your Arduino sketch. 0b00000
};
byte Check[8] = {
0b00000,
0b00001,
0b00011,
0b10110,
0b11100,
0b01000,
0b00000,
0b00000
};
byte Speaker[8] = {
0b00001,
0b00011,
0b01111,
0b01111,
0b01111,
0b00011,
0b00001,
0b00000
};
byte Sound[8] = {
0b00001, lcd.setCursor(6, 1);
0b00011, lcd.write(byte(3));
0b00101,
0b01001, lcd.setCursor(8, 1);
0b01001, lcd.write(byte(4));
0b01011,
0b11011, lcd.setCursor(10, 1);
0b11000 lcd.write(byte(5));
};
lcd.setCursor(12, 1);
lcd.write(byte(6));
byte Skull[8] = {
0b00000, lcd.setCursor(14, 1);
0b01110, lcd.write(byte(7));
0b10101, }
0b11011,
0b01110,
0b01110, Pembahasan Program
0b00000,
0b00000 After including the library, we need initialize the custom
}; character array of eight bytes.
void setup() In the setup we have to create the custom character using
{ the createChar() function. This function takes two parameters.
// initialize LCD and set up the number of columns and The first one is a number between 0 and 7 in order to reserve one
rows: of the 8 supported custom characters. The second parameter is
lcd.begin(16, 2); the name of the array of bytes.
lcd.setCursor(2, 1);
lcd.write(byte(1));
lcd.setCursor(4, 1);
lcd.write(byte(2));
Analog Input
ADC 10 bit = 210 = 1024 0..1023
Analog Input Vin (max) = VCC = 5 volt
Fungsi : Mengukur tegangan Analog 𝑉𝑖𝑛 𝐴𝐷𝐶
Lib - =
𝑉𝐶𝐶 1023
init - Sensor Suhu (LM35) OC/10mV
setup - 𝑉𝑖𝑛 𝐴𝐷𝐶 𝑉𝐶𝐶 500
loop int analogRead(pin) 𝑆𝑢ℎ𝑢 = = 𝑥 = 𝐴𝐷𝐶 𝑥 O
C
0.01 1023 0.01 1023