# Fast PWM and Electromagnetic Interference

## fun with ATMega328 and rtl-sdr

Yesterday came to my attention a project that turns the Raspberry Pi into a FM transmitter. It is totally awesome and you should check it out.
While reading an article about it, I thought to check ATMega328 and its fast PWM mode I showed you in the previous article for electromagnetic interference (EMI). The process is extremely easy and it shows how far the the hobbyist electronics have come. I loaded the code of the previous article to an Arduino Pro Mini, connected a wire on pin 5 to act as an antenna, fired up gqrx and that was about it.
Below is a screenshot from gqrx, with automatic gain control (AGC) turned off, showing the 9th harmonic of the signal.

## SDR, GNU Radio, harmonics and other funny words.

I am not the best person to explain these, so I’ll keep it short and let you do more research on your own.

Every signal can be approximated by sums of trigonometric functions (sines and cosines). What this means in our case, is that a pulse (the PWM output) is essentially the sum of many sine waves. These sine waves come at frequencies (called harmonics) that are integer multiples of our main frequency. In our case the PWM runs at 8MHz, so we expect to see harmonics at 8MHz (1st), 16MHz (2nd), 24MHz (3rd), …, 80MHz (10th), etc. A perfect square wave would have only odd-integer harmonics, but if you watch our PWM in an oscilloscope, calling it square —or rectangle— is a bit of a stretch.
Anyway, these frequencies belong to the radio part of the electromagnetic spectrum, so in order to capture and visualize them we need a radio tuner/receiver and an antenna. Enter the world of software defined radio (SDR), GNU Radio and rtl-sdr. Usually radio processing happens in hardware because it is a better fit due to processing power needed but, when you transfer the radio processing to software (hence SDR), you get all this tremendous potential since you can do anything you want. Hardware processing is mostly firmware locked and may only include the necessary functions for its given task, or use a difficult language (i.e. DSP’s assembly).
GNU Radio is a free software toolkit to help you implement SDRs. Few years ago, some people discovered that certain, cheap USB TV tuners could get into debug mode, where they could tune to a wide range of frequencies and transfer the captured signal to the user’s PC. These tuners use Real tek’s RTL2832U chip. The rtlsdr project was created to implement SDR drivers for these tuners as well as a source block for GNU Radio, hence cheap SDR receivers for everyone. Woo—hoo!