Skip to content

Commit f800a7e

Browse files
kilograhamPaulo Marquesmartinmatiasilvaurish
authored
Updates along with SDK1.3.0 release (#181)
Bug fixes and new examples Co-authored-by: Paulo Marques <[email protected]> Co-authored-by: martin <[email protected]> Co-authored-by: matiasilva <[email protected]> Co-authored-by: Uri Shaked <[email protected]> Co-authored-by: Diego Solano <[email protected]> Co-authored-by: Andrew Scheller <[email protected]> Co-authored-by: Adrian Hesketh <[email protected]> Co-authored-by: Emircan Gündoğdu <[email protected]> Co-authored-by: Josef Wegner <[email protected]> Co-authored-by: pmarques-dev <[email protected]> Co-authored-by: Paulo Marques <[email protected]> Co-authored-by: mjcross <[email protected]> Co-authored-by: martin <[email protected]>
1 parent 146680d commit f800a7e

File tree

150 files changed

+4832
-98
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

150 files changed

+4832
-98
lines changed

CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ project(pico_examples C CXX ASM)
77
set(CMAKE_C_STANDARD 11)
88
set(CMAKE_CXX_STANDARD 17)
99

10+
if (PICO_SDK_VERSION_STRING VERSION_LESS "1.3.0")
11+
message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.3.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")
12+
endif()
13+
1014
set(PICO_EXAMPLES_PATH ${PROJECT_SOURCE_DIR})
1115

1216
# Initialize the SDK

CONTRIBUTING.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Contributing to Raspberry Pi Pico C/C++ Examples
2+
3+
## How to Report a Bug
4+
5+
We use GitHub to host code, track [issues](https://github.com/raspberrypi/pico-examples/issues) and feature requests, and to accept [pull requests](https://github.com/raspberrypi/pico-examples/pulls). If you find think you have found a bug, please report it by [opening a new issue](https://github.com/raspberrypi/pico-examples/issues/new). Please include as much detail as possible, and ideally some code to reproduce the problem.
6+
7+
## How to Contribute Code
8+
9+
In order to contribute new or updated code, you must first create a GitHub account and fork the original repository to your own account. You can make changes, save them in your repository, then [make a pull request](https://docs.github.com/en/github/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request-from-a-fork) against this repository. The pull request will appear [in the repository](https://github.com/raspberrypi/pico-examples/pulls) where it can be assessed by the maintainers, and if appropriate, merged with the official repository.
10+
11+
**NOTE:** Development takes place on the `develop` branch in this repository. Please open your https://github.com/raspberrypi/pico-examples/pulls[pull request] (PR) against the [`develop`](https://github.com/raspberrypi/pico-examples/tree/develop) branch, pull requests against the `master` branch will automatically CI fail checks and will not be accepted. You will be asked to rebase your PR against `develop` and if you do not do so, your PR will be closed.
12+
13+
### Code Style
14+
15+
If you are contributing new or updated code please match the existing code style, particularly:
16+
17+
* Use 4 spaces for indentation rather than tabs.
18+
* Braces are required for everything except single line `if` statements.
19+
* Opening braces should not be placed on a new line.
20+
21+
### Licensing
22+
23+
Code in this repository is lisensed under the [BSD-3 License](LICENSE.TXT). By contributing content to this repository you are agreeing to place your contributions under this licence.

README.md

+27-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ App|Description
2020
[hello_adc](adc/hello_adc)|Display the voltage from an ADC input.
2121
[joystick_display](adc/joystick_display)|Display a Joystick X/Y input based on two ADC inputs.
2222
[adc_console](adc/adc_console)|An interactive shell for playing with the ADC. Includes example of free-running capture mode.
23+
[microphone_adc](adc/microphone_adc)|Read analog values from a microphone and plot the measured sound amplitude.
2324

2425
### Clocks
2526

@@ -76,8 +77,16 @@ App|Description
7677
App|Description
7778
---|---
7879
[bus_scan](i2c/bus_scan) | Scan the I2C bus for devices and display results.
80+
[bmp280_i2c](i2c/bmp280_i2c) | Read and convert temperature and pressure data from a BMP280 sensor, attached to an I2C bus.
7981
[lcd_1602_i2c](i2c/lcd_1602_i2c) | Display some text on a generic 16x2 character LCD display, via I2C.
82+
[lis3dh_i2c](i2c/lis3dh_i2c) | Read acceleration and temperature value from a LIS3DH sensor via I2C
83+
[mcp9808_i2c](i2c/mcp9808_i2c) | Read temperature, set limits and raise alerts when limits are surpassed.
84+
[mma8451_i2c](i2c/mma8451_i2c) | Read acceleration from a MMA8451 accelerometer and set range and precision for the data.
85+
[mpl3115a2_i2c](i2c/mpl3115a2_i2c) | Interface with an MPL3115A2 altimeter, exploring interrupts and advanced board features, via I2C.
8086
[mpu6050_i2c](i2c/mpu6050_i2c) | Read acceleration and angular rate values from a MPU6050 accelerometer/gyro, attached to an I2C bus.
87+
[oled_i2c](i2c/oled_i2c) | Convert and display a bitmap on a 128x32 SSD1306-driven OLED display
88+
[pa1010d_i2c](i2c/pa1010d_i2c) | Read GPS location data, parse and display data via I2C.
89+
[pcf8523_i2c](i2c/pcf8523_i2c) | Read time and date values from a real time clock. Set current time and alarms on it.
8190

8291
### Interpolator
8392

@@ -109,13 +118,15 @@ App|Description
109118
[differential_manchester](pio/differential_manchester)| Send and receive differential Manchester-encoded serial (BMC).
110119
[hub75](pio/hub75)| Display an image on a 128x64 HUB75 RGB LED matrix.
111120
[i2c](pio/i2c)| Scan an I2C bus.
121+
[ir_nec](pio/ir_nec)| Sending and receiving IR (infra-red) codes using the PIO.
112122
[logic_analyser](pio/logic_analyser)| Use PIO and DMA to capture a logic trace of some GPIOs, whilst a PWM unit is driving them.
113123
[manchester_encoding](pio/manchester_encoding)| Send and receive Manchester-encoded serial.
114124
[pio_blink](pio/pio_blink)| Set up some PIO state machines to blink LEDs at different frequencies, according to delay counts pushed into their FIFOs.
115125
[pwm](pio/pwm)| Pulse width modulation on PIO. Use it to gradually fade the brightness of an LED.
116126
[spi](pio/spi)| Use PIO to erase, program and read an external SPI flash chip. A second example runs a loopback test with all four CPHA/CPOL combinations.
117127
[squarewave](pio/squarewave)| Drive a fast square wave onto a GPIO. This example accesses low-level PIO registers directly, instead of using the SDK functions.
118128
[st7789_lcd](pio/st7789_lcd)| Set up PIO for 62.5 Mbps serial output, and use this to display a spinning image on a ST7789 serial LCD.
129+
[quadrature_encoder](pio/quadrature_encoder)| A quadrature encoder using PIO to maintain counts independent of the CPU.
119130
[uart_rx](pio/uart_rx)| Implement the receive component of a UART serial port. Attach it to the spare Arm UART to see it receive characters.
120131
[uart_tx](pio/uart_tx)| Implement the transmit component of a UART serial port, and print hello world.
121132
[ws2812](pio/ws2812)| Examples of driving WS2812 addressable RGB LEDs.
@@ -172,13 +183,14 @@ App|Description
172183
App|Description
173184
---|---
174185
[hello_uart](uart/hello_uart) | Print some text from one of the UART serial ports, without going through `stdio`.
186+
[lcd_uart](uart/lcd_uart) | Display text and symbols on a 16x02 RGB LCD display via UART
175187
[uart_advanced](uart/uart_advanced) | Use some other UART features like RX interrupts, hardware control flow, and data formats other than 8n1.
176188

177189
### USB Device
178190

179191
#### TinyUSB Examples
180192

181-
All but one of the USB device examples come directly from the TinyUSB device examples directory [here](https://github.com/hathach/tinyusb/tree/master/examples/device).
193+
Most of the USB device examples come directly from the TinyUSB device examples directory [here](https://github.com/hathach/tinyusb/tree/master/examples/device).
182194
Those that are supported on RP2040 devices are automatically included as part of the pico-examples
183195
build as targets named `tinyusb_dev_<example_name>`, e.g. https://github.com/hathach/tinyusb/tree/master/examples/device/hid_composite
184196
is built as `tinyusb_dev_hid_composite`.
@@ -198,11 +210,24 @@ At the time of writing, these examples are available:
198210
- tinyusb_dev_hid_multiple_interface
199211
- tinyusb_dev_midi_test
200212
- tinyusb_dev_msc_dual_lun
213+
- tinyusb_dev_net_lwip_webserver
201214
- tinyusb_dev_uac2_headset
202215
- tinyusb_dev_usbtmc
216+
- tinyusb_dev_video_capture
203217
- tinyusb_dev_webusb_serial
204218

205-
#### Low Level examples
219+
Whilst these examples ably demonstrate how to use TinyUSB in device mode, their `CMakeLists.txt` is set up in a way
220+
tailored to how TinyUSB builds their examples within their source tree.
221+
222+
For a better example of how to configure `CMakeLists.txt` for using TinyUSB in device mode with the Raspberry Pi SDK
223+
see below:
224+
225+
#### SDK build example
226+
App|Description
227+
---|---
228+
[dev_hid_composite](usb/device/dev_hid_composite) | A copy of the TinyUSB device example with the same name, but with a CMakeLists.txt which demonstrates how to add a dependency on the TinyUSB device libraries with the Raspberry Pi Pico SDK
229+
230+
#### Low Level example
206231
App|Description
207232
---|---
208233
[dev_lowlevel](usb/device/dev_lowlevel) | A USB Bulk loopback implemented with direct access to the USB hardware (no TinyUSB)

adc/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ if (NOT PICO_NO_HARDWARE)
33
add_subdirectory(dma_capture)
44
add_subdirectory(hello_adc)
55
add_subdirectory(joystick_display)
6+
add_subdirectory(microphone_adc)
67
endif ()

adc/microphone_adc/CMakeLists.txt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
add_executable(microphone_adc
2+
microphone_adc.c
3+
)
4+
5+
# pull in common dependencies and adc hardware support
6+
target_link_libraries(microphone_adc pico_stdlib hardware_adc)
7+
8+
# create map/bin/hex file etc.
9+
pico_add_extra_outputs(microphone_adc)
10+
11+
# add url via pico_set_program_url
12+
example_auto_set_url(microphone_adc)

adc/microphone_adc/README.adoc

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
= Attaching a microphone using the ADC
2+
3+
This example code shows how to interface the Raspberry Pi Pico with a standard analog microphone via the onboard analog to digital converter (ADC). In this example, we use an ICS-40180 breakout board by SparkFun but any analog microphone should be compatible with this tutorial. SparkFun have https://learn.sparkfun.com/tutorials/mems-microphone-hookup-guide[written a guide] for this board that goes into more detail about the board and how it works.
4+
5+
[TIP]
6+
======
7+
An analog to digital converter (ADC) is responsible for reading continually varying input signals that may range from 0 to a specified reference voltage (in the Pico's case this reference voltage is set by the supply voltage and can be measured on pin 35, ADC_VREF) and converting them into binary, i.e. a number that can be digitally stored.
8+
======
9+
10+
The Pico has a 12-bit ADC (ENOB of 8.7-bit, see https://datasheets.raspberrypi.com/rp2040/rp2040-datasheet.pdf[RP2040 datasheet section 4.9.3 for more details]), meaning that a read operation will return a number ranging from 0 to 4095 (2^12 - 1) for a total of 4096 possible values. Therefore, the resolution of the ADC is 3.3/4096, so roughly steps of 0.8 millivolts. The SparkFun breakout uses an OPA344 operational amplifier to boost the signal coming from the microphone to voltage levels that can be easily read by the ADC. An important side effect is that a bias of 0.5*Vcc is added to the signal, even when the microphone is not picking up any sound.
11+
12+
The ADC provides us with a raw voltage value but when dealing with sound, we're more interested in the amplitude of the audio signal. This is defined as one half the peak-to-peak amplitude. Included with this example is a very simple Python script that will plot the voltage values it receives via the serial port. By tweaking the sampling rates, and various other parameters, the data from the microphone can be analysed in various ways, such as in a Fast Fourier Transform to see what frequencies make up the signal.
13+
14+
[[microphone_adc_plotter_image]]
15+
[pdfwidth=75%]
16+
.Example output from included Python script
17+
image::microphone_adc_plotter.png[]
18+
19+
== Wiring information
20+
21+
Wiring up the device requires 3 jumpers, to connect VCC (3.3v), GND, and AOUT. The example here uses ADC0, which is GP26. Power is supplied from the 3.3V pin.
22+
23+
WARNING: Most boards will take a range of VCC voltages from the Pico's default 3.3V to the 5 volts commonly seen on other microcontrollers. Ensure your board doesn't output an analogue signal greater than 3.3V as this may result in permanent damage to the Pico's ADC.
24+
25+
[[ics-40180-adc_wiring]]
26+
[pdfwidth=75%]
27+
.Wiring Diagram for ICS-40180 microphone breakout board.
28+
image::microphone_adc_bb.png[]
29+
30+
== List of Files
31+
32+
CMakeLists.txt:: CMake file to incorporate the example in to the examples build tree.
33+
microphone_adc.c:: The example code.
34+
35+
== Bill of Materials
36+
37+
.A list of materials required for the example
38+
[[ics-40180-adc-bom-table]]
39+
[cols=3]
40+
|===
41+
| *Item* | *Quantity* | Details
42+
| Breadboard | 1 | generic part
43+
| Raspberry Pi Pico | 1 | https://www.raspberrypi.com/products/raspberry-pi-pico/
44+
| ICS-40180 microphone breakout board or similar | 1 | https://www.sparkfun.com/products/18011[From SparkFun]
45+
| M/M Jumper wires | 3 | generic part
46+
|===
47+
48+

adc/microphone_adc/microphone_adc.c

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright (c) 2021 Raspberry Pi (Trading) Ltd.
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#include <stdio.h>
8+
#include "pico/stdlib.h"
9+
#include "hardware/gpio.h"
10+
#include "hardware/adc.h"
11+
#include "hardware/uart.h"
12+
#include "pico/binary_info.h"
13+
14+
/* Example code to extract analog values from a microphone using the ADC
15+
with accompanying Python file to plot these values
16+
17+
Connections on Raspberry Pi Pico board, other boards may vary.
18+
19+
GPIO 26/ADC0 (pin 31)-> AOUT or AUD on microphone board
20+
3.3v (pin 36) -> VCC on microphone board
21+
GND (pin 38) -> GND on microphone board
22+
*/
23+
24+
#define ADC_NUM 0
25+
#define ADC_PIN (26 + ADC_NUM)
26+
#define ADC_VREF 3.3
27+
#define ADC_RANGE (1 << 12)
28+
#define ADC_CONVERT (ADC_VREF / (ADC_RANGE - 1))
29+
30+
int main() {
31+
stdio_init_all();
32+
printf("Beep boop, listening...\n");
33+
34+
bi_decl(bi_program_description("Analog microphone example for Raspberry Pi Pico")); // for picotool
35+
bi_decl(bi_1pin_with_name(ADC_PIN, "ADC input pin"));
36+
37+
adc_init();
38+
adc_gpio_init( ADC_PIN);
39+
adc_select_input( ADC_NUM);
40+
41+
uint adc_raw;
42+
while (1) {
43+
adc_raw = adc_read(); // raw voltage from ADC
44+
printf("%.2f\n", adc_raw * ADC_CONVERT);
45+
sleep_ms(10);
46+
}
47+
48+
return 0;
49+
}

adc/microphone_adc/microphone_adc.fzz

70.2 KB
Binary file not shown.
161 KB
Loading
59 KB
Loading

adc/microphone_adc/plotter.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env python3
2+
3+
# Grabs raw data from the Pico's UART and plots it as received
4+
5+
# Install dependencies:
6+
# python3 -m pip install pyserial matplotlib
7+
8+
# Usage: python3 plotter <port>
9+
# eg. python3 plotter /dev/ttyACM0
10+
11+
# see matplotlib animation API for more: https://matplotlib.org/stable/api/animation_api.html
12+
13+
import serial
14+
import sys
15+
import matplotlib.pyplot as plt
16+
import matplotlib.animation as animation
17+
from matplotlib.lines import Line2D
18+
19+
# disable toolbar
20+
plt.rcParams['toolbar'] = 'None'
21+
22+
class Plotter:
23+
def __init__(self, ax):
24+
self.ax = ax
25+
self.maxt = 250
26+
self.tdata = [0]
27+
self.ydata = [3.3/2]
28+
self.line = Line2D(self.tdata, self.ydata)
29+
30+
self.ax.add_line(self.line)
31+
self.ax.set_ylim(0, 3.3)
32+
self.ax.set_xlim(0, self.maxt)
33+
34+
def update(self, y):
35+
lastt = self.tdata[-1]
36+
if lastt - self.tdata[0] >= self.maxt: # drop old frames
37+
self.tdata = self.tdata[1:]
38+
self.ydata = self.ydata[1:]
39+
self.ax.set_xlim(self.tdata[0], self.tdata[0] + self.maxt)
40+
41+
t = lastt + 1
42+
self.tdata.append(t)
43+
self.ydata.append(y)
44+
self.line.set_data(self.tdata, self.ydata)
45+
return self.line,
46+
47+
48+
def serial_getter():
49+
# grab fresh ADC values
50+
# note sometimes UART drops chars so we try a max of 5 times
51+
# to get proper data
52+
while True:
53+
for i in range(5):
54+
line = ser.readline()
55+
try:
56+
line = float(line)
57+
except ValueError:
58+
continue
59+
break
60+
yield line
61+
62+
if len(sys.argv) < 2:
63+
raise Exception("Ruh roh..no port specified!")
64+
65+
ser = serial.Serial(sys.argv[1], 115200, timeout=1)
66+
67+
fig, ax = plt.subplots()
68+
plotter = Plotter(ax)
69+
70+
ani = animation.FuncAnimation(fig, plotter.update, serial_getter, interval=1,
71+
blit=True, cache_frame_data=False)
72+
73+
ax.set_xlabel("Samples")
74+
ax.set_ylabel("Voltage (V)")
75+
fig.canvas.manager.set_window_title('Microphone ADC example')
76+
fig.tight_layout()
77+
plt.show()

blink/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ add_executable(blink
22
blink.c
33
)
44

5-
# Pull in our pico_stdlib which pulls in commonly used features
5+
# pull in common dependencies
66
target_link_libraries(blink pico_stdlib)
77

88
# create map/bin/hex file etc.

clocks/detached_clk_peri/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ add_executable(clocks_detached_clk_peri
22
detached_clk_peri.c
33
)
44

5-
# Pull in our pico_stdlib which pulls in commonly used features
5+
# pull in common dependencies
66
target_link_libraries(clocks_detached_clk_peri pico_stdlib)
77

88
# create map/bin/hex file etc.
99
pico_add_extra_outputs(clocks_detached_clk_peri)
1010

1111
# add url via pico_set_program_url
12-
example_auto_set_url(clocks_detached_clk_peri)
12+
example_auto_set_url(clocks_detached_clk_peri)

clocks/hello_48MHz/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ add_executable(hello_48MHz
22
hello_48MHz.c
33
)
44

5-
# Pull in our pico_stdlib which pulls in commonly used features
5+
# pull in common dependencies and additional clocks hardware support
66
target_link_libraries(hello_48MHz pico_stdlib hardware_clocks)
77

88
# create map/bin/hex file etc.

clocks/hello_gpout/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ add_executable(hello_gpout
22
hello_gpout.c
33
)
44

5-
# Pull in our pico_stdlib which pulls in commonly used features
5+
# pull in common dependencies
66
target_link_libraries(hello_gpout pico_stdlib)
77

88
# create map/bin/hex file etc.
99
pico_add_extra_outputs(hello_gpout)
1010

1111
# add url via pico_set_program_url
12-
example_auto_set_url(hello_gpout)
12+
example_auto_set_url(hello_gpout)

clocks/hello_resus/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ add_executable(hello_resus
22
hello_resus.c
33
)
44

5-
# Pull in our pico_stdlib which pulls in commonly used features
5+
# pull in common dependencies
66
target_link_libraries(hello_resus pico_stdlib)
77

88
# create map/bin/hex file etc.
99
pico_add_extra_outputs(hello_resus)
1010

1111
# add url via pico_set_program_url
12-
example_auto_set_url(hello_resus)
12+
example_auto_set_url(hello_resus)

divider/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ add_executable(hello_divider
22
hello_divider.c
33
)
44

5-
# Pull in our pico_stdlib which pulls in commonly used features
5+
# pull in common dependencies
66
target_link_libraries(hello_divider pico_stdlib)
77

88
# create map/bin/hex file etc.
99
pico_add_extra_outputs(hello_divider)
1010

1111
# add url via pico_set_program_url
12-
example_auto_set_url(hello_divider)
12+
example_auto_set_url(hello_divider)

0 commit comments

Comments
 (0)