Điện tử vuotlen.com

Arduino truyền dữ liệu phần 1

Arduino truyền dữ liệu Phần 1

1.  UART:

a. Truyền từ Arduino sang máy tính:

Linh kiện: Pot-hg.

Proteus:

Arduino IDE:

int Bientro = A0;

void setup() {

  // put your setup code here, to run once:

  Serial.begin(19200);

}

 

void loop() {

  // put your main code here, to run repeatedly:

  int dienap = analogRead(Bientro); // Đọc mức điện áp vào chân A0 (0V - 5V)

  int dientro = map(dienap, 0, 1023, 0, 10000);

  Serial.print("Gia tri bien tro: ");  

  Serial.print(dientro);  

  Serial.println("Ohm");  

  delay(1000);

}

b. Truyền từ máy tính sang Arduino:

Linh kiện: Led.

Proteus:

 

Arduino IDE:

int LED = 13;

String data = "";

 

void setup() {

  // put your setup code here, to run once:

  pinMode(LED, OUTPUT);

  digitalWrite(LED, 0);

  Serial.begin(19200);

}

 

void loop() {

  // put your main code here, to run repeatedly:

  // Serial.available(); // trả về số lượng kí tự lưu trong bộ nhớ arduino output buffer

  // Serial.read(); // đọc

  if( Serial.available() > 0 ){

    char c = Serial.read();

    if(c == '\n'){

      // hết dòng

      Serial.print(data);

      if(data = "bat"){

        digitalWrite(LED, 1);

      }

      else if(data = "tat"){

        digitalWrite(LED, 0);

      }

      data = "";

    }

    else data = data + c;

  }

 

  delay(1000);

}

Khi kết nối Mạch Arduino thật vào laptop, COM4: nhập bat thì led sẽ sáng, nhập tat thì led sẽ tối.

2. I2C:

a. Test:

Proteus:

Arduino IDE:

#include <Wire.h>

void setup() {

  // put your setup code here, to run once:

  Wire.begin();

  Serial.begin(9600);

  byte error_i2c, address_i2c;

  int I2C_Devices = 0;

  for(address_i2c = 1; address_i2c < 127; address_i2c++){

    Wire.beginTransmission(address_i2c);

    error_i2c = Wire.endTransmission();

    if(error_i2c == 0){

      Serial.print("Dia chi I2C cua thiet bi la: 0x");

      if(address_i2c < 16){

        // hệ hexa 0 1 2 .... A .... F, sau đó thêm số 0 nữa

        Serial.print("0");

      }

      Serial.println(address_i2c, HEX);

      I2C_Devices++;  

    }else if(error_i2c == 4){

      Serial.print("Khong xac dinh duoc I2C dia chi 0x");

      if(address_i2c < 16){

        // hệ hexa 0 1 2 .... A .... F, sau đó thêm số 0 nữa

        Serial.print("0");

      }

      Serial.println(address_i2c);

    }

    if(I2C_Devices == 0){

      Serial.println("Khong co thiet bi I2C nao duoc ket noi");

    }

  }

}

 

void loop() {

  // put your main code here, to run repeatedly:

 

}

b. Thời gian:

Thư viện: RTClib.

Proteus:

Arduino IDE:

#include <RTClib.h>

RTC_DS1307 rtc;

char dayOfTheWeek[7][12] = {"CN", "T2", "T3", "T4", "T5", "T6", "T7"};

 

void Thoigian();

 

String Time = "";

unsigned int ngay = 0;

unsigned int thang = 0;

unsigned int nam = 0;

unsigned int gio = 0;

unsigned int phut = 0;

unsigned int giay = 0;

unsigned int thu = 0;

 

String Thu;

String Ngay;

String Thang;

String Nam;

String Gio;

String Phut;

String Giay;

 

void setup() {

  // put your setup code here, to run once:

  Serial.begin(9600);

  rtc.begin();

  // rtc.adjust(DateTime(2022, 22, 8, 12, 0, 0)); //Tự cài đặt thời gian

  rtc.adjust(DateTime(__DATE__, __TIME__)); // lấy thời gian bắt đầu theo máy tính

 

}

 

void loop() {

  // put your main code here, to run repeatedly:

  Thoigian();

  delay(2000);

}

 

void Thoigian(){

  Serial.print("Thoi gian hien tai: ");

  DateTime now = rtc.now();

  Serial.print(dayOfTheWeek[now.dayOfTheWeek()]);

  Serial.print(", ");

  Time = "";

  ngay = now.day(), DEC;

  thang = now.month(), DEC;

  nam = now.year(), DEC;

  gio = now.hour(), DEC;

  phut = now.minute(), DEC;

  giay = now.second(), DEC;

 

  Ngay = "00";

  Thang = "00";

  Nam = "00";

  Gio = "00";

  Phut = "00";

  Giay = "00";

 

  Ngay = String(ngay);

  Thang = String(thang);

  Nam = String(nam);

  Gio = String(gio);

  Phut = String(phut);

  Giay = String(giay);

 

  if(Ngay.length() == 1){

    Ngay = "0" + Ngay; // thêm số 0 vào cho đẹp ví dụ là 3 thì ra 03

  }

  if(Thang.length() == 1){

    Thang = "0" + Thang;

  }

  if(Nam.length() == 1){

    Nam = "0" + Nam;

  }

  if(Gio.length() == 1){

    Gio = "0" + Gio;

  }

  if(Phut.length() == 1){

    Phut = "0" + Phut;

  }

  if(Giay.length() == 1){

    Giay = "0" + Giay;

  }

 

  Time = String(Ngay) + "-" + String(Thang) + "-" + String(Nam) + " " + String(Gio) + ":" + String(Phut) + ":" + String(Giay);

  Serial.println(Time);

}