Is it possible to use a microcontroller?  - Page 1
Forum section: electronics / beginners

author: LoveLaika
author_rank: Frequent Contributor
timestamp: August 10, 2021, 03:10:50 pm
content: I've recently finished building a circuit board. Long story short, it takes 4 200 MHz signals and uses op-amps to do some simple math on them (4 inputs, 3 outputs with a max output of +8/-8 volts). I don't know if you need more details about the project to give a good answer, but I'm just thinking of this from a high level POV. While I hope the board works, I can't help but think about other ways I could have done it. So, that got me thinking: is it possible to use a microcontroller to accomplish the same thing, to replace the op-amps and just slap on a microcontroller and let software handle the math? Thinking about it more and more, I kind of went down a bit of a hole, so I wanted to get some opinions here.On a high level, it seems like a good idea. With microcontroller boards having analog inputs and outputs, my inputs can just go into the board; software does the math and outputs the respective voltages. Plus, assuming that the board has lots of analog input channels, that would allow for more versatility in the future. I could just reprogram the microcontroller to more or less do whatever I want.Once I started thinking more and more, it became more complicated. Looking at the Teensy 4.1, its processor runs at 600 MHz, and the 18 analog inputs go directly into the microcontroller itself. I may be totally wrong about this, but I'm assuming that processor speed is enough to handle my 200 MHz signals. Unfortunately, the Teensy 4.1 has no analog outputs, so that's a bust. Plus, looking at the max voltage ratings for GPIO, assuming that I could somehow hack a pin to be an output, it's limited to having a max voltage of VCC (3.6 volts). Looking at the next thing, the Teensy 3.6, it has 2 analog outputs, and its analog I/O pins go straight to the processor. However, the processor runs at 180 MHz (unless you decide to overclock the thing), but you're also limited to the IC's max voltage.So, obviously, my choice of microcontroller board doesn't meet my requirements. Nevertheless, thinking about it like how I did above, am I on the right track? Given the voltage limitations of the microcontroller (and the I/O availability), there would most likely need to be some kind of circuitry to output the correct voltage, so I can't just go straight to an output connector from the microcontroller board without something in the middle. Plus, there may be some latency in the software that I'm not accounting for. Nevertheless, could it work with a little tweaking?Teensy 4.1:https://www.pjrc.com/store/teensy41.htmlTeensy 3.6:https://www.pjrc.com/store/teensy36.html

author: dferyance
author_rank: Regular Contributor
timestamp: August 10, 2021, 03:19:23 pm
content: That sounds more like you should be looking at DSP processors. They are designed more to this kind of use case than a microcontroller. You very well could need external ADC and DACs for your needs though.

author: dietert1
author_rank: Super Contributor
timestamp: August 10, 2021, 03:28:45 pm
content: If you mean "200 MHz bandwidth" you can do that with a FPGA. Sampling rate will be at least 400 MHz, more likely 1 GHz, so the ADCs and DACs will be special (expensive), not something you buy inside a cheap MCU. Also at 200 MHz you will likely use terminated signal lines and +/- 8 V into 100 Ohm or 50 Ohm is something expensive, too. A digital solution will add extra delay, in case it matters.Regards, Dieter

author: Just_another_Dave
author_rank: Regular Contributor
timestamp: August 10, 2021, 03:29:18 pm
content: If you are using a digital output, you can use an external mosfet (connecting its input to the desired voltage and adding a pull down resistor). In case it is an analog one, an external op-amp might be enough to amplify it (if the analog output is generated using a PWM, a low pass filter might also be required, although in most cases it won’t)On the other hand, the latency might be a problem if the circuit is closing a control loopNevertheless, your solution will probably work fine with little tweaking. Have you considered building your own controller board? Nowadays it is possible to find cheap microcontrollers that have analog outputsEdit: I didn’t notice that a 200MHz bandwidth was required. As other have stated an FPGA might be more adequate for that purpose

author: LoveLaika
author_rank: Frequent Contributor
timestamp: August 10, 2021, 04:17:47 pm
content: Thanks for replying. To be clear, this isn't something I'm actively pursuing. It's just an idea that popped into my head, and I can't help but think about it more and how to make it work.On that note, adding ADCs and DACs into the mix seems to be troublesome. I was hoping that there would be a small low-cost board that anyone could get their hands on, but it doesn't seem like it. There doesn't seem to be any low cost DSPs. I found this interesting little site which seems interesting to say the least, though its applications seem focused on audio.https://freedsp.github.io/

author: LoveLaika
author_rank: Frequent Contributor
timestamp: August 10, 2021, 04:31:31 pm
content: Thanks for your reply. An FPGA is a route that I haven't considered. I see that an Arduino-branded FPGA (MKR VIDOR 4000) and TinyFPGA boards exist, so it doesn't seem outside the realm of possibility. Question is, is an FPGA fast enough? With an FPGA, I suppose you could add a clocking module to get any clock speed that you want if I remember correctly, but you would still have to add external circuitry in order to make sure that the I/O are within the correct voltage range, or boost it to the range that you want.Latency is an issue to be sure, so in that sense, any microcontroller or FPGA is out of the question. This makes me wonder, when is it a good idea to use a microcontroller/FPGA?

author: LoveLaika
author_rank: Frequent Contributor
timestamp: August 10, 2021, 04:39:40 pm
content: Thanks. I never really considered making my own board. I was hoping that there would be a straightforward, off-the-shelf board that would provide a lot of versatility.If I may, putting aside project requirements such as bandwidth and the like, for basic analog processing, having it be handled by a microcontroller or FPGA seems very appealing. Looking at some of the small-sized boards, what would be their limitations if we just connect signals straight to the board (and therefore to the microcontroller)? Processor clock speed? Maximum voltage input/output? On paper, it sounds great, but taking these factors in, more work needs to be done to make it more versatile.

author: Just_another_Dave
author_rank: Regular Contributor
timestamp: August 10, 2021, 04:58:49 pm
content: Quote from: LoveLaika on August 10, 2021, 04:39:40 pmFor achieving that bandwidth, processor clock speed and cycles required to complete each instruction will surely be one of the major limitations. Using a dsp helps with that, as they are able to execute several instructions simultaneously and they normally have 1 cycle multiply (or multiply and accumulate) instructions. In fpgas, clock speed might be important, but it will probably be less critical as hardware allows implementing more parallel algorithms. Therefore, the amount of CLBs and the availability of dedicated hardware for implementing multiply and accumulate operations might be your main limitations.Another important factor is that ADCs require time to perform measurements and, if you need a high resolution input at that sampling frequency, they might be quite expensives. Sigma-delta ADCs can be really fast, but they just provide a 1 bit output (some modules include a decimation to provide a higher resolution)

author: LoveLaika
author_rank: Frequent Contributor
timestamp: August 10, 2021, 06:03:39 pm
content: That's a lot of limiting factors right there. It certainly seems simpler to just build a circuit for it then, rather than let a microcontroller handle it. This doesn't sound like something a small, $100 Arduino-like board can handle, especially if dedicated hardware is necessary; nor does it sound like something I'm comfortable working on. It feels a little outside of my scope.Maybe I just can't let this go, but this does make me think a lot. So, key thing that limits everything more or less is bandwidth. The smaller the bandwidth, the more likely an Arduino or a small FPGA can handle it. So, say if I stuck to an Arduino Uno for its simplicity, what would be the limiting bandwidth? This idea sounds really flexible and very versatile, though I can't imagine anyone not having an idea like this before.

author: dietert1
author_rank: Super Contributor
timestamp: August 10, 2021, 06:17:23 pm
content: Arm Cortex MCUs with their DSP capabilities have been used to implement digital audio systems with sampling rates of 96 or 192 KHz. In a time interval of 5 usec a 400 MHz processor will execute about 2000 instructions. Sufficient to implement filter algorithms for several filters, like in a cross-over or an equalizer. Even enough for a simple DRC (digital room correction). But again: reasonable quality audio ADCs and DACs will be separate from the MCU.The built-in ADCs of MCUs are better for simple monitoring tasks, like CPU temperature monitoring. The presence of multimillion gate digital circuitry on chip will always cause extra noise and jitter, except for very low bandwidth like 100 Hz. Then you can have separate time slots for the digital and analog circuitry to reduce disturbance. We can record ECG or pulse oximetry signals or measure mains energy consumption without external ADC.Regards, Dieter

author: Renate
author_rank: Super Contributor
timestamp: August 10, 2021, 06:39:30 pm
content: Are you making an oscilloscope or something?What is "math"? Addition? Subtraction? Multiplication? Logarithm? FFT?200 MHz carrier morse code can easily be handled by an Arduino (with external mod/demod).DC-200 MHz is more difficult.

author: rstofer
author_rank: Super Contributor
timestamp: August 10, 2021, 09:20:19 pm
content: Quote from: LoveLaika on August 10, 2021, 06:03:39 pmFor the ATmega328, the maximum sample rate is 76.9k samples per second. That assumes that all other code and hardware peripherals don't get in the way - like converting and printing the samples over the serial port at some slow baud rate.https://embedds.com/adc-on-atmega328-part-1/The real question is how many samples does it take to faithfully reproduce the incoming waveform. For this, a little Fourier Analysis may be necessary but for a square wave, the maximum would be about 1/5th the stated value or 15 kHz and that's not going to be a great reproduction. Maybe 7 kHz for the incoming frequency would look better.

author: LoveLaika
author_rank: Frequent Contributor
timestamp: August 10, 2021, 10:37:25 pm
content: Quote from: Renate on August 10, 2021, 06:39:30 pmFor a lack of a better explanation, think of it as a dynamic platform for analog signals, where a microcontroller takes in analog signals, performs some calculations on them (for simplicity, let's limit it to addition and subtraction in this case), and outputs the appropriate analog sum/difference. This isn't something I'm working on now, just a concept that popped into my head and now I'm curious as to whether or not it is possible. Something like this sounds very versatile, so that's why I'm curious about it.

author: LoveLaika
author_rank: Frequent Contributor
timestamp: August 10, 2021, 10:50:21 pm
content: Quote from: rstofer on August 10, 2021, 09:20:19 pmFrom what I recall, Nyquist sampling says that we have to sample a signal at more than 2x the bandwidth. How did you come up with 1/5th of the sampling rate for a square wave? Did it have to do something about the components that make up the square wave?

author: rstofer
author_rank: Super Contributor
timestamp: August 11, 2021, 12:50:47 am
content: Quote from: LoveLaika on August 10, 2021, 10:50:21 pmBut bandwidth is also related to the rise and fall times. BW = 0.35 / RThttps://www.thorlabs.com/newgrouppage9.cfm?objectgroup_id=9817By sampling higher and higher harmonics, we can reduce the apparent rise and fall times at a cost of increasing bandwidth.Nyquist also requires a hard bandwidth limitation before sampling and that's the problem. A square wave is composed of perhaps a DC component, the fundamental frequency and all of the odd harmonics at diminishing amplitude clear out to daylight. Maybe we cut it off at the 9th harmonic or wherever. This harmonic frequency is what you would sample at 2x. But technically, for Nyquist to apply, there can be no harmonics beyond the 9th, in this case.Here is a nice animation of the Fourier series being used to construct a square wave. Pick any 'quality' of waveform that you desire. 3rd harmonic? 5th, 7th, 9th?The thing to remember about sampling a square wave is that the waveform contains all of the odd harmonics from DC to daylight limited only by external circuitry.

author: brucehoult
author_rank: Super Contributor
timestamp: August 11, 2021, 07:06:44 am
content: Quote from: LoveLaika on August 10, 2021, 03:10:50 pmI wouldn't have thought op-amps with high accuracy at 200 MHz would be cheap. What actual characteristics do they have? (or, which op-amps?) Are you talking about under 1% accuracy here, or something more rough and ready?If you're really doing the math with a resister network or something, and just using the op-amps to buffer signals then you're not going to do better, because a microcontroller or FPGA will want some op-amps or similar to do level conversion and buffering too.200 MHz analogue is not a very common requirement outside of expensive test equipment.

author: Doctorandus_P
author_rank: Super Contributor
timestamp: August 11, 2021, 11:18:47 am
content: If the signal you want to control has a frequency of upto 200MHz, then you want a control loop which has at least 3 times (and preferably 5 to 10 times) the bandwidth.(You need some headroom above the 2x nyquist limit)But it's not only nyquist. For a control system you want a phase margin of 45 degrees or lower. which implies your control loop has to run at least at 8x the signal frequency.This means your control loop has to work above 1 GHz, and you have less then 1ns to do:1. ADC conversion.2. Math.3. DAC conversion.And that is not one of those three steps, but all three of them, and not just the conversion speed of the ADC's but also the settling time of the ADC's and surrounding signalling conditioning circuitry.So it's never going to work with a microcontroller that runs at 600MHz.With a microcontroller that runs at 600MHz you're lucky if you can get a control loop working at a few MHz.Somewhere around 1MHz would be a safe limit. That would mean you have 600 clock cycles to do the I/O and math. But it depends a lot on how much math you have to do, and if your microcontroller has a math coprocessor and DSP extensions.On the software side, you can dedicate the whole microcontroller to the task. Easiest way to do that is to run an ADC at a fixed rate, do software polling for the ADC, and then run the control loop as soon as data is available. Or, if you also want your uC to do other tasks (such as handling some I/O for keyboard and display) then the control loop is probably best done in an ISR. And you have to add the overhead for an ISR to your timing. (Another solution is to use a microcontroller with multiple cores, and dedicate a core to your control loop.==========But as this is posted in the beginners section...I have some doubts about the 200MHz bandwidth.An opamp with a 200MHz gain bandwidth product is not going to be able to handle 200MHz signals properly.So I'm curious, what opamps are you using?A 200MHz analog signal is even difficult to route from one side of a PCB to the other side of the PCB accurately, and measuring it with a 200MHz bandwidth oscilloscope would also be very marginal. Note that the stated bandwidth of an oscilloscope is usually specified as -3dB, which CAN mean you already lost half the signal when it's displayed on your oscilloscope screen. (Although most scopes are better in this regard these days).With lower signal frequencies on my 50MHz DS1052E I can see significant signal changes if I hold the probe with one hand, and then move the probe cable around with the other hand.