Showing posts with label Automation. Show all posts
Showing posts with label Automation. Show all posts

Thursday, April 30, 2026

Claude Helps with the Automatic Antenna Selector

Automatic Antenna Selector
I thought I was nearly finished with the Auto Antenna Selector. During my first week of retirement, I finally managed to locate something I had been looking for months -- A male-to-male DE15 cable. 

This cable connects the Auto Antenna Selector to the AUX port of a K3. I bought two of these a couple of years ago, and I easily found one. But the second one eluded me. Without it, I couldn't test the Selector in Design C (Dual Radio) mode. I had done all my testing in Design B (Single Radio) mode. 

Having the proper cables, I did my testing and found several bugs. Some of them were going to be difficult to fix. Part of the problem was my approach. I had organized my antenna selection table by the selections, then by band. This made it difficult to determine the choices available per band. So, I organized the table by band, then listed the choices. From this, I could determine which choices were available for a given band, skipping over those that would conflict with an existing port. 

This greatly simplified the code for making the mode selections, which are very different in Design B (Single Radio) and Design C (Dual Radios). In Design B, the first selection is assigned to the first port, so there can only be three additional choices. In Design C, only one port is selected per radio, so we can potentially use all four selections (so long as there's no conflict with the other radio).

Design C bugs were all addressed, and Design B worked better than ever.

Before I retired, I'd been messing around with Anthropic's Claude Code. I was curious what Claude might think of the firmware, so I requested an in-depth code review.

Claude Code came up with a number of good suggestions. It found some dead code, made several type suggestions, a minor code re-organization, and way better comments.

What impressed me, though, was that Claude Code had a deeper understanding of the particular PIC processor that I did. It suggested I turn on the Watch-Dog Timer (WDT). That I change how Timer 0 (TMR0) was used. And, that I use the PMD register to power-down the modules that I wasn't using in the device. 

I thought I was using TMR0 correctly. And I knew about the WDT, I just didn't think it was necessary. But the PMD register I knew nothing about. All of these suggestions seemed like a good idea to me, so I made them.

All in all, Claude Code did eight rounds of review of this code. It made a couple of suggestions that I knew were wrong, and it took corrections well. 

The next day, I burned a chip and plugged it in the unit for testing.

It bricked, Nothing. Not even a single flash of the LEDs on startup.

Somewhere in the midst of Claude's suggestions, we had changed the software in a way that it no longer worked at all. 

I made a new branch of the code and backed out some suggestions. I determined it had nothing to do with the WDT or PMD register. That left something amiss with TMR0. This could be a problem with Microchip Code Configurator (MCC).

I had switched TMR0 to use the HFINTOSC. It was programmed to generate an interrupt every 5 ms. Except it didn't work. I switched it back to us the LFINTOSC, also programmed for every 5 ms, and it worked as expected. I'm not sure why it didn't work with the HFINTOSC, but having several oscillators gives one options. I'm happy it works.

Once it was working again, I walked through all of the Design B and Design C test scenarios. Everything worked with flying colors. 

AI is certainly useful, even for amateur radio projects. Just don't blindly trust it. Remain in the loop for all changes.


Saturday, December 27, 2025

New Modes for the Auto Antenna Selector

Automatic Antenna Selector in use
I wrote previously about debugging selection modes for the Auto Antenna Selector. With that working, I wondered if I could do more. 

Originally, I thought that modes should swap the selections on the A and B ports. With Standard Mode, I was missing that. But how to allow more modes?

Flip-Flop Mode

I don't need Test Mode that often. Holding down the mode button could be divided into a short and long hold. A long hold -- 1 second or more -- would invoke Test Mode. A short hold -- 1/4 of a second, could invoke something else. 

Flip-Flop mode was born. With a short hold, the selections for Port A and B are swapped. This works both in single-radio and two-radio configurations.

The selector signals Flip-Flop mode by blinking the mode LED on and off over 1/2 second. As coded, it actually pulses for 1/2 second, then is off for 1/2 second. Not what I intended, but distinctive. I decided I didn't need to "fix" it.

Testing

Unlike the last code changes, these worked the first time. I found Flip-Flop mode helpful with the single radio when trying to use a different antenna with the AL-80A amplifier. Since it is only connected to the antenna on Port A, this mode makes this possible.

At the moment, tapping the mode button in Flip-Flop Mode doesn't do anything, still thinking about that.

Sunday, October 26, 2025

Debugging the Automatic Antenna Selector

When I last wrote about the Automatic Antenna Selector, I mentioned adding modes to select more antenna options. Getting that to work took some doing.

Test Mode

Holding down the mode button invokes test mode. When entered, it selects port A0. Tapping the mode button advances to port A1, A2, to A5, then it goes to B0, B1, to B5, then back to A0. In this way, all antenna / port combinations can be selected. This allows new antennas or conducting tests or experiments before a new configuration can be programmed. 

The unit signals Test Mode with the mode LED being on continuously. Holding down the mode button again goes back to Standard Mode. 

Standard Mode

Standard mode determines antenna selections according to the connected K3 BAND0-3 signals. When only one radio is connected, both ports are based on the current band for that radio. The first port is the primary, the second part gets the secondary selection. Tapping the mode button cycles through the secondary port selections, the primary port being unchanged. 

When two radios are connected, port selections are based on the K3 BAND0-3 signal for both radios. Radio A gets the primary selection. Radio B gets its primary selection, unless that port conflicts with Radio A. It then gets the secondary selection (unless that also conflicts). Tapping the mode button cycles through the Radio B selections. 

The unit signal Standard Mode with a mostly dark LED. Off completely for the primary selection, pulsing twice for secondary, three times for tertiary. At the moment, there are only three stages. Adding a fourth would be easy.

Easy in concept, but after the code changes, it didn't all work.

Test mode worked great. Entering and exiting were reliable, and each tap selected the correct port.

Standard mode, however, didn't seem to do anything. The LED indication showed the mode selected, but the port selection did not change. I had only implemented the single radio logic, since I couldn't find a second cable to connect a K3.

Debugging

Debugging this over the last couple of weeks was driving me mad. No matter what changes I made to the code, the behavior did not change. Further, I noticed that when the K3 was on 6m, port B was also selecting a dipole antenna. That was unexpected, as it wasn't a valid antenna for 6m.

Eventually, it dawned on me that this was not single-radio mode. For some reason, the selector believed there were two radios connected.

That was a revelation. I knew there was a problem when the K3 powered down. When no K3s are connected, the selector was supposed to deselect all relays. Instead, it had two dipoles selected. 

This was connected with how Elecraft encoded the bands on the BAND0-3 pins. 60m is represented as all zeros. Even with the weak pull-ups enabled on the PIC, it wasn't enough to overcome the loading of the connected but powered-down K3.

The same problem was evident on the disconnected port -- it was registering 60m, which selected the dipole. 

Fixing

The first fix was hardware. I added 2.2k pull-up resistors to the BAND0-3 pins on both ports. After that, the relays deactivated when the K3 was powered down. But, I still saw the dipole selecting coming up on 6m when switching models. 

This was a software problem. One of the internal variables was initialized incorrect, which was the source of the 60m selection. When initialized correctly, single-radio Standard Mode selections worked as they should. 

With that working, I'm full of new ideas for improving mode selections. Once I figure out which ideas are best, hopefully it will be a small matter of code changes....

Saturday, September 13, 2025

Automatic Antenna Selector Project

Auto Antenna Selector under test.
Some projects are years in the making. October 2020, I ordered the KK1L 2x6 Antenna Switch board. I assembled it and by December I rigged up a manual antenna selection switch. Two years later, I mounted the KK1L to an aluminum panel to create a Single Point Ground (SPG). While all that work was beneficial from a bonding and grounding standpoint, antennas were selected manually. If I changed bands and forgot to change the antenna selection, trouble could ensue.

The purpose in buying the KK1L 2x6 Antenna Switch was fully automatic antenna selection. I needed a controller that could communicate with the Elecraft K3 and select the right antenna. 

A PIC microcontroller seemed suitable. I'd had success using one of these chips to build a K9AY Controller. For that project I had used a PIC16F1503. After that, I picked up the PIC16F18426 and PIC16F18446 chips -- these offered more features than the '1503, including a serial port and way more program memory. The Microchip tools were free, and I had a PICkit3 programmer.

I sketched out three designs.

Design A - 1 radio, 1 set of relays

The most basic design - it does little more than replace the manual switch. A single DE-15 jack brings the BAND0-3 information from the K3 into the PIC. Three outputs drive a 74LS145 BCD decoder to select one of the six relays through a 2N3906 driver transistor. Two other outputs allow selection of the 160 or 80/75m shunt matching networks.

Total I/O required nine pins, which any of the three chips could provide.

Design B - 1 radio, 2 sets of relays

A limitation with Design A is that a K3 with the KAT3 has two antenna jacks, but the selector only chooses one antenna. Design B reads BAND0-3 from one radio, and selects the best antenna on port A of the switch, which is connected to ANT1, and the second best antenna on port B, which is connected to ANT2. The operator can then use the ANT button on the K3 to switch between the two antennas. 

This design retained the four inputs for the BAND0-3 information, plus six outputs feeding two separate 74LS145 BCD decoders, and two additional outputs for the 160 or 80/75m shunt matching network. 

That's exactly 12 pins -- still possible using any three of the chips.

Design C - 2 radios, 2 sets of relays

I liked Design B, but along the way I purchased a second Elecraft K3. If I were trying to use both radios, what would I need to switch the antennas?

Two DE-15 jacks facilitate the BAND0-3 data from each radio, requiring eight inputs. The selector could then choose the best antenna for both Radio A and Radio B, unless that choice caused a conflict, in which case Radio B would get the second-best choice.  Outputs were the same as in Design B. 

This required the 20-pin '18446, because the 14-pin controllers don't have enough I/O available. 

It occurred to me I might want to switch the antenna selection priority sometimes, so Radio B gets the best antenna in a conflict and Radio A gets second best. That required an input pin for a pushbutton and an output to light an LED. This used all the 18 pins available on the '18446. 

Design Choice - B/C - 1 or 2 radios, 2 sets of relays

First look with front panel assembled
The end design combines Design B and C features. Two DE-15 jacks are used, and the PIC software decides if a K3 is connected or not based on the pattern of BAND0-3. Unconnected pins are pulled up, so a value of all ones indicates no connection. If only one radio is connected, the software acts like Design B, if both radios are connected, it acts like Design C. 

The front panel has LEDs for the six relays on port A and B, so you can visually see which antenna is selected for each radio. An LED each for the 160 and 80/75m shunt selection, and a pushbutton and LED for the mode selection rounds out the front panel. A power switch and a switch to choose between the 80 and 75m shunt network round things out.

Power requirements are simple. A port relays take 88 mA, and B port relays require 44 mA. Selecting one relay for both ports is less than 140 mA. The 160m shunt relay requires 120 mA and 80m relay requires less. The power requirements of the PIC, 74LS145 and LEDs are negligible by comparison -- 300 mA covers everything. 

Construction

A look at the guts of the box. The relay
drive transistors dominate the board
I searched for a smart-looking cabinet for this project fitting the dimensions of the station. I found a reasonably priced enclosure on Amazon.com. I took lot of care drilling the front panel so that everything lined up correctly. I figured I might be staring at it for years. In retrospect, the rear panel doesn't look so pretty.

With the cabinet in hand, how to construct the hardware? I considered developing a PC board, but I was eager to build. I ended up using a bit of perfboard and some 3M Scotchflex prototyping sockets. The Scotchflex system is now obsolete, mainly because everything is surface mount, but I had most of this in the junk box. I had to engineer a 20-pin 0.3 inch socket -- which I accomplished with two 14-pin sockets back to back.

The downside to this approach is all the wiring required for the relay and LED driver transistors -- there are fourteen 2N3906s, three 2N3904s and a bunch of related resistors. A PC board would have taken more design work ahead of time, but the construction would have gone quickly and taken less space.

I worked on this project off and on in six different locations - Gwinnett county, Fulton county, Warren county, two locations in Gordon county and finally from Floyd county. For a while, I carried the whole project with me in a small cardboard box wherever I was.

Software

Like the K9AY controller, everything is interrupt-driven. The '18466 CPU is configured with a 500 kHz clock speed, and a timer interrupt occurring every 5 ms. 

During the interrupt, we sample the A and B ports from the radios, plus the mode button. All of these values go through debounce logic - the value must hold for 10 interrupts (50 ms) before taking action.

If either the A or B values change, or the mode selection changes, then the antenna selection logic is followed. If either port is all ones, it indicates a radio isn't connected, so the Design B rules are used to select the antenna. If neither port is all ones, we assume two radios are connected, and Design C rules are used to make the selection. 

Debugging

Unlike the K9AY controller, I had a bit of trouble getting the software working. Part of the struggle was knowing if a problem was a wiring problem or a software issue.

At first, nothing seemed to work. In the end, I wrote really simple firmware that just blinks the mode LED based on the timer. Nothing. Setting up the chip on a solderless protoboard, still nothing. After some experiments, I got the timer interrupt straightened out and had a blinking LED on the protoboard. 

Then came the wiring issues on mode LED. One issue was the transistor drivers for the LED didn't have proper pull-ups. Eventually, I settled on a one second startup routine that would blink the mode LED briefly once, then twice. After that, the chip would be looking at the input ports and selecting antennas.

In the next phase, I wrote an additional startup routine that selected the antenna for ports A and B. Every second or so, it selected a different relay, and hence light the LED. I coded it to go in a pattern so I could verify that each relay driver and LED worked correctly. Of course, it didn't work.

Several wiring problems became apparent. First, the 74LS145 chips weren't getting any +5 volt power, so they weren't doing anything. They had been wired, but one of the wires broke. Once fixed, the LEDs lit. Then it was apparent they were going in the wrong order. 

The wiring between the PIC16F18466 and the 74LS145 chips had two problems. First, the pins for the A port and B port had been reversed. Second, outputs from the 74LS145 were reversed. Putting out a value of 001 into the 74LS145 selected antenna 5, and a value of 110 selected antenna 0. Rather than doing a massive amount of re-wiring, it was easier to change the software and re-draw that part of the schematic. 

Then we finally come to the business end -- hooking up the K3 BAND0-3 inputs. That's when I found my last wiring error. Turns out, I had swapped the pins between rig A and rig B. Another problem solved with a software change.

It Works! Now What?

Even with the basic software running, I felt the need for change. I've disassembled my station in Gwinnett county, and the antenna configuration I programmed, even designed this Automatic Antenna Selector for no longer exists.

The antenna selection logic originally was a bunch of switch statements embedded inside if / else statements -- not very easy to change. I re-coded to use a short table of eleven rows and four columns. The rows represent each band, 160 through 10m, including 60m. The columns are the first, second, third and fourth choices of antennas. Much easier to understand and update.

I expanded on the idea about the Mode button. Originally, it was just primary / secondary. To allow for up to four antenna choices, perhaps there are more than two modes. How does one tell which mode you are in? The mode LED was programmed as just on or off. I could have it be off for primary, blink once for secondary, twice for tertiary, and three times for quaternary.

The last issue was how to test the relay action without having to program a new antenna configuration.  Currently, there's nothing programmed for relay position 4. If one wanted to hook an antenna there to test it, how do we do that without burning a new chip? 

Holding down the mode button could select a special mode that selects ports A0 ad B5. Then, each time you tap the mode button, it would switch: A1 and B4, A2 and B3, etc. In this way, each port can connect to each antenna. And the mode LED would indicate this with a solid on condition. Hiding the mode button again would switch back to the to the normal program.

How about using a radio other than the K3? Maybe I want to use the Novice Transmitter, or perhaps my trusty Elecraft K2/100. Perhaps we re-purpose the seven-position manual selection switch to encode K3 band values. A simple diode matrix would work, and I have a bunch of 1N270 diodes, 

This project is now working, but it is far from done.