Connect the HC-SR04 ultrasonic distance sensor to the Glyph ESP32-C6 board, send a trigger pulse, and measure distance using the echo response.
Interfacing an Ultrasonic Sensor with GLYPH-C6
Introduction
Ultrasonic sensors are electronic devices that calculate the target’s distance by emission of ultrasonic sound waves and convert those waves into electrical signals. The speed of emitted ultrasonic waves is faster than the audible sound.Working Principle of Ultrasonic sensorUltrasonic sensor working principle is either similar to SONAR or RADAR which evaluates the target/object attributes by understanding the received echoes from sound/radio waves correspondingly.This guide will help you interface an HC-SR04 Ultrasonic Sensor with a GLYPH-C6 board.The HC-SR04 has mainly two essential elements- the transmitter and receiver. Using the piezoelectric crystals, the transmitter generates sound, and from there it travels to the target and gets back to the receiver component. The HC-SR04 Sensor emits ultrasonic waves at a frequency of around 40,000 Hz (though the actual frequency may vary slightly). These waves travel through the air until they encounter an object in their path. Upon hitting the object, the waves bounce back to the sensor.By measuring the time, it takes for the ultrasonic pulse to travel to the object and return, and knowing the speed of sound in air (approximately 340 meters per second at room temperature), you can accurately calculate the distance between the sensor and the object.To initiate the ultrasound emission, we set the Trig pin to a High state for 10 microseconds. This action triggers the transmission of an 8-cycle ultrasonic burst, traveling at the speed of sound. Immediately after sending this burst, the Echo pin goes high, indicating its readiness to receive the reflected wave from any objects in the vicinity.If there’s no object or reflected pulse detected, the Echo pin will return to a low state after 38 milliseconds due to a timeout. If there is a reflected pulse, causes the Echo pin to transition to a low state before the 38-millisecond timeout. By measuring the duration for which the Echo pin remains HIGH, we can ascertain the distance traveled by the sound wave, and consequently, the distance from the sensor to the object.The formula for calculating distance using the HC-SR04 sensor is:Distance = (Time taken by the wave to return(T) x Speed of sound(S) ) / 2S corresponds to Sound speed = 343 measured in m/secThis formula divides the time by 2 because the wave travels from the sensor to the object and back again, so the total distance traveled is twice the distance between the sensor and the object.Here is a concise summary of its characteristics:
Power Supply: +5V DC
Quiescent Current: < 2mA
Working Current: 15mA
Effectual Angle: < 15
Ranging Distance: 2cm – 400 cm
Resolution: 0.3 cm
Measuring Angle: 30 degrees
Trigger Input Pulse width: 10uS TTL pulse
Echo Output Signal: TTL pulse proportional to the distance range
Dimensions: 45mm x 20mm x 15mm
You can interface the HC-SR04 sensor with microcontrollers like Arduino, Raspberry Pi, or any other microcontroller that supports GPIO (General Purpose Input Output). By reading the signal from the Echo pin, you can measure the time taken for the ultrasonic pulse to return, and hence calculate the distance using the formula mentioned above. The HC-SR04 ultrasonic sensor is widely used in applications owing to its precision in distance measurement using sound waves.Here’s a breakdown of some prevalent uses:-Robotics: Within robotics, it serves purposes such as obstacle detection, evasion, pathfinding, and environment mapping.-Speed and Direction Measurement: The sensor is adept at determining speed and direction between two entities.-Medical Appliances: It finds application in medical ultrasonography, offering a non-invasive means to examine the body.-Burglar Alarms: Integrating the sensor into security setups enables intruder detection.-Sonar Systems: Underwater, it aids sonar equipment in object detection.-Humidifiers: In the realm of humidifiers, it aids in gauging water levels.-Wireless Charging: The sensor plays a role in aligning devices for efficient wireless charging.-Non-Destructive Testing: Its utilization in inspecting materials and structures without causing harm proves invaluable.These applications capitalize on the sensor’s non-contact detection capabilities, delivering both safety and convenience across diverse domains. While the HC-SR04 Ultrasonic Sensor is a versatile and widely used component for distance measurement in various projects, it does have some limitations.Here are the constraints or limitations on its usage:-Limited Range: The HC-SR04 sensor has a maximum range typically around 2 meters, beyond which accuracy decreases.-Blind Spots: The sensor may struggle to detect objects with irregular shapes or surfaces that absorb sound waves, leading to blind spots in detection.-Angle Dependency: Accuracy may vary based on the angle of incidence of the sound waves, affecting the reliability of distance measurements.-Interference: Environmental factors such as noise and other ultrasonic sources can interfere with sensor readings, potentially leading to inaccuracies.-Temperature Sensitivity: The speed of sound in air, which the sensor relies on for distance calculation, can vary with temperature, affecting measurement accuracy.-Resolution: The sensor’s resolution may not be fine enough for applications requiring precise distance measurements, as it typically provides distance readings in centimeters.-Limited Functionality in Certain Conditions: HC-SR04 may encounter challenges in highly reflective or absorbent environments, affecting its performance.
Step 1: Hardware Required
Glyph Boards
HC-SR04 Ultrasonic Sensor
Jumper wires
Breadboard (optional)
Step 2: Circuit Diagram
Connect the HC-SR04 sensor to the GLYPH-C6 board using the following pin configurations:
VCC to 5V (Standard HC-SR04 requires 5V)
GND to GND
TRIG to GPIO 18
ECHO to GPIO 15
Step 3: Code Setup
Choose the code tab for your firmware below.
Arduino Code
CircuitPython Code
const int trigPin = 18;const int echoPin = 15;//define sound speed in cm/uS#define SOUND_SPEED 0.034#define CM_TO_INCH 0.393701long duration;float distanceCm;float distanceInch;void setup() { Serial.begin(115200); // Starts the serial communication pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input}void loop() { // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Calculate the distance distanceCm = duration * SOUND_SPEED/2; // Convert to inches distanceInch = distanceCm * CM_TO_INCH; // Prints the distance in the Serial Monitor Serial.print("Distance (cm): "); Serial.println(distanceCm); Serial.print("Distance (inch): "); Serial.println(distanceInch); delay(1000);}
The HC-SR04 ECHO pin sends a 5 V signal. Use a level shifter or a divider with 10 kΩ from ECHO to GPIO 15 and 10 kΩ from GPIO 15 to GND; connect TRIG to GPIO 18. Follow the CircuitPython setup guide, copy adafruit_hcsr04.mpy from the matching CircuitPython library bundle into CIRCUITPY/lib, and save this example as code.py.
Select the Board and PortDo the following settings in your Arduino IDE,
Tools > Board > esp32 > Pcbcupid GLYPH C6
For the Pcbcupid GLYPH C6 to appear under Tools > Board > esp32, the esp32 board version installed in the Arduino IDE should be greater or equal to 3.1.0.
Tools > Port and select the port connected to your GLYPH.
Tools > USB CDC on Boot >Enabled
If USB CDC on BOOT is not enabled, you won’t see any serial data in the Arduino IDE.
Upload the Code
Click the upload button (➡️ icon) or use the shortcut CTRL + U in Arduino IDE to upload the code to the board.
Step 5: Check the Output on Serial Monitor
The Serial Monitor will display the distance to the detected object in cm as well as inches
{"docsConfig":{"theme":"mint","$schema":"https://mintlify.com/docs.json","name":"PCB Cupid","colors":{"primary":"#0D9373"},"favicon":"https://mintcdn.com/pcbcupid-bae3761c/4dXNuJjZhqL5zqVd/images/favicon_PCBcupid.png?fit=max\u0026auto=format\u0026n=4dXNuJjZhqL5zqVd\u0026q=85\u0026s=697c4189e0a983e1722bddbedde79f61","appearance":{"strict":true},"background":{"decoration":"gradient"},"navbar":{"links":[{"href":"https://spell.pcbcupid.com","label":"Circuit Spell"},{"href":"https://shop.pcbcupid.com","label":"Shop"},{"href":"/","label":"Learn"},{"href":"https://events.pcbcupid.com","label":"Events"},{"href":"https://forum.pcbcupid.com","label":"Forum"},{"href":"https://pcbcupid.com/articles","label":"Articles"},{"href":"https://pcbcupid.com/tools","label":"Tools"}]},"seo":{"indexing":"all"},"fonts":{"family":"Space Grotesk","weight":400},"icons":{"library":"lucide"},"styling":{"eyebrows":"breadcrumbs","latex":true},"contextual":{"options":["copy","chatgpt","claude","cursor","vscode","mcp"],"display":"toc"},"thumbnails":{"appearance":"dark"},"navigation":{"pages":[]}},"deployment":{"deploymentMetadataValue":{"subdomain":"pcbcupid-bae3761c","hasAudioNarration":false,"actualSubdomain":"pcbcupid-bae3761c","customDomains":["learn.pcbcupid.com"],"gitSource":{"type":"github","owner":"pcbcupid","repo":"pcbcupid-docs","deployBranch":"main","contentDirectory":"","isPrivate":false,"mountPath":"","label":""},"feedback":{"thumbs":true,"edits":true,"issues":true},"entitlements":{"AI_CHAT":{"status":"DISABLED"}},"buildId":"6ae268427b458a5c875a9a81","clientVersion":"0.0.4177","searchFilterCounts":[{"language":"default","tag":"Documentation","count":77},{"language":"default","tag":"Modules","count":67},{"language":"default","tag":"PCB Design","count":45},{"language":"default","tag":"Examples","count":32},{"language":"default","tag":"Glyph","count":25},{"language":"default","tag":"G-Mod","count":17},{"language":"default","tag":"KiCad","count":16},{"language":"default","tag":"Basic","count":15},{"language":"default","tag":"Welcome","count":9},{"language":"default","tag":"G-Sense","count":8}]},"assistantConfig":{"enableAskAiButton":false,"enableStarterQuestions":false,"enableContextualStarterQuestions":false,"enableAskAiSearchBar":true},"basePath":"","hasChatPermissions":false,"hasWebMcpPermissions":false,"toggles":[{"name":"kill-isr","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"custom-rbac-roles","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"agent-mintlify-slack","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"agent-integrations-settings","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"rbac-expansion","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automations-pr-grouping","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"custom-prefix","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"analytics-v7","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"user-flows-analytics","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"no-publish-flow","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workos-onboarding","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"snapshot-serving-live","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"stripTemperatureMiddleware","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"support-platform-pylon-zendesk","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"snapshot-serving","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"instant-rollback","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"granular-notifications-server","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"granular-notifications","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automations-fable","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"fixed-ai-action-pricing","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automation-resolve-merge-conflict","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"scoped-publishing","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"mintql","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"page-level-analytics","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"editor-version-history-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"billing-page-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"full-page-search","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workspace-projects","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"assistant-v5","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"fork-pr-previews","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"revalidation-jwt","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workos-sso-scim","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"hobby-abuse-scan","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"dashboard-inbox","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workos-signup","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"turbopuffer-dual-write","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workos-enabled","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"approve-review-flow","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"editor-metadata","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"sso-setup-deadline-banner","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"turbopuffer-serve","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"live-agents","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"starter-pricing","enabled":true,"variant":{"name":"control","enabled":true,"payload":{"type":"string","value":"control"},"feature_enabled":true},"impressionData":false},{"name":"llms-txt-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false}],"googleFontsInlined":true},"customCss":[],"page":{"pagePath":"guides/basic-sensors/ultrasonic-sensor","slug":"/guides/basic-sensors/ultrasonic-sensor","pageMetadata":{"description":"Connect the HC-SR04 ultrasonic distance sensor to the Glyph ESP32-C6 board, send a trigger pulse, and measure distance using the echo response.","title":"HC-SR04 Ultrasonic Sensor","sidebarTitle":"Ultrasonic Sensor","href":"/guides/basic-sensors/ultrasonic-sensor"},"description":{"compiledSource":"\"use strict\";\nconst {jsx: _jsx} = arguments[0];\nconst {useMDXComponents: _provideComponents} = arguments[0];\nfunction _createMdxContent(props) {\n const _components = {\n p: \"p\",\n ..._provideComponents(),\n ...props.components\n };\n return _jsx(_components.p, {\n children: \"Connect the HC-SR04 ultrasonic distance sensor to the Glyph ESP32-C6 board, send a trigger pulse, and measure distance using the echo response.\"\n });\n}\nfunction MDXContent(props = {}) {\n const {wrapper: MDXLayout} = {\n ..._provideComponents(),\n ...props.components\n };\n return MDXLayout ? _jsx(MDXLayout, {\n ...props,\n children: _jsx(_createMdxContent, {\n ...props\n })\n }) : _createMdxContent(props);\n}\nreturn {\n default: MDXContent\n};\n","frontmatter":{},"scope":{}},"pageType":"default","mdx":{"mdxSource":{"compiledSource":"\"use strict\";const{Fragment:_Fragment,jsx:_jsx,jsxs:_jsxs}=arguments[0];const{useMDXComponents:_provideComponents}=arguments[0];function _createMdxContent(props){const _components={a:`a`,code:`code`,em:`em`,img:`img`,li:`li`,ol:`ol`,p:`p`,pre:`pre`,span:`span`,strong:`strong`,ul:`ul`,..._provideComponents(),...props.components},{CodeBlock,Heading,Tab,Tabs,Warning}=_components;if(!CodeBlock)_missingMdxReference(`CodeBlock`,true);if(!Heading)_missingMdxReference(`Heading`,true);if(!Tab)_missingMdxReference(`Tab`,true);if(!Tabs)_missingMdxReference(`Tabs`,true);if(!Warning)_missingMdxReference(`Warning`,true);return _jsxs(_Fragment,{children:[_jsx(Heading,{level:`1`,id:`interfacing-an-ultrasonic-sensor-with-glyph-c6`,children:`Interfacing an Ultrasonic Sensor with GLYPH-C6`}),`\n`,_jsx(Heading,{level:`2`,id:`introduction`,children:`Introduction`}),`\n`,_jsx(_components.p,{children:`Ultrasonic sensors are electronic devices that calculate the target’s distance by emission of ultrasonic sound waves and convert those waves into electrical signals. The speed of emitted ultrasonic waves is faster than the audible sound.`}),`\n`,_jsx(_components.p,{children:_jsx(_components.strong,{children:`Working Principle of Ultrasonic sensor`})}),`\n`,_jsx(_components.p,{children:`Ultrasonic sensor working principle is either similar to SONAR or RADAR which evaluates the target/object attributes by understanding the received echoes from sound/radio waves correspondingly.`}),`\n`,_jsxs(_components.p,{children:[`This guide will help you interface an `,_jsx(_components.strong,{children:`HC-SR04`}),` Ultrasonic Sensor with a GLYPH-C6 board.`]}),`\n`,_jsxs(_components.p,{children:[`The `,_jsx(_components.strong,{children:`HC-SR04`}),` has mainly two essential elements- the transmitter and receiver. Using the piezoelectric crystals, the transmitter generates sound, and from there it travels to the target and gets back to the receiver component. The `,_jsx(_components.strong,{children:`HC-SR04`}),` Sensor emits ultrasonic waves at a frequency of around 40,000 Hz (though the actual frequency may vary slightly). These waves travel through the air until they encounter an object in their path. Upon hitting the object, the waves bounce back to the sensor.`]}),`\n`,_jsx(_components.p,{children:`By measuring the time, it takes for the ultrasonic pulse to travel to the object and return, and knowing the speed of sound in air (approximately 340 meters per second at room temperature), you can accurately calculate the distance between the sensor and the object.`}),`\n`,_jsx(_components.p,{children:`To initiate the ultrasound emission, we set the Trig pin to a High state for 10 microseconds. This action triggers the transmission of an 8-cycle ultrasonic burst, traveling at the speed of sound. Immediately after sending this burst, the Echo pin goes high, indicating its readiness to receive the reflected wave from any objects in the vicinity.`}),`\n`,_jsx(_components.p,{children:`If there’s no object or reflected pulse detected, the Echo pin will return to a low state after 38 milliseconds due to a timeout. If there is a reflected pulse, causes the Echo pin to transition to a low state before the 38-millisecond timeout. By measuring the duration for which the Echo pin remains HIGH, we can ascertain the distance traveled by the sound wave, and consequently, the distance from the sensor to the object.`}),`\n`,_jsx(_components.p,{children:`The formula for calculating distance using the HC-SR04 sensor is:`}),`\n`,_jsx(_components.p,{children:`Distance = (Time taken by the wave to return(T) x Speed of sound(S) ) / 2`}),`\n`,_jsx(_components.p,{children:`S corresponds to Sound speed = 343 measured in m/sec`}),`\n`,_jsx(_components.p,{children:`This formula divides the time by 2 because the wave travels from the sensor to the object and back again, so the total distance traveled is twice the distance between the sensor and the object.`}),`\n`,_jsx(_components.p,{children:`Here is a concise summary of its characteristics:`}),`\n`,_jsxs(_components.ul,{children:[`\n`,_jsxs(_components.li,{children:[`\n`,_jsxs(_components.p,{children:[_jsx(_components.strong,{children:`Power Supply`}),`: +5V DC`]}),`\n`]}),`\n`,_jsxs(_components.li,{children:[`\n`,_jsxs(_components.p,{children:[_jsx(_components.strong,{children:`Quiescent Current`}),`: \u003c 2mA`]}),`\n`]}),`\n`,_jsxs(_components.li,{children:[`\n`,_jsxs(_components.p,{children:[_jsx(_components.strong,{children:`Working Current`}),`: 15mA`]}),`\n`]}),`\n`,_jsxs(_components.li,{children:[`\n`,_jsxs(_components.p,{children:[_jsx(_components.strong,{children:`Effectual Angle`}),`: \u003c 15`]}),`\n`]}),`\n`,_jsxs(_components.li,{children:[`\n`,_jsxs(_components.p,{children:[_jsx(_components.strong,{children:`Ranging Distance`}),`: 2cm – 400 cm`]}),`\n`]}),`\n`,_jsxs(_components.li,{children:[`\n`,_jsxs(_components.p,{children:[_jsx(_components.strong,{children:`Resolution`}),`: 0.3 cm`]}),`\n`]}),`\n`,_jsxs(_components.li,{children:[`\n`,_jsxs(_components.p,{children:[_jsx(_components.strong,{children:`Measuring Angle`}),`: 30 degrees`]}),`\n`]}),`\n`,_jsxs(_components.li,{children:[`\n`,_jsxs(_components.p,{children:[_jsx(_components.strong,{children:`Trigger Input Pulse width`}),`: 10uS TTL pulse`]}),`\n`]}),`\n`,_jsxs(_components.li,{children:[`\n`,_jsxs(_components.p,{children:[_jsx(_components.strong,{children:`Echo Output Signal`}),`: TTL pulse proportional to the distance range`]}),`\n`]}),`\n`,_jsxs(_components.li,{children:[`\n`,_jsxs(_components.p,{children:[_jsx(_components.strong,{children:`Dimensions`}),`: 45mm x 20mm x 15mm`]}),`\n`]}),`\n`]}),`\n`,_jsx(_components.p,{children:`You can interface the HC-SR04 sensor with microcontrollers like Arduino, Raspberry Pi, or any other microcontroller that supports GPIO (General Purpose Input Output). By reading the signal from the Echo pin, you can measure the time taken for the ultrasonic pulse to return, and hence calculate the distance using the formula mentioned above. The HC-SR04 ultrasonic sensor is widely used in applications owing to its precision in distance measurement using sound waves.`}),`\n`,_jsx(_components.p,{children:`Here’s a breakdown of some prevalent uses:`}),`\n`,_jsxs(_components.p,{children:[`-`,_jsx(_components.strong,{children:`Robotics`}),`: Within robotics, it serves purposes such as obstacle detection, evasion, pathfinding, and environment mapping.`]}),`\n`,_jsxs(_components.p,{children:[`-`,_jsx(_components.strong,{children:`Speed and Direction Measurement`}),`: The sensor is adept at determining speed and direction between two entities.`]}),`\n`,_jsxs(_components.p,{children:[`-`,_jsx(_components.strong,{children:`Medical Appliances`}),`: It finds application in medical ultrasonography, offering a non-invasive means to examine the body.`]}),`\n`,_jsxs(_components.p,{children:[`-`,_jsx(_components.strong,{children:`Burglar Alarms`}),`: Integrating the sensor into security setups enables intruder detection.`]}),`\n`,_jsxs(_components.p,{children:[`-`,_jsx(_components.strong,{children:`Sonar Systems`}),`: Underwater, it aids sonar equipment in object detection.`]}),`\n`,_jsxs(_components.p,{children:[`-`,_jsx(_components.strong,{children:`Humidifiers`}),`: In the realm of humidifiers, it aids in gauging water levels.`]}),`\n`,_jsxs(_components.p,{children:[`-`,_jsx(_components.strong,{children:`Wireless Charging`}),`: The sensor plays a role in aligning devices for efficient wireless charging.`]}),`\n`,_jsxs(_components.p,{children:[`-`,_jsx(_components.strong,{children:`Non-Destructive Testing`}),`: Its utilization in inspecting materials and structures without causing harm proves invaluable.`]}),`\n`,_jsx(_components.p,{children:`These applications capitalize on the sensor’s non-contact detection capabilities, delivering both safety and convenience across diverse domains. While the HC-SR04 Ultrasonic Sensor is a versatile and widely used component for distance measurement in various projects, it does have some limitations.`}),`\n`,_jsx(_components.p,{children:`Here are the constraints or limitations on its usage:`}),`\n`,_jsxs(_components.p,{children:[`-`,_jsx(_components.strong,{children:`Limited Range`}),`: The HC-SR04 sensor has a maximum range typically around 2 meters, beyond which accuracy decreases.`]}),`\n`,_jsxs(_components.p,{children:[`-`,_jsx(_components.strong,{children:`Blind Spots`}),`: The sensor may struggle to detect objects with irregular shapes or surfaces that absorb sound waves, leading to blind spots in detection.`]}),`\n`,_jsxs(_components.p,{children:[`-`,_jsx(_components.strong,{children:`Angle Dependency`}),`: Accuracy may vary based on the angle of incidence of the sound waves, affecting the reliability of distance measurements.`]}),`\n`,_jsxs(_components.p,{children:[`-`,_jsx(_components.strong,{children:`Interference`}),`: Environmental factors such as noise and other ultrasonic sources can interfere with sensor readings, potentially leading to inaccuracies.`]}),`\n`,_jsxs(_components.p,{children:[`-`,_jsx(_components.strong,{children:`Temperature Sensitivity`}),`: The speed of sound in air, which the sensor relies on for distance calculation, can vary with temperature, affecting measurement accuracy.`]}),`\n`,_jsxs(_components.p,{children:[`-`,_jsx(_components.strong,{children:`Resolution`}),`: The sensor’s resolution may not be fine enough for applications requiring precise distance measurements, as it typically provides distance readings in centimeters.`]}),`\n`,_jsxs(_components.p,{children:[`-`,_jsx(_components.strong,{children:`Limited Functionality in Certain Conditions`}),`: HC-SR04 may encounter challenges in highly reflective or absorbent environments, affecting its performance.`]}),`\n`,_jsx(Heading,{level:`2`,id:`step-1-hardware-required`,children:`Step 1: Hardware Required`}),`\n`,_jsxs(_components.ul,{children:[`\n`,_jsx(_components.li,{children:`Glyph Boards`}),`\n`,_jsx(_components.li,{children:`HC-SR04 Ultrasonic Sensor`}),`\n`,_jsx(_components.li,{children:`Jumper wires`}),`\n`,_jsx(_components.li,{children:`Breadboard (optional)`}),`\n`]}),`\n`,_jsx(Heading,{level:`2`,id:`step-2-circuit-diagram`,children:`Step 2: Circuit Diagram`}),`\n`,_jsxs(_components.p,{children:[`Connect the `,_jsx(_components.strong,{children:`HC-SR04`}),` sensor to the GLYPH-C6 board using the following pin configurations:`]}),`\n`,_jsxs(_components.ul,{children:[`\n`,_jsxs(_components.li,{children:[_jsx(_components.strong,{children:`VCC`}),` to 5V (Standard HC-SR04 requires 5V)`]}),`\n`,_jsxs(_components.li,{children:[_jsx(_components.strong,{children:`GND`}),` to GND`]}),`\n`,_jsxs(_components.li,{children:[_jsx(_components.strong,{children:`TRIG`}),` to GPIO 18`]}),`\n`,_jsxs(_components.li,{children:[_jsx(_components.strong,{children:`ECHO`}),` to GPIO 15`]}),`\n`]}),`\n`,_jsx(_components.p,{children:_jsx(_components.img,{src:`https://files.pcbcupid.com/Documentation/Boards/Examples/ultrasonic_sensor/ultrasonic%20sensor%20circuit%20diagram/ultrasonicsensor_circuitdiagram.jpg`,alt:`pcbcupid_ultrasaonic_sensor`})}),`\n`,_jsx(Heading,{level:`2`,id:`step-3-code-setup`,children:`Step 3: Code Setup`}),`\n`,_jsx(_components.p,{children:`Choose the code tab for your firmware below.`}),`\n`,_jsxs(Tabs,{children:[_jsx(Tab,{title:`Arduino Code`,id:`arduino-code`,children:_jsx(CodeBlock,{filename:``,numberOfLines:`46`,language:`cpp`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`cpp`,children:_jsxs(_components.code,{language:`cpp`,numberOfLines:`46`,children:[_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`const`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` int`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` trigPin `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:` 18`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`;`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`const`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` int`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` echoPin `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:` 15`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`;`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`//define sound speed in cm/uS`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`#define`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#569CD6`},children:` SOUND_SPEED`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:` 0.034`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`#define`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#569CD6`},children:` CM_TO_INCH`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:` 0.393701`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`long`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` duration;`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`float`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` distanceCm;`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`float`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` distanceInch;`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`void`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` setup`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`() `})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`{`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:` Serial`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`.`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:`begin`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`115200`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`);`}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:` // Starts the serial communication`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` pinMode`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(trigPin, OUTPUT);`}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:` // Sets the trigPin as an Output`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` pinMode`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(echoPin, INPUT);`}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:` // Sets the echoPin as an Input`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`}`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`void`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` loop`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`() `})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`{`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:` // Clears the trigPin`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` digitalWrite`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(trigPin, LOW);`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` delayMicroseconds`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`2`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`);`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:` // Sets the trigPin on HIGH state for 10 micro seconds`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` digitalWrite`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(trigPin, HIGH);`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` delayMicroseconds`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`10`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`);`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` digitalWrite`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(trigPin, LOW);`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` `})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:` // Reads the echoPin, returns the sound wave travel time in microseconds`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` duration `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` pulseIn`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(echoPin, HIGH);`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` `})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:` // Calculate the distance`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` distanceCm `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` duration `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`*`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` SOUND_SPEED`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`/`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`2`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`;`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` `})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:` // Convert to inches`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` distanceInch `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` distanceCm `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`*`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` CM_TO_INCH;`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` `})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:` // Prints the distance in the Serial Monitor`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:` Serial`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`.`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:`print`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"Distance (cm): \"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`);`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:` Serial`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`.`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:`println`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(distanceCm);`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:` Serial`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`.`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:`print`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"Distance (inch): \"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`);`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:` Serial`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`.`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:`println`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(distanceInch);`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` `})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` delay`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`1000`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`);`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`}`})}),`\n`]})})})}),_jsxs(Tab,{title:`CircuitPython Code`,id:`circuitpython-code`,children:[_jsxs(_components.p,{children:[`The HC-SR04 ECHO pin sends a 5 V signal. Use a level shifter or a divider with `,_jsx(_components.strong,{children:`10 kΩ from ECHO to GPIO 15 and 10 kΩ from GPIO 15 to GND`}),`; connect TRIG to GPIO 18. Follow the `,_jsx(_components.a,{href:`/documentation/welcome/prerequisites/circuitpython-setup-guide`,children:`CircuitPython setup guide`}),`, copy `,_jsx(_components.code,{children:`adafruit_hcsr04.mpy`}),` from the matching `,_jsx(_components.a,{href:`https://circuitpython.org/libraries`,children:`CircuitPython library bundle`}),` into `,_jsx(_components.code,{children:`CIRCUITPY/lib`}),`, and save this example as `,_jsx(_components.code,{children:`code.py`}),`.`]}),_jsx(CodeBlock,{filename:``,numberOfLines:`17`,language:`python`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`python`,children:_jsxs(_components.code,{language:`python`,numberOfLines:`17`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`import`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` time`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`import`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` microcontroller`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`import`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` adafruit_hcsr04`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`sonar `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` adafruit_hcsr04.HCSR04(`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:` trigger_pin`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`microcontroller.pin.`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`GPIO18`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`,`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:` echo_pin`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`microcontroller.pin.`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#D4D4D4`},children:`GPIO15`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`,`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`)`})}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`while`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#569CD6`},children:` True`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`:`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:` try`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`:`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` distance_cm `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` sonar.distance`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:` print`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"Distance (cm):\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:`round`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(distance_cm, `}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`2`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`))`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:` print`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"Distance (inch):\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:`round`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(distance_cm `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`*`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:` 0.393701`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`2`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`))`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:` except`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#4EC9B0`},children:` RuntimeError`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`:`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:` print`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"No echo; check the sensor or move the target closer.\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`)`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` time.sleep(`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`1`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`)`})]}),`\n`]})})})]})]}),`\n`,_jsx(Heading,{level:`2`,id:`step-4-upload-the-code`,children:`Step 4: Upload the Code`}),`\n`,_jsxs(_components.ol,{children:[`\n`,_jsx(_components.li,{children:_jsx(_components.strong,{children:`Connect the Board`})}),`\n`]}),`\n`,_jsxs(_components.ul,{children:[`\n`,_jsx(_components.li,{children:`Connect your GLYPH board to your computer`}),`\n`]}),`\n`,_jsxs(_components.ol,{start:`2`,children:[`\n`,_jsxs(_components.li,{children:[`\n`,_jsx(_components.p,{children:_jsx(_components.strong,{children:`Select the Board and Port`})}),`\n`,_jsx(_components.p,{children:`Do the following settings in your Arduino IDE,`}),`\n`,_jsxs(_components.ul,{children:[`\n`,_jsx(_components.li,{children:_jsx(_components.code,{children:`Tools > Board > esp32 > Pcbcupid GLYPH C6`})}),`\n`]}),`\n`]}),`\n`]}),`\n`,_jsx(Warning,{children:_jsxs(_components.p,{children:[`For the `,_jsx(_components.code,{children:`Pcbcupid GLYPH C6`}),` to appear under `,_jsx(_components.code,{children:`Tools > Board > esp32`}),`, the esp32 board version installed in the Arduino IDE should be greater or equal to 3.1.0.`]})}),`\n`,_jsxs(_components.ul,{children:[`\n`,_jsxs(_components.li,{children:[_jsx(_components.code,{children:`Tools > Port`}),` and select the port connected to your GLYPH.`]}),`\n`,_jsxs(_components.li,{children:[_jsx(_components.code,{children:`Tools > USB CDC on Boot >`}),` `,_jsx(_components.em,{children:_jsx(_components.strong,{children:`Enabled`})})]}),`\n`]}),`\n`,_jsx(Warning,{children:_jsxs(_components.p,{children:[`If `,_jsx(_components.code,{children:`USB CDC on BOOT`}),` is not enabled, you won’t see any serial data in the Arduino IDE.`]})}),`\n`,_jsxs(_components.ol,{start:`3`,children:[`\n`,_jsx(_components.li,{children:_jsx(_components.strong,{children:`Upload the Code`})}),`\n`]}),`\n`,_jsxs(_components.ul,{children:[`\n`,_jsxs(_components.li,{children:[`Click the upload button (➡️ icon) or use the shortcut `,_jsx(_components.code,{children:`CTRL + U`}),` in Arduino IDE to upload the code to the board.`]}),`\n`]}),`\n`,_jsx(Heading,{level:`2`,id:`step-5-check-the-output-on-serial-monitor`,children:`Step 5: Check the Output on Serial Monitor`}),`\n`,_jsx(_components.p,{children:`The Serial Monitor will display the distance to the detected object in cm as well as inches`}),`\n`,_jsx(_components.p,{children:_jsx(_components.img,{src:`https://files.pcbcupid.com/Documentation/Boards/Examples/ultrasonic_sensor/ultrasonic_sensor_serialmon_glyph.avif`,alt:`pcbcupid_ultrasonic_serial_output`})})]})}function MDXContent(props={}){const{wrapper:MDXLayout}={..._provideComponents(),...props.components};return MDXLayout?_jsx(MDXLayout,{...props,children:_jsx(_createMdxContent,{...props})}):_createMdxContent(props)}return{default:MDXContent};function _missingMdxReference(id,component){throw new Error(`Expected `+(component?`component`:`object`)+\" `\"+id+\"` to be defined: you likely forgot to import, pass, or provide it.\")}","frontmatter":{},"scope":{"config":{},"pageMetadata":{"description":"Connect the HC-SR04 ultrasonic distance sensor to the Glyph ESP32-C6 board, send a trigger pulse, and measure distance using the echo response.","title":"HC-SR04 Ultrasonic Sensor","sidebarTitle":"Ultrasonic Sensor","href":"/guides/basic-sensors/ultrasonic-sensor"}}},"mdxSourceWithNoJs":null},"mdxExtracts":{"tableOfContents":[{"title":"Interfacing an Ultrasonic Sensor with GLYPH-C6","slug":"interfacing-an-ultrasonic-sensor-with-glyph-c6","depth":1,"children":[]},{"title":"Introduction","slug":"introduction","depth":2,"children":[]},{"title":"Step 1: Hardware Required","slug":"step-1-hardware-required","depth":2,"children":[]},{"title":"Step 2: Circuit Diagram","slug":"step-2-circuit-diagram","depth":2,"children":[]},{"title":"Step 3: Code Setup","slug":"step-3-code-setup","depth":2,"children":[]},{"title":"Step 4: Upload the Code","slug":"step-4-upload-the-code","depth":2,"children":[]},{"title":"Step 5: Check the Output on Serial Monitor","slug":"step-5-check-the-output-on-serial-monitor","depth":2,"children":[]}],"codeExamples":{}},"jsLinks":[],"subdomain":"pcbcupid-bae3761c","siteName":"PCB Cupid","title":"HC-SR04 Ultrasonic Sensor - PCB Cupid","navMaps":{"firstHrefInVersion":{},"firstHrefInLanguage":{}},"lastModified":"2026-10-17T08:18:02.761Z","originalFileLocation":"guides/basic-sensors/ultrasonic-sensor.mdx"},"headHtml":"\u003cscript data-mint-head>(function(){var d=document.documentElement,p=null;if(!p){try{p=localStorage.getItem(\"isDarkMode\")}catch(e){}if(p==='true')p='dark';else if(p==='false')p='light';if(p!=='light'\u0026\u0026p!=='dark'\u0026\u0026p!=='system')p=\"system\"}var r=p==='system'?(matchMedia(\"(prefers-color-scheme: dark)\").matches?'dark':'light'):p;d.classList.remove(r==='dark'?'light':'dark');d.classList.add(r);d.setAttribute(\"data-theme-preference\",p)})();\u003c/script>\u003cmeta data-mint-head name=\"description\" content=\"Connect the HC-SR04 ultrasonic distance sensor to the Glyph ESP32-C6 board, send a trigger pulse, and measure distance using the echo response.\">\u003cmeta data-mint-head name=\"application-name\" content=\"PCB Cupid\">\u003cmeta data-mint-head name=\"generator\" content=\"Mintlify\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"192x192\" href=\"/mintlify-assets/_mintlify/favicons/pcbcupid-bae3761c/TFd0Eu1csuxrLpjH/_generated/favicon/android-chrome-192x192.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"apple-touch-icon\" type=\"image/png\" sizes=\"180x180\" href=\"/mintlify-assets/_mintlify/favicons/pcbcupid-bae3761c/TFd0Eu1csuxrLpjH/_generated/favicon/apple-touch-icon.png\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"/mintlify-assets/_mintlify/favicons/pcbcupid-bae3761c/TFd0Eu1csuxrLpjH/_generated/favicon/favicon-16x16.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/mintlify-assets/_mintlify/favicons/pcbcupid-bae3761c/TFd0Eu1csuxrLpjH/_generated/favicon/favicon-32x32.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/mintlify-assets/_mintlify/favicons/pcbcupid-bae3761c/TFd0Eu1csuxrLpjH/_generated/favicon/favicon.ico\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"192x192\" href=\"/mintlify-assets/_mintlify/favicons/pcbcupid-bae3761c/TFd0Eu1csuxrLpjH/_generated/favicon-dark/android-chrome-192x192.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"/mintlify-assets/_mintlify/favicons/pcbcupid-bae3761c/TFd0Eu1csuxrLpjH/_generated/favicon-dark/favicon-16x16.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/mintlify-assets/_mintlify/favicons/pcbcupid-bae3761c/TFd0Eu1csuxrLpjH/_generated/favicon-dark/favicon-32x32.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/mintlify-assets/_mintlify/favicons/pcbcupid-bae3761c/TFd0Eu1csuxrLpjH/_generated/favicon-dark/favicon.ico\" media=\"(prefers-color-scheme: dark)\">\u003cmeta data-mint-head property=\"og:title\" content=\"HC-SR04 Ultrasonic Sensor - PCB Cupid\">\u003cmeta data-mint-head property=\"og:description\" content=\"Connect the HC-SR04 ultrasonic distance sensor to the Glyph ESP32-C6 board, send a trigger pulse, and measure distance using the echo response.\">\u003cmeta data-mint-head property=\"og:site_name\" content=\"PCB Cupid\">\u003cmeta data-mint-head property=\"og:url\" content=\"https://learn.pcbcupid.com/guides/basic-sensors/ultrasonic-sensor\">\u003cmeta data-mint-head property=\"og:type\" content=\"website\">\u003cmeta data-mint-head property=\"og:image\" content=\"https://pcbcupid-bae3761c.mintlify.site/_mintlify/api/og/v2?division=Basic+Sensors\u0026amp;title=HC-SR04+Ultrasonic+Sensor\u0026amp;description=Connect+the+HC-SR04+ultrasonic+distance+sensor+to+the+Glyph+ESP32-C6+board%2C+send+a+trigger+pulse%2C+and+measure+distance+using+the+echo+response.\u0026amp;theme=2c7e3818cca0f9e7f4434aa9\u0026amp;signature=5871ee8e80236a9e698d6ab34d1a6f9f981b65ffca61ecb6c79a959edf710219\">\u003cmeta data-mint-head property=\"og:image:width\" content=\"1200\">\u003cmeta data-mint-head property=\"og:image:height\" content=\"630\">\u003cmeta data-mint-head name=\"twitter:card\" content=\"summary_large_image\">\u003cmeta data-mint-head name=\"twitter:title\" content=\"HC-SR04 Ultrasonic Sensor - PCB Cupid\">\u003cmeta data-mint-head name=\"twitter:description\" content=\"Connect the HC-SR04 ultrasonic distance sensor to the Glyph ESP32-C6 board, send a trigger pulse, and measure distance using the echo response.\">\u003cmeta data-mint-head name=\"twitter:image\" content=\"https://pcbcupid-bae3761c.mintlify.site/_mintlify/api/og/v2?division=Basic+Sensors\u0026amp;title=HC-SR04+Ultrasonic+Sensor\u0026amp;description=Connect+the+HC-SR04+ultrasonic+distance+sensor+to+the+Glyph+ESP32-C6+board%2C+send+a+trigger+pulse%2C+and+measure+distance+using+the+echo+response.\u0026amp;theme=2c7e3818cca0f9e7f4434aa9\u0026amp;signature=5871ee8e80236a9e698d6ab34d1a6f9f981b65ffca61ecb6c79a959edf710219\">\u003cmeta data-mint-head name=\"twitter:image:width\" content=\"1200\">\u003cmeta data-mint-head name=\"twitter:image:height\" content=\"630\">\u003cmeta data-mint-head name=\"msapplication-config\" content=\"/mintlify-assets/_mintlify/favicons/pcbcupid-bae3761c/TFd0Eu1csuxrLpjH/_generated/favicon/browserconfig.xml\">\u003cmeta data-mint-head name=\"apple-mobile-web-app-title\" content=\"PCB Cupid\">\u003cmeta data-mint-head name=\"msapplication-TileColor\" content=\"#0D9373\">\u003clink data-mint-head rel=\"canonical\" href=\"https://learn.pcbcupid.com/guides/basic-sensors/ultrasonic-sensor\">\u003clink data-mint-head rel=\"alternate\" type=\"application/xml\" href=\"/sitemap.xml\">\u003clink data-mint-head rel=\"alternate\" type=\"text/markdown\" href=\"/guides/basic-sensors/ultrasonic-sensor.md\">\u003cscript data-mint-head type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https://learn.pcbcupid.com/#organization\",\"name\":\"PCB Cupid\",\"url\":\"https://learn.pcbcupid.com\"},{\"@type\":\"WebSite\",\"@id\":\"https://learn.pcbcupid.com#website\",\"name\":\"PCB Cupid\",\"url\":\"https://learn.pcbcupid.com\",\"publisher\":{\"@id\":\"https://learn.pcbcupid.com/#organization\"}},{\"@type\":\"WebPage\",\"@id\":\"https://learn.pcbcupid.com/guides/basic-sensors/ultrasonic-sensor#webpage\",\"url\":\"https://learn.pcbcupid.com/guides/basic-sensors/ultrasonic-sensor\",\"name\":\"HC-SR04 Ultrasonic Sensor\",\"description\":\"Connect the HC-SR04 ultrasonic distance sensor to the Glyph ESP32-C6 board, send a trigger pulse, and measure distance using the echo response.\",\"dateModified\":\"2026-10-17T08:18:02.761Z\",\"isPartOf\":{\"@id\":\"https://learn.pcbcupid.com#website\"},\"breadcrumb\":{\"@id\":\"https://learn.pcbcupid.com/guides/basic-sensors/ultrasonic-sensor#breadcrumb\"}},{\"@type\":\"BreadcrumbList\",\"@id\":\"https://learn.pcbcupid.com/guides/basic-sensors/ultrasonic-sensor#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"PCB Cupid\",\"item\":\"https://learn.pcbcupid.com\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"HC-SR04 Ultrasonic Sensor\",\"item\":\"https://learn.pcbcupid.com/guides/basic-sensors/ultrasonic-sensor\"}]},{\"@type\":[\"Article\",\"TechArticle\"],\"@id\":\"https://learn.pcbcupid.com/guides/basic-sensors/ultrasonic-sensor#article\",\"headline\":\"HC-SR04 Ultrasonic Sensor\",\"name\":\"HC-SR04 Ultrasonic Sensor\",\"description\":\"Connect the HC-SR04 ultrasonic distance sensor to the Glyph ESP32-C6 board, send a trigger pulse, and measure distance using the echo response.\",\"url\":\"https://learn.pcbcupid.com/guides/basic-sensors/ultrasonic-sensor\",\"mainEntityOfPage\":{\"@id\":\"https://learn.pcbcupid.com/guides/basic-sensors/ultrasonic-sensor#webpage\"},\"dateModified\":\"2026-10-17T08:18:02.761Z\",\"publisher\":{\"@id\":\"https://learn.pcbcupid.com/#organization\"},\"isPartOf\":{\"@id\":\"https://learn.pcbcupid.com#website\"}}]}\u003c/script>\u003cscript data-mint-head type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@type\":\"WebSite\",\"name\":\"PCB Cupid\",\"publisher\":{\"@type\":\"Organization\",\"name\":\"PCB Cupid\"},\"provider\":{\"@type\":\"Organization\",\"name\":\"Mintlify\",\"url\":\"https://mintlify.com\"}}\u003c/script>","agentPointerHref":"/guides/basic-sensors/ultrasonic-sensor.md","siteName":"PCB Cupid","nav":{"global":null,"tabs":[{"tab":"Documentation","pages":[{"title":"Documentation","href":""}]},{"tab":"Examples","pages":[{"group":"Basic","pages":[{"title":"Blink LED","sidebarTitle":"Blink LED","href":"/guides/arduino-ide-blink-led"},{"title":"Analog Input and Output","sidebarTitle":"Reading Analog Input","href":"/guides/reading-analoginput"},{"title":"Battery Voltage Measurement","sidebarTitle":"Battery Measurement","href":"/guides/battery-measurement"},{"title":"I2C Scanner","sidebarTitle":"I2C Scanner","href":"/guides/i2c-scan-test"},{"title":"BH1750 Adafruit Sensor","sidebarTitle":"BH1750 Sensor","href":"/guides/bh1750adafruit-compatibility"},{"title":"Parallel LCD Display","sidebarTitle":"Parallel LCD","href":"/guides/lcd"},{"title":"I2C LCD Display","sidebarTitle":"I2C LCD","href":"/guides/lcd-i2c"},{"title":"Rotary Encoder with Glyph ESP32-C6","sidebarTitle":"Rotary Encoder","href":"/guides/rotor-encoder-with-glyph-c6"},{"title":"Servo Motor Control with Glyph C6","sidebarTitle":"Servo Motor","href":"/guides/servo-motor-with-glyph-c6"},{"title":"Stepper Motor","sidebarTitle":"Stepper Motor","href":"/guides/stepper-motor-with-drv8833"},{"title":"OpenWeatherMap REST API","sidebarTitle":"REST API Weather","href":"/guides/temperature-monitoring-using-glyph"},{"title":"OLED Display Test","sidebarTitle":"OLED Display","href":"/guides/display-test"},{"title":"REST API Weather on SH1106 OLED","sidebarTitle":"REST API Weather with OLED","href":"/guides/temperaturemonitoring-glyph+sh1106oled"},{"title":"DHT Sensor One-Wire Communication","sidebarTitle":"DHT Sensor One-Wire","href":"/guides/dht-onewire-communication"},{"title":"Unipolar Stepper Motor (28BYJ-48)","sidebarTitle":"Unipolar Stepper Motor","href":"/guides/unipolar-stepper-motor"}]},{"group":"Kits","pages":[{"title":"Beginner Kit with GLYPH-C6","sidebarTitle":"Beginner Kit","href":"/guides/kits/beginner-kit"},{"title":"GLYPH Voice Kit","sidebarTitle":"GLYPH Voice Kit","href":"/guides/kits/glyph-voice"},{"title":"GLYPH MIDI Controller Kit","sidebarTitle":"GLYPH MIDI Controller","href":"/guides/kits/glyph-midi-controller"},{"title":"GLYPH Tank Kit","sidebarTitle":"GLYPH Tank Kit","href":"/guides/kits/glyph-tank"}]},{"group":"AI","pages":[{"title":"ESP32-C3 Voice Chatbot","sidebarTitle":"Voice Chatbot","href":"/guides/ai/esp32-c3-voice-chatbot"},{"title":"Z-Claw AI","sidebarTitle":"Z-Claw AI","href":"/guides/ai/z-claw"},{"description":"Train and deploy a gesture recognition model on ESP32 boards using Edge Impulse, covering data collection, model training, and on-device inference.","title":"Edge Impulse Gesture Recognition","sidebarTitle":"Edge Impulse","href":"/guides/ai/edge-impulse"}]},{"group":"Basic Sensors","pages":[{"title":"HC-SR04 Ultrasonic Sensor","sidebarTitle":"Ultrasonic Sensor","href":"/guides/basic-sensors/ultrasonic-sensor"},{"description":"Wire a flame sensor to a Glyph ESP32 board and read analog input values to detect open flames or infrared light sources for fire safety projects.","title":"Flame Sensor Test","sidebarTitle":"Flame Sensor","href":"/guides/basic-sensors/flame-sensor"},{"title":"IR Sensor","sidebarTitle":"IR Sensor","href":"/guides/basic-sensors/infrared-sensor"},{"title":"LDR Light Sensor","sidebarTitle":"LDR Light Sensor","href":"/guides/basic-sensors/ldr"},{"title":"MQ2 Gas Sensor","sidebarTitle":"MQ2 Gas Sensor","href":"/guides/basic-sensors/mq2-sensor"},{"title":"Touch Sensor (TTP223)","sidebarTitle":"Touch Sensor","href":"/guides/basic-sensors/touch-sensor"}]},{"group":"Bluetooth","pages":[{"title":"BLE Server Demo","sidebarTitle":"BLE Server","href":"/guides/bluetooth/bluetoothglyph"}]},{"group":"WiFi","pages":[{"title":"Control a Dual Relay over Wi-Fi","sidebarTitle":"Wi-Fi Dual Relay","href":"/guides/wifi/local-webserver-to-control-relay"},{"title":"Create a Wi-Fi Access Point on Glyph","sidebarTitle":"Wi-Fi Access Point","href":"/guides/wifi/wifiaccess-point"}]},{"group":"Zigbee","pages":[{"title":"Arduino Zigbee Blink: C6 to H2","sidebarTitle":"Zigbee Blink","href":"/guides/zigbee/arduino-zigbee-blink"}]}]},{"tab":"PCB Design","pages":[{"title":"PCB Design","href":"/pcb-design/design/kicad/introduction-on-kicad"}]}]},"basePath":"","title":"HC-SR04 Ultrasonic Sensor - PCB Cupid","deploymentId":"dpl_HhUetB7eHHDAmK9T1Teip38TgFx9","buildId":"dpl_HhUetB7eHHDAmK9T1Teip38TgFx9","dynamics":["a74be98ba7"],"chunks":["zoom-image-lightbox-DQG3o8Eu.js","lightbox-handle-r17bVUz3.js"]}