728x90
반응형
const int trig_pin = 11;
const int echo_pin = 12;

void setup()
{
  pinMode(trig_pin, OUTPUT);
  pinMode(echo_pin, INPUT);

  Serial.begin(115200);
}

void loop()
{
  digitalWrite(trig_pin, LOW);
  delayMicroseconds(2);
  digitalWrite(trig_pin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig_pin, LOW);

  long duration = pulseIn(echo_pin, HIGH);
  long distance = (duration/2) / 29.1;

  Serial.print(distance);
  Serial.println("cm");  
}
728x90
반응형

'Firmware & Embedded > Components' 카테고리의 다른 글

Arduino Serial (to Python)  (0) 2022.10.31
Sequential Brightness LED using Button  (0) 2022.10.14
Alarm operation based on distance using ultrasonic sensor  (0) 2022.10.14
BUZZER Control  (0) 2022.09.22
Servo  (0) 2022.09.21

+ Recent posts