Post by rappel on Apr 29, 2023 14:36:06 GMT
Let's try this out together! I'm usin a raspberry Pi Pico, but could also use an Arduino Nano...
The scope of this thread is to try get a very basic working Scope...
I think it would be a fun project to pull out and share amongst us!
Here is my very first prompt! Mind the Oled display info can change:
Create a code in micro python for a oscilloscope sound visualizer with the output sent to a 480x220 oled display
ChatGPT
Here's an example code in MicroPython that reads audio input from an ADC pin and displays the sound waveform on a 480x220 OLED display:
import machine
import ssd1306
import math
# Set up ADC input
adc = machine.ADC(0)
# Set up OLED display
i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4))
oled = ssd1306.SSD1306_I2C(480, 220, i2c)
# Set up sampling parameters
num_samples = 480
sample_rate = 10000
# Main loop
while True:
# Read audio input and convert to voltage
values = [adc.read() for _ in range(num_samples)]
voltage_values = [(value/1024)*3.3 for value in values]
# Compute sound waveform
sound_waveform = [(2*v-3.3)/3.3 for v in voltage_values]
# Display sound waveform on OLED
oled.fill(0)
for i in range(num_samples-1):
x0 = int(i*480/(num_samples-1))
y0 = int(110 - 100*sound_waveform)
x1 = int((i+1)*480/(num_samples-1))
y1 = int(110 - 100*sound_waveform[i+1])
oled.line(x0, y0, x1, y1, 1)
oled.show()
# Delay to maintain sample rate
machine.freq(160000000)
machine.Timer(-1).init(period=int(1000000/sample_rate), mode=machine.Timer.ONE_SHOT, callback=lambda t:None)
#End of code
Note that this code assumes that the audio input is connected to ADC pin 0, and that the OLED display is connected to I2C pins 4 and 5. You may need to adjust these pin numbers depending on your specific hardware setup. Additionally, this code uses a simple linear mapping to convert ADC readings to voltages, but you may need to adjust this mapping based on your specific ADC and voltage range.
--------------
Then I asked to verify the code and this was the answer:
The code looks correct to me. It sets up an ADC input and an OLED display, reads audio input from the ADC pin, converts it to voltage, and displays the sound waveform on the OLED display. It also includes a delay to maintain the desired sample rate.
However, it's worth noting that without the actual hardware setup, it's difficult to verify whether the code will work as intended or not. So, it's possible that there may be issues that only become apparent when the code is run on the actual hardware.
---------------
I then asked to write it for an Arduino Nano and this is the code.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET -1
#define SCREEN_WIDTH 480
#define SCREEN_HEIGHT 220
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define NUM_SAMPLES 480
#define SAMPLE_RATE 10000
#define ADC_PIN A0
void setup() {
// Set up ADC input
analogReference(DEFAULT);
pinMode(ADC_PIN, INPUT);
// Set up OLED display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(100);
display.clearDisplay();
display.setTextColor(WHITE);
}
void loop() {
// Read audio input and convert to voltage
int values[NUM_SAMPLES];
float voltage_values[NUM_SAMPLES];
for (int i=0; i<NUM_SAMPLES; i++) {
values = analogRead(ADC_PIN);
voltage_values = (float)values/1023*5.0;
}
// Compute sound waveform
float sound_waveform[NUM_SAMPLES-1];
for (int i=0; i<NUM_SAMPLES-1; i++) {
sound_waveform = (2*voltage_values-5)/5;
}
// Display sound waveform on OLED
display.clearDisplay();
for (int i=0; i<NUM_SAMPLES-1; i++) {
int x0 = i*SCREEN_WIDTH/(NUM_SAMPLES-1);
int y0 = 110 - 100*sound_waveform;
int x1 = (i+1)*SCREEN_WIDTH/(NUM_SAMPLES-1);
int y1 = 110 - 100*sound_waveform[i+1];
display.drawLine(x0, y0, x1, y1, WHITE);
}
display.display();
// Delay to maintain sample rate
delayMicroseconds(1000000/SAMPLE_RATE);
}
Note that this code assumes that the audio input is connected to analog pin A0, and that the OLED display is connected to the I2C pins (SDA and SCL) of the Arduino Nano. You may need to adjust these pin numbers depending on your specific hardware setup. Additionally, this code uses a simple linear mapping to convert ADC readings to voltages, but you may need to adjust this mapping based on your specific ADC and voltage range.
------------------------
So I need to test these out, in the meanwhile I hope to have raised some interest here and there...
It's just too much fun, and an incredible learning tool!
The scope of this thread is to try get a very basic working Scope...
I think it would be a fun project to pull out and share amongst us!
Here is my very first prompt! Mind the Oled display info can change:
Create a code in micro python for a oscilloscope sound visualizer with the output sent to a 480x220 oled display
ChatGPT
Here's an example code in MicroPython that reads audio input from an ADC pin and displays the sound waveform on a 480x220 OLED display:
import machine
import ssd1306
import math
# Set up ADC input
adc = machine.ADC(0)
# Set up OLED display
i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4))
oled = ssd1306.SSD1306_I2C(480, 220, i2c)
# Set up sampling parameters
num_samples = 480
sample_rate = 10000
# Main loop
while True:
# Read audio input and convert to voltage
values = [adc.read() for _ in range(num_samples)]
voltage_values = [(value/1024)*3.3 for value in values]
# Compute sound waveform
sound_waveform = [(2*v-3.3)/3.3 for v in voltage_values]
# Display sound waveform on OLED
oled.fill(0)
for i in range(num_samples-1):
x0 = int(i*480/(num_samples-1))
y0 = int(110 - 100*sound_waveform)
x1 = int((i+1)*480/(num_samples-1))
y1 = int(110 - 100*sound_waveform[i+1])
oled.line(x0, y0, x1, y1, 1)
oled.show()
# Delay to maintain sample rate
machine.freq(160000000)
machine.Timer(-1).init(period=int(1000000/sample_rate), mode=machine.Timer.ONE_SHOT, callback=lambda t:None)
#End of code
Note that this code assumes that the audio input is connected to ADC pin 0, and that the OLED display is connected to I2C pins 4 and 5. You may need to adjust these pin numbers depending on your specific hardware setup. Additionally, this code uses a simple linear mapping to convert ADC readings to voltages, but you may need to adjust this mapping based on your specific ADC and voltage range.
--------------
Then I asked to verify the code and this was the answer:
The code looks correct to me. It sets up an ADC input and an OLED display, reads audio input from the ADC pin, converts it to voltage, and displays the sound waveform on the OLED display. It also includes a delay to maintain the desired sample rate.
However, it's worth noting that without the actual hardware setup, it's difficult to verify whether the code will work as intended or not. So, it's possible that there may be issues that only become apparent when the code is run on the actual hardware.
---------------
I then asked to write it for an Arduino Nano and this is the code.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET -1
#define SCREEN_WIDTH 480
#define SCREEN_HEIGHT 220
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define NUM_SAMPLES 480
#define SAMPLE_RATE 10000
#define ADC_PIN A0
void setup() {
// Set up ADC input
analogReference(DEFAULT);
pinMode(ADC_PIN, INPUT);
// Set up OLED display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(100);
display.clearDisplay();
display.setTextColor(WHITE);
}
void loop() {
// Read audio input and convert to voltage
int values[NUM_SAMPLES];
float voltage_values[NUM_SAMPLES];
for (int i=0; i<NUM_SAMPLES; i++) {
values = analogRead(ADC_PIN);
voltage_values = (float)values/1023*5.0;
}
// Compute sound waveform
float sound_waveform[NUM_SAMPLES-1];
for (int i=0; i<NUM_SAMPLES-1; i++) {
sound_waveform = (2*voltage_values-5)/5;
}
// Display sound waveform on OLED
display.clearDisplay();
for (int i=0; i<NUM_SAMPLES-1; i++) {
int x0 = i*SCREEN_WIDTH/(NUM_SAMPLES-1);
int y0 = 110 - 100*sound_waveform;
int x1 = (i+1)*SCREEN_WIDTH/(NUM_SAMPLES-1);
int y1 = 110 - 100*sound_waveform[i+1];
display.drawLine(x0, y0, x1, y1, WHITE);
}
display.display();
// Delay to maintain sample rate
delayMicroseconds(1000000/SAMPLE_RATE);
}
Note that this code assumes that the audio input is connected to analog pin A0, and that the OLED display is connected to the I2C pins (SDA and SCL) of the Arduino Nano. You may need to adjust these pin numbers depending on your specific hardware setup. Additionally, this code uses a simple linear mapping to convert ADC readings to voltages, but you may need to adjust this mapping based on your specific ADC and voltage range.
------------------------
So I need to test these out, in the meanwhile I hope to have raised some interest here and there...
It's just too much fun, and an incredible learning tool!