Bora Acer
projects
Laser Data Transfer

Laser Data Transfer

7mo ago·10 min read
ESP32Embedded C

Basic Optical Communications.


LaserPhone: A Laser-Based Audio Walkie-Talkie

ECE 1100 Individual Discovery Project

The Idea

I wanted to build a walkie-talkie that sends voice over a laser beam instead of radio. Two small units, each one modulates its laser with compressed audio and picks up the other unit's laser with a photodiode. Headphones plug into each unit so you hear the person on the other end.

The reason I picked this is because I like networking and this intrigues me. No radio waves, no Wi-Fi, no Bluetooth. It has to be line-of-sight which makes it directional, and it sounded like a fun way to learn about analog circuits, signal processing, and embedded systems all at once.


System Architecture

Each unit is an ESP32-S3-DevKitC-1-N8R8. It runs both paths at the same time, so both ends can talk and listen at once. The mic and speaker use separate I2S peripherals, and the laser and photodiode don't conflict with each other either.

LaserPhoneV1.jpg


The Frame Protocol

I had to design a simple packet format so the receiver knows where each audio chunk starts in the bit stream:

The preamble is three bytes of alternating 1s and 0s which gives the receiver time to lock onto the bit timing. The sync byte tells it where the real data begins. Sequence numbers let me detect dropped packets so Opus can fill in the gaps. The CRC catches bit errors so garbage doesn't get played as audio.


Software Architecture

Three tasks pinned to specific cores so they don't fight each other. Mic encoding runs with Opus TX, the receive slicer gets its own core because it needs tight timing, and the decoder runs whenever the queue has something in it.

What Worked

I ended up with a lot of pieces that do real work:

A full diagnostic test suite with seven tests that runs on boot. Laser blink, photodiode sanity check, Goertzel tone detection, OOK (On-Off Keying) bit error rate sweep, mic self check, speaker tones, and a full loopback. Whenever something broke I could run this and figure out which part of the chain was the problem.

The optical link really does work. I can transmit known byte patterns across the beam and decode them correctly. At 100 bps there are zero errors. At 500 bps I get about 0.3% of frames through, and seeing the first rx_ok=1 in the serial log after days of debugging felt amazing.

Swapping the analog mic for an INMP441 digital I2S mic was the right call. It outputs 24-bit samples directly with no analog bias or noise to worry about. Ratio of 29x between silent and speaking means it picks up my voice cleanly. However this is also where I learned that analog signals and breadboards don't quite mix which would continue to create problems for me.

The full Opus pipeline is wired up with packet loss concealment. When a frame gets dropped the decoder makes up a believable replacement from the audio before it. If frames were arriving reliably, it would genuinely sound good.

The three FreeRTOS tasks run forever without stepping on each other, without deadlocking, and without tripping the watchdog. That took a while to get right.

The moment the first frame made it through, after days of debugging:

I (36507) AUDIO: [  32s] tx=128+4  rx=1+1 bad=0 plc=0 | bits/s=485 edges/s=262 AAAA=5 rb=0xffdf v=[1705..2079]
I (37532) AUDIO: [  33s] tx=132+4  rx=1+0 bad=0 plc=1 | bits/s=473 edges/s=258 AAAA=6 rb=0xd7dd v=[1704..2024]

rx=1+1 was the first time a frame had ever survived the full trip through the beam with a correct CRC. I stared at that line for a while.


What Didn't Work (and What I Learned)

This is where most of the learning actually happened. Every one of these took hours to figure out.

The analog mic preamp was hopeless

My first mic circuit was an LM358 op-amp running as a 33x inverting amplifier with an electret mic. When I ran the self-test, the peak-to-peak reading when I was talking was the same as when I was silent. The signal was buried in noise.

Two things were wrong. The mic bias resistor I had was 200 ohms when it should have been around 10k. And the 1.65V virtual ground that the op-amp uses as its reference didn't have a bypass capacitor, so any noise on the 3.3V rail coupled straight into the output through the gain of 33. I2S switching noise was basically the loudest thing in the system.

I eventually gave up trying to fix it and switched to a digital mic. Lesson learned: single-supply op-amp circuits need a really clean virtual ground, and mic circuits are brutal to get right without lab equipment.

The feedback resistor was a Goldilocks problem

The transimpedance amplifier that turns photodiode current into a voltage has a feedback resistor Rf. I tried three values before finding the right one:

RfSignal eyeBandwidthResult
1 MΩ1842 counts~1 kHzBig signal but too slow for audio
10 kΩ24 countswideFast but signal disappeared into noise
100 kΩ170 counts~30 kHzJust right

Too much gain kills your bandwidth. Too little gain and your signal is below the noise floor. I only really got this intuition after screwing it up twice.

ADC calibration in the wrong mode

I calibrated my slicer thresholds using adc_oneshot_read at boot, then the actual running code used adc_continuous_read through DMA for speed. I figured they'd give the same readings. They don't. The two ADC driver paths in ESP-IDF have different sample and hold times, so they return slightly different raw counts for the same voltage.

My threshold was calibrated against the wrong reference frame. The slicer was set up to trigger on 1842 counts but the runtime signal was centered at 1850-ish. Small difference but enough to mess up decoding completely. Moved the calibration inside the continuous stream and it started working.

The ADC ringbuffer ambushed me

Related to above. When I moved calibration to continuous mode, it still didn't work. Turns out adc_continuous_read returns the oldest samples in the buffer first (FIFO), and after I toggled the laser from OFF to ON during calibration, the buffer still had old OFF samples in it. So my "ON" measurement was actually reading old OFF samples. Had to add aggressive drain loops to flush the buffer between state changes.

The LM358 was always going to lose

After fixing every software bug, every calibration issue, and adding every missing bypass capacitor, I hit a wall at about 500 bps. The problem is physics. The LM358 has a slew rate of 0.5 V/µs and a gain-bandwidth product of 1 MHz. When I modulate the laser at 15 kbps, each transition produces hundreds of counts of ringing on the op-amp output that takes hundreds of microseconds to settle. By the time it settles, the next bit has already started.

I realized you can't software-engineer your way around analog physics. The correct fix is to swap the LM358 for an MCP6292, which is pin-compatible but has 10x the slew rate. One dollar chip swap. I ordered some but they didn't arrive in time for the showcase.


ECE Skills I Picked Up

Analog circuits. I actually understand now why op-amps need feedback caps for stability, what a transimpedance amplifier does, why every op-amp needs a bypass cap next to it, and what happens if you forget one. I learned to read op-amp datasheets and care about things like slew rate and gain-bandwidth product that I used to skim past.

Embedded development. ESP-IDF and CMake build system, the component manager for adding libraries like Opus, and how to tune sdkconfig for PSRAM and flash settings. FreeRTOS tasks, priorities, core pinning, and watchdog debugging.

ADC and I2S. There are two ways to read an ADC on this chip and I now know when to use which. I set up I2S separately for the MEMS mic and for the speaker amplifier, which meant dealing with two different I2S ports doing RX and TX simultaneously.

Digital communications. On-off keying, framing protocols, preambles, sync bytes, CRC checking. Writing a bit slicer with Schmitt trigger hysteresis and phase recovery. Measuring bit error rates with PRBS patterns.

DSP. I implemented a Goertzel algorithm for single-frequency tone detection, integrated an Opus codec with packet loss concealment, and learned the hard way about sample rate and clock domain mismatches between peripherals.

Debugging. Honestly the biggest skill. Every failure above got fixed because I added telemetry (counters, runtime min/max values, state dumps) to see what was actually happening instead of guessing. Measure first, fix second.


Final Thoughts

The goal was real-time voice over laser. I didn't quite get there. The hardware caps out at about 500 bps of reliable throughput and Opus needs closer to 8000. But I got everything else working. The laser modulates, the photodiode demodulates, Opus frames encode and decode, and one of my frames made a full round trip through the beam with a correct CRC. That one frame was worth a lot of hours of debugging.

Things I'd do next:

Swap the LM358 for the MCP6292 op-amp that's on the way. That single change should push the link from 500 bps to around 20 kbps which is more than enough for Opus. Everything else in the code is ready.

Build the second unit properly and have an actual conversation over the beam.

Add automatic gain control on the receive side so aiming isn't so touchy.

Put it in a real enclosure with a lens mounted in a tube, instead of a bare breadboard.

This project convinced me I like embedded systems and the place where analog meets digital. Every hard problem was at that boundary. Writing code is one thing, but getting the code to actually agree with what the physical circuit is doing is a different skill, and I feel like I got a lot better at it this semester.

I'm keeping the hardware. When the new op-amps arrive I'll try again.


Here is what the receive stats look like during a successful burst, with the sliding preamble pattern visible in rb and repeated AAAA matches as the slicer locks onto the bit timing:

I (22903) AUDIO: [  12s] tx=12+1  rx=0+0 bad=0 plc=1 | bits/s=1398 edges/s=1591 AAAA=142 rb=0xabf7
I (29323) AUDIO: [  16s] tx=16+1  rx=0+0 bad=0 plc=1 | bits/s=1330 edges/s=1549 AAAA=89  rb=0x5555
I (45373) AUDIO: [  26s] tx=26+1  rx=0+0 bad=0 plc=1 | bits/s=1390 edges/s=1494 AAAA=93  rb=0x5354

The 0x5555 and 0xabf7 in the rb column are the 16-bit sliding window of decoded bits snapping onto the 10101010... preamble pattern.


Command Palette

Search for a command to run...