4 ms·
I used the ESP32 to build a two lead ECG SD logger with real time viewing over wifi. I randomly had to get a pacemaker this year, and it's been very useful to s
by sgtnoodle 6y ago
I used the ESP32 to build a two lead ECG SD logger with real time viewing over wifi. I randomly had to get a pacemaker this year, and it's been very useful to self-diagnose device issues (such as why my heart rate was jumping back and forth between 50bpm and 100bpm!) The pacemaker generates 350uS 2V "capture pulses", and they're very easy to detect coming out of an analog ECG amplifier front end. The ESP32 would completely miss maybe 1 out of every 5 capture pulses until I switched to I2S. Now it reliably samples every single pulse. The data coming out is good enough to see what I need to see; qualitatively how my heart is beating.
When using software triggered ADC conversions via an 8Khz timer interrupt, the racey issue would occur periodically for several milliseconds at a time. The ADC would act as if its input was disconnected and floating. With a changing input signal, the samples would read as a nearly constant voltage and completely miss blips that were well within the Nyquist frequency.
Switching to I2S/DMA seemed to work around that issue. Even with the ADC working reliably, the samples suffer from what I can only guess is horrendous differential non-linearity. The waveform really looks quite ugly, despite the ADC being driven by a very low impedance amplifier output. At least I can clean up the ECG plots with a low pass filter to make them pretty. The pacemaker pulse is a 350uS square wave, but the heart's natural impulses are significantly lower frequency.
- elcritch 6y agoFantastic that you're able to track down your health issues. One day I really want to get into making health tracking sensors. Oh I've read about the ESP32 ADC being pretty non-linear, but the racey issues are new to me. Do you have example code/repo for how you use the I2S to read the ADC? I'd like to add it to my ESP32 wrapper library (see `elcritch/Nesper` on GitHub)). It's mostly started as Nim wrapper's for the ESP-IDF api's, but it is letting me wrap certain errors conditions in the ESP-IDF and configure workarounds. Mostly small stuff, but handy.
- sgtnoodle 6y agoDue to all the medical scans I've had done, I can confidently say that my heart is in perfect health other than the nerves being flaky. The pacemaker is a nearly flawless replacement, but it has its technical limitations. It's been very handy to see what's going on when my heart is beating funny. Here's the relevant section. I was getting pretty fed up with battling the ESP32, so it's probably not the prettiest. http://sgtnoodle.com/files/adc_snippet.txt http://sgtnoodle.com/files/adc_snippet.txt The function runs as an RTOS task, woken up once every millisecond by a timer. I also pinned it to one of the two CPUs because I was originally doing software triggered sampling at 8Khz. The timer and pinning is rather overkill since I2S clocks the sampling and has tons of buffer. In an effort to get better data, I have the I2S sample at 40Khz. The function copies the samples out the I2S buffers, and down-samples to 8Khz. The 8Kz samples are pushed into a circular buffer that uses a std::atomic head pointer to make it thread safex.
- elcritch 6y agoThanks! I'll take a look at it and try and port it over. Hopefully it'll help some other poor souls (possibly myself) who have to deal with the same issues.