Skip to content

Commit 64c24c9

Browse files
committed
updating content
1 parent e82c29b commit 64c24c9

File tree

9 files changed

+347
-8
lines changed

9 files changed

+347
-8
lines changed

docs/advanced-labs/14-memory-management.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,23 @@ print('Freeing the block of memory')
132132
gc.mem_free(ptr)
133133
show_memory()
134134
```
135+
136+
## Image Size
137+
138+
You can determine how much space we have available on the Pico flash after programming an ELF or UF2 file. For example, if we have an ELF file that's 1 MB and we were to program via openocd, then where should we offset my user data in flash? (i.e. XIP_BASE + OFFSET)
139+
140+
With an elf or uf2 file, using ```picotool info -a``` on it will show the start and end of the binary (the start is usually 0x10000000 unless you did something to change it).
141+
142+
Inside your code, you can use the symbols defined by the linker script __flash_binary_start (defined here) and __flash_binary_end (defined here) like this:
143+
144+
```c
145+
extern char __flash_binary_start; // defined in linker script
146+
extern char __flash_binary_end; // defined in linker script
147+
uintptr_t start = (uintptr_t) &__flash_binary_start;
148+
uintptr_t end = (uintptr_t) &__flash_binary_end;
149+
printf("Binary starts at %08x and ends at %08x, size is %08x\n", start, end, end-start);
150+
```
151+
135152
## References
136153
137154
* [Code that Prints Memory Block Types](https://github.com/micropython/micropython/blob/fabaa6143745cf09928dfc13367ddc91e0eb9ad2/py/gc.c#L837-L856)
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Test Incoming Sound
2+
3+
## Prompt
4+
5+
```linenums="0"
6+
OK, I now have a Raspberry Pi Pico and the OLED display hooked up. I also have a microphone hooked up to ADC0 on port GPIO26. How can I test that the signal is coming into the ADC? Can you write a MicroPython program that displays the audio signal on the OLED?
7+
8+
I am using a ssd1306 and I use the following MicroPython:
9+
```
10+
11+
```python
12+
from machine import Pin
13+
import ssd1306
14+
15+
WIDTH = 128
16+
HEIGHT = 64
17+
18+
clock=Pin(2) #SCL
19+
data=Pin(3) #SDA
20+
RES = machine.Pin(4)
21+
DC = machine.Pin(5)
22+
CS = machine.Pin(6)
23+
24+
spi=machine.SPI(0, sck=clock, mosi=data)
25+
display = ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, DC, RES, CS)
26+
# clear the display
27+
display.fill(0)
28+
# draw a diagonal line
29+
display.line(0, 0, WIDTH, HEIGHT, 1)
30+
```
31+
32+
## GPT-4 Reponse
33+
34+
To test that the signal is coming into the ADC (Analog-to-Digital Converter) on the Raspberry Pi Pico via GPIO26, and to display the audio signal on your SSD1306 OLED, you can use the following MicroPython script. This script will read the analog values from the microphone connected to GPIO26 and plot these values in real-time on the OLED display, creating a simple waveform visualization.
35+
36+
Note: Before running this script, ensure you have properly initialized and connected your microphone to GPIO26 (ADC0), and the OLED display is correctly wired for SPI communication as per your provided initialization.
37+
38+
```python
39+
import machine
40+
import utime
41+
from machine import Pin, ADC, SPI
42+
import ssd1306
43+
44+
# OLED display width and height
45+
WIDTH = 128
46+
HEIGHT = 64
47+
48+
# SPI pins for OLED
49+
clock = Pin(2) # SCL
50+
data = Pin(3) # SDA
51+
RES = Pin(4)
52+
DC = Pin(5)
53+
CS = Pin(6)
54+
55+
# Initialize SPI and OLED Display
56+
spi = SPI(0, sck=clock, mosi=data)
57+
display = ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, DC, RES, CS)
58+
59+
# Initialize ADC for GPIO26 (ADC0)
60+
adc = ADC(Pin(26))
61+
62+
def plot_signal():
63+
display.fill(0) # Clear the display
64+
old_x = 0
65+
old_y = HEIGHT // 2
66+
67+
# For simplicity, we're plotting every other pixel
68+
for x in range(0, WIDTH, 2):
69+
# Read from ADC (values will be from 0 to 4095)
70+
val = adc.read_u16()
71+
# Scale the ADC value to fit the OLED height
72+
y = int((val / 65535) * HEIGHT)
73+
# Invert y to plot correctly on the OLED
74+
y = HEIGHT - y
75+
# Draw a line from the last point to the new point
76+
display.line(old_x, old_y, x, y, 1)
77+
old_x, old_y = x, y
78+
79+
display.show() # Update the display with the new data
80+
81+
while True:
82+
plot_signal()
83+
utime.sleep(0.1) # Small delay to reduce flickering
84+
```
85+
86+
This script continuously reads the analog signal from the microphone and plots a simplified version of the waveform across the OLED display. Here's a breakdown of the plotting part:
87+
88+
- The screen is cleared at the beginning of each frame with `display.fill(0)`.
89+
- We read a value from the ADC and scale it to fit the OLED's screen height.
90+
- We draw a line from the previous point to the current point to create a continuous waveform.
91+
- The display is updated with `display.show()` to visualize the waveform.
92+
93+
This example provides a basic way to visualize audio signals. The resolution and quality of the visualization can be further enhanced or adjusted according to your requirements.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Adding A Gain Potentiometer.
2+
3+
## Prompt
4+
The signal is very low on the display. I have added a Potentiometer on ADC1 which is on GPIO PIN 27. Can you please regenerate the entire program with sound on ADC0 and gain on ADC1. sound = ADC(Pin(26)) gain = ADC(Pin(27))
5+
6+
7+
## ChatGPT-
8+
9+
Certainly! To adjust the signal's amplitude on the display based on the gain from a potentiometer connected to ADC1 (GPIO PIN 27), you can modify the program as follows. This updated script will use the value from the potentiometer to scale the audio signal from the microphone, making the waveform larger or smaller on the OLED display according to the potentiometer's position.
10+
11+
```python
12+
import machine
13+
import utime
14+
from machine import Pin, ADC, SPI
15+
import ssd1306
16+
17+
# OLED display width and height
18+
WIDTH = 128
19+
HEIGHT = 64
20+
21+
# SPI pins for OLED
22+
clock = Pin(2) # SCL
23+
data = Pin(3) # SDA
24+
RES = Pin(4)
25+
DC = Pin(5)
26+
CS = Pin(6)
27+
28+
# Initialize SPI and OLED Display
29+
spi = SPI(0, sck=clock, mosi=data)
30+
display = ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, DC, RES, CS)
31+
32+
# Initialize ADC for sound input (GPIO26) and gain control (GPIO27)
33+
sound = ADC(Pin(26))
34+
gain = ADC(Pin(27))
35+
36+
def plot_signal_with_gain():
37+
display.fill(0) # Clear the display
38+
old_x = 0
39+
old_y = HEIGHT // 2
40+
41+
# Read gain control (potentiometer) value
42+
gain_value = gain.read_u16() + 1 # Adding 1 to avoid division by zero
43+
44+
for x in range(0, WIDTH, 2):
45+
# Read from ADC (sound input)
46+
val = sound.read_u16()
47+
48+
# Adjust the sound value based on the gain
49+
# Note: This scaling might need adjustment depending on your specific potentiometer and desired sensitivity
50+
adjusted_val = min(((val * gain_value) >> 16), 65535) # Ensure the adjusted value does not exceed ADC's max value
51+
52+
# Scale the adjusted value to fit the OLED height
53+
y = int((adjusted_val / 65535) * HEIGHT)
54+
# Invert y to plot correctly on the OLED
55+
y = HEIGHT - y
56+
# Draw a line from the last point to the new point
57+
display.line(old_x, old_y, x, y, 1)
58+
old_x, old_y = x, y
59+
60+
display.show() # Update the display with the new data
61+
62+
while True:
63+
plot_signal_with_gain()
64+
utime.sleep(0.1) # Small delay to reduce flickering
65+
```
66+
67+
In this script, the gain control (potentiometer) reading is used to adjust the amplitude of the sound signal before plotting it. The `gain_value` is read from ADC1 and is used to scale the audio signal from the microphone connected to ADC0. The line `adjusted_val = min(((val * gain_value) >> 16), 65535)` scales the microphone input based on the potentiometer's position, and the bit shift `>> 16` is used to keep the value within the 16-bit range of the ADC's output.
68+
69+
This method should help make the displayed signal more responsive to the potentiometer's adjustments, allowing you to visually enhance or reduce the waveform amplitude on the OLED display.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Display Scroll
2+
3+
## Prompt
4+
5+
```linenums="0"
6+
The display is a bit slow. Can you rewrite the plot\_signal\_with\_gain function so that the scroll() function is used to move the display to the left by one pixel?
7+
```
8+
9+
### ChatGPT GPT-4 Response
10+
11+
To make the display update faster and to create a scrolling effect that moves the waveform to the left by one pixel, you can modify the `plot_signal_with_gain` function to use the `scroll()` method of the SSD1306 library. This will shift the entire display content to the left by one pixel, and then you can plot the new data point at the right edge of the display. Here's how you can implement this:
12+
13+
```python
14+
def plot_signal_with_gain():
15+
# Scroll the display content to the left by one pixel
16+
display.scroll(-1, 0)
17+
18+
# Read gain control (potentiometer) value
19+
gain_value = gain.read_u16() + 1 # Adding 1 to avoid division by zero
20+
21+
# Read from ADC (sound input)
22+
val = sound.read_u16()
23+
24+
# Adjust the sound value based on the gain
25+
adjusted_val = min(((val * gain_value) >> 16), 65535) # Ensure the adjusted value does not exceed ADC's max value
26+
27+
# Scale the adjusted value to fit the OLED height
28+
y = int((adjusted_val / 65535) * HEIGHT)
29+
# Invert y to plot correctly on the OLED
30+
y = HEIGHT - y
31+
32+
# Since we've scrolled, we only need to update the last column (x = WIDTH - 1)
33+
# Clear the last column first (optional, depending on the desired visual effect)
34+
for clear_y in range(HEIGHT):
35+
display.pixel(WIDTH - 1, clear_y, 0)
36+
37+
# Draw the new data point at the right edge of the display
38+
display.pixel(WIDTH - 1, y, 1)
39+
40+
# Update the display with the new data
41+
display.show()
42+
```
43+
44+
In this updated function, the display is scrolled left by one pixel at the beginning of each update cycle using `display.scroll(-1, 0)`. This creates room to draw the new data point at the far right edge (`WIDTH - 1`) of the display. The loop that clears the last column before drawing the new point is optional and can be adjusted or removed depending on the visual effect you're aiming for.
45+
46+
Remember, while `scroll()` improves the visual fluidity by creating a continuous scrolling effect, the update rate can still be limited by the display's physical refresh rate and the speed at which your MicroPython script can run. Experiment with the `utime.sleep(0.1)` delay at the end of the main loop to find a balance between smoothness and speed that works for your specific application.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Spectrum Analyzer
2+
3+
## ChatGPT Prompt
4+
5+
```linenums="0"
6+
I would like to create an audio spectrum analyzer using a Raspberry Pi Pico and a 128x64 OLED display using MicroPython. How would you suggest I go about this project. Break it down step-by-step.
7+
```
8+
9+
## ChatGPT GPT-4 Response
10+
11+
Creating an audio spectrum analyzer with a Raspberry Pi Pico and a 128x64 OLED display using MicroPython is a fascinating project that combines hardware interfacing with digital signal processing. Below is a step-by-step guide to help you get started on this project.
12+
13+
### Step 1: Gather the Components
14+
15+
1. **Raspberry Pi Pico**: A microcontroller board based on the RP2040 microcontroller chip.
16+
2. **128x64 OLED Display**: This will be used for visualizing the audio spectrum. Ensure it's compatible with the Pi Pico, ideally one that communicates over I2C or SPI.
17+
3. **Electret Microphone Amplifier**: This captures the audio signals. A module like the MAX4466 or a similar one would be suitable.
18+
4. **Jump Wires**: For connecting the components.
19+
5. **Breadboard**: Useful for prototyping without soldering.
20+
6. **USB Cable**: For programming the Raspberry Pi Pico.
21+
22+
### Step 2: Set Up the Raspberry Pi Pico
23+
24+
1. **Install MicroPython**: Download and flash MicroPython onto the Raspberry Pi Pico. The Raspberry Pi Pico documentation provides clear instructions for this process.
25+
2. **Prepare Your Development Environment**: Install any necessary drivers and a code editor that supports MicroPython, such as Thonny or Visual Studio Code with the appropriate extensions.
26+
27+
### Step 3: Connect the Hardware
28+
29+
1. **Microphone to Pi Pico**: Connect the output of the electret microphone amplifier to one of the analog input pins of the Pi Pico. Also, connect the ground and Vcc pins accordingly.
30+
2. **OLED Display to Pi Pico**: Connect the OLED display to the Pi Pico using I2C or SPI, depending on your display module. For I2C, you'll need to connect SDA, SCL, Vcc, and GND. For SPI, the connections will include SCK, MOSI, RES, DC, CS, Vcc, and GND.
31+
32+
### Step 4: Write the Software
33+
34+
1. **Initialize the OLED Display**: Write or adapt a MicroPython script to initialize the OLED display using the appropriate library. There are libraries available for both I2C and SPI OLED displays in MicroPython.
35+
2. **Capture Audio Data**: Write a script to capture audio data from the microphone using the ADC (Analog to Digital Converter) of the Pi Pico. The data capture should be done in real-time, and you may need to experiment with sampling rates.
36+
3. **Process Audio Data**: Implement a Fast Fourier Transform (FFT) algorithm to convert the time-domain audio signals into the frequency domain. This step is crucial for creating the spectrum analysis.
37+
4. **Visualize the Spectrum**: Map the frequency domain data to the OLED display. You will likely need to scale the data to fit nicely on the 128x64 display, representing different frequencies and their intensities.
38+
39+
### Step 5: Testing and Troubleshooting
40+
41+
1. **Run Your Script**: Test the entire system by running your script. You may need to adjust the audio capture settings or the way the data is visualized on the OLED display.
42+
2. **Troubleshoot**: If you encounter issues, use serial print statements to debug and understand where things might be going wrong.
43+
44+
### Step 6: Refinement and Expansion
45+
46+
1. **Refine the Display**: You can refine how the audio spectrum is displayed, perhaps by adjusting the resolution, colors (if your OLED supports it), or adding additional visual elements like peak hold.
47+
2. **Experiment with Features**: Consider adding features such as different modes for the spectrum analyzer, audio input from different sources, or even a GUI for interacting with the device.
48+
49+
### Resources
50+
51+
- **MicroPython Documentation**: Essential for understanding how to program the Raspberry Pi Pico.
52+
- **FFT Libraries**: Look for MicroPython libraries or examples of FFT implementations suitable for the Raspberry Pi Pico.
53+
- **OLED Libraries**: Find libraries specific to your OLED display model for easier programming.
54+
55+
This project involves a mix of hardware setup, digital signal processing, and real-time data visualization. Take it step by step, and don't hesitate to seek out community forums and resources if you encounter challenges.

docs/displays/non-graph/05-4-digit.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Four Digit LED Display
22

3-
In this lesson, we will use a 4-digit LED display to create a clock that displays the time of day. These clocks will use the [**tm1637**](https://github.com/mcauser/micropython-tm1637) library to communicate
3+
In this lesson, we will use a 4-digit LED display to create a clock that displays the time of day.
4+
These clocks will use [Mike Causer's **tm1637**](https://github.com/mcauser/micropython-tm1637) library to communicate
45
with the four-digit display. Some of these displays also have a "colon"
56
between the hour and minute digits that flashes every second.
67

@@ -16,10 +17,10 @@ You can purchase 4-digit LED displays on eBay for about $2 each.
1617

1718
These displays have four pins:
1819

19-
1. Ground
20+
1. Ground (GND)
2021
2. Power (3.2 v or 5 v)
21-
3. Data
22-
4. Clock
22+
3. Data (DIO)
23+
4. Clock (CLK)
2324

2425
In our examples, we will connect the power to our 3.3 regulated output
2526
of the Pico. We will connect Data to GP0 and Clock to GP1.
@@ -45,7 +46,8 @@ The ```tm.write()``` function takes a sequence of numbers and will shifts them i
4546
## Clock
4647

4748
We can create a simple clock by using the ```localtime()``` function when the
48-
programs first starts up and then we just update the time after the sleep() functions run for a second. This also can updates the colon between the hours
49+
programs first starts up and then we just update the time after the sleep() functions run for a second.
50+
This also can updates the colon between the hours
4951
and minutes.
5052

5153
```localtime()``` returns an array of numbers for date, hour, minute and second. In our example here, we only need the hour and minutes.

docs/misc/references.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,18 @@
3030
1. [WeAct RP2040 board adds 16MB flash, USB-C port to Raspberry Pi Pico form factor](https://www.cnx-software.com/2022/07/13/weact-rp2040-board-adds-16mb-flash-usb-c-port-to-raspberry-pi-pico-form-factor/) - JULY 13, 2022 BY JEAN-LUC AUFRANC (CNXSOFT) - CNX SOFTWARE – EMBEDDED SYSTEMS NEWS
3131

3232

33-
3433
## Open-Source References
3534

3635
1. [Open Source Guide](https://opensource.guide/)
3736

3837
## Coding General
3938

40-
* [Sam Altman on Coding](https://www.youtube.com/shorts/OK0YhF3NMpQ)
39+
* [Sam Altman on Coding](https://www.youtube.com/shorts/OK0YhF3NMpQ)
40+
41+
## Getting Started Videos
42+
43+
* [Raspberry Pi Pico W LESSON 1: Write Your First Program for Absolute Beginners](https://www.youtube.com/watch?v=SL4_oU9t8Ss) by Paul McWhorter. A nice gentle introduction to getting started with Thonny on the Raspberry Pi Pico.
44+
45+
## Snake Game
46+
47+
[Micropython Snake Game](https://github.com/hwiguna/HariFun_203_PicoSnake)

mkdocs.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,11 @@ nav:
138138
- Operating System: advanced-labs/11-os.md
139139
- Measuring Battery Voltage: advanced-labs/15-measuring-vsys.md
140140
- File System: advanced-labs/17-file-system.md
141-
141+
- Spectrum Analyzer:
142+
- Introduction: advanced-labs/30-spectrum-analyzer/index.md
143+
- Test Incomming: advanced-labs/30-spectrum-analyzer/01-test-incoming-sound.md
144+
- Gain Pot: advanced-labs/30-spectrum-analyzer/02-gain-pot.md
145+
- Display Scroll: advanced-labs/30-spectrum-analyzer/display-scroll.md
142146
- Debugging:
143147
- Debugging Python: debugging/28-debugging-python.md
144148
- Debugging I2C: debugging/29a-debugging-i2c.md

0 commit comments

Comments
 (0)