Arduino Uno의 A0에서 A5 핀은 아날로그 입력으로 작동할 수 있습니다. 아날로그 입력 핀은 전압(0v와 VCC 사이)을 정수 값(0에서 1023 사이), 즉 ADC 값 또는 아날로그 값으로 변환합니다.
힘 센서의 핀을 아날로그 입력 핀에 연결함으로써, analogRead() 함수를 사용하여 핀에서 아날로그 값을 읽을 수 있으며, 그러면 얼마나 눌렸는지 알 수 있습니다.
아두이노 코드
/* * 이 아두이노 코드는 newbiely.kr 에서 개발되었습니다 * 이 아두이노 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino/arduino-force-sensor */#define FORCE_SENSOR_PIN A0 // FSR과 10K 풀다운이 A0에 연결되어 있습니다.voidsetup() {Serial.begin(9600);}voidloop() {int analogReading = analogRead(FORCE_SENSOR_PIN);Serial.print("Force sensor reading = ");Serial.print(analogReading); // 원시 아날로그 읽기 값을 출력합니다if (analogReading < 10) // 0에서 9까지Serial.println(" -> no pressure");elseif (analogReading < 200) // 10에서 199까지Serial.println(" -> light touch");elseif (analogReading < 500) // 200에서 499까지Serial.println(" -> light squeeze");elseif (analogReading < 800) // 500에서 799까지Serial.println(" -> medium squeeze");else// 800에서 1023까지Serial.println(" -> big squeeze");delay(1000);}
사용 방법
위의 코드를 복사하여 아두이노 IDE로 열기
아두이노 IDE에서 Upload 버튼을 클릭하여 아두이노에 코드 업로드하기
힘 센서를 누르기
시리얼 모니터에서 결과 보기
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' 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 on COM15
2
동영상
비디오 제작은 시간이 많이 걸리는 작업입니다. 비디오 튜토리얼이 학습에 도움이 되었다면, YouTube 채널 을 구독하여 알려 주시기 바랍니다. 비디오에 대한 높은 수요가 있다면, 비디오를 만들기 위해 노력하겠습니다.
아래 동영상 데모는 아래 코드를 사용합니다. 동영상은 Arduino Uno R4를 보여주지만 Arduino Uno R3에서도 동일하게 작동합니다:
/* * 이 아두이노 코드는 newbiely.kr 에서 개발되었습니다 * 이 아두이노 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino/arduino-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 -1Adafruit_SSD1306display( SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);// Force-sensitive resistor input#define FORCE_SENSOR_PIN A0// LED pins used to indicate force levelconstint ledPins[8] = { 2, 3, 4, 5, 6, 7, 8, 9};// Calibrated force sensor rangeconstint FORCE_MIN = 0;constint FORCE_MAX = 1000;// Exponential moving average filterfloat smoothValue = 0;constfloat alpha = 0.3;// Initialize hardware and displayvoidsetup() {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);}voidloop() {// 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);}