Translate

Membuat Simulasi Komunikasi 2 Mikrokontroller Menggunakan 2 Serial UART Master Slave / kirim terima BASCOM AVR

Membuat Simulasi Komunikasi 2 Mikrokontroller Menggunakan 2 Serial UART Master Slave / kirim terima BASCOM AVR


           Pada kesempatan yang berbahagia kali ini saya akan menjelaskan mengenai sebuah simulasi dari 2 buah mikrokontroller yang saling berhubungan menggunakan 2 buah serial UART yang mana masing masing mikrokontroller bertindak sebagai master dan slave. jadi saat tombol ditekan pada mikro 1 maka led yang berada pada mikro 2 akan menyala begitu juga jika tombol yang ada pada mikro 2 ditekan maka led pada mikro 1 akan menyala. kemudian pada mikro 1 terdapat sensor yang mana nilai sensor ditampilkan ke 2 buah mikrokontroller yaitu mikro 1 dan mikro 2. untuk lebih jelasnya berikut adalah skema dan programnya. 




a. Skema Alat





b. Program Mikro 1

$regfile = "m128def.dat"
$crystal = 12000000
$baud = 9600

Config Lcdpin = Pin , Rs = Portd.0 , E = Portd.1 , Db4 = Portd.6
Config Lcdpin = Pin , Db5 = Portd.7 , Db6 = Portd.4 , Db7 = Portd.5
Config Lcd = 16 * 2


Config Com1 = 9600 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
Config Com2 = 9600 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0

Open "com1:" For Binary As #1
Open "Com2:" For Binary As #2

Cls
Cursor Off

On Urxc Getchar


Dim Wtime As Word
Dim Inchar As String * 1
Dim Inchar2 As Word

Dim A As Byte

Enable Interrupts
Enable Urxc


Config Pinb.0 = Output
Config Pinb.1 = Output
Config Pine.2 = Input
Config Pine.3 = Input
Config Pine.4 = Input
Config Pine.5 = Input
Config Pine.6 = Input
Porte.2 = 1
Porte.3 = 1
Porte.4 = 1
Porte.5 = 1
Porte.6 = 1



Do

A = Waitkey(#2)

Upperline
Lcd A

If Pine.2 = 1 And Pine.3 = 1 And Pine.4 = 1 And Pine.5 = 1 And Pine.6 = 1 Then
Print #1 , "9"
End If

If Pine.2 = 0 And Pine.3 = 1 And Pine.4 = 1 And Pine.5 = 1 And Pine.6 = 1 Then
Print #1 , "0"
End If

If Pine.2 = 0 And Pine.3 = 0 Then
Print #1 , "1"
End If

If Pine.2 = 0 And Pine.4 = 0 Then
Print #1 , "2"
End If

If Pine.2 = 0 And Pine.5 = 0 Then
Print #1 , "3"
End If

If Pine.2 = 0 And Pine.6 = 0 Then
Print #1 , "4"
End If

If Pine.2 = 1 And Pine.3 = 0 Then
Print #1 , "5"
End If

If Pine.2 = 1 And Pine.4 = 0 Then
Print #1 , "6"
End If

If Pine.2 = 1 And Pine.5 = 0 Then
Print #1 , "7"
End If

If Pine.2 = 1 And Pine.6 = 0 Then
Print #1 , "8"
End If


Loop



Getchar:

Inchar = Inkey(#1)
  Select Case Inchar
    Case "1" :
     Portb.0 = 1
     Portb.1 = 1

    Case "2" :
     Portb.0 = 0
     Portb.1 = 1

    Case "3" :
     Portb.0 = 1
     Portb.1 = 0

    Case "4" :
     Portb.0 = 0
     Portb.1 = 0

  End Select
Return

End






c. Program Mikro 2

$regfile = "m128def.dat"
$crystal = 12000000
$baud = 9600

Config Lcdpin = Pin , Rs = Porte.2 , E = Portd.7 , Db4 = Porte.3
Config Lcdpin = Pin , Db5 = Porte.4 , Db6 = Porte.5 , Db7 = Porte.6
Config Lcd = 16 * 2
Config Adc = Single , Prescaler = Auto , Reference = Avcc


Config Com1 = 9600 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
Config Com2 = 9600 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0

Open "com1:" For Binary As #1
Open "Com2:" For Binary As #2


On Urxc Getchar

Dim Dataadc As Word
Dim Wtime As Word
Dim Inchar As String * 1

Enable Interrupts
Enable Urxc

Cls
Cursor Off

Config Pinb.2 = Output
Config Pinb.3 = Output
Config Pinb.5 = Output
Config Pinb.6 = Output
Config Pinb.7 = Output

Config Pinf.0 = Input
Config Pinf.1 = Input
Portf.0 = 1
Portf.1 = 1

Start Adc

Do

Dataadc = Getadc(3)

Locate 1 , 1
Lcd "ADC="
Locate 2 , 1
Lcd Dataadc

Printbin #2 , Dataadc

If Pinf.0 = 0 And Pinf.1 = 0 Then
Print "1"
End If

If Pinf.0 = 0 And Pinf.1 = 1 Then
Print "2"
End If

If Pinf.0 = 1 And Pinf.1 = 0 Then
Print "3"
End If

If Pinf.0 = 1 And Pinf.1 = 1 Then
Print "4"
End If



Loop



Getchar:

Inchar = Inkey(#1)
  Select Case Inchar
    Case "1" :
     Portb.2 = 1
     Portb.3 = 1
     Portb.5 = 0
     Portb.6 = 0
     Portb.7 = 0

    Case "2" :
     Portb.2 = 1
     Portb.3 = 0
     Portb.5 = 1
     Portb.6 = 0
     Portb.7 = 0

    Case "3" :
     Portb.2 = 1
     Portb.3 = 0
     Portb.5 = 0
     Portb.6 = 1
     Portb.7 = 0

    Case "4" :
     Portb.2 = 1
     Portb.3 = 0
     Portb.5 = 0
     Portb.6 = 0
     Portb.7 = 1

    Case "5" :
     Portb.2 = 0
     Portb.3 = 1
     Portb.5 = 0
     Portb.6 = 0
     Portb.7 = 0

    Case "6" :
     Portb.2 = 0
     Portb.3 = 0
     Portb.5 = 1
     Portb.6 = 0
     Portb.7 = 0

    Case "7" :
     Portb.2 = 0
     Portb.3 = 0
     Portb.5 = 0
     Portb.6 = 1
     Portb.7 = 0

    Case "8" :
     Portb.2 = 0
     Portb.3 = 0
     Portb.5 = 0
     Portb.6 = 0
     Portb.7 = 1

    Case "9" :
     Portb.2 = 0
     Portb.3 = 0
     Portb.5 = 0
     Portb.6 = 0
     Portb.7 = 0

    Case "0" :
     Portb.2 = 1
     Portb.3 = 0
     Portb.5 = 0
     Portb.6 = 0
     Portb.7 = 0


  End Select
Return

End






d. VIDEO HASILNYA









Membuat Alat Monitoring Gas Polutan ( CO / Co2 / NO ) dan Debu Menggunakan Data Logger SD Card / Micro SD ARDUINO

Membuat Alat Monitoring Gas Polutan ( CO / Co2 / NO ) dan Debu Menggunakan Data Logger SD Card / Micro SD ARDUINO


         Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang bisa digunakan untuk monitor gas polutan yaitu CO, CO2, NO dll. dan juga debu. alat ini dilengkapi dengan data logger berupa micro SD atau SD card sehingga data akan disimpan kedalam memori. alat ini memang sengaja tidak menggunakan LCD untuk interfacenya. untuk lebih jelasnya berikut adalah skema dan programnya.




a. Arduino Uno




b. Sensor Gas Polutan MQ-7 dan MQ-135





c. Sensor Debu




d. Modul Micro SD






e. Program Arduino IDE

//https://github.com/R2D2-2017/R2D2-2017/wiki/MQ-7-gas-sensor

#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include "MQ135.h"
#define ANALOGPIN A1    //  Define Analog PIN on Arduino Board
#define RZERO 206.85    //  Define RZERO Calibration Value
MQ135 gasSensor = MQ135(ANALOGPIN);

  float RS_air; //  Get the value of RS via in a clear air
  float R0 = 0.25;  // Get the value of R0 via in H2
  float sensorValue;
  float sensor_volt;
  float RS_gas;
  float ratio;


//mosi - 11
//miso - 12
//clk - 13
//cs - 4

int cacah = 0;
File myFile;
int ledx = 2;

float Dustval;

float ppm;
float ppm2;


void setup(){
  Serial.begin(9600);
 pinMode(ledx,OUTPUT);

  float rzero = gasSensor.getRZero();
  delay(3000);
  Serial.print("MQ135 RZERO Calibration Value : ");
  Serial.println(rzero);
    
}

void loop(){
gasmq135();
gasmq7();
debu();

  Serial.print(ppm);
  Serial.print(" ");
  Serial.print(ppm2);
  Serial.print(" ");
  Serial.println(Dustval);

delay(200);

cacah++;

if(cacah > 10){
cacah = 0;
simpan();
}

}


void gasmq135(){

  ppm = gasSensor.getPPM();
  delay(1000);
  //Serial.print("CO2 ppm value : ");
  //Serial.println(ppm);
    
}


void gasmq7(){

analogWrite(A5, 1023);
//Heather 1.4 V 90 s
    analogWrite(A5, (1023/5)*1.4 );
    for(int i = 0; i<90; i++){
        sensorValue = analogRead(A2);
        sensor_volt = sensorValue/1024*5.0;
        RS_gas = (5.0-sensor_volt)/sensor_volt;
        ratio = RS_gas/R0; //Replace R0 with the value found using the calibration code
        float kal = 83.39 / ratio;
        ppm2 = pow(kal , 0.63);      
        delay(100);
    }

}


void debu(){
//aktif low
//GP2Y1010AU0F_SAMPLEDELAY should be 280us to perform the correct reading
//this delay should consider that ADC conversion takes 13 ADC clock cycles
//ADCtime(s) = (1/ADCclock)*13 = (1/FCPU/ADCprescaler)*13
// es (1/(8000000/64))*13 = 0.000104s = 104us
//so to perform reading at correct time
//280 - (1/FCPU/ADCprescaler)*13*1000000   , 1000000 is the conversion factor from s to us
// es. 280 - 104 = 176

digitalWrite(ledx,LOW);    //on
delayMicroseconds(176);

int dataadc = analogRead(A0);
delayMicroseconds(40);

digitalWrite(ledx,HIGH);   //off
delayMicroseconds(9680);

float v = dataadc * (5.0 / 1023.0);

//y = 0.166x - 0.129
 Dustval = (v * 0.166) - 0.129;

}



void simpan(){

while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
  pinMode(10, OUTPUT);

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
     
    myFile.print("CO2= ");
    myFile.println(ppm);
    myFile.print("CO= ");
    myFile.println(ppm2);
    myFile.print("Debu= ");
    myFile.println(Dustval);
     
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }


}






f. VIDEO HASILNYA









Membuat Alat Pasien Monitor ( Suhu Tubuh, Heart Beat / HB, GSR / keringat tubuh, Respirasi) Menggunakan Arduino dan Monitor via Android Smartphone

Membuat Alat Pasien Monitor ( Suhu Tubuh, Heart Beat / HB, GSR / keringat tubuh, Respirasi) Menggunakan Arduino dan Monitor via Android Smartphone


         Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat alat yang berfungsi untuk monitor kesehatan pasien mengenai suhu tubuh, heart beat atau detak jantung / HB, GSR atau keringat tubuh, dan respirasi atau laju pernafaan, semua dimonitor dengan waktu satu menit, jadi lama pengambilan HB dan respirasi adalah 1 menit kemudian untuk GSR dan suhu sudah otomatis mendeteksi ketika alat dihidupkan. alat ini bisa mengirimkan hasil ke smartphone android terkait hasil 4 parameter tersebut dan juga terdapat kesimpulan dan saran yang harus dilakukan. untuk lebih jelasnya berikut adalah skema dan programnya.  



a. Arduino Uno




b. Bluetooth HC-05




c. Sensor Suhu DS18b20




d. Sensor Pulse / Heart Beat




e. Sensor GSR / Keringat




f. Sensor Suara / Hembusan




g. LCD 16x2 + I2C





h. Program Android







i. Program Arduino

#include "Wire.h"
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
LiquidCrystal_I2C lcd(0x27,16,2);

OneWire  ds(10);  // on pin 10 (a 4.7K resistor is necessary)

int n = 0;
int bpm = 0;
int x = 0;

int m = 0;
int resp = 0;
int w = 0;

int suhux = 0;
int bpmx = 0;

int ledgsr = 6;
int ledbpm = 7;
int ledrespirasi = 8;
int ledsuhu = 9;

int bt1 = 2;
int bt2 = 3;
int bt3 = 4;
int bt4 = 5;

int bt1x = 0;
int bt2x = 0;
int bt3x = 0;
int bt4x = 0;

  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;
  float voltgsr;

void setup() {
  
  Serial.begin(9600);
  lcd.begin();
  lcd.clear();
  lcd.noCursor();

  pinMode(ledgsr,OUTPUT);
  pinMode(ledbpm,OUTPUT);
  pinMode(ledrespirasi,OUTPUT);
  pinMode(ledsuhu,OUTPUT);

  pinMode(bt1,INPUT_PULLUP);
  pinMode(bt2,INPUT_PULLUP);
  pinMode(bt3,INPUT_PULLUP);
  pinMode(bt4,INPUT_PULLUP);
  
  digitalWrite(ledsuhu,HIGH);
  digitalWrite(ledgsr,HIGH);

  lcd.setCursor(0, 0);
  lcd.print("WELLCOME");
  delay(3000);
  lcd.clear();
  
}


void loop() {
n = 0;
m = 0;

  lcd.setCursor(0, 0);
  lcd.print("Tekan Tombol");
  lcd.setCursor(0, 1);
  lcd.print("HIJAU");
  
  bt1x = digitalRead(bt1);
  bt2x = digitalRead(bt2);
  bt3x = digitalRead(bt3);
  bt4x = digitalRead(bt4);

if(bt3x == 0){
  lcd.clear();
  delay(2000);

  digitalWrite(ledsuhu,LOW);
  digitalWrite(ledgsr,LOW);
  digitalWrite(ledbpm,LOW);
  digitalWrite(ledrespirasi,LOW);
bpm = 0;
resp = 0;

tampilbpm();
  digitalWrite(ledbpm,LOW);
tampilrespirasi();
  digitalWrite(ledrespirasi,LOW);

tampilsuhu();
tampilgsr();
kirimbt();

}

  digitalWrite(ledbpm,LOW);
  digitalWrite(ledrespirasi,LOW);
         
}


void tampilbpm(){
n = n + 1;

  int dataadc1 = analogRead(A1);           
  lcd.setCursor(0, 0);
  lcd.print("n = ");
  lcd.print(n);
    
  lcd.setCursor(0, 1);
  lcd.print("BPM = ");
  lcd.print(bpm);
  if ((dataadc1 > 512) && (x > 1)) {   
  digitalWrite(ledbpm, HIGH);
  x = 0;
  bpm = bpm + 1;
  }
  else if ((dataadc1 < 512) && (x < 1)) {
  x = 2; 
  digitalWrite(ledbpm, LOW);
  }
  delay(200);
  if ( n > 300 ) {
  lcd.clear();
  return;
  }
 tampilbpm(); 
}




void tampilrespirasi(){
m = m + 1;
  
  int dataadc2 = analogRead(A2);           
  lcd.setCursor(0, 0);
  lcd.print("m = ");
  lcd.print(m);
    
  lcd.setCursor(0, 1);
  lcd.print("RESP = ");
  lcd.print(resp);
  if ((dataadc2 > 512) && (w > 1)) {   
  digitalWrite(ledrespirasi, HIGH);
  w = 0;
  resp = resp + 1;
  }
  else if ((dataadc2 < 512) && (w < 1)) {
  w = 2; 
  digitalWrite(ledrespirasi, LOW);
  }
  delay(200);
  if ( m > 300 ) {
  lcd.clear();
  return;
  }
  
  tampilrespirasi();
}





void tampilsuhu(){

   
  if ( !ds.search(addr)) {
    //Serial.println("No more addresses.");
    //Serial.println();
    ds.reset_search();
    delay(250);
    return;
  }
  //Serial.print("ROM =");
  for( i = 0; i < 8; i++) {
    //Serial.write(' ');
    //Serial.print(addr[i], HEX);
  }

  if (OneWire::crc8(addr, 7) != addr[7]) {
      //Serial.println("CRC is not valid!");
      return;
  }
  //Serial.println();

   //the first ROM byte indicates which chip
  switch (addr[0]) {
    case 0x10:
      //Serial.println("  Chip = DS18S20");  // or old DS1820
      type_s = 1;
      break;
    case 0x28:
      //Serial.println("  Chip = DS18B20");
      type_s = 0;
      break;
    case 0x22:
      //Serial.println("  Chip = DS1822");
      type_s = 0;
      break;
    default:
      //Serial.println("Device is not a DS18x20 family device.");
      return;
  }

  ds.reset();
  ds.select(addr);
  ds.write(0x44, 1);        // start conversion, with parasite power on at the end
  delay(1000);     // maybe 750ms is enough, maybe not
  // we might do a ds.depower() here, but the reset will take care of it.
  present = ds.reset();
  ds.select(addr);   
  ds.write(0xBE);         // Read Scratchpad

  //Serial.print("  Data = ");
  //Serial.print(present, HEX);
  //Serial.print(" ");
  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
    //Serial.print(data[i], HEX);
    //Serial.print(" ");
  }
  //Serial.print(" CRC=");
  //Serial.print(OneWire::crc8(data, 8), HEX);
  //Serial.println();

  // Convert the data to actual temperature
  // because the result is a 16 bit signed integer, it should
  // be stored to an "int16_t" type, which is always 16 bits
  // even when compiled on a 32 bit processor.
  int16_t raw = (data[1] << 8) | data[0];
  if (type_s) {
    raw = raw << 3; // 9 bit resolution default
    if (data[7] == 0x10) {
      // "count remain" gives full 12 bit resolution
      raw = (raw & 0xFFF0) + 12 - data[6];
    }
  } else {
    byte cfg = (data[4] & 0x60);
    // at lower res, the low bits are undefined, so let's zero them
    if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
    else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
    else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
    //// default is 12 bit resolution, 750 ms conversion time
  }
  celsius = (float)raw / 16.0;
//  lcd.setCursor(0,0);
//  lcd.print("SUHU = ");
//  lcd.print(celsius);
//  lcd.print(" C ");
  return;
}




void tampilgsr(){

int dataadc0 = analogRead(A0);
 voltgsr = dataadc0 * (4.2 / 1023);

return;

}




void kirimbt(){

  bt1x = digitalRead(bt1);
  bt2x = digitalRead(bt2);
  bt3x = digitalRead(bt3);
  bt4x = digitalRead(bt4);

lcd.setCursor(0,0);
lcd.print("SUHU =");
lcd.print(celsius);

if(bt1x == 0){
delay(200);  
lcd.setCursor(0,1);
lcd.print("BPM =");
lcd.print(bpm);
lcd.print("      ");
}

if(bt2x == 0){
delay(200);  
lcd.setCursor(0,1);
lcd.print("Resp =");
lcd.print(resp);
lcd.print("      ");
}

if(bt4x == 0){
delay(200);  
lcd.setCursor(0,1);
lcd.print("GSR =");
lcd.print(voltgsr);
lcd.print("      ");
}

if(bt3x == 0){
lcd.clear();  
delay(3000);
  return;
}

  Serial.print("suhu = ");
  Serial.print(celsius);
  Serial.println(" c");
  Serial.print("BPM = ");
  Serial.println(bpm);

  Serial.print("Resp = ");
  Serial.print(resp);
  Serial.println(" kali/menit");
  Serial.print("GSR = ");
  Serial.print(voltgsr); 
  Serial.println(" v");

  Serial.println("  ");
 if ( bpm >= 100 ) {
 Serial.println("perbanyak istirahat dan ");
 }
else if ( bpm < 60 ) {
 Serial.println("segera hubungi dokter ");
 }

 if ( celsius >= 37.5 ) {
 Serial.println("kompres dengan air dingin");
 }
else if ( celsius < 34.0 ) {
 Serial.println("perbanyak aktifitas ");
 }

 if ( voltgsr >= 4 ) {
 Serial.println("Perbanyak menghibur diri");
 }

  
 if (( bpm > 100 ) && ( celsius > 37.5 ) && ( voltgsr > 4 )) {
 Serial.println("Terdeteksi STRESS");
 Serial.println("hibur diri dan konsul ke psikiater");
 } 


 kirimbt(); 

}






j. VIDEO HASILNYA