{"id":28403670,"url":"https://github.com/alixahedi/srf0x-stm32","last_synced_at":"2025-09-21T21:42:25.817Z","repository":{"id":293879067,"uuid":"631037402","full_name":"alixahedi/SRF0X-STM32","owner":"alixahedi","description":"STM32 SRF0X Library","archived":false,"fork":false,"pushed_at":"2025-05-17T17:09:08.000Z","size":633,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-02T03:49:41.318Z","etag":null,"topics":["bluepill","digispark","distance-measures","lcd","srf","srf04","srf05","stm32","ultrasonic-sensor"],"latest_commit_sha":null,"homepage":"https://digispark.ir/running-srf05-with-stm32-f103c8t6-board/","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alixahedi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-04-21T19:02:30.000Z","updated_at":"2025-05-30T08:48:25.000Z","dependencies_parsed_at":"2025-05-17T18:20:50.132Z","dependency_job_id":"802f2a33-41f0-428f-bd41-6e3ada977cc6","html_url":"https://github.com/alixahedi/SRF0X-STM32","commit_stats":null,"previous_names":["alixahedi/srf05_stm32"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/alixahedi/SRF0X-STM32","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alixahedi%2FSRF0X-STM32","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alixahedi%2FSRF0X-STM32/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alixahedi%2FSRF0X-STM32/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alixahedi%2FSRF0X-STM32/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alixahedi","download_url":"https://codeload.github.com/alixahedi/SRF0X-STM32/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alixahedi%2FSRF0X-STM32/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262222458,"owners_count":23277435,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bluepill","digispark","distance-measures","lcd","srf","srf04","srf05","stm32","ultrasonic-sensor"],"created_at":"2025-06-01T18:38:16.641Z","updated_at":"2025-09-21T21:42:20.799Z","avatar_url":"https://github.com/alixahedi.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SRF0X STM32 Library\n\nA lightweight library for interfacing SRF04 and SRF05 ultrasonic distance sensors with STM32 microcontrollers using HAL drivers. Supports blocking measurements in microseconds and centimeters.\n\n## Features\n\n* **SRF04 \u0026 SRF05 Support**: Works with both popular ultrasonic modules.\n* **Microsecond Resolution**: Uses a 1 MHz timer for precise pulse-width measurement.\n* **Simple API**: Easy-to-use functions to trigger, read raw pulse width, and get distance in cm.\n* **Modular Handle**: Encapsulate per-sensor configuration in an `SRF0X_HandleTypeDef`.\n* **HAL-Based**: Leverages STM32Cube HAL; minimal external dependencies.\n\n## Table of Contents\n\n* [Getting Started](#getting-started)\n\n  * [Prerequisites](#prerequisites)\n  * [Installation](#installation)\n* [Usage](#usage)\n\n  * [Initialization](#initialization)\n  * [Measuring Distance](#measuring-distance)\n  * [Example](#example)\n* [API Reference](#api-reference)\n* [Contributing](#contributing)\n* [License](#license)\n\n## Getting Started\n\n### Prerequisites\n\n* **Hardware**:\n\n  * STM32 microcontroller with Cortex-M core\n  * SRF04 or SRF05 ultrasonic sensor\n  * Connections:\n\n    * *Trig* pin → any GPIO output\n    * *Echo* pin → any GPIO input\n    * Shared ground and VCC (5 V or 3.3 V as your sensor supports)\n* **Software**:\n\n  * STM32CubeIDE (or any toolchain with HAL libraries)\n  * C compiler supporting STM32 HAL\n\n### Installation\n\n1. **Clone or download** this repository:\n\n   ```bash\n   git clone https://github.com/alixahedi/SRF0X-STM32.git\n   ```\n2. **Copy source files** into your project:\n\n   * `srf0x.h` → your project's `Inc/` folder\n   * `srf0x.c` → your project's `Src/` folder\n3. **Include header** where needed:\n\n   ```c\n   #include \"srf0x.h\"\n   ```\n4. **Configure peripherals** as described below.\n\n## Usage\n\n### Initialization\n\n1. **Configure GPIOs** in CubeMX (or code):\n\n   * *TrigPin*: Output push-pull\n   * *EchoPin*: Input (no pull or pull-down as needed)\n\n2. **Configure a timer** (e.g., `TIM2`) for 1 MHz counter:\n\n   * Prescaler = `(HAL_RCC_GetPCLK1Freq()/1000000 - 1)`\n   * Counter mode = up\n\n3. **Enable DWT cycle counter** (optional, for 10 µs trigger delay):\n\n   ```c\n   CoreDebug-\u003eDEMCR |= CoreDebug_DEMCR_TRCENA_Msk;\n   DWT-\u003eCTRL    |= DWT_CTRL_CYCCNTENA_Msk;\n   ```\n\n4. **Fill the handle** and call `srf0x_init()`:\n\n   ```c\n   SRF0X_HandleTypeDef hsrfx;\n   hsrfx.TrigPort = GPIOA;\n   hsrfx.TrigPin  = GPIO_PIN_9;\n   hsrfx.EchoPort = GPIOA;\n   hsrfx.EchoPin  = GPIO_PIN_10;\n   hsrfx.htim     = \u0026htim2;  // must be 1 MHz timer\n\n   srf0x_init(\u0026hsrfx);\n   ```\n\n### Measuring Distance\n\n* **Get raw pulse width (µs)**:\n\n  ```c\n  uint32_t pulse_us = srf0x_read_us(\u0026hsrfx);\n  ```\n* **Get distance (cm)**:\n\n  ```c\n  float dist_cm = srf0x_read_cm(\u0026hsrfx);\n  ```\n\n### Example\n\n```c\n#include \"main.h\"\n#include \"srf0x.h\"\n\nextern TIM_HandleTypeDef htim2;\n\nint main(void)\n{\n    HAL_Init();\n    SystemClock_Config();\n    MX_GPIO_Init();\n    MX_TIM2_Init();\n    \n    // Enable DWT for microsecond delay\n    CoreDebug-\u003eDEMCR |= CoreDebug_DEMCR_TRCENA_Msk;\n    DWT-\u003eCTRL    |= DWT_CTRL_CYCCNTENA_Msk;\n    \n    // Sensor handle\n    SRF0X_HandleTypeDef sensor = {\n        .TrigPort = GPIOA,\n        .TrigPin  = GPIO_PIN_9,\n        .EchoPort = GPIOA,\n        .EchoPin  = GPIO_PIN_10,\n        .htim     = \u0026htim2\n    };\n    srf0x_init(\u0026sensor);\n    \n    while (1)\n    {\n        float distance = srf0x_read_cm(\u0026sensor);\n        // Use distance (e.g., print via UART or display)\n        HAL_Delay(100);\n    }\n}\n```\n\n## API Reference\n\n### `SRF0X_HandleTypeDef`\n\n| Member   | Type                  | Description                   |\n| -------- | --------------------- | ----------------------------- |\n| TrigPort | `GPIO_TypeDef *`      | GPIO port for trigger         |\n| TrigPin  | `uint16_t`            | GPIO pin number for trigger   |\n| EchoPort | `GPIO_TypeDef *`      | GPIO port for echo            |\n| EchoPin  | `uint16_t`            | GPIO pin number for echo      |\n| htim     | `TIM_HandleTypeDef *` | Pointer to 1 MHz timer handle |\n\n### Functions\n\n```c\nvoid srf0x_init(SRF0X_HandleTypeDef *hsrf);\n```\n\n* Initialize sensor handle (peripherals must be ready).\n\n```c\nvoid srf0x_trigger(SRF0X_HandleTypeDef *hsrf);\n```\n\n* Send a 10 µs pulse to trigger the sensor.\n\n```c\nuint32_t srf0x_read_us(SRF0X_HandleTypeDef *hsrf);\n```\n\n* Measure echo pulse width, returns microseconds.\n\n```c\nfloat srf0x_read_cm(SRF0X_HandleTypeDef *hsrf);\n```\n\n* Measure distance, returns centimeters (`pulse_us / 58.0f`).\n\n## Contributing\n\n1. Fork the repository\n2. Create a feature branch (`git checkout -b feature/NewFeature`)\n3. Commit your changes (`git commit -m \"Add NewFeature\"`)\n4. Push to branch (`git push origin feature/NewFeature`)\n5. Open a pull request\n\nPlease ensure code is formatted and documented in the same style.\n\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.\n\n## 📞 Contact\nFor questions or suggestions, feel free to reach out via email:\n📧 Alixahedi@gmail.com\n\n\u003cbr/\u003e\n\u003ca href=\"https://twitter.com/alixahedi\"\u003e\n\u003cimg align=\"left\" alt=\"Alix | Twitter\" width=\"22px\" src=\"https://github.com/alixahedi/alixahedi/blob/main/assests/img/social/Twitter.png\" /\u003e\n\u003c/a\u003e\n\u003ca href=\"https://www.linkedin.com/in/ali-zahedi-b5a360158//\"\u003e\n\u003cimg align=\"left\" alt=\"Ali's LinkedIN\" width=\"22px\" src=\"https://github.com/alixahedi/alixahedi/blob/main/assests/img/social/Linkedin.png\" /\u003e\n\u003c/a\u003e\n\u003ca href=\"https://www.facebook.com/Alixahedi/\"\u003e\n\u003cimg align=\"left\" alt=\"Ali's FaceBook\" width=\"22px\" src=\"https://github.com/alixahedi/alixahedi/blob/main/assests/img/social/fb.png\" /\u003e\n\u003c/a\u003e\n\u003ca href=\"https://www.instagram.com/Alixahedi\"\u003e\n\u003cimg align=\"left\" alt=\"Ali's Instagram\" width=\"22px\" src=\"https://github.com/alixahedi/alixahedi/blob/main/assests/img/social/insta.png\" /\u003e\n\u003c/a\u003e\n\u003cbr/\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falixahedi%2Fsrf0x-stm32","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falixahedi%2Fsrf0x-stm32","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falixahedi%2Fsrf0x-stm32/lists"}