Skip to content

Commit

Permalink
samples: Add blinky tone
Browse files Browse the repository at this point in the history
- Use `tone` and `noTone` functions to blink led at 1hz with duty cycle
  50% on any digital pin

Signed-off-by: Ayush Singh <ayushdevel1325@gmail.com>
  • Loading branch information
Ayush1325 committed Jun 6, 2024
1 parent f5d1b9e commit bda0532
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
13 changes: 13 additions & 0 deletions samples/blinky_tone/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)

cmake_path(SET ZephyrBase $ENV{ZEPHYR_BASE})
set(DTC_OVERLAY_FILE ${ZephyrBase}/../modules/lib/Arduino-Zephyr-API/variants/${BOARD}/${BOARD}.overlay)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(blinky)

target_sources(app PRIVATE src/main.cpp)

zephyr_compile_options(-Wno-unused-variable -Wno-comment)
71 changes: 71 additions & 0 deletions samples/blinky_tone/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
.. _blinky-tone-sample:

Blinky Tone
############

Overview
********

This Arduino Blinky sample blinks an LED forever using the `tone` and `noTone`.

Requirements
************

Your board must:

#. Have an LED connected via a GPIO pin (these are called "User LEDs" on many of
Zephyr's :ref:`boards`).
#. Have the LED configured using the ``led0`` devicetree alias.

Building and Running
********************

Build and flash Blinky as follows,

```sh
$> west build -p -b arduino_nano_33_ble samples/basic/blinky_tone/ -DZEPHYR_EXTRA_MODULES=/home/$USER/zephyrproject/modules/lib/Arduino-Core-Zephyr
$> west flash --bossac=/home/$USER/.arduino15/packages/arduino/tools/bossac/1.9.1-arduino2/bossac
```

After flashing, the LED starts to blink. If a runtime error occurs, the sample
exits without printing to the console.

Adding board support
********************

To add support for your board, add something like this to your devicetree:

.. code-block:: DTS
/ {
aliases {
led0 = &myled0;
};
leds {
compatible = "gpio-leds";
myled0: led_0 {
gpios = <&gpio0 13 GPIO_ACTIVE_LOW>;
};
};
};
The above sets your board's ``led0`` alias to use pin 13 on GPIO controller
``gpio0``. The pin flags :c:macro:`GPIO_ACTIVE_HIGH` mean the LED is on when
the pin is set to its high state, and off when the pin is in its low state.

Tips:

- See :dtcompatible:`gpio-leds` for more information on defining GPIO-based LEDs
in devicetree.

- If you're not sure what to do, check the devicetrees for supported boards which
use the same SoC as your target. See :ref:`get-devicetree-outputs` for details.

- See :zephyr_file:`include/zephyr/dt-bindings/gpio/gpio.h` for the flags you can use
in devicetree.

- If the LED is built in to your board hardware, the alias should be defined in
your :ref:`BOARD.dts file <devicetree-in-out-files>`. Otherwise, you can
define one in a :ref:`devicetree overlay <set-devicetree-overlays>`.
3 changes: 3 additions & 0 deletions samples/blinky_tone/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CONFIG_GPIO=y
CONFIG_CPLUSPLUS=y
CONFIG_ARDUINO_API=y
20 changes: 20 additions & 0 deletions samples/blinky_tone/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* SPDX-License-Identifier: Apache-2.0
*/

/* Blink inbuilt LED with tone */

#include <Arduino.h>

void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
tone(LED_BUILTIN, 1, 10000);
delay(15000);
tone(LED_BUILTIN, 1);
delay(10000);
noTone(LED_BUILTIN);
delay(5000);
}

0 comments on commit bda0532

Please sign in to comment.