1.Ultrasonic Basic project

Ultrasonic Basic project

Materials Needed:

  • Arduino board
  • Ultrasonic sensor (e.g., HC-SR04)
  • Jumper wires

Connections:

  1. Ultrasonic Sensor to Arduino:
    • TRIG pin → Pin 9
    • ECHO pin → Pin 10
    • VCC pin → 5V
    • GND pin → GND
  2. Procedure:

    1. Connect the Ultrasonic Sensor:

      • Use the jumper wires to connect the ultrasonic sensor pins as mentioned above.
      • Ensure proper power and ground connections for the sensor to work correctly.
    2. Write the Arduino Code:

      • Copy and paste the following code into the Arduino IDE:

  1. C++ Code
const int trigPin = 9;
const int echoPin = 10;

void setup() {
  Serial.begin(9600);  // Start the serial communication at 9600 baud rate
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  long duration;
  int distance;

  // Send a 10us pulse to trigger the sensor
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Measure the duration of the pulse on the echo pin
  duration = pulseIn(echoPin, HIGH);

  // Calculate the distance in centimeters
  distance = duration * 0.034 / 2;

  // Print the distance to the serial monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  delay(500);  // Wait for a short while before the next measurement
}


Comments

Popular posts from this blog

Arduino projects

1.Using an LDR (Light Dependent Resistor) with Arduino to Measure Light Intensity