Dual Trace Oscilloscope

61,812

569

103

Introduction: Dual Trace Oscilloscope

When I build my previous mini oscilloscope I wanted to see how well I could make my smallest ARM microcontroller a STM32F030 (F030) perform, and it did a nice job.

In one of the comments it was suggested that a "Blue Pill" with a STM32F103 (F103) might be better, smaller than the development board with the F030 and possibly even cheaper. But for the mini oscilloscope I did not use the development board but the F030 on an even smaller SMD-DIP board, so there a Blue Pill would certainly not be any smaller and I doubt that it would be cheaper too.

Code is now available on Gitlab:

https://gitlab.com/WilkoL/dual-trace-oscilloscope

Supplies

Part list:
- plastic box - perfboard (double sided prototype board 8x12cm) - Blue Pill - ST7735s TFT display - lithium-ion battery - HT7333 3.3V low dropout regulator - MCP6L92 dual opamp - TSSOP8 to DIP8 board - 12 MHz crystal (not necessary) - rotary encoder plus knob (2x) - powerswitch - banana terminals (4x) - lithium-ion charger board - several resistors and capacitors - nylon spacers, nuts and screws

Tools:

- soldering station - solder 0.7mm - some wire - side cutter - glasses and loupe - drill - multimeter - oscilloscope - STLink-V2

Software:

- STM32IDE - STM32CubeMX - STLink Utility - LowLayer library - adapted library for ST7735s - Notepad++ - Kicad

Step 1: Interleave or SImultaneous Mode

Blue Pill

But the idea was there, and I knew that the F103 has two ADCs! What if I used those two ADCs together in "interleave" mode, something I have done before with the STM32F407 (F407). The sampling speed would double. That, combine that with a faster microcontroller and it would make for a great successor to the mini oscilloscope.

Interleave mode
Oddly the ADCs in the F103 are less advanced than the one in the F030 (and the F407), you cannot choose the resolution. More important is that you also cannot change the timing between the two ADCs. Now, when you use the interleave mode usually you want the sampling as fast as possible with the shortest time between any samples, but with an oscilloscope it is neccessary to change the timing. Maybe it still can be done, I'm not a professional oscilloscope designer, but I dropped the plan to use interleave-mode.

Simultaneous mode

But, having two ADCs gives many more options, the two ADCs can be set to "regular-simultaneous" mode too. How about a dual trace-oscilloscope?

Having decided to try to make a dual trace oscilloscope I also wanted to have variable input sensitivity, an option that I did not have on the mini oscilloscope. That means an attenuator (and amplifier) on the inputs. And maybe I wanted even more? So I made a small list of "nice-to-haves".

WISH LIST

two channels

variable sensitivity on both channels

triggering on both channels

variable trigger level on both channels

variable offset

single battery power

fit in the same box as the mini-oscilloscope

Step 2: Prototyping

As usual I started this projects on a breadboard. (See picture) And before soldering everything on the perfboard I try to find out if and how it will fit in the chosen project box. It fits, but only just. Some parts are hidden under the screen, other under the Blue Pill. And again, just as for most of my projects, this is a once-only project and I will not design a PCB for it.

Step 3: Attenuators

In regular oscilloscopes the input attenuators are circuits that change attenuation and amplification by switching in and out resistors with small signal relays. While I have some of those relays, I know they will not switch at less than 4 Volt, that means that they will only work with a fully loaded Lithium Ion battery (4.2V). So I needed another way to switch those resistors. Of course I could just install mechanical switches, but that would certainly no longer fit in the project box in had in mind, perhaps I could try a better digital potentiometer again (the one I have is way too noisy).

Then I thought of "analog switches", with those I can make a digital potentiometer myself. In my parts collection I found the CD4066 with four analog switches. The idea is to make the feedback resistor of an opamp variable by switching in and out resistors parallel to the feedback resistor.

It works very well, but having just 4 switches in the 4066 and having 2 channels it was not possible to make more than three sensitivity levels. I chose 500mV, 1V and 2V per division as those are the voltage levels that I use most. The screen is divided into 6 divisions, so that makes for the ranges -1.5V to +1.5V, -3V to +3V and -6V to 6V.

With the "virtual-ground" you can move these ranges up and down so even 0v to +12V is possible.

Step 4: Virtual Ground

Because the oscilloscope uses a single power rail (3.3V) the opamps need a virtual ground level or they will not work. This virtual ground level is made with PWM on one output channel of TIM4, the duty cycle of it changes from just a few percent to almost a hundred percent. A low pass filter with a 1k resistor and a 10uF capacitor transforms that into a voltage of (almost) 0V to (almost) 3.3V. The frequency of the squarewave is just under 100kHz, so the simple low pass filter is good enough.

Rather late in the building of this oscilloscope I realized that you cannot have two separate offsets for the channels. This is because of the fact that with a single power supply the input-ground-level has to be separate from the real ground level of the opamps. So both channels move in the same way as you change the GND-setting.

Step 5: Rotary Encoders and Debugging

On the mini oscilloscope I used just one rotary encoder for all functions. That would make a dual oscilloscope very difficult to use, so here I need two. One encoder for the attenuators and virtual ground level and the other encoder for the timebase and triggering. Sadly, just as in my other project, these rotary encoders are very "noisy". They are so bad that they simply would not work with timers in "encoder-mode", the standard way of reading them. I had to make a debouncing mechanism with timer TIM2, checking the encoders every 100us. This timer in turn is started (only) when there is some activity on the encoders, this is checked with the EXTI functionality on the input ports. Now the encoders work well.

And as you can see, having a display can also be very handy to display debugging information.

Step 6: Display and Timebase

The display has a resolution of 160 x 128 pixels so there are 160 samples needed for one screenfull, I managed to speed up the ADCs to do 1.6 million samples per second and that, with the much overclocked microcontroller (more on that later), gives a minimum timebase of 20us per division (100us per screen). Thus a waveform of 10kHz will fill the whole screen.

That is only twice as fast a the mini oscilloscope I made before. Oh well, now it is with two channels :-).

As said, the display is 160 pixels wide so only 160 values are needed per screen. But all buffers actually contain 320 samples. So the DMA stores 320 values before it triggers a transmission complete interrupt (TC). This is because the triggering is done in software. The sampling starts at a random moment, so it is very unlikely that the first value in the buffer is the place where the trigger point should be.

Therefore the trigger point is found by reading through the trace_x_buffer, if the value is at the wanted trigger value en if the previous value is just below it, the trigger_point is found. This works quite well, but you need a bigger buffer than the actual display size is.

This too is the reason that the refresh rate on the lower timebase settings is slower than you might expect. When you use the 200ms/div setting one screen full of data is 1 second, but because double the amount of conversions is done, that takes 2 seconds. On the faster timebase settings you will not notice it that much.

TIM3 is used to generate the timebase. It triggers the ADCs with the speed as required by the selected timebase setting. Its clock of TIM3 is 120MHz (see OVERCLOCKING), the maximum number to which it counts (ARR) determines how other it overflows or, in ST language it updates. Via TRGO these update pulses trigger the ADCs. The lowest frequency it generates is 160 Hz, the highest is 1.6MHz.

Step 7: ADCs and DMA

The two ADCs convert the voltage on their inputs at the same time, they store those two 12 bit values in a single 32bit variable. So the DMA has just one variable per (double) conversion to transfer.

To use these values it is therefore necessary to split them into the two values so they can be used to display the two traces. As said, ADCs in the F103 cannot be set to other resolutions than 12 bits. They are always in 12 bit mode and so conversions always take the same number of clock pulses. Still, with the overclocking of the ADCs , 1.6 MSamples per second can be done (see Extra: Overclocking).

The reference of the ADCs is Vdd, the 3.3V rail. To convert that to more convenient values (per division) I have calculated the values of the attenuators, because I do not have the exact resistor values that come out of those calculations some corrections are done in software.

In this project I use DMA in "regular-mode". In this mode the DMA stops transferring data (from de ADCs to memory) when the number of words (or half-words or bytes) all are transferred. In the other possible mode, "circular mode" the DMA resets itself and continues transferring data un-interrupted. That did not work with the F103, it is so fast that it overwrites the data in the adc_buffer[] before the rest of the program could read it. So now the process is as follows:

- setup DMA to the number of data to be transferred and enable DMA

- start the triggering of the ADCs, these will request DMA transfers after each (double) conversion

- after the set number of conversions are transfered, DMA stops

- immediately also stop triggering of the ADCs

- do all manipulation needed on the data in memory

- show traces on the screen

- start the process again

Step 8: User Interface

A 160 by 128 pixel screen isn't very big and I want to use as much of it as possible. So there is no part of it reserved for the currents settings. In the last few rows the vertical sensitivity, timebase, trigger level and trigger channel are displayed, but when the signals are big enough they will appear in the same area. The option that is active is shown in yellow, the rest is shown in white.

Step 9: Building and Possible Improvements

I'm pretty happy about this project. It works fine and does the job, but it could be better.

The project box is too small to fit everything in comfortably, this results in having to put components under the Blue Pill. To make that possible the Blue Pill couldn't be soldered to the "main-board" directly. And because this made it all too high I had to remove many parts from the Blue Pill, such as the jumpers for selecting BOOT0 and BOOT1 (things I never use anyway) and I even had to move the crystal from the top to the bottom of the pcb.

I made life more difficult by using banana connectors instead of BNC or SMA connectors, it meant that a big part of the perfboard was a "no-go-area", to make that clear for myself I put kapton tape over it to prevent myself from putting parts on it.

Another problem with putting it all in such a small project box is that the analog and digital circuits are very close together. You can see that there is quite a lot of noise visible on both traces. This I did not even have on the breadboard! By moving the power lines for analog and digital circuits as far apart as possible a small improvement was made, but not enough for my liking. Reducing all resistor values in the analog circuits even further than I did (the input resistance is 100kOhm instead of 1MOhm) did not help. I suspect that triggering on the fastest timebase setting (20us/div), which isn't great, will also improve with less noise on the signals.

If you make this design on a "real" pcb, with all smd parts and separate layers for analog, digital and power (that's 4 layers!) it will probably work very well. It'll be much smaller, it will not use a complete Blue Pill but just the F103 and that will make it possible to supply it with a separate (clean) analog Vdda for the ADCs.

As a final touch I decided to spray the box black, it makes a change from all the beige boxes it have.

Step 10: The Code and a Short Video

Step 11: EXTRA: Overclocking

Just as I did with the F03, I wanted to see how well a F103 can be overclocked. The specifications for this microcontroller claim that the maximum clock speed should not exceed 72MHz (which of course is already faster than the F030) but I had read in several blogs that overclocking it was easy, so why not?

The Blue Pill is provided with an 8MHz crystal, the PLL multiplies that with a factor of 9 to 72MHz. The PLL can be increased up to 16 giving a clock of 128MHz. That was no problem at all for my Blue Pill, in fact, all my Blue Pills work without any problems on 128MHz.

But now I wanted to find out what the real limit is. So I removed the 8MHz crystal and replaced it with one of 12MHz. Again I increased the PLL multiplier until the microcontroller finally gave up. That was at 168MHz ! On 156MHz it still worked well. I left it running at that speed for hours and never saw it crash. In this oscilloscope I settled for 120MHz, a speed that can be selected with a 12MHz crystal and PLL on 10, as well as with an 8 MHz crystal and the PLL on 15. (see SystemClock_Config in main.c)

The ADCs now also work faster, I have them running at 30MHz (instead of 14), they were still working well on 60MHz, STMicroelectronics makes some nice hardware!

STMicroelectronics puts these limits in the datasheet for good reason, they guarantee that the microcontroller works at the specified 72MHz under all conditions.

But as I do not use the microcontroller at -40 Celsius, +85 Celsius, on just 2.0 Volt or 3.6 Volt I think it is safe to overclock it. Do NOT do this when you intent to sell a device with their microcontrollers, you never know where they will be used.

1 Person Made This Project!

Recommendations

  • Stone, Concrete, Cement Challenge

    Stone, Concrete, Cement Challenge
  • Fandom Contest

    Fandom Contest
  • DIY Summer Camp Contest

    DIY Summer Camp Contest

103 Comments

0
cgin59
cgin59

6 days ago on Step 4

Unable to read schematic in step 4. Same with PDF. Too small and blurry...

0
cgin59
cgin59

Reply 5 days ago

Interesting.... It's blurry on my machine.

0
cgin59
cgin59

Reply 5 days ago

It's really not any better. Everything is blurred.

0
kaveh2
kaveh2

Question 4 months ago

I currently can't get my hands on a MCP6L92. Would you please tell me if you know of any substitutes?

0
WilkoL
WilkoL

Answer 4 months ago

Any low voltage and somewhat fast dual opamp will do. So a MCP6022, LMV722, OPA2316 or a LM6132 and many others are fine. Search for a Dual opamp, 5 to 10 MHz gain-bandwidth, 1.7V to 10V and not too expensive :-) A high slewrate is also nice to have.

0
kaveh2
kaveh2

Reply 4 months ago

I checked and I found MPC6022, but also I found NE5532 which has a slightly higher slewrate. I checked everything about it and I think I can use NE5532 but before I do, I'd really appreciate it if you tell me your opinion about it.

0
WilkoL
WilkoL

Reply 4 months ago

The NE5532 needs (+3V AND -3V) OR (6V) minimum according to the datasheet I read on nl.farnell.com. So you will have to feed it 6V al least.
I'd go for the MCP6022 as that one will work with a single powersupply of between 2.5V and 5.5V which is nice when your microcontroller works on 3.3V

0
skowerr
skowerr

Question 6 months ago

Hi, You wrote"With the "virtual-ground" you can move these ranges up and down so even 0v to +12V is possible.". I would like to know how to achieve 0v-5v and 0v -12v ranges?

1
WilkoL
WilkoL

Answer 6 months ago

If you put the amplifier/attenuator in the -3V to 3V range and shift the virtual ground (offset) up to the limit the measurement range will be from 0v to 6V. The same applies to the -6V to 6V range, shift the offset up all the way and the measurement will be from 0v to 12V.

0
skowerr
skowerr

Reply 6 months ago

Thank You very much :)

0
LarryG7
LarryG7

6 months ago

Well, it is a great instructable. Very thorough. But I don't understand why build one from scratch. Kits are available for as little as $15.00 with the case. Double trace? Get two.

0
WilkoL
WilkoL

Reply 6 months ago

Yeah well, I have a Rigol 4-channel scope, an (old) Hitachi 2-channel and an (ancient) Hameg 2-channel. I didn't need another one :-)
But I wanted to do something with the ADC's in a STM32 microcontroller and this was a nice project to do just that. A lot of things shown on Instructables can be bought in a shop, often of higher quality and sometimes even a cheaper. But where is the fun in that?!

Oh, and two separate scopes are often not as usefull as one dual channel, as on a dual channel you can see the relationship (in time) between the two signals.

0
LarryG7
LarryG7

Reply 6 months ago

I just think that building something like your dual trace is a lot like building another wheel. I would take the wheel and either improve it or build something that actually meant a better wheel or something beyond. ADC's are rather an old hat, today and really just a tool to use in a bigger context.

I don't have time in my life to waste building something I can buy cheaper and better and use to build a useful or needed project I couldn't otherwise afford. My biggest fear is that I will waste time I could have used to impact my life or someone else's.

Using ADC's no longer requires even understanding how they work. Just that they do. A little like it is not necessary to understand how a cordless drill works to use it. Or even a simple transistor. No one any longer cares how the transistor works, just the results.

0
WilkoL
WilkoL

Reply 6 months ago

So what better things have you made?

0
LarryG7
LarryG7

Reply 6 months ago

Well offhand, a house, and most of the furniture. I didn't build power tools out of wood. I didn't use pallet wood either. A ham radio transceiver. In the days gone by RTTY equipment and slow scan TV. The last two not impressive now and I would not build either since what is available commercially is much prettier and better. At the time I built it the RTTY equipment was state of the art. Now a computer does all the frequency conversions better, quicker and much easier. I used an oscilloscope on some of those projects. Didn't build it though.

0
WilkoL
WilkoL

Reply 6 months ago

While impressive, building your own house, I chose to simply buy one. Which was easier and as it was build by professionals of better quality than I could possibly make.
Forty years ago I bought a Kenwood tranceiver, because why should I try to rebuild that wheel... Later I did pretty much the same with my television, microwave and laptop. No need to reinvent those wheels either, right?

The thing is, I like to learn about the inner workings of ADCs, the many variants and when to use one type or the other, I like to learn about the workings of bipolar npn and pnp transistors, j-fets, n-channel and p-channel mosfets, uni-junction transistors etc and when using one type is more appropriate to use than another.

And most certainly, a modern tv is much better than one with a Nipkow disk, but I still intent to make one.
For fun.

Happy 2021

0
LarryG7
LarryG7

Reply 6 months ago

If you remember I made the statement "that I couldn't afford to buy", I built the house because labor is about 1/2 the cost of a house. My labor was more or less free. AND it was at least the quality of anything a "professional" could have built for me. Fifty years ago I bought several ham radio transceivers. Collins, Icom and Kenwood. They were far better than what I could have built. There was no machine for RTTY nearly as good as what I could and did build. It could do what no commercial machine could do until the 1990"s. Of course today a computer decodes innumerable types of digital signal. Through none of this did I attempt to build the submodules to see how they work. They did work and I wanted to spend my time building something I could use. In the 70's there was no ham radio slow scan TV available. Pretty primitive today.

I'll bet your TV you built was a Heathkit. The first ones were innovative, did you build each of the innovations separately before you tried the kit? Microwave, a kit I'm sure. Did you build the submodules to learn how they worked first? Laptop, come on, you used prebuild modules.

So you like to learn about the inner workings of ADC's. That's great, but not a reason to publish an article since most folks learned they work 10 years? ago. You're wrong about wanting to publish your lessons on ADC's. They are like tubes long ago, transisters, battery charging modules, led drivers, and various other mudules. People need to know how to use them, not your wonderment on how they work in something they can use and can't buy. Your instructable was predicated on ego. You're getting old and living in the past.

I like to know about the workings of bipolar npn and pnp transistors, j-fets, n-channel and p-channel mosfets, uni-junction transistors etc and when using one type is more appropriate to use than another. But if I spend time experimenting with them instead of just reading, I wouldn't have time to build anything with them. And again most folks just need to know what each does, not what material they were made from, or how each is physically different.



0
WilkoL
WilkoL

Reply 6 months ago

Dude, it is so simple, if you don't like what I write in an article, don't read it. People are different you know. I do like to know how things work, ADCs, transistors, GPS, diesel engines, particle accelerators, and yes, even valves (tubes, in your language) but if you don't , don't learn about them.

If you don't like the program on your telly, don't watch it. If you do not want to climb a mountain, don't do it. If you aren't interested in sub-atomic particles, ignore them. But realize that many other people do. And seeing that 58000 people read this article and over 500 even added it to their list of "liked" articles, there are at least some here who did enjoy reading it.
I am looking forward to your first article here on Instructables, no doubt it is going to be awesome.

Have a nice day.