Pelatihan Arduino
Pelatihan Arduino
Materi
Kelistrikan
Kelistrikan Kirchhoffs First Law – The Current Law, (KCL)
Jika
E
Komponen Elektronika
• Komponen Pasif , untuk bekerja tidak membutuhkan tegangan
contoh : resistor, kapasitor, inductor, button , dll
• Komponen Aktif, untuk bekerja tidak membutuhkan tegangan
contoh : dioda, led, transistor, IC
Resistor
Kapasitor
LED
Button
https://store.arduino.cc/usa/arduino-uno-rev3
Hardware
Arduino
Hardware Arduino
Hardware Arduino
Hardware Arduino
Hardware Arduino
Arduino IDE
https://www.arduino.cc/en/software
https://www.arduino.cc/en/Guide
Instalasi Arduino IDE di Windows
Arduino Software (IDE)
Programs written using Arduino Software (IDE) are
called sketches
https://www.arduino.cc/en/Guide/Environment
Arduino Software (IDE) - Menu File
Arduino Software (IDE) - Menu Edit
Arduino Software (IDE) - Menu Sketch
Arduino Software (IDE) - Menu Tools
Arduino Software (IDE) - Serial Monitor
Arduino Software (IDE) - Serial Ploter
Arduino Software (IDE) - Menu Help
Help offline
Cara Verify dan Upload
1. Siampkan sketch, misal ambil dari example blink
Cara Verify dan Upload
2. Pilih board dan port
Cara Verify dan Upload
2. Pilih board dan port
Cara Verify dan Upload
3. Klik Verify atau Sketch > Verify/compile
Cara Verify dan Upload
4. Klik Upload
Burn Bootloader
Bootloader merupakan sebuah program yang
ada di dalam chip/mikrokontroller dan akan
dijalankan pertama kali saat mikrokontroller
dijalankan.
Pada Arduino, bootloader berfungsi sebagai
jembatan agar IDE dapat mengupload
program ke arduino melalui USB.
https://dl.espressif.com/dl/package_esp32_index.json
http://arduino.esp8266.com/stable/package_esp8266com_index.json
https://github.com/stm32duino/BoardManagerFiles/raw/master/STM32/package_stm_index.json
https://mcudude.github.io/MightyCore/package_MCUdude_MightyCore_index.json
Cara Menambah Board (diluar paket)
2. Klik Tools > Board > Board Manager
Menambah Library via Manager Libraries
LiquidCrystal PCF8574
Menambahkan library via Zip File
Klik DigitShield
Komponen Bahasa Pemrograman
Arduino
Struktur Variabel Fungsi
Contoh : Hasil :
void loop() {
int a = 9, b = 4, c;
c=a c=9
c = a + b; c=5
c = a – b; c = 36
c = a * b; c=2
c = a / b; c=1
c = a % b;
}
Struktur : Operator Perbandingan
Hasil dari pernyataan dengan operator pembanding adalah TRUE atau FALSE
Nama Operator Operator Deskripsi
Memeriksa apakah nilai dua operan sama atau tidak, jika
Sama dengan == ya maka kondisinya menjadi true. Penggunaannya
Tidak sama Memeriksa apakah nilai dua operan sama atau tidak, jika
dengan
!= nilai tidak sama maka kondisinya menjadi true. pada struktur
Memeriksa apakah nilai operan kiri kurang dari nilai operan
Lebih kecil < kanan, jika ya maka kondisinya menjadi true. percabangan
Memeriksa apakah nilai operan kiri lebih besar dari nilai
Lebih besar > operan kanan, jika ya maka kondisinya menjadi true.
Memeriksa apakah nilai operan kiri kurang dari atau sama
Lebih kecil atau dengan nilai operan kanan, jika ya maka kondisinya
<=
sama dengan menjadi true.
Lebih besar Memeriksa apakah nilai operan kiri lebih besar dari atau
atau sama >= sama dengan nilai operan kanan, jika ya maka kondisinya
dengan menjadi benar.
Struktur : Operator Boolean
Nama Operator Operator Deskripsi
Jika kedua operan tidak nol maka kondisi menjadi
AND &&
benar.
Jika salah satu dari dua operan tidak nol maka
OR ||
kondisi menjadi benar.
Jika suatu kondisi true maka hasil dari NOT adalah
NOT !
false.
if (A > B)
{
A++;
}
if (A > B) {
A++;
}
else {
B -= A;
}
}
Struktur : Percabangan if . . . elseif . . .
else
void loop() {
int A = 5;
int B = 9;
int C = 15;
if (A > B) {
A++;
}
else if((A == B) || (B < C ))
C = B * A;
}
else {
C++;
}
}
Struktur : Percabangan switch case
switch (range)
{
case 0:
Serial.println("dark");
break;
case 1:
Serial.println("dim");
break;
case 2:
Serial.println("medium");
break;
case 3:
Serial.println(“bright");
break;
default:
// default: break; bisa dihapus
break;
}
Struktur : Perulangan for
for (int i = 2; i < 8; i++) {
// turn the pin on:
digitalWrite(i, HIGH);
delay(1000);
// turn the pin off:
digitalWrite(i, LOW);
}
Infinite loop
for (;;) {
digitalWrite(13, HIGH)
}
Struktur : Perulangan for . . . for
Infinite loop
while (true) {
digitalWrite(13, HIGH)
}
Struktur : Fungsi Pendukung
continue :skip iterasi break : keluar dari loop
for (x = 0; x <= 255; x ++) { for (x = 0; x < 255; x ++)
if (x > 40 && x < 120){ {
// create jump in values analogWrite(PWMpin, x);
continue; sens = analogRead(sensorPin);
} if (sens > threshold){
analogWrite(PWMpin, x); // bail out on sensor detect
delay(50); x = 0;
} break;
}
delay(50);
}
Struktur : Perulangan do . . . while
do {
delay(50);
x =
readSensors();
} while (x < 100);
Infinite loop
do {
digitalWrite(13, HIGH)
}
while(1)
Komponen Bahasa Pemrograman
Arduino
Struktur Variabel Fungsi
void loop()
{
// ...
}
boolean true atau false bool a = true;
bool val = false;
bool bol;
Variabel : Tipe Data
z = myMultiplyFunction(i, j);
}
Komponen Bahasa Pemrograman
Arduino
Struktur Variabel Fungsi
PIN = 0 – 5 atau A0 – A5
Fungsi : Analog I/O – AnalogWrite
• Writes an analog value (PWM wave) to a pin
• Can be used to light a LED at varying brightnesses or drive a motor at
various speeds
• the pin will generate a steady rectangular wave of the specified duty
cycle until the next call to analogWrite()
Fungsi : Analog I/O – Tune
• Generates a square wave of the specified frequency (and 50% duty cycle) on a pin.
• A duration can be specified, otherwise the wave continues until a call to noTone().
• The pin can be connected to a piezo buzzer or other speaker to play tones.
• Only one tone can be generated at a time.
• If a tone is already playing on a different pin, the call to tone()will have no effect.
• If the tone is playing on the same pin, the call will set its frequency.
Fungsi : Math
min(x, y) Calculates the minimum of two numbers
max(x, y) Calculates the maximum of two numbers
abs(x) Computes the absolute value of a number
pow(base, exponent) Calculates the value of a number raised to a power
sqrt(x) Calculates the square root of a number
sq(x) Calculates the square of a number: the number multiplied by itself.
map(value, fromLow, Re-maps a number from one range to another.That is, a value of fromLow would get
fromHigh, toLow, mapped to toLow, a value of fromHigh to toHigh, values in between to values in between.
toHigh)
Trigonometri functions such as sin, cos, and tan are also available.
c
Fungsi : Komunikasi Serial
Serial.begin(baudrate); baudrate = 300, 2400, 9600, 19200, 38400, 115200 dll
Serial.print(val,format);
Prints data to the serial port as human-readable ASCII text.
Examples:
Serial.print(78) gives "78"
Serial.print(1.23456) gives "1.23"
Serial.println(1.23456, 4) gives "1.2346"
Serial.print("Hello world.") gives "Hello world."
Serial.println(val);
Prints val followed by carriage return (enter)
Fungsi : Komunikasi Serial
delayMicroseconds(time us)
Time = micros()
Returns the number of microseconds since the Arduino board began running the current program.(unsigned
long)
Time= millis()
Returns the number of milliseconds passed since the Arduino board began running the
current program. This number will overflow (go back to zero), after approximately 50 days.
Fungsi : External Interrupt
attachInterrupt(digitalPinToInterrupt(pin), ISR, mode) Mengaktifkan interupsi
Pin Arduino Uno yang dapat digunakan sebagai external interrupt adalah 2 dan 3
Fungsi : Buatan sendiri
void setup(){
Serial.begin(9600);
}
void loop() {
int i = 2;
int j = 3;
int k;
k = myMultiplyFunction(i, j);
// k now contains 6
Serial.println(k);
delay(500);
}