Như các bạn đã biết Raspberry pi là một máy tính mini nên mạnh mẽ hơn Arduino rất nhiều. Tuy nhiên nó lại không an toàn cho các dự án phần cứng. Còn Arduino dễ dàng hơn trong tích hợp phần cứng nhưng về mảng IoT thì cũng hạn chế hơn raspberry pi. Mỗi cái riêng lẻ là một hạn chế, nhưng tại sao chúng ta không kết hợp chúng lại để dự án của mình tối ưu hóa hơn. Vâng, hôm nay mình xin hướng dẫn các bạn cách để Raspberry Pi giao tiếp Arduino thông qua UART.
Mục lục
Giao tiếp UART là gì?
UART có tên đầy đủ là Universal Asynchronous Receiver – Transmitter. Nó là một mạch tích hợp được sử dụng trong việc truyền dẫn dữ liệu nối tiếp giữa máy tính và các thiết bị ngoại vi.
UART có chức năng chính là truyền dữ liệu nối tiếp. Trong UART, giao tiếp giữa hai thiết bị có thể được thực hiện theo hai phương thức là giao tiếp dữ liệu nối tiếp và giao tiếp dữ liệu song song.

Project Overview

Đối với bản demo này, chúng tôi đã chọn Raspberry Pi & Arduino UNO vì cả hai bo mạch này đều hỗ trợ Giao tiếp nối tiếp UART.
Raspberry Pi 3
Raspberry Pi là một loại máy tính mini có nhiều chân IO để giao tiếp với các cảm biến khác nhau . Dưới đây là sơ đồ chân của Raspberry Pi 3 Model B:

Arduino UNO
Arduino / Genuino Uno là một bo mạch vi điều khiển dựa trên ATmega328P (datasheet). Nó có 14 chân I/O digital (trong đó 6 chân PWM), 6 đầu vào tương tự, thạch anh 16 MHz, kết nối USB, giắc cắm nguồn, một ICSP header và một reset button..

Chuẩn bị phần cứng
Bạn sẽ cần phần cứng sau để thực hiện dự án này:
- Raspberry Pi 3 Board
- Arduino UNO
- Push Buttons (2 nos.)
- Resistors, 220 Ohms ( 4 nos.), 1k Ohms (1 no.), 2.2k Ohms ( 1 no.)
- 5mm LED (2 nos.)
- Breadboard
Kết nối phần cứng
Trước khi đi vào chi tiết cụ thể của kết nối phần cứng, chúng ta hãy xác định vai trò của từng thiết bị:
- Arduino UNO – Hoạt động như một UART receiver. Nhận dữ liệu thông qua UART, được gửi từ Raspberry Pi và bật tắt đèn LED.
- Raspberry Pi – Hoạt động như một UART transmitter. Truyền dữ liệu khi nhấn nút.

Trước khi cấp nguồn cho mạch, chúng ta phải biết mức điện áp chuyển từ Arduino sang Raspberry Pi. Vì Arduino hoạt động với 5V và Raspberry Pi hoạt động với 3.3V. Chúng ta có thể đạt được mức dịch chuyển từ 5V sang 3.3V bằng cách sử dụng mạch phân áp.

Lập trình cho Arduino
1: Sao chép đoạn code:
/*
Receiving the Data from the UART to toggle the LED
Pinouts
GPIO6 -> LED 1
GPIO7 -> LED 2
GPIO10 -> Rx
GPIO11 -> Tx
*/
//Import the Library needed
#include <SoftwareSerial.h>
#include <stdlib.h>
//Initialize the Serial Communication
SoftwareSerial mySerial(10, 11); // RX, TX
//Code Initialization Starts
void setup() {
//Setting the Baud to 9600 Bits/Second
Serial.begin(9600);
mySerial.begin(9600);
//Setting up the LED Pins as the Output
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
}
void loop() {
char num[2];
//If the data is available serially, then receive it
if(mySerial.available()>0){
for(int i= 0 ;i<2;i++){
num[i] = mySerial.read();
delay(10);
}
int x;
x = atoi(num);
//If the data matches, toggles the LED's
switch(x){
case 10:
Serial.println("LED 1 LOW");
digitalWrite(6,LOW);
break;
case 11:
Serial.println("LED 1 HIGH");
digitalWrite(6,HIGH);
break;
case 20:
Serial.println("LED 2 LOW");
digitalWrite(7,LOW);
break;
case 21:
Serial.println("LED 2 HIGH");
digitalWrite(7,HIGH);
break;
}
}
}
2: Chọn board từ Tools –> Board –> Arduino UNO
3: Chọn USB Port từ Tools –> Port –> COMXX
4: Upload Code lên board Arduino UNO.
Thế là xong phần của Arduino, tiếp tục nhé.
Raspberry Pi UART Python Script
1: Sao chép đoạn code Python sau:
#Sending the data thorugh UART
#UART PINOUT
#Rx -> GPIO15
#Tx -> GPIO14
# get the GPIO Library
import RPi.GPIO as GPIO
import serial
import time
#Open named port
ser = serial.Serial ("/dev/ttyAMA0")
# the input buttons
up = 7
down = 8
#Setting up the GPIO Pins
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()
#Setting up the Button Inputs
GPIO.setup(up, GPIO.IN)
GPIO.setup(down, GPIO.IN)
#Set baud rate to 9600
ser.baudrate = 9600
g_button_1_pressed = 0
g_button_1_flag = 0
g_button_1_released = 0
g_button_2_pressed = 0
g_button_2_released = 0
g_button_2_flag = 0
led_1_flag = 0
led_2_flag = 0
while True:
#Handling the Button Debounce
if GPIO.input(up):
if g_button_1_pressed < 600:
g_button_1_pressed += 1
if g_button_1_pressed >= 500 and g_button_1_flag == 1:
g_button_1_flag = 0
g_button_1_released = 0
#Controlling the LED 1
if(led_1_flag == 0):
ser.write("11")
led_1_flag = 1
else:
ser.write("10")
led_1_flag = 0
else:
if g_button_1_released < 600:
g_button_1_released += 1
if g_button_1_released >= 500 and g_button_1_flag == 0:
g_button_1_pressed = 0
g_button_1_flag = 1
if GPIO.input(down):
if g_button_2_pressed < 600:
g_button_2_pressed += 1
if g_button_2_pressed >= 500 and g_button_2_flag == 1:
g_button_2_flag = 0
g_button_2_released = 0
#Controlling LED 2
if(led_2_flag == 0):
ser.write("21")
led_2_flag = 1
else:
ser.write("20")
led_2_flag = 0
else:
if g_button_2_released < 600:
g_button_2_released += 1
if g_button_2_released >= 500 and g_button_2_flag == 0:
g_button_2_pressed = 0
g_button_2_flag = 1
#Close the Serial Connection
ser.close()
#End of the Script
2: Điều hướng đến thư mục chứa chương trình trên terminal:
cd your_folder
....
3: run python code
python uart_rpi.py
thế là xong, và đây là kết quả:

Chúc các bạn thành công. Xem thêm Tự động chạy một Python Script trên Raspberry Pi khi khởi động.