Audio Out Over A UART With An FTDI USB-To-TRS Cable

What is the easiest way to get audio from a WAV file into a line-level format, ready to be plugged into the amplifier of a HiFi audio set (or portable speaker)? As [Konrad Beckmann] demonstrated on Twitter, all you really need is a UART, a cable and a TRS phono plug. In this case a USB-TTL adapter based around the FTDI FT232R IC: the TTL-232R-3V3-AJ adapter with 12 Mbps USB on one end, and a 3 Mbps UART on the other end.

[Konrad] has made the C-based code available on GitHub. Essentially what happens underneath the hood is that it takes in a PCM-encoded file (e.g. WAV). As a demonstration project, it requires the input PCM files to be a specific sample rate, as listed in the README, which matches the samples to the baud rate of the UART. After this it’s a matter of encoding the audio file, and compiling the uart-sound binary.

The output file is the raw audio data, which is encoded in PDM, or Pulse-Density Modulation. Unlike Pulse-Code Modulation (PCM), this encoding method does not encode the absolute sample value, but uses binary pulses, the density of which corresponds to the signal level. By sending PDM data down the UART’s TX line, the other side will receive these bits. If said receiving device happens to be an audio receiver with an ADC, it will happily receive and play back the PDM signal as audio. As one can hear in the video embedded in the tweet, the end result is pretty good.


If we look at at the datasheet for the TTL-232R-3V3-AJ adapter cable, we can see how it is wired up:

When we compare this to the wiring of a standard audio TRS jack, we can see that the grounds match in both wirings, and TX (RX on the receiving device) would match up with the left channel, with the right channel unused. A note of caution here is also required: this is the 3.3V adapter version, and it lists its typical output high voltage as 2.8V, which is within tolerances for line-level inputs. Not all inputs will be equally tolerant of higher voltages, however.

Plugging random TRS-equipped devices into one’s HiFi set, phone or boombox is best done only after ascertaining that no damage is likely to result.  Be safe, and enjoy the music.

Cute USART Trick Brings PWM To IR LEDs

We love little tricks like this. Suppose that you want to generate an IR remote’s signal. It’s easy, because most of the codes are known. But it can be slightly harder because most IR remotes and receivers modulate the on pulses with a square wave at roughly 38 kHz for background lighting immunity.

With a competent PWM generator on a microcontroller, you can create this carrier modulation easily enough yourself. Set the PWM frequency to 38 kHz and the duty cycle somewhere in the 33%-50% range, and you’re set. But what if you don’t have a competent PWM generator? Such was the case that prompted [AnalysIR Blog] to fake it, with USART.

Here’s the trick. You set up the serial port to communicate at ten times the desired carrier frequency, and then transmit “special” data. (The number ten comes from eight bits of data plus a start and a stop bit.) If you want a 50% duty cycle, you simply send 0b11110000, as fast as the microcontroller will allow, for a mark and nothing for a space.

There’s some extra detail with inverting the signal if, as most do, your USART idles high. But that’s really it. It’s a cute trick for when you’re desperate enough to need it. And if you’d like to brush up some more on your asynchronous serial skills, check out our guide on troubleshooting USART, and the great comments that ensued.

Serially, Are You Syncing Or Asyncing?

I know you’ve heard of both synchronous and asynchronous communications. But do you really know the differences between the two?

Serial communication was used long before computers existed. A predecessor is the telegraph system using Morse Code, one of the first digital modes of communication. Another predecessor is the teletype, which set standards that are still used today in your Arduino or Raspberry Pi.

All you need is two wires for serial communications, which makes it simple and relatively robust. One wire is ground and the other the signal. By interrupting the power with predefined patterns, information can be transferred over both short and long distances. The challenge is receiving the patterns correctly and quickly enough to be useful.

I was a bit surprised to find out the serial port on the Arduino Uno’s ATmega328P microcontroller is a Universal Synchronous Asynchronous Transmitter Receiver (USART). I’d assumed it was only a UART (same name, just leave out synchronous) probably because my first work with serial communications was with the venerable Intel 8251 “Programmable Communication Interface”, a UART, and I didn’t expect the microcontroller to be more advanced. Silly me. Later I worked with the Zilog 8530 Serial Controller Chip, a USART, the term I’ll use for both device types.

All these devices function in the same way. You send a byte by loading it into a register and it is shifted out one bit at a time on the transmit (TX) line as pulses. The receiver accepts the pulses on a receive (RX) input and shifts them into a register, which is then read by the system. The transmitter’s job is pretty easy it just shifts out the bits at a known clock rate. The receiver’s task is more complex because it needs to know when to sample the incoming signal. How it does this is the difference between asynchronous and synchronous communications.

Continue reading “Serially, Are You Syncing Or Asyncing?”

What Could Go Wrong: Asynchronous Serial Edition

It’s the easiest thing in the world — simple, straightforward serial data. It’s the fallback communication protocol for nearly every embedded system out there, and so it’s one that you really want to work when the chips are down. And yet! When you need it most, you may discover that even asynchronous serial can cost you a few hours of debugging time and add a few gray hairs to your scalp.

In this article, I’m going to cover most (all?) of the things that can go wrong with asynchronous serial protocols, and how to diagnose and debug this most useful of data transfer methods. The goal is to make you aware enough of what can go wrong that when it does, you’ll troubleshoot it systematically in a few minutes instead of wasting a few hours.

Continue reading “What Could Go Wrong: Asynchronous Serial Edition”

Meizu MX Rooted Using The Bus Pirate

It is just amazing how small the boards are for some really powerful smart phones. For instance, the diminutive size of this Meizu MX Android phone’s board is only outshone by the intricate packaging the phone arrived in. [Adam Outler] did an unboxing of the device. But for him that mean tearing down all of the components and using a Bus Pirate to root the device.

In the video after the break he gives us a candid look at what it takes to exploit this piece of hardware. You might be a little spooked by the commands, which he reads aloud character by character, but watch closely and you’ll see they’re really quite common functions.

His rooting quest began by reading the datasheet for the main processor to find the USART parameters. With that information he hooked his Bus Pirate to ground, then probed around various test points on the board while it was rebooting until serial data started scrolling on the screen. He had found the USART lines and soldered a breakout connector onto them so that he had access after reassembling the phone.

From there he used the Bus Pirate to merge with the board’s terminal, then rebooted the phone using the Android Debug Bridge. Once it fires up, the Bus Pirate terminal window is sitting at a root prompt (many companies disable this but [Adam] was lucky). He remounts the internal file system to be rewritable, then uses the ADB to push the Linux substitute user (su) command onto the device as it will be needed by the Superuser.apk program. That is the next thing to be installed and once it is he officially has root.

Continue reading “Meizu MX Rooted Using The Bus Pirate”

AltSoftSerial Looks To Speed Up Arduino Software UART

So let’s say your using an Arduino in your project. You already have the hardware-based serial interface working with one portion of the project and need a second serial port for unrelated hardware. The obvious solution is to write one in software. But this is a place where working in the Arduino environment gets really hairy. Since there’s a layer of abstraction between the code and the hardware interrupts, it can be difficult to know if you are going to have timing problems. But there’s a new library available which seeks to reduce the latency of software-based serial communications so that you don’t have to worry about it.

It’s named AltSoftSerial because it is a software-based serial library that is an alternative to the NewSoftSerial package. The former can function with just  2-3 microseconds of latency, while the latter has as much as a 174 microsecond hit. If it functions as advertised that’s quite an improvement. It’s not hard to put together a hardware test platform, and the example program is only about a dozen lines of code (which is the beauty of working in this environment) so give it a try if you have a free hour here or there.

ARM Programming Primer; Getting The USART Running

We find it interesting that PIC and AVR programming is very common in hobby electronics but ARM doesn’t have nearly the same foothold. This is partly because there’s a knowledge barrier involved with making the transition (the other part is probably the lack of DIP packaged chips). But if you’ve worked with 8-bit microcontrollers you can certainly make the jump into the 32-bit realm. Here’s a great opportunity to get your feet wet. This guide will show you how to get the USART on an STM32 Discovery Board working, which makes it easy to get feedback about what’s going on in your program.

One difference you’ll notice when moving to ARM microcontrollers is that there is almost always a library bundle available from the manufacturer which includes all of the functions you need for hardware control (USART, USB, Ethernet, ADC, etc.). That’s the case here, so simply including the USART library makes it a snap to finish the rest of the program. Once you hook up your communications hardware (an FTDI cable in this case) just use the library initialization functions, followed by the send and receive commands and you’ll be pushing messages to a computer terminal in no time.

If you’re trying to use the STM32 Discovery Board with a Linux box here’s a shove in the right direction.