Arduino UNO R4 Cảm Biến Lực

Hướng dẫn này sẽ chỉ cho bạn cách sử dụng cảm biến lực với Arduino UNO R4. Cụ thể, chúng ta sẽ học:

Arduino UNO R4 và cảm biến lực

Phần Cứng Cần Thiết

1×Arduino UNO R4 WiFi hoặc Arduino UNO R4 Minima
1×(Tùy chọn) DIYables STEM V4 IoT, tương thích với Arduino Uno R4 WiFi Amazon
1×Arduino UNO R4 Minima (Thay thế) Amazon
1×Cáp USB Type-C Amazon
1×Cảm Biến Lực Amazon
1×Điện trở 10 kΩ Amazon
1×breadboard Amazon
1×Dây Jumper Amazon
1×(Khuyến nghị) Screw Terminal Block Shield for Arduino UNO R4 Amazon
1×(Khuyến nghị) Sensors/Servo Expansion Shield for Arduino UNO R4 Amazon
1×(Khuyến nghị) Breadboard Shield for Arduino UNO R4 Amazon
1×(Khuyến nghị) Enclosure for Arduino UNO R4 Amazon
1×(Khuyến nghị) Power Splitter for Arduino UNO R4 Amazon
1×(Khuyến nghị) Prototyping Base Plate & Breadboard Kit for Arduino UNO Amazon

Or you can buy the following kits:

1×DIYables STEM V4 IoT Starter Kit (Arduino included) Amazon
1×DIYables STEM V4B IoT Starter Kit (Arduino included) Amazon
1×DIYables Sensor Kit (18 sensors/displays) Amazon
Tiết lộ: Một số liên kết trong bài viết này là liên kết tiếp thị Amazon và có thể nhận hoa hồng theo chính sách của chương trình.
Arduino Uno R4 WiFi Compatible Boards

Về Cảm Biến Lực

sơ đồ chân cảm biến lực

Cảm biến lực đôi khi được gọi là điện trở cảm ứng lực, điện trở nhạy lực, hoặc đơn giản là FSR. Đây là một loại điện trở thay đổi điện trở khi có áp lực tác động lên nó.

  • Giá cả phải chăng và dễ vận hành.
  • Hiệu quả trong việc cảm nhận áp lực vật lý hoặc lực ép.
  • Không hiệu quả trong việc đo trọng lượng theo đơn vị pound.

Cảm biến lực được sử dụng trong trống điện tử, điện thoại di động, thiết bị chơi game cầm tay và nhiều thiết bị điện tử di động khác.

Sơ Đồ Chân

Cảm biến lực có hai chân. Vì nó hoạt động như một điện trở, chúng ta không cần phân biệt giữa các chân này. Chúng hoàn toàn giống nhau.

Cách Hoạt Động

Cảm biến lực giống như một điện trở thay đổi điện trở dựa trên lực ép tác động. Càng ép mạnh, điện trở giữa hai đầu càng thấp.

Sơ Đồ Kết Nối

sơ đồ kết nối Arduino UNO R4 với cảm biến lực

This image is created using Fritzing. Click to enlarge image

Đây là cách nối dây giống vậy nhưng không dùng breadboard:

sơ đồ kết nối Arduino UNO R4 với cảm biến lực (không dùng breadboard)

This image is created using Fritzing. Click to enlarge image

Nếu dự án chạy trong thời gian dài, hoặc đặt trong hộp, tốt hơn hết là cố định dây bằng vít. Bạn có thể gắn Proto Screw Terminal Block Shield hoặc Screw Shield V2 lên Arduino UNO R4 và nối dây vào các cầu đấu vít của nó. Đây là cùng mạch điện, nối qua Screw Shield V2:

sơ đồ kết nối Arduino UNO R4 với cảm biến lực (screw shield v2)

This image is created using Fritzing. Click to enlarge image

Cách Lập Trình Cho Cảm Biến Lực

Arduino UNO R4 có các chân được ghi nhãn từ A0 đến A5 được sử dụng cho đầu vào analog. Các chân này chuyển đổi điện áp, từ 0 volt đến VCC, thành các số nguyên từ 0 đến 1023. Số này được gọi là giá trị ADC hoặc giá trị analog.

Bằng cách kết nối một chân của cảm biến lực với chân đầu vào analog, chúng ta có thể sử dụng hàm analogRead() để đọc giá trị analog từ chân đó. Điều này cho chúng ta biết mức độ áp lực đang được tác động.

Code Arduino UNO R4

/* * Mã Arduino UNO R4 này được phát triển bởi newbiely.vn * Mã Arduino UNO R4 này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/arduino-uno-r4/arduino-uno-r4-force-sensor */ #define FORCE_SENSOR_PIN A0 // The Arduino UNO R4 pin connected to the FSR force sensor with 10K pulldown void setup() { Serial.begin(9600); } void loop() { int analogReading = analogRead(FORCE_SENSOR_PIN); Serial.print("Force sensor reading = "); Serial.print(analogReading); // print the raw analog reading if (analogReading < 10) // from 0 to 9 Serial.println(" -> no pressure"); else if (analogReading < 200) // from 10 to 199 Serial.println(" -> light touch"); else if (analogReading < 500) // from 200 to 499 Serial.println(" -> light squeeze"); else if (analogReading < 800) // from 500 to 799 Serial.println(" -> medium squeeze"); else // from 800 to 1023 Serial.println(" -> big squeeze"); delay(1000); }

Các Bước Thực Hiện

Làm theo các hướng dẫn từng bước sau:

  • Nếu đây là lần đầu tiên bạn sử dụng Arduino Uno R4 WiFi/Minima, hãy tham khảo hướng dẫn về Arduino UNO R4 - Cài Đặt Phần Mềm.
  • Kết nối cảm biến lực với Arduino Uno R4 theo sơ đồ được cung cấp.
  • Kết nối board Arduino Uno R4 với máy tính của bạn bằng cáp USB.
  • Khởi chạy Arduino IDE trên máy tính của bạn.
  • Chọn board Arduino Uno R4 phù hợp (ví dụ: Arduino Uno R4 WiFi) và cổng COM.
  • Copy code ở trên và mở nó trong Arduino IDE.
  • Nhấn nút Upload trong Arduino IDE để tải code lên Arduino UNO R4.
  • Nhấn vào cảm biến lực.
  • Kiểm tra kết quả trên Serial Monitor.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno R4 WiFi
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno R4 WiFi' on 'COM15')
New Line
9600 baud
Force sensor reading = 0 -> no pressure Force sensor reading = 0 -> no pressure Force sensor reading = 132 -> light touch Force sensor reading = 147 -> light touch Force sensor reading = 394 -> light squeeze Force sensor reading = 421 -> light squeeze Force sensor reading = 607 -> medium squeeze Force sensor reading = 791 -> medium squeeze Force sensor reading = 921 -> big squeeze Force sensor reading = 987 -> big squeeze Force sensor reading = 0 -> no pressure Force sensor reading = 0 -> no pressure
Ln 11, Col 1
Arduino Uno R4 WiFi on COM15
2

Video Tutorial

Việc sản xuất video tốn rất nhiều thời gian. Nếu video hướng dẫn hữu ích cho việc học của bạn, hãy đăng ký kênh YouTube để ủng hộ. Nếu nhu cầu đủ cao, chúng tôi sẽ cố gắng làm thêm nhiều video.

Video demo bên dưới sử dụng đoạn mã bên dưới:

/* * Mã Arduino UNO R4 này được phát triển bởi newbiely.vn * Mã Arduino UNO R4 này được cung cấp để sử dụng công khai, không có ràng buộc. * Để xem hướng dẫn chi tiết và sơ đồ kết nối, vui lòng truy cập: * https://newbiely.vn/tutorials/arduino-uno-r4/arduino-uno-r4-force-sensor */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> // OLED display configuration #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 Adafruit_SSD1306 display( SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET ); // Force-sensitive resistor input #define FORCE_SENSOR_PIN A0 // LED pins used to indicate force level const int ledPins[8] = { 2, 3, 4, 5, 6, 7, 8, 9 }; // Calibrated force sensor range const int FORCE_MIN = 0; const int FORCE_MAX = 1000; // Exponential moving average filter float smoothValue = 0; const float alpha = 0.3; // Initialize hardware and display void setup() { Serial.begin(115200); // Configure LED outputs and turn all LEDs off. for (int i = 0; i < 8; i++) { pinMode(ledPins[i], OUTPUT); digitalWrite(ledPins[i], LOW); } // Initialize the OLED using I2C communication. if (!display.begin( SSD1306_SWITCHCAPVCC, 0x3C )) { Serial.println("OLED failed!"); // Stop execution if the OLED cannot be initialized. while (1); } display.clearDisplay(); display.setTextColor(SSD1306_WHITE); // Display startup screen. display.setTextSize(2); display.setCursor(20, 25); display.println("DIYables"); display.display(); delay(1500); // Initialize the filter with the first sensor reading // to prevent an incorrect initial value. smoothValue = analogRead(FORCE_SENSOR_PIN); } void loop() { // Read the current FSR value. int rawValue = analogRead(FORCE_SENSOR_PIN); // Apply exponential moving average filtering // to reduce sensor noise and fluctuations. smoothValue = alpha * rawValue + (1.0 - alpha) * smoothValue; int forceValue = (int)smoothValue; // Convert the sensor value into 8 force levels. int level = map( forceValue, FORCE_MIN, FORCE_MAX, 0, 8 ); level = constrain(level, 0, 8); // Turn on LEDs progressively according to the force level. for (int i = 0; i < 8; i++) { if (i < level) { digitalWrite(ledPins[i], HIGH); } else { digitalWrite(ledPins[i], LOW); } } // Output sensor data for monitoring and calibration. Serial.print("FSR = "); Serial.print(forceValue); Serial.print(" | Level = "); Serial.println(level); // Refresh OLED display. display.clearDisplay(); // Display title. display.setTextSize(2); display.setCursor(50, 0); display.println("FSR"); // Configure the force indicator as a battery-style bar. int barX = 10; int barY = 25; int barWidth = 108; int barHeight = 18; // Draw the outer bar. display.drawRect( barX, barY, barWidth, barHeight, SSD1306_WHITE ); // Draw the battery terminal. display.fillRect( barX + barWidth, barY + 5, 4, 8, SSD1306_WHITE ); // Calculate the filled portion based on the force level. int fillWidth = map( level, 0, 8, 0, barWidth - 4 ); if (fillWidth > 0) { display.fillRect( barX + 2, barY + 2, fillWidth, barHeight - 4, SSD1306_WHITE ); } // Display current force level. display.setTextSize(1); display.setCursor(5, 50); display.print("LEVEL: "); display.print(level); display.print("/8"); // Display project branding. display.setCursor(78, 50); display.print("DIYables"); display.display(); // Short delay for stable display updates. delay(30); }

Bình Luận

Bài hướng dẫn liên quan