Skip to content

Commit a465524

Browse files
committed
samples: Add blinky tone
- Use `tone` and `noTone` functions to blink led at 1hz with duty cycle 50% on any digital pin Signed-off-by: Ayush Singh <[email protected]>
1 parent f5d1b9e commit a465524

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed

samples/blinky_tone/CMakeLists.txt

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
cmake_path(SET ZephyrBase $ENV{ZEPHYR_BASE})
6+
set(DTC_OVERLAY_FILE ${ZephyrBase}/../modules/lib/Arduino-Zephyr-API/variants/${BOARD}/${BOARD}.overlay)
7+
8+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
9+
project(blinky)
10+
11+
target_sources(app PRIVATE src/main.cpp)
12+
13+
zephyr_compile_options(-Wno-unused-variable -Wno-comment)

samples/blinky_tone/README.rst

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
.. _blinky-sample:
2+
3+
Blinky Tone
4+
############
5+
6+
Overview
7+
********
8+
9+
This Arduino Blinky sample blinks an LED forever using the `tone` and `noTone`.
10+
11+
Requirements
12+
************
13+
14+
Your board must:
15+
16+
#. Have an LED connected via a GPIO pin (these are called "User LEDs" on many of
17+
Zephyr's :ref:`boards`).
18+
#. Have the LED configured using the ``led0`` devicetree alias.
19+
20+
Building and Running
21+
********************
22+
23+
Build and flash Blinky as follows,
24+
25+
```sh
26+
$> west build -p -b arduino_nano_33_ble samples/basic/blinky_tone/ -DZEPHYR_EXTRA_MODULES=/home/$USER/zephyrproject/modules/lib/Arduino-Core-Zephyr
27+
28+
$> west flash --bossac=/home/$USER/.arduino15/packages/arduino/tools/bossac/1.9.1-arduino2/bossac
29+
```
30+
31+
After flashing, the LED starts to blink. If a runtime error occurs, the sample
32+
exits without printing to the console.
33+
34+
Adding board support
35+
********************
36+
37+
To add support for your board, add something like this to your devicetree:
38+
39+
.. code-block:: DTS
40+
41+
/ {
42+
aliases {
43+
led0 = &myled0;
44+
};
45+
46+
leds {
47+
compatible = "gpio-leds";
48+
myled0: led_0 {
49+
gpios = <&gpio0 13 GPIO_ACTIVE_LOW>;
50+
};
51+
};
52+
};
53+
54+
The above sets your board's ``led0`` alias to use pin 13 on GPIO controller
55+
``gpio0``. The pin flags :c:macro:`GPIO_ACTIVE_HIGH` mean the LED is on when
56+
the pin is set to its high state, and off when the pin is in its low state.
57+
58+
Tips:
59+
60+
- See :dtcompatible:`gpio-leds` for more information on defining GPIO-based LEDs
61+
in devicetree.
62+
63+
- If you're not sure what to do, check the devicetrees for supported boards which
64+
use the same SoC as your target. See :ref:`get-devicetree-outputs` for details.
65+
66+
- See :zephyr_file:`include/zephyr/dt-bindings/gpio/gpio.h` for the flags you can use
67+
in devicetree.
68+
69+
- If the LED is built in to your board hardware, the alias should be defined in
70+
your :ref:`BOARD.dts file <devicetree-in-out-files>`. Otherwise, you can
71+
define one in a :ref:`devicetree overlay <set-devicetree-overlays>`.

samples/blinky_tone/prj.conf

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIG_GPIO=y
2+
CONFIG_CPLUSPLUS=y
3+
CONFIG_ARDUINO_API=y

samples/blinky_tone/src/main.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*/
4+
5+
/* Blink inbuilt LED with tone */
6+
7+
#include <Arduino.h>
8+
9+
void setup() {
10+
pinMode(LED_BUILTIN, OUTPUT);
11+
}
12+
13+
void loop() {
14+
tone(LED_BUILTIN, 1, 10000);
15+
delay(15000);
16+
tone(LED_BUILTIN, 1);
17+
delay(10000);
18+
noTone(LED_BUILTIN);
19+
delay(5000);
20+
}

0 commit comments

Comments
 (0)