QC

Cảm biến độ Nghiêng Arduino (Tilt Sensor)

 Tilt Sensor



How does it work? ( Nó hoạt động như thế nào).

Cảm biến độ nghiêng có dạng hình trụ và chứa một viên bi lăn dẫn điện tự do bên trong với hai phần tử dẫn điện (cực) bên dưới.

Đây là cách nó hoạt động:

- Khi cảm biến hoàn toàn thẳng đứng, quả bóng rơi xuống đáy của cảm biến và kết nối các cực, cho phép dòng điện chạy qua.
-Khi nghiêng cảm biến, quả bóng không chạm vào các cực, mạch điện bị hở và dòng điện không chạy qua.
Bằng cách này, cảm biến độ nghiêng hoạt động giống như một công tắc được bật hoặc tắt tùy thuộc vào độ nghiêng của nó. Vì vậy, nó sẽ cung cấp thông tin kỹ thuật số cho Arduino, tín hiệu CAO hoặc THẤP.

Kết nối !


Code


 
int ledPin = 12;         
int sensorPin = 4;
int sensorValue;
int lastTiltState = HIGH;   // the previous reading from the tilt sensor
    
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers
 
void setup(){
  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, HIGH);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}
 
void loop(){
  sensorValue = digitalRead(sensorPin);
  // If the switch changed, due to noise or pressing:
  if (sensorValue == lastTiltState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  } 
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:
    lastTiltState = sensorValue;
  }
  digitalWrite(ledPin, lastTiltState);

  Serial.println(sensorValue);
  delay(500);
}

Nap Code vào PY32F003 dùng Stlink

 Nap Code vào PY32F003 dùng Stlink Bước 1: Cài đặt  KeilC v5.39 theo link sau ( chú ý 5.39 keil c mới nạp ok). https://edge07.111.ir.cdn.ir/...