I am a ham radio operator. I am learning Morse code. The class I am taking requires that I am able to send a audio signal (sine wave) in the 600hz to 750hz range over Zoom. I have used the Arduino IDE to make USB keyboards for volume control and sending special keys. (ctrl-left and ctrl-right). What I would like to do now is use a switch (Morse Key) connected to a pin (pull_up) to send a USB audio signal. Is there a USB library that works somewhat like keyboard.h except for audio signals?
Latency is critical in this application.
Any direction would be helpful. Most of the posts I have seen here are people trying to use the Arduino as a soundcard. Since I am not doing any digitizing I only need a small subset of the features of a sound card but I need the connected PC to see the Arduino as a soundcard.
It is very easy to use the Arduino as a tone generator, which you could feed into the line in or microphone input of a laptop or desktop PC. You will need a passive voltage divider/ low pass filter to clean up the signal.
It is not easy to have the Arduino simulate an arbitrary USB slave device, and I've not heard of a library that generates a "USB audio signal".
If I just need an audio generator, a 555 timer circuit is we cheaper and easier. I could use a custom chip to generate the USB interface. The PCM2796 looks like a pretty simple project. It’s a $10 IC however and would still need a tone generator. Looking at the specifications for USB sound cards it seems like most of it would not be used by my project. It’s been a very long time since I had to write driver software but making a single function library for a Trinket M0 might be doable. The specification is here: https://www.usb.org/sites/default/files/audio10.pdf
That is a lot of effort. I’ll keep digging in. This is a spare time project so it will be start and stop. I’ll post back here if I have any success.
To send a USB audio signal from an Arduino, you can use the Arduino MIDIUSB library. This library allows you to create a USB MIDI device that can be used to send and receive MIDI messages. MIDI messages can be used to control musical instruments and other electronic devices, but they can also be used to send audio data.
To send a sine wave tone over USB using the Arduino MIDIUSB library, you can use the following code:
`C++#include <MIDIUSB.h>
// Define the frequency of the sine wave tone
const int frequency = 750;
void setup() {
// Initialize the MIDIUSB library
MIDIUSB.begin();
}
void loop() {
// Send a MIDI Note On message with the specified frequency
MIDIUSB.sendNoteOn(frequency, 127);
// Delay for 10 milliseconds
delay(10);
// Send a MIDI Note Off message with the specified frequency
MIDIUSB.sendNoteOff(frequency, 0);
// Delay for 10 milliseconds
delay(10);
}`
This code will send a sine wave tone at the specified frequency over USB every 20 milliseconds. You can adjust the frequency and delay to create different tones and rhythms.
To connect the Morse key to the Arduino, you can connect it to a digital pin with a pull-up resistor. When the Morse key is pressed, the digital pin will go low. You can then use the Arduino to detect when the Morse key is pressed and send the appropriate MIDI messages.
The following code shows how to detect when the Morse key is pressed and send the appropriate MIDI messages:
`C++int morseKeyPin = 2;
void setup() {
// Set the Morse key pin as an input with a pull-up resistor
pinMode(morseKeyPin, INPUT_PULLUP);
// Initialize the MIDIUSB library
MIDIUSB.begin();
}
void loop() {
// Read the Morse key pin
int morseKeyState = digitalRead(morseKeyPin);
// If the Morse key is pressed
if (morseKeyState == LOW) {
// Send a MIDI Note On message with the specified frequency
MIDIUSB.sendNoteOn(frequency, 127);
} else {
// Send a MIDI Note Off message with the specified frequency
MIDIUSB.sendNoteOff(frequency, 0);
}
// Delay for 10 milliseconds
delay(10);
}`
This code will send a sine wave tone at the specified frequency over USB whenever the Morse key is pressed. You can adjust the frequency and delay to create different tones and rhythms.
This is just a basic example of how to send a USB audio signal from an Arduino. You can modify the code to create your own custom Morse code sender.