Si5351a VFO for IC Based Receiver

This is a drop-in replacement for a Colpitt’s oscillator as the local oscillator for the mixer in a direct conversion receiver using a NE602. So it has no frills — it just generates a square wave at the required frequencies. The tuning is done with a rotary encoder. The receiver I want it for works on the CW part of the 40m band so the VFO tunes from 7.000 MHz to 7.020 MHz. An LED lights when a frequency limit has been hit. The encoder button is used to toggle the tuning rate between fast (1000 Hz per click) and slow (100 Hz per click). These can be changed to any pair of values, as can the frequency limits.

A Si5351a synthesiser is used to generate the square wave. It is controlled by a PIC micro-controller.

PIC 887 VFO Layout

I wanted to use a small 8-pin PIC for this to make the VFO have a small footprint. I chose a PIC12F1840 which has the I2C built-in needed to drive the Si5351a. But I found that I couldn’t program this PIC with my ageing PICkit2. So I tried my even more ancient K150 programmer — no joy here either. So I’ve had to use a PIC16F887 for now — even though it must be twenty times bigger. I considered bit-banging the I2C with a PIC12F683 or a PIC12F688, but it looks pretty onerous and I don’t want to spend my time chasing I2C errors. It may be the time has come to move on from PICs though I haven’t seen any alternatives that appeal yet.

The code traps encoder turns as interrupts and works out which way the encoder is turning. The debounced encoder button and frequency changes are done in the main (endless) loop. The Si5351a I2C is driven using a port of Hans Summer’s Si5351a Arduino demo. Thanks Hans! I’m also using one of Hans’ Si5351a module kits. I ‘trimmed’ the 27 MHz crystal by changing the value of SI_XTAL_FREQ in si5351a.h to 27003934 which gave the expected frequencies as seen on my ancient RACAL-DANA 9916 frequency counter.

Here are the source files and make file to build the VFO.

I haven’t tried this in the receiver yet, but it’s hopefully useful as it is.

Sometimes an encoder turn isn’t seen, but this doesn’t seem to be too obvious in use. I intend to fix that when I port the code to whatever PIC replacement I choose.

Rotary Encoder Test for PIC

I’ve been playing about making simple direct conversion receivers using an NE602 for the mixer. These receivers have some sort of input network for the signal captured by the aerial, mix that with a VFO and amplify the (possibly low pass filtered) audio output from the mixer.

The “classic” version uses a Colpitt’s oscillator for the VFO. This is an analogue oscillator that feeds back via a voltage divider made by a pair of capacitors. This works well and tunes nicely and is magical in its own way. However, the tuning is quite coarse with the variable capacitors I have, and the cost of replacement variable capacitors is getting eye-watering. They can also be quite large.

So I’m trying a replacement VFO made from an Si5351a clock generator. The Si5351a will be controlled by a PIC, being my microcontroller of choice (and available in my junk box). The tuning will be done by turning a rotary encoder. 

I had a rotary encoder of the right size in my junk box and but had no way to find out how it performs. So I wrote a small test program for a PIC16F887 which drives the rotary encoder and shows on LEDs which way the encoder was turned, whether it was turned recently and whether the button on the encoder was pressed or not. I often use a PIC16F887 for tests like this as it has many i/o pins that can be digital or analogue and can be pulled up at will. I don’t think I’ll be using it for the VFO as it is quite big (40 pins!) but I’ll be able to re-use the code amended for whichever PIC is choose.

The schematic is simple: pins on the PIC PORTB are weakly pulled-up and are used for input from the encoder. Four LEDs are connected to PORTD each with a current-limiting resistor. One is the power on LED, the others show what is happening (or what the PC thinks is happening) when you play with the encoder. All the pins are digital.

Encoder Test Schematic

The built test is shown below. The wires you can see that are not shown in the schematic are connections to allow the programming of the PIC using a PICkit2. Ignore the LED on the bottom left as it shouldn’t be there.

Encoder Test on Breadboard

The code and make file is in this zip file. Changes made at the encoder by turning the knob are seen via interrupt. The button changes are polled. The LEDs are lit (or not) in the same polling loop.

Tune in later for the VFO itself.

Morse Sending Tutor Mock-up

I realised that now I’ve made a CW Sidetone Oscillator, a Morse Keyer and a Morse Decoder, I could make a useful tutor for testing my sent morse.

The trick of sending good morse is in the timing. Getting the inter-character and inter-word gaps the correct length is quite difficult especially at higher speeds. So a tutor to check this could be a big help.

Morse Sending Tutor Diagram

So I lashed this together using a breadboard to do the connections. 

IMG 0070

And tested it as seen in this video: Morse Tutor

As you can see it’s very rough and ready. If I were to make a proper version I’d remove the WPM and tuning aid on the top line which aren’t necessary. It would fit into a small box quite easily.

It was fun to throw together!

Simple Morse Keyer

My home-brew 40m transmitter only handles a straight key at the moment, but I use a paddle. I currently use a separate keyer but this is a bit clumsy. I found a simple keyer circuit that should do the job. The keyer is by N1HFX: see Thanks, Mike!

The keyer is not iambic, but that’s ok for me as I usually use a single lever paddle. When I’m using a dual paddle key I don’t squeeze.

I didn’t have the exact components so I tweaked the design to cope with what I did have. Here is the new schematic and layout. I guess you shouldn’t mix IC families, but it works!

40mtx Keyer Schematic

40mtx Keyer Layout

The circuit works like this. Firstly, the oscillator. 

R2 is the variable resistor between SPEED_POT_1 and SPEED_POT_2.

Assume pins 5 and 6 are 0 V, and C1 has no charge. Pin 4 will be at 5 V.
C1 will get charged through R2 and R1. 0nce most of the 5 V is across C1, pins 1 and 2 will be at 0 V and pin 3 will change to 5 V. Pin 3 is directly connected to pins 5 and 6 so pin 4 will go to 0 V. C1 will discharge and once discharged enough pins 1 and 2 will be at 5 V and pin 3 will become 0 V which is where we came in.
The speed of the oscillation depends on C1, R2 and R1. R2 is variable and is used to change the speed. The speed could be calculated by using the RC curve and seeing where it hits logic high and low. But I decided to do the calculations in MATLAB using these scripts. This gives a plot for R2:

Sidetone WPM

This suggests you can’t key faster than about 27 WPM. This isn’t yet a problem for me as I can barely do 17 WPM.

The keying logic is like this.  Assume the both paddles are up initially. The logic levels are like this for dots:

^ marks dot clock transition to high

CLK  N0R8     N0R9     D    Q    DOT Paddle
-    1        x        0    x    up
^    1        0        0    0    up
-    0        0        1    0    down
^    0        0        1    1    down
-    0        1        0    1    down
^    0        1        0    0    down
-    0        0        1    0    down
^    0        0        1    1    down
-    0        1        0    1    down
^    0        1        0    0    down
and so on giving equally spaced dots
-    1        1        0    0    up
^    1        0        0    0    up
and so on giving no dots.

Or, less analytically: the dot paddle grounds one input to the NOR gate and the output of this is clocked into the dot flip-flop. The Q output of the flip-flop is fed back into the other NOR gate input. This gives clocked dots.

Single Dot Screenshot
The dash flip-flop uses the Q signal from the dot flip-flop as its clock. So the dash ‘stays on’ for two dits. To make it three dits as required to make it a proper dash the ~Q from the dash flip-flop is fed to the dot paddle input making the dash have an extra dit (as ~Q will be low when Q is high thus pulling the dot paddle low).
Single Dash Screenshot

Keyer on Pad board

All this works fine. Breadboarding was fun, but I made the mistake of putting it on pad board and the soldering was onerous — there’s just too many connections between the two chips hidden under the board. Next time I’ll brush up my coding skills and use a micro-controller.

CW Sidetone Oscillator

My 40m transmitter doesn’t have any frills at all. It is CW only and so far I’ve been listening to its signal on the receiver that I’ve been using with it. This works fine as the transmitting aerial is completely separate from the receiving aerial and the transmitter power is only around one watt.

So I’ve added a sidetone oscillator to the transmitter so that I can hear what I’m sending for when I do proper transmit/receive switching with one aerial.

I looked at PIC-based and LM386-based solutions, but decided on the circuit in chapter 1 of “Experimental Methods in RF Design” by Hayward et al (EMRFD). The circuit is based on a 555 timer. It beats other circuits I looked at because it is simple to build, uses through-hole components and is plenty loud enough for my ageing ears. Although it’s a square wave oscillator it sounds fine. I may add an LPF as the harmonics are louder than the fundamental.

I didn’t have all the components specified in EMRFD so I substituted others that seem to work fine.

Here’s the circuit and layout as ‘designed’ in EAGLE.

Sidetone Board

Sidetone SchematicThe circuit works as follows. The morse key is attached to the JP1-KEY pin and when the key is keyed it grounds R2 making Q1 switch on. The 555 timer is configured as a multivibrator triggered through D1. The square wave output goes to JP2-HEADPHONE pin.

I will replace R7 with a trimmer pot as the current value makes the output too loud even for me. 

Tuning a loop with nanoVNA

One of the aerials I use is a small transmit loop. These loops have a really narrow bandwidth which is good and bad. Good in that you get much less noise, bad in that you have to keep retuning. If you stick to one frequency such as with some digital modes it is ok as you don’t have to retune too much. Other modes such as SSB and CW are less easy to use with a loop.

You can tune the loop by transmitting into it and tuning the loop until the SWR is good. This is a bit anti-social as you can be transmitting for quite some time as you tweak the tuning. I used to use a noise bridge which works quite well as long as your ears don’t get tired. The nanoVNA offers easier, more visual way of tuning. 

nanoVNA loop tuningThe nanoVNA sweep is quite slow so you need to have a fairly wide sweep span so that you can see the dip in SWR when you are tuning the loop. So you set the centre frequency and then the span and then tune until you see the dip disappearing off one end. Then you fine-tune the loop and end up with the dip at the centre frequency. The loop is now tuned as in the photo above.

This works fine with the nanoVNA GUI, but as I am nearly always running a Mac when I’m on the radio I wrote a Matlab script to do the tuning. The script is run and sets the centre frequency and span for you.

Here’s a sample tuning transcript (user input in green):

>> ajfTuneLoop
connect: Serial-/dev/tty.usbmodem4001
status:  open
press RETURN when loop is connected to ch0
Frequency to tune to (MHz) ? 7.074
tune loop for dip at the marker, press RETURN when tuned ok

The script is ajfTuneLoop.m.

Blog Moved

I have moved this blog to this new server. I’ve copied all the posts and changed them as necessary to work with the new server. I tried doing that the clever way by copying folders but that didn’t work — I suspect due to having different versions of WordPress. So it was back to the backups. 

There are two different sorts of people: those who make backups and those who wish they had made backups.

The blog is still at the old server for the time being. If I did the move properly there should be no real difference between them. But this is the one that will get updated with new posts.

Please leave a comment if you find any bad links.

nanoVNA, MATLAB and (short) cable length

There are various methods of determining the length of a cable acting as a transmission line. The one I’ve used up until getting the nanoVNA is to inject a fast impulse into the cable and measure the reflection time on an oscilloscope. As usual, Alan W2AEW has a good video about this: #88 Cheap and simple TDR using an oscilloscope and 74AC14 Schmitt Trigger Inverter.

Alan also shows a good method of using the nanoVNA to measure cable length: #316: Use NanoVNA to measure coax length – BONUS Transmission Lines and Smith Charts, SWR and more

The author of the code I used in my previous post about nanoVNA and MATLAB, Alex, has as part of his code the use of a TDR Step Response. With this you can look at the step response graph and derive a cable’s length by looking at the dips in the response.

TDR Short Cable

It seemed to me that his code could be extended so that the cable length could be worked out in the code with no need to look at the graph other than to check that it was all ok. Here’s the script that does the calculations: ajfCableLength.m. Make sure you read on to the end of the post to understand its limitations.

You connect the cable you’re measuring to the S11 port of the nanoVNA and run the script. Here’s a sample MATLAB session using one of the cables supplied with my nanoVNA:

>> ajfCableLength
connect: Serial-/dev/tty.usbmodem4001
status:  open
freq: 101
s11:  101
s21:  101
Measuring cable connected to ch0 ...
Velocity factor of cable? 0.66
The calculated cable length is 0.19775 metres.
Show TDR Response plot (Y/n)

The script is perhaps worth explaining. The first part is the difficult bit which Alex provided. The S11 scattering parameters are obtained from the nanoVNA. These s-parameters are converted into a MATLAB rationalfit object which is a mathematical representation of the s-parameter curve. This rationalfit object is then used to derive the TDR step response mathematically. The second part, which I added, looks at the step response to find the last dip in the curve. This gives the return trip time just like in a real TDR. Given the return trip time the cable length is calculated using the velocity factor which the script has already asked the user to provide.

This all works well for short lengths of cable, say up to 2m. But beyond that it all goes haywire and the results are not correct. I fried my brain reading books (see below) on TDR step responses to find out why. I haven’t managed to understand it properly yet. So it’s a matter for further study.

Some of the books I looked at are:

  • Electromagnetics by Kraus. An excellent book, Chapter 12 is the pertinent part.
  • Electromagnetism for Electronic Engineers by Carter.
  • Signals and Systems by Hsu.
  • Techniques for Advanced Cable Testing which is on the Keysight web site.

None of these are easy reading and all assume an understanding of vector arithmetic and calculus.

Easier, is:

  • Practical Digital Signal Processing using Microcontrollers by Ibrahim.

The nanoVNA and MATLAB

The nanoVNA is a cheap but usable vector network analyser. It is good value and very useful for all sorts of RF design and radio ham activities. The model I have has a tiny screen which I can read adequately but it isn’t as comfortable as reading a computer screen. The NanoVNA is a touchscreen device so you need to tap on tiny text to do anything. Again, this is possible but far from comfortable for those of us with ageing eyes. There’s also a bit of screen reflection.


The nanoVNA can be driven from a PC using a program called nanoVNA-saver which is pretty good. Unfortunately it runs on Microsoft Windows and as I don’t have a PC running Microsoft Windows I have to run it in a VirtualBox VM on my Mac Mini. This works but is a bit clunky and to be honest my Mac mini is a bit long in the tooth and struggles to run virtualised Windows 10 at a reasonable speed.

There is a cross-platform solution based on Python which looks promising. It uses Homebrew on MacOS which clashes with MacPorts which I use. I tried to make it work under MacPorts but fell into the usual maze of incompatible library versions causing the make to fail. So I gave up as I want to play with electronics and not software building. [Update: Rudi, DL5FA kindly helped with this and it looks like the Python Virtual Environment will help when I’ve got the time to try again.]

MATLAB to the rescue! — alex_m has created some MATLAB scripts which allow you to interrogate and control the nanoVNA from MATLAB. Thanks Alex! So if you have MATLAB this may be useful. If you don’t you may be able to convert much of this to Octave but it does use the MATLAB RF Toolbox so you may end up doing a lot of work.  Alex’s scripts produce LogMag, Smith Chart and TDR Step Response graphs. 

Here’s a graph showing my 40m LPF characteristics made using Alex’s scripts.

40m LPF LogMag

I’ve written a couple of scripts based on Alex’s work which I think makes using the nanoVNA easier. Both scripts need an edit to configure the serial link to the nanoVNA. You’ll need Alex’s code too.

The first script ajfCalibrate.m calibrates the nanoVNA for a particular sweep on the S11 port. You have to do this before every measurement that changes the sweep values, so you do it a lot. Here’s how it looks in the MATLAB console. The inputs are in italics.

>> ajfCalibrate
connect: Serial-/dev/tty.usbmodem4001
status: open
Sweep start (MHz)? 7
Sweep stop (MHz) ? 7.2
press RETURN when S11 is open
press RETURN when S11 is shorted
press RETURN when S11 has 50 ohm load
Save [0-4]? 4

The second script ajfVSWR.m produces a chart of VSWR against frequency. Most radio hams are happier with this than the LogMag chart preferred by RF designers. 

I used ajfVSWR to measure my small transmitting loop tuned to the 40m FT8 frequency. As expected it shows the narrow bandwidth in low VSWR that is the drawback of small transmitting loops. 

40m FT8 VSWR

The graph can be zoomed in MATLAB and have data cursors added to show more details.

40m FT8 VSWR 2

The VSWR could be lower. I think I left a metal step-ladder in the attic which is affecting the loop.

ajf? My initials.

50 Hz strobe

Some of my friends were in a band ’The Valves’ in the late 70s and more recently they have been playing the occasional (and excellent) gig here in Edinburgh. This year they brought out their first album — on vinyl. It’s called ‘Better Late …’. You can find all about them on Facebook: search for ‘The Mighty Valves’. 

Now I was supposed to be getting copy of the album at mates’ rates. But that involved meeting one of them which is not possible during COVID lockdown here on Scotland. But to prepare for its eagerly awaited arrival I thought I’d better dust off my turntable. I had ripped all my albums to MP3 ages ago and rarely used it. I found that it only worked at 45 rpm. The cure was to spray switch cleaner at the 33/45 rpm switch which was hidden inside — the switch that you press was pressing a plastic piece at right angles to the actual switch. 

I though I might as well check it was still at the correct speed and remembered I had a strobe ‘disc’. It’s actually a piece of paper from

Stobe disc on turntable

The idea of the strobe disc is that the lines on the concentric circles will normally be blurred when the disc is spinning. When you shine a light strobing at 50 Hz the lines freeze and appear to be still if the disc is turning at 33 rpm. If the disc is spinning at some other speed the lines may look like they are spinning slowly backwards or forwards. I tried to take some videos of this working but they don’t show properly presumably due to the frame rate of the camera.

So how to get a 50 Hz light in these days without incandescent bulbs? Here’s a quick program for an Arduino which toggles a digital output at 50 Hz. A white LED and a 330 ohm resistor in series to ground is the entire circuit. About as simple as it gets.

strobe LED at 50 Hz
int ledPin = 12;

// the setup function runs once when you press reset or power the board
void setup() {
pinMode(ledPin, OUTPUT);

// the loop function runs over and over again forever
void loop() {
int delayms = 20/2; // 50 Hz has a period of 1/50 seconds = 20 ms
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin, LOW);

I compiled this for an Arduino DUE which I had to hand, though I imagine it would work fine on an UNO. The ledPin value may need changed though. I compiled it using Arduino-cli with this incantation:

arduino-cli compile -b arduino:sam:arduino_due_x_dbg -p /dev/cu.usbmodem141101 -u

Arduino with shield

That meant I didn’t need to use the Arduino IDE and could use my code editor of choice (vim). Arduino-cli works well — thanks to the developers! BTW the Arduino IDE is fine, I’m just a terminal junkie. An oscilloscope confirmed the signal was at 50 Hz.

The picture shows me using a prototype shield on top of the Arduino. The shield is not necessary but the reset switch is easier to get at with the shield so I used it.

Did it work? Well… My turntable is a Pioneer PL-120 and the designers decided to make the speed control impossible to use while the turntable is spinning. See the yellow screwdriver which is at the speed control adjust screw in the picture of the strobe ‘disc’. So my fond dreams of shining the strobe at the disc and simultaneously adjusting the speed were dashed. I found it really difficult to do. Also it is summer here in Scotland and the weather has been good so the sun is in the sky nearly all of the time I am awake. So the LED could have been brighter to compete with the sun. It would work fine in winter or if I had better curtains.

In the end I put on a record and counted how long 10 revolutions took and did the arithmetic. Adjusted the speed (did I tell you the screwdriver is difficult to align with the speed control slot?) And repeated until good. So I didn’t actually use the strobe in the end.

A final test: ‘Oh! Wot A Dream’ by Kevin Ayers is billed as taking 2m 47s and I timed it at about 2m 46s. That’s good enough for rock and roll. Maybe someone with perfect pitch would howl in frustration — as am I waiting for the ‘Better Late…’ album.