The MKE-S01 Ultrasonic Distance Sensor is an electronic device that measures the distance to an object by emitting ultrasonic waves and calculating the time it takes for the waves to reflect back from the object. This sensor is widely used in robotics, automotive parking sensors, obstacle avoidance systems, and various automation applications due to its non-contact measurement capability and high accuracy.
Pin Number | Pin Name | Description |
---|---|---|
1 | VCC | Power supply (5V DC) |
2 | Trig | Trigger input (TTL pulse) |
3 | Echo | Echo output (TTL level signal) |
4 | GND | Ground |
// Define sensor pins
const int trigPin = 9;
const int echoPin = 10;
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Define pin modes
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// Clear the trigPin by setting it LOW
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Set the trigPin HIGH for 10 microseconds to send the ultrasonic pulse
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin; pulseIn returns the duration in microseconds
long duration = pulseIn(echoPin, HIGH);
// Calculate the distance (in cm) based on the speed of sound (34300 cm/s)
long distance = duration * 0.0343 / 2;
// Print the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Delay between measurements
delay(1000);
}
Serial.print
function to debug and monitor the duration and distance values.Q: Can the sensor measure distances beyond 400 cm? A: The sensor is designed to measure up to 400 cm. Measurements beyond this range may be unreliable.
Q: Is the sensor waterproof? A: The MKE-S01 is not typically waterproof. Special care should be taken to avoid exposure to moisture.
Q: How can I increase the measurement angle of the sensor? A: The measurement angle is fixed due to the design of the sensor. To cover a wider area, use multiple sensors or mechanically rotate the sensor.
Q: Can the sensor be used with a 3.3V system? A: The sensor is rated for 5V operation. Using it with a 3.3V system may result in improper functioning. Use a level shifter if necessary.