{"id":16389899,"url":"https://github.com/mnishiguchi/nerves_hello_pwm","last_synced_at":"2026-06-12T23:31:36.699Z","repository":{"id":84540796,"uuid":"318362500","full_name":"mnishiguchi/nerves_hello_pwm","owner":"mnishiguchi","description":null,"archived":false,"fork":false,"pushed_at":"2020-12-04T14:06:16.000Z","size":14,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-03T13:47:08.085Z","etag":null,"topics":["elixir","nerves","pwm"],"latest_commit_sha":null,"homepage":"https://dev.to/mnishiguchi/elixir-nerves-pulse-width-modulation-pwm-for-led-mj2","language":"Elixir","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/mnishiguchi.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}},"created_at":"2020-12-04T00:58:34.000Z","updated_at":"2022-05-31T20:01:57.000Z","dependencies_parsed_at":"2023-03-05T14:30:39.445Z","dependency_job_id":null,"html_url":"https://github.com/mnishiguchi/nerves_hello_pwm","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mnishiguchi%2Fnerves_hello_pwm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mnishiguchi%2Fnerves_hello_pwm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mnishiguchi%2Fnerves_hello_pwm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mnishiguchi%2Fnerves_hello_pwm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mnishiguchi","download_url":"https://codeload.github.com/mnishiguchi/nerves_hello_pwm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240216791,"owners_count":19766651,"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":["elixir","nerves","pwm"],"created_at":"2024-10-11T04:34:30.771Z","updated_at":"2025-11-12T23:03:55.589Z","avatar_url":"https://github.com/mnishiguchi.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# NervesHelloPwm\n\nThis is a simple project that demonstrates Pulse Width Modulation (PWM) using GenServer.\n## Targets\n\nNerves applications produce images for hardware targets based on the\n`MIX_TARGET` environment variable. If `MIX_TARGET` is unset, `mix` builds an\nimage that runs on the host (e.g., your laptop). This is useful for executing\nlogic tests, running utilities, and debugging. Other targets are represented by\na short name like `rpi3` that maps to a Nerves system image for that platform.\nAll of this logic is in the generated `mix.exs` and may be customized. For more\ninformation about targets see:\n\nhttps://hexdocs.pm/nerves/targets.html#content\n\n## Getting Started\n\nTo start your Nerves app:\n  * `export MIX_TARGET=my_target` or prefix every command with\n    `MIX_TARGET=my_target`. For example, `MIX_TARGET=rpi3`\n  * Install dependencies with `mix deps.get`\n  * Create firmware with `mix firmware`\n  * Burn to an SD card with `mix firmware.burn`\n\n## Usage\n\nHere is an example of blinking an LED in various timings.\n\n### Handmade GenServer powered Pwm Scheduler\n\n```ex\n# A GPIO pin for an LED.\ngpio_pin = 12\n\n# Get a reference to the LED.\n{:ok, led_ref} = Circuits.GPIO.open(gpio_pin, :output)\n\n# Start a scheduler with on/off callback functions and initial settings for the\n# period (frequency in Hz and duty cycle in percentage).\nNervesHelloPwm.PwmScheduler.start_link(%{\n  id: gpio_pin,\n  frequency: 1,\n  duty_cycle: 50,\n  on_fn: fn -\u003e Circuits.GPIO.write(led_ref, 1)  end,\n  off_fn: fn -\u003e Circuits.GPIO.write(led_ref, 0) end\n})\n\n# Change the on/off ratio to 4:1.\nNervesHelloPwm.PwmScheduler.change_period(gpio_pin, 1, 80)\n\n# Change the frequency to 2Hz (2x faster than 1Hz).\nNervesHelloPwm.PwmScheduler.change_period(gpio_pin, 2, 80)\n\n# Stop the scheduler.\nNervesHelloPwm.PwmScheduler.stop(gpio_pin)\n** (EXIT from #PID\u003c0.1202.0\u003e) shell process exited with reason: shutdown\n```\n\nPWM scheduler processes are registered with an ID so it is possible to start\nmultiple PWM scheduler processes as long as IDs are unique.\n\nSince Elixir's `Process.send_after/3` function is millisecond precision,\nthere is a limitation on the pulse precision.\nSo `NervesHelloPwm.PwmScheduler` has the maximum frequency of 100Hz (10ms / period),\nwhich is probably fast enough to dim the brightness of an LED.\n\nIf you need faster and more precise PWM, please consider alternative approaches, such as accessing\nthe target device's built-in hardware PWM, using an external PWM driver board, etc.\n\n### Hardware PWM using the [tokafish/pigpiox](https://github.com/tokafish/pigpiox) library\n\n```\ngpio = 12\nfrequency = 800\nPigpiox.Pwm.hardware_pwm(gpio, frequency, 1_000_000) # 100%\nPigpiox.Pwm.hardware_pwm(gpio, frequency, 500_000)   # 50%\nPigpiox.Pwm.hardware_pwm(gpio, frequency, 100_000)   # 10%\nPigpiox.Pwm.hardware_pwm(gpio, frequency, 10_000)    # 1%\n```\n\n## Learn more\n\n  * Official docs: https://hexdocs.pm/nerves/getting-started.html\n  * Official website: https://nerves-project.org/\n  * Forum: https://elixirforum.com/c/nerves-forum\n  * Discussion Slack elixir-lang #nerves ([Invite](https://elixir-slackin.herokuapp.com/))\n  * Source: https://github.com/nerves-project/nerves\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmnishiguchi%2Fnerves_hello_pwm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmnishiguchi%2Fnerves_hello_pwm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmnishiguchi%2Fnerves_hello_pwm/lists"}