KOMUNIKASI DIGITAL
Serial communication
Digital signal processing
Basic Internet of Thing
TIM KOMDIG
1
DAFTAR ISI
2
PERCOBAAN 1. KOMUNIKASI SERIAL
3
Susun rangkaian elektronik yang menghubungkan antara mikrontroller dan
LED. Hubungkan masing-masing LED ke PIN 11, 12, dan 13 dari Arduino
seperti yang ditunjukkan pada Gambar 1.4. SESUAIKAN DENGAN ARDUINO
NANO.
4
int lamp3 = 13;
int databaca;
void setup()
{
pinMode(lamp3, OUTPUT);
Serial.begin(9600);
}
void loop()
{
if (Serial.available())
{
int bacadata = Serial.read();
if (bacadata =='a') {databaca=1;}
if (bacadata =='b') {databaca=2;}
if(databaca==1)
{
digitalWrite(13, LOW);
}
if(databaca==2)
{
digitalWrite(13, HIGH);
}
}
}
void setup() {
pinMode(lamp1, OUTPUT);
pinMode(lamp2, OUTPUT);
pinMode(lamp3, OUTPUT);
Serial.begin(9600);
}
void loop()
{
if (Serial.available())
{
5
int bacadata = Serial.read();
if (bacadata =='a') {databaca=1;}
if (bacadata =='b') {databaca=2;}
if (bacadata =='c') {databaca=3;}
if(databaca==1)
running: //LED akan berjalan pada mode running
{
digitalWrite(lamp1, HIGH);
delay(250);
digitalWrite(lamp1, LOW);
delay(250);
digitalWrite(lamp2, HIGH);
delay(250);
digitalWrite(lamp2, LOW);
delay(250);
digitalWrite(lamp3, HIGH);
delay(250);
digitalWrite(lamp3, LOW);
delay(250);
Serial.println("LED mode running");
loop();
goto running;
}
if(databaca==2)
flipflop: // LED akan berjalan pada mode flip flop
{
digitalWrite(lamp1, LOW);
digitalWrite(lamp2, LOW);
digitalWrite(lamp3, LOW);
delay(500);
digitalWrite(lamp1, HIGH);
digitalWrite(lamp2, HIGH);
digitalWrite(lamp3, HIGH);
delay(500);
Serial.println("LED mode flip flop");
loop();
goto flipflop;
}
if(databaca==3)
modeoff: // Semua LED akan off
{
digitalWrite(lamp1,LOW);
digitalWrite(lamp2,LOW);
digitalWrite(lamp3,LOW);
delay(1000);
Serial.println("LED mode off");
6
loop();
goto modeoff; }
}
}
7
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
void loop() {
sensorVal = analogRead(sensorPin); // read the value from the sensor
float voltage = (sensorVal/1024.0) * 5.0; // convert the sensor reading to
voltage
float temperatureC = (voltage - .5) * 100; // convert the voltage to
temperature
float temperatureF = temperatureC * (9.0/5.0) + 32.0;
Serial.print("Sensor Value: "); // send the sensor value to the serial
Serial.print(sensorVal);
Serial.print(", Volts: ");
Serial.print(voltage);
Serial.print(", degrees C: ");
Serial.print(temperatureC);
Serial.print(", degrees F: ");
Serial.println(temperatureF);
delay(1000);
}
8
1.3. Percobaan komunikasi arduino to arduino
Pada percobaan ini anda akan berkomunikasi secara kabel menggunakan
komunikasi serial. Untuk melakukan percobaan ini anda harus
menggunakan dua buah board arduino, dapat menghubungkan arduino
anda dengan kelompok disebelah anda.
a. Hubungkan TX (Arduino 1) ke RX (Arduino 2) demikian pula sebaliknya,
hubungkan RX (Arduino 1) ke TX (Arduino 2), seperti yang ditunjukkan
pada Gambar 1.4. SESUAIKAN DENGAN ARDUINO NANO.
void loop() {
Serial.write(mystr,5); //Write the serial data
delay(1000);
}
9
Listing Program 1.5. Menerima teks “ Hello” pada mikrokontroller 2
void loop() {
Serial.readBytes(mystr,5); //Read the serial data and store in var
Serial.println(mystr); //Print data on Serial Monitor
delay(1000);
}
10
PERCOBAAN 2. KOMUNIKASI WIRELESS
11
Table 2.2. Hubungan antara board Arduino dan Bluetooth module
#include <SoftwareSerial.h>
void setup()
{
pinMode(9, OUTPUT); // this pin will pull the HC-05 pin 34 (key pin)
HIGH to switch module to AT mode
digitalWrite(9, HIGH);
Serial.begin(9600);
Serial.println("Enter AT commands:");
BTSerial.begin(38400); // HC-05 default speed in AT command more
}
void loop()
{
// Keep reading from HC-05 and send to Arduino Serial Monitor
if (BTSerial.available())
Serial.write(BTSerial.read());
12
2.1. Percobaan Kendali LED melalui mobile phone
Pada percobaan ini akan dibuat sebuah komunikasi wireless menggunakan
koneksi bluetooth, dengan menggunakan Arduino Nano dan Bluetooth HC05
atau HC06. Pada percobaan ini, sebuah LED (BUILTIN) yang terhubung ke
D13 pin Arduino akan dikendalikan (ON/ OFF) melalui komunikasi bluetooth
antara bluetooth mobile phone dan bluethooth HC05.
1. Lakukan instalasi bluetooth terminal (dari: electrotoolbox) melalui Google
Play Store. Sebelum menghubungkan Arduino dan bluetooth, perhatikan
pin-pin sebagaimana yang ditunjukkan pada Gambar 2.1.
2. Pasang board Arduino nano dan bluetooth pada papan project board
(white board) dan hubungkan pin-pin yang digunakan menggunakan
kabel jumper, dengan hubungan antara arduino dan bluetooth module
ditunjukkan pada Table 2.3.
13
Table 2.3. Hubungan antara board Arduino dan Bluetooth module
#include <SoftwareSerial.h>
SoftwareSerial BT(10, 11);
// creates a "virtual" serial port/UART
// connect BT module TX to D10
// connect BT module RX to D11
// connect BT Vcc to 5V, GND to GND
void setup()
{
// set digital pin to control as an output
pinMode(13, OUTPUT);
// set the data rate for the SoftwareSerial port
BT.begin(9600);
// Send test message to other device
BT.println("Hello, This is from Arduino");
BT.println("Press 1=ON; 2=OFF");
}
char a; // stores incoming character from other device
void loop()
{
if (BT.available())
// if text arrived in from BT serial...
{
a=(BT.read());
if (a=='1')
{
digitalWrite(13, HIGH);
BT.println ("LED on");
}
if (a=='2')
{
digitalWrite(13, LOW);
BT.println("LED off");
}
14
if (a=='?')
{
BT.println("Send '1' to turn LED on");
BT.println("Send '2' to turn LED on");
}
// you can add more "if" statements with other characters
}
}
(a) Menu terminal (b) Pilihan devices (c) Cara kendali led
15
6. Lakukan modifikasi pada program arduino sehingga kendali LED
dilakukan dengan character yang lain, misal character 8=ON dan
character 7=OFF.
void setup()
{
Serial.begin(9600);
Serial.println("Arduino is ready");
void loop()
{
// Keep reading from HC-05 and send to Arduino Serial Monitor
if (BTserial.available())
{
c = BTserial.read();
Serial.write(c);
}
// Keep reading from Arduino Serial Monitor and send to HC-05
if (Serial.available())
{
c = Serial.read();
BTserial.write(c);
}
}
16
2. Buka serial monitor pada program Arduino dan pastikan pilihan BOTH
NL & SCROOL dipilih, seperti yang ditunjukkan pada Gambar 2.3.
17
Table 2.2. Hubungan antara board Arduino 1 dan Bluetooth module
Gambar 2.4. (a) board arduino NANO (b) board bluetooth HC05
18
(KELOMPOK X). Untuk mengkonfigurasikan bluetooth sebagai
MASTER maka terlebih dahulu BUTTON SW pada modul bluetooth
ditekan, selanjutnya hubungkan USB ke PC untuk mendapatkan
power supply. Apabila LED pada BT berkedip setiap dua detik maka BT
memasuki mode AT COMMAND. Pastikan BOTH NL & CR dipilih pada
serial monitor Arduino.
#include <SoftwareSerial.h>
#define button 8
int buttonState = 0;
SoftwareSerial BT(10, 11);
void setup() {
pinMode(button, INPUT);
BT.begin(9600);
}
void loop() {
// Reading the button
buttonState = digitalRead(button);
if (buttonState == LOW) {
19
BT.println ("0");
delay(1000);
}
else {
BT.println ("1");
delay(1000);
}
}
#include <SoftwareSerial.h>
SoftwareSerial Bluetooth(10, 11); // RX, TX
int LED = 13; // the on-board LED
int Data; // the data received
void setup() {
Bluetooth.begin(9600);
Serial.begin(9600);
Serial.println("Waiting for command...");
Bluetooth.println("Send 1 to turn on the LED. Send 0 to turn Off");
pinMode(LED,OUTPUT);
}
void loop() {
if (Bluetooth.available()){ //wait for data received
Data=Bluetooth.read();
if(Data=='1'){
20
digitalWrite(LED,1);
Serial.println("LED On!");
Bluetooth.println("LED On!");
}
else if(Data=='0'){
digitalWrite(LED,0);
Serial.println("LED Off!");
Bluetooth.println("LED On D13 Off ! ");
}
else{;}
}
delay(100);
}
21
PERCOBAAN 3. DASAR DIGITAL SIGNAL PROCESSING
22
3.2. Percobaan display spectrum sinyal SINUS
Lakukan percobaan menampilkan power spektrum dari sinyal SINUS (tiga
buah sinyal SINUS: 20, 100, dan 1000) yang telah dicampur dengan langkah
pembuatan generator isyarat seperti yang ditunjukkan pada Percobaan 1.
Panggil fungsi FFT pada program MATLAB untuk menghitung spektrum
sinyal dan selanjutnya tampilkan hasil pada layar komputer
23
Gambar 3.3. Tampilan spektrum isyarat sinus.
Pada Gambar 1.2. tampak bahwa isyarat tiga gelombang sinus yang telah
bercampur. Gambar 1.3 menunjukkan spektrum frekuensi yang terdiri dari
tiga frekuensi utama, yaitu frekuensi 20, 100 dan 1000 Hz.
1. Lakukan modifikasi pada program tersebut diatas untuk pemberian
frekuensi sampling, menjadi 1000, 2000, 3000, lakukan pencatatan
hasil, dan copy paste hasil (program dan grafik) ke WORD untuk
pembuatan laporan.
2. Lakukan modifikasi pada program tersebut diatas dengan merubah
isyarat SINUS menjadi 1000, 5000, 10.000 Hz. Tentukan panjang
isyarat (L) yang paling tepat dan lakukan percobaan untuk
menentukan frekuensi samping yang paling tepat. Lakukan pencatatan
hasil, dan lakukan copy paste hasil (program dan grafik) ke WORD
untuk pembuatan laporan.
3. Lakukan modifikasi pada program tersebut diatas dengan merubah
isyarat SINUS yang diproses menjadi 5, 20, 500, 2000 Hz. Tentukan
panjang isyarat (L) yang paling tepat dan lakukan percobaan untuk
menentukan frekuensi sampling yang paling tepat. Lakukan
pencatatan hasil, dan lakukan copy paste hasil (program dan grafik) ke
WORD untuk pembuatan laporan.
24
PERCOBAAN 4. DIGITAL FILTER FIR
25
x7=0;x6=0;x5=0;x4=0;x3=0;x2=0;x1=0;x0=0;
for i=1:L
x8=x7;
x7=x6;
x6=x5;
x5=x4;
x4=x3;
x3=x2;
x2=x1;
x1=x0;
x0=x(i);
y=b0*x0 + b1*x1 + b2*x2 + b3*x3 + b4*x4 + b5*x5 + b6*x6...
+ b7*x7 + b8*–x8;
yfilter(i)=y;
end
y = yfilter';
figure;
plot(t,y);
title ('Filtered Signal');
c. Amati dan copy-paste plot (grafik sinyal, sinyal setelah penapisan, dan
spektrum sinyal) yang dihasilkan dari MATLAB ke WORD untuk
pembuatan laporan akhir.
d. Lakukan modifikasi pada variabel frekuensi sampling, yang sebelumnya
4000 Hz, ganti dengan frekuensi sampling 3000, 2000, dan 1000 Hz.
Lakukan pencatatan dan perekaman hasil plot (copy/paste) dari masing-
masing frekuensi sampling ke WORD. Beri ulasan pengaruh frekuensi
sampling terhadap hasil penapisan.
e. Pada percobaan diatas telah dibuat sebuah high pass filter FIR dengan
frekuensi cutoff 500 Hz orde 8 dan frekuensi sampling 4000 Hz.
26
Selanjutnya rencanakan low pass filter FIR dengan frekuensi cutoff 50
Hz orde 8. (b=fir1(8,50/(Fs/2),’low’));
27
b. Lanjutkan percobaan merancang sebuah digital filter, yaitu band pass
filter FIR dengan frekuensi cutoff bawah 50 Hz dan cut off atas 200 Hz
orde 20 dengan frekuensi sampling 4000 Hz.
c. Pada percobaan diatas telah dibuat sebuah high pass filter FIR dengan
frekuensi cutoff 500 Hz orde 20 dan frekuensi sampling 4000 Hz.
d. Lakukan copy-paste plot untuk setiap percobaan (untuk pembuatan
laporan akhir).
e. Berikan ulasan pengaruh orde terhadap hasil atau keluaran dari filter.
28
PERCOBAAN 5. DIGITAL FILTER IIR
29
b. Lakukan pengaturan pada Response Type, Design Method, Filter
Order, Frequency sampling dan frequency cuttoff. Pada FDATOOL.
c. Setelah pengaturan telah dilakukan, maka tekan BUTTON Design
Filter.
d. Pada menu EDIT FDATOOL pilih CONVERT TO SINGLE SECTION
untuk mendapatkan koefisien filter IIR. Tekan tombol pada icon [b,a]
untuk melihat hasil perhitungan koefisien filter IIR, seperti yang
ditunjukkan pada Gambar 6.2.
30
%---------------- Signal Generator ------------------------------------
Fsampling = 4000; % Sampling frequency
T = 1/Fsampling; % Sampling period
L = 1500; % Length of signal
t = (0:L-1)*T; % Time vector
x = sin(2*pi*20*t)+sin(2*pi*100*t)+sin(2*pi*1000*t); % First row wave
figure;
plot(t,x);
title ('Original Signal');
%-----------------IIR Digital Filter (Fcutoff HPF 500 Hz)-------------
% Numerator
b0= 0.569035593728849; b1=-1.13807118745770; b2= 0.569035593728849;
% Denumerator
a1= -0.942809041582064; a2= 0.333333333333333;
x1=0;x0=0;
y1=0;y=0;
for i=1:L
y2=y1;
y1=y;
%
x2=x1;
x1=x0;
x0=x(i);
y=b0*x0 + b1*x1 + b2*x2 ...
-a1*y1 - a2*y2 ;
yfilter(i)=y;
end
y = yfilter';
figure;
plot(t,y);
title ('Filtered Signal');
g. Amati dan copy-paste plot (grafik sinyal, sinyal setelah penapisan, dan
spektrum sinyal) yang dihasilkan dari MATLAB ke WORD untuk
pembuatan laporan akhir.
h. Lakukan modifikasi pada variabel frekuensi sampling, yang sebelumnya
4000 Hz, ganti dengan frekuensi sampling 3000, 2000, dan 1000 Hz.
31
Lakukan pencatatan dan perekaman hasil plot (copy/paste) dari masing-
masing frekuensi sampling ke WORD. Beri ulasan pengaruh frekuensi
sampling terhadap hasil penapisan.
i. Lakukan modifikasi pada desain digital filter tersebut untuk low pass
filter IIR orde 2 dengan frekuensi cutoff 50, untuk melewatkan frekuensi
20 Hz.
j. Lakukan modifikasi pada desain digital filter tersebut untuk band pass
filter IIR orde 2 dengan frekuensi dengan frekuensi cutoff bawah 50 Hz
dan cut off atas 200 Hz orde 2 dengan frekuensi sampling 4000 Hz.
Sehingga frekuensi 100 Hz akan dilewatkan.
k. Lakukan langkah (g) sampai dengan (h).
32
a5=-3.1560252607305666; a6=1.0016965795512844;
a7=-0.1863424776774854; a8=0.015507615254986895;
%
x7=0;x6=0;x5=0;x4=0;x3=0;x2=0;x1=0;x0=0;
y7=0;y6=0;y5=0;y4=0;y3=0;y2=0;y1=0;y=0;
for i=1:L
y8=y7;
y7=y6;
y6=y5;
y5=y4;
y4=y3;
y3=y2;
y2=y1;
y1=y;
%
x8=x7;
x7=x6;
x6=x5;
x5=x4;
x4=x3;
x3=x2;
x2=x1;
x1=x0;
x0=x(i);
y=b0*x0 + b1*x1 + b2*x2 + b3*x3 + b4*x4 + b5*x5 + b6*x6 + b7*x7 + b8*x8...
-a1*y1 - a2*y2 - a3*y3 - a4*y4 - a5*y5 - a6*y6 - a7*y7 - a8*y8;
yfilter(i)=y;
end
y = yfilter';
figure;
plot(t,y);
title ('Filtered Signal');
%-----------------Fast Fourier Transform (FFT) -------------------------
n = 2^nextpow2(L);
Yfft = fft(y,n);
P2 = abs(Yfft/L);
P1 = P2(1:n/2+1);
P1(1:end-1) = 2*P1(1:end-1);
figure;
plot(0:(Fsampling/n):(Fsampling/2-Fsampling/n),P1(1:n/2));
title ('Frequency of the signal');
33
PERCOBAAN 6.
PEMROSESAN SINYAL BIOMEDIK
Pada praktikum kali ini, kita akan melakukan percobaan pengolahan isyarat
ECG yang berasal dari ATM physionet.org (https://physionet.org/cgi-
bin/atm/ATM). Physionet.org merupakan salah satu sumber public dataset
yang berisi berbagai macam hasil rekaman sinyal biomedik yang telah
dihasilkan oleh beberapa peneliti-peneliti terdahulu. Submenu pada
physionet adalah Physiobank ATM (Gambar 6.1.) yang berisi berbagaimacam
sinyal biomedik dengan berbagai macam fasilitas baik berupa view maupun
rekaman dalam bentuk text, csv dan lain-lain.
34
Gambar 6.2. Pilihan database sinyal biomedik
Hasil rekaman sinyal ECG yang berasal dari ATM physioBANK adalah berupa
file CSV. File CSV dibuka menggunakan program Microsoft Excell dan
selanjutnya file disimpan dengan ekstension TXT.
Pada percobaan pertama ini, kita akan mengambil rekaman isyarat ECG yang
berasal dari ATM physioBANK. Berdasarkan informasi yang didapatkan dari
rekaman tersebut, sinyal ECG dan sinyal yang lainnya direkam dengan
menggunakan frekuensi sampling 5000 Hz dengan lama rekaman adalah 10
detik. Karakteristik frekuensi isyarat ECG adalah 0.05 sd. 100 Hz. Untuk
mengetahui pengaruh noise terhadap sinyal ECG maka pada percobaan ini,
sinyal ECG akan ditambahkan dengan noise buatan yang seolah-olah berasal
dari jala-jala listrik yaitu noise dengan frekuensi 50 Hz. Noise buatan, yaitu
noise yang dibangkitkan akibat gerakan dari anggota tubuh manusia (noise
artifact) juga dibangkitkan secara buatan dengan menambahkan sinyal sinus
frekuensi rendah (1 sd. 3 Hz).
a. Ketik program pengambilan data ECG seperti yang ditunjukkan pada
Listring program berikut, terlebih dahulu download di google drive dengan
link sebagai berikut:
35
https://drive.google.com/open?id=10DEp-A14y_oy4ObedksecP-Xouki-TF8
% https://physionet.org/cgi-bin/atm/ATM
% https://physionet.org/physiobank/database/cebsdb/
clear all;
clc;
StopTime=10;
dt=0.0002;
Fsampling=1/dt;
L=50000;
load datasetecg.txt;
LEADI=datasetecg(1:L,2);
LEADII=datasetecg(1:L,3);
RESP=datasetecg(1:L,4);
SCG=datasetecg(1:L,5);
t = (0:dt:StopTime-dt)';
noise50 = sin(2*pi*50*t);
artifact= sin(2*pi*1*t);
y=LEADI;
figure;
plot(t,y);
36
y=LEADI;
y=LEADI+noise50;
y=LEADI+artifact;
37
c. Tambahkan fungsi FFT pada percobaan sinyal ECG dan noise 50 Hz
untuk melihat spektrum sinyal. Lakukan modifikasi pada program
sebelumnya dengan menambahkan instruksi untuk melihat spektrum
isyarat ECG dan noise.
% https://physionet.org/cgi-bin/atm/ATM
% https://physionet.org/physiobank/database/cebsdb/
clear all;
clc;
StopTime=10;
dt=0.0002;
Fsampling=1/dt;
L=50000;
load datasetecg.txt;
LEADI=datasetecg(1:L,2);
LEADII=datasetecg(1:L,3);
RESP=datasetecg(1:L,4);
SCG=datasetecg(1:L,5);
t = (0:dt:StopTime-dt)';
noise50 = sin(2*pi*50*t);
artifact= sin(2*pi*1*t);
y=LEADI+noise50;
38
figure;
plot(t,y);
%-----------------Fast Fourier Transform (FFT) ----------------------
n = 2^nextpow2(L);
Yfft = fft(y,n);
P2 = abs(Yfft/L);
P1 = P2(1:n/2+1);
P1(1:end-1) = 2*P1(1:end-1);
figure;
plot(0:(Fsampling/n):(Fsampling/2-Fsampling/n),P1(1:n/2));
title ('Frequency of the signal');
39
6.2. Pembuatan tapis NOTCH IIR 50 Hz untuk sinyal ECG
40
Untuk mendapatkan koefisien filter IIR 50 Hz order 2 maka lakukan konversi
ke single section melalui menu EDIT dan CONVERT SINGLE SECTION,
sebagaimana yang ditunjukkan pada Gambar 6.11.
41
t = (0:dt:StopTime-dt)';
noise50 = sin(2*pi*50*t);
artifact= sin(2*pi*1*t);
x=LEADI+noise50;
figure;
plot(t,x);
%-----------------IIR Digital Filter (Fcutoff HPF 500 Hz)-------------
% Numerator
b0= 0.997493021323277; b1=-1.99105568169751;
b2= 0.997493021323277;
% Denumerator
a1= -1.99105568169751; a2= 0.994986042646553;
x1=0;x0=0;
y1=0;y=0;
for i=1:L
y2=y1;
y1=y;
%
x2=x1;
x1=x0;
x0=x(i);
y=b0*x0 + b1*x1 + b2*x2 ...
-a1*y1 - a2*y2 ;
yfilter(i)=y;
end
y = yfilter';
figure;
plot(t,y);
title ('Filtered Signal');
42
Gambar 6.13. Sinyal ECG yang mendapat noise 50 Hz
43
PERCOBAAN 7
Implementasi Digital Filter ke Arduino
44
b. Lakukan pengaturan pada Response Type, Design Method, Filter
Order, Frequency sampling dan frequency cuttoff. Pada FDATOOL.
c. Setelah pengaturan telah dilakukan, maka tekan BUTTON Design
Filter.
d. Pada menu EDIT FDATOOL pilih CONVERT TO SINGLE SECTION
untuk mendapatkan koefisien filter IIR. Tekan tombol pada icon [b,a]
untuk melihat hasil perhitungan koefisien filter IIR, seperti yang
ditunjukkan pada Gambar 6.2.
45
#define ts 0.0002
//double b[9]={-0.0000,-0.0161,-0.0860,-0.1948,0.7501,-0.1948,-0.0860,-
0.0161,-0.0000};
double b[3]={0.569,-1.138,0.569};
double a[3]={1,-0.942,0.333};
int i;
double x,noise50,y2,y1,y,x2,x1,x0;
void setup() {
y1=0;y=0;x1=0;x0=0;
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
for(i;i<5000;i++)
{
noise50=sin(2*3.14*50*i*ts);
x=sin(2*3.14*20*i*ts)+ sin(2*3.14*100*i*ts)+ sin(2*3.14*1000*i*ts);
y2=y1;
y1=y;
//
x2=x1;
x1=x0;
x0=x;
y=b[0]*x0 + b[1]*x1 + b[2]*x2 - a[1]*y1 - a[2]*y2;
Serial.println(y);
delay(1);
}
}
46
g. Jalankan serial monitor
Jalankan program serial monitor untuk melihat pengiriman data dari
mikrokontroller ke PC melalui komunikasi serial. Dengan menggunakan
fasilitas ini maka kita dapat membandingkan sebelum dan sesudah proses
penapisan. Blok semua data yang ada pada serial monitor dengan CTRL-A
dan CTRL-C, selanjutnya PASTE ke program Excell data sebelum dan
sesudah di tapis.
-2
-3
-4
1.5
1 (b) Signal=1000 Hz
0.5
0
0 10 20 30 40 50 60 70 80 90 100
-0.5
-1
-1.5
Gambar 7.5. Tampilan (a) signal campuran (b) signal 1000 Hz.
47
2. Implementasi FIR ke Arduino
Rencanakan program Arduino untuk membangkitkan frekuensi 20, 100 dan
1000 Hz dengan menggunakan fungsi sinus sebagaimana percobaan 7.1.
Rencanakan sebuah high pass filter IIR orde 2 dengan frekuensi cutoff 500
Hz. Sehingga isyarat yang akan ditekan adalah frekuensi 20 dan 100 Hz dan
frekuensi yang akan dilewatkan adalah 1000 Hz. Frekuensi sampling 4000
Hz.
clear all;
clc;
L=50000;
load datasetecg.txt;
LEADI=datasetecg(1:L,2);
LEADII=datasetecg(1:L,3);
RESP=datasetecg(1:L,4);
SCG=datasetecg(1:L,5);
LEADIIds=downsample(LEADII,100);
48
b. Tentukan nilai terendah dari isyarat ECG LEAD 2 (dengan
#define ts 0.0002
int ecg[500] ={
56, 57, 56, 57, 57, 57, 58, 58, 66, 66, 62, 59, 55, 56, 56, 70, 85,
28, 46, 61, 63, 62, 62, 63, 64, 65, 66, 69, 72, 75, 77, 79, 74, 64,
56, 54, 53, 54, 55, 55, 56, 56, 56, 56, 55, 56, 55, 56, 55, 55, 56,
49
55, 57, 56, 59, 64, 65, 61, 57, 54, 55, 57, 77, 75, 18, 57, 61, 63,
63, 63, 64, 65, 67, 68, 71, 75, 78, 80, 80, 72, 62, 56, 55, 55, 56,
57, 57, 58, 58, 57, 57, 57, 57, 57, 57, 58, 57, 58, 58, 58, 59, 66,
67, 63, 59, 56, 56, 57, 74, 86, 20, 52, 62, 62, 63, 62, 64, 64, 66,
67, 70, 73, 77, 79, 80, 73, 63, 56, 54, 53, 55, 55, 57, 57, 56, 57,
55, 56, 56, 56, 56, 56, 56, 56, 56, 57, 57, 57, 62, 65, 64, 59, 55,
54, 54, 60, 80, 63, 23, 60, 61, 61, 61, 61, 62, 64, 65, 67, 70, 73,
76, 78, 76, 68, 58, 54, 53, 53, 55, 56, 56, 56, 56, 56, 56, 54, 55,
56, 55, 56, 56, 54, 57, 58, 61, 66, 63, 60, 53, 55, 55, 63, 82, 36,
41, 58, 59, 60, 59, 60, 62, 60, 61, 65, 68, 71, 71, 74, 70, 60, 52,
49, 48, 48, 50, 51, 54, 50, 49, 53, 50, 50, 49, 52, 54, 52, 50, 50,
53, 53, 42, 56, 53, 49, 45, 42, 50, 48, 69, 67, 0, 48, 45, 45, 42,
38, 44, 48, 53, 54, 56, 66, 62, 67, 70, 60, 55, 49, 45, 46, 48, 49,
46, 55, 50, 50, 53, 52, 51, 54, 52, 53, 55, 54, 53, 56, 57, 55, 61,
65, 64, 59, 56, 53, 53, 58, 77, 73, 15, 57, 58, 61, 61, 61, 61, 63,
65, 66, 70, 72, 75, 78, 76, 70, 60, 56, 54, 58, 52, 56, 56, 59, 53,
55, 56, 57, 60, 60, 61, 59, 60, 59, 58, 60, 66, 67, 61, 61, 56, 57,
57, 75, 85, 24, 53, 64, 65, 64, 66, 66, 66, 70, 70, 73, 77, 79, 84,
82, 78, 67, 61, 58, 57, 59, 60, 60, 61, 60, 61, 60, 59, 61, 60, 60,
60, 60, 61, 60, 61, 62, 69, 70, 67, 61, 59, 58, 59, 75, 87, 29, 52,
64, 66, 66, 66, 67, 68, 70, 71, 72, 77, 79, 84, 83, 78, 68, 61, 59,
59, 59, 60, 61, 62, 60, 62, 60, 61, 60, 61, 60, 60, 62, 61, 60, 60,
61, 63, 68, 69, 65, 63, 58, 59, 59, 78, 83, 18, 57, 65, 65, 65, 66,
67, 67, 68, 70, 74, 76, 78, 81, 81, 74, 65, 58, 57, 57, 57, 58, 58,
60, 59, 58, 57, 58, 59, 57, 59, 57, 58, 58, 57, 58, 63, 66, 65, 59,
56, 55, 55, 61, 82, 56, 29, 62, 60, 61, 61, 63, 63, 65, 66, 68, 70,
74, 76, 76, 73, 66, 56, 53};
int i;
50
double x;
void setup() {
Serial.begin(115200);
}
void loop() {
for(i;i<500;i++)
{
x=ecg[i];
Serial.println(x);
delay(1);
}
}
51
4. Rencanakan program digital filter IIR (orde 4, frekuensi
sampling 500 Hz, HPF frekuensi cutoff 10 Hz) dengan Arduino,
gunakan rekaman isyarat ECG sebelumnya yang telah didown-
sampling (ke 500 Hz) yang diganggu dengan gerakan anggota
tubuh (artifact) dengan frekuensi 5 Hz.
52
PERCOBAAN 8
ON/OFF LED MELALUI WEBSERVER
53
b. Install library ESP32 pada IDE Arduino apabila belum
terpasang. Selanjutnya pilih ESP32 Dev Module pada
pemilihan BOARD. Jangan lupa juga pilih PORT SERIAL
yang terdeteksi pada saat tersebut.
c. Ketik program untuk kendali LED ON-OFF seperti yang
ditunjukkan pada Listing program di bawah ini.
void setup() {
Serial.begin(115200);
// Initialize the output variables as outputs
pinMode(output26, OUTPUT);
pinMode(output27, OUTPUT);
// Set outputs to LOW
digitalWrite(output26, LOW);
digitalWrite(output27, LOW);
54
void loop(){
WiFiClient client = server.available(); // Listen for incoming clients
55
// Web Page Heading
client.println("<body><h1>ESP32 Web Server</h1>");
56
f. Ketik ADDRESS yang terdeteksi tersebut misal:
192.168.0.165 dan ENTER, maka program akan
menampilkan hasil sebagai berikut.
57
PERCOBAAN 8
DISPLAY DATA ADC KE WEBSERVER
#include "esp_system.h"
#include <WiFi.h>
//
const char* ssid = "BENGKEL";
const char* password = "wargatekmed";
int *aDcData0;
58
int *aDcData1;
int *aDcData2;
int *aDcData3;
WiFiServer server(80);
void GetReading(){
//ulMeasCount++;
//if(ulMeasCount>(siZe-1)){ulMeasCount=1;clearArray();}
//for(int i=0;i<SampleCount;i++){
total0 += analogRead(36); // Read ADC4
//total1 += analogRead(36); // Read ADC5
//total2 += analogRead(36); // Read ADC6
//total3 += analogRead(36); // Read ADC7
//}
aDcData0[0] = total0 / 1;
//aDcData1[ulMeasCount] = total1 / SampleCount;
//aDcData2[ulMeasCount] = total2 / SampleCount;
//aDcData3[ulMeasCount] = total3 / SampleCount;
//total0=0;
//total1=0;
//total2=0;
//total3=0;
}
void clearArray(){
for(int i=0;i<1;i++){
aDcData0[i]=0;
//aDcData1[i]=0;
//aDcData2[i]=0;
//aDcData3[i]=0;
}
}
void setup()
{
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT); // set the LED pin mode
delay(10);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
59
Serial.println(ssid);
WiFi.begin(ssid, password);
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
aDcData0 = new int[0];
//aDcData1 = new int[siZe];
//aDcData2 = new int[siZe];
//aDcData3 = new int[siZe];
clearArray();
// Start an alarm
timerAlarmEnable(timer);
}
int value = 0;
int val = 0;
void loop(){
WiFiClient client = server.available(); // listen for incoming clients
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
60
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:
int adcnum=3;
for(int loOp=36;loOp<37;loOp++){
String out = "";
char temp[100];
int dIspV;
if(loOp==32) { y = aDcData0[1] / 30; dIspV=aDcData0[ulMeasCount];}
else if(loOp==33) { y = aDcData1[1] / 30; dIspV=aDcData1[ulMeasCount];}
else if(loOp==34) { y = aDcData2[1] / 30; dIspV=aDcData2[ulMeasCount];}
else if(loOp==35) { y = aDcData3[1] / 30; dIspV=aDcData3[ulMeasCount];}
client.print("ADC");
client.print(adcnum++);
client.print(" Last Value Sampled on pin ");
client.print(loOp);
client.print(" : ");
client.print(dIspV);
client.print("<BR>");
out += "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"1000\"
height=\"150\">\n";
out += "<rect width=\"1000\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-
width=\"1\" stroke=\"rgb(0, 0, 0)\" />\n";
out += "<g stroke=\"black\">\n";
record = 2;
for (int x = 1; x < ulMeasCount; x+=1) {
if(loOp==32) { y2 = aDcData0[record] / 30;}
else if(loOp==33) { y2 = aDcData1[record] / 30;}
else if(loOp==34) { y2 = aDcData2[record] / 30;}
else if(loOp==35) { y2 = aDcData3[record] / 30;}
sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"1\"
/>\n", x, 140 - y, x + 1, 140 - y2);
out += temp;
y = y2;
record++;
if(out.length()>2000){
client.print(out);
out="";
}
}
out += "</g>\n</svg><BR>";
client.print(out);
}
client.print("<BR>Click <a href=\"/H\">here</a> turn the Onboard LED
on<br>");
client.print("Click <a href=\"/L\">here</a> turn the Onboard LED off<br>");
// client.print("Hall Sensor Value : ");
//client.print(String(val));
client.print("<BR>Local IP Address : ");
client.print(ip);
client.print("<BR> Free RAM = ");
client.print((uint32_t)system_get_free_heap_size() / 1024);
client.print(" KB<BR> SDK Version = ");
client.print(ESP.getSdkVersion());
61
client.print("<BR>");
client.printf(" Flash Chip Mode = %s\n", (ideMode == FM_QIO ? "QIO" : ideMode
== FM_QOUT ? "QOUT" : ideMode == FM_DIO ? "DIO" : ideMode == FM_DOUT ? "DOUT" :
"UNKNOWN"));
client.print("<BR> Flash Size (IDE) = ");
client.print(ESP.getFlashChipSize()/1024);
client.print(" KBytes<BR> Flash Speed = ");
client.print(ESP.getFlashChipSpeed()/1000000);
client.print("MHz<BR>");
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
} else { // if you got a newline, then clear currentLine:
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
// Check to see if the client request was "GET /H" or "GET /L":
if (currentLine.endsWith("GET /H")) {
digitalWrite(LED_BUILTIN, HIGH); // GET /H turns the LED on
// put your main code here, to run repeatedly:
}
if (currentLine.endsWith("GET /L")) {
digitalWrite(LED_BUILTIN, LOW); // GET /L turns the LED off
}
}
}
// close the connection:
client.stop();
Serial.println("client disonnected");
}
}
62
Gambar 8.2. Tampilan program pembacaan data ADC di server
63
PERCOBAAN 9
DISPLAY DATA DHT11 KE THINKSPEAK
64
PIN 15
/*
https://vegardpaulsen.wordpress.com/2015/07/18/soldering-iron-and-
nodemcu-iot-device/
https://www.instructables.com/id/Iot-Pan-With-ESP32-and-
Thingspeak/
https://roboindia.com/tutorials/nodeMCU-dht11-thingspeak-data-
upload
*/
#include <WiFi.h>
#include "DHT.h"
WiFiClient client;
65
void setup()
{
Serial.begin(115200);
delay(10);
dht.begin();
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
void loop()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t))
{
Serial.println("Failed to read from DHT sensor!");
return;
}
if (client.connect(server,80)) // "184.106.153.149" or
api.thingspeak.com
{
66
client.print("Content-Type: application/x-www-form-
urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" degrees Celcius, Humidity: ");
Serial.print(h);
Serial.println("%. Send to Thingspeak.");
}
client.stop();
Serial.println("Waiting...");
67
Gambar 9.5. Pembuatan channel baru
68