More Autumn Antenna Adventures….

…with Arduino.

Last time I showed you how easy it is to hook up a relay to an Arduino MCU. Without some human control, however, it is a pretty useless setup. It needs switches and the switches most used in the Arduino world are momentary push buttons. They are versatile as just by altering some code you can change a momentary switch into a toggle switch or a rotary switch. Nifty! Hooking them up is not quite straightforward, though, because they need either a pull-up resistor or a pull-down resistor. The voltages on Arduinos I/O (input/output) pins alter between two states: 0 and +5 Volt. When an I/O pin is connected to either ground (pulled down, LOW) or +5V (pulled up, HIGH) by shorting a switch, this state is easily read by the Arduino. But when the switch is not shorted the I/O pin “floats” and its state is not certain due to electrical noise; it might be anywhere between 0 and +5V. It’s difficult to tell what the Arduino will read then: either HIGH or LOW, but this is anybody’s guess. To prevent this from happening both ground and +5V are connected to the switch to make sure the I/O pin will see two clear states. To prevent damage due to high currents a 10k resistor is added.

With that out of the way and everything hooked up the fun stuff can begin.
Just like last time we first define what I/O pins we use. Apart from the relayPin we now also have a buttonPin, namely A0. In the setup we now declare that buttonPin is used for INPUT, as we already defined that the relayPin is used as an OUTPUT pin.

The part which the Arduino loops through (over and over) looks a bit more complex now. It features an “if” statement:

if (digitalRead(buttonPin) == HIGH) {
digitalWrite(relayPin, HIGH);
}

This is not so difficult as it looks. In plain English it reads: “if you read out the buttonPin and it is equal to HIGH, execute the commands that are sandwiched in between { and }“. And the single command is the same as last time: write to the relayPin and make it active, or HIGH.

But what in other cases? That is the next part, which begins with “else“.

else {
digitalWrite(relayPin, LOW);
}

In all other cases make the relayPin LOW or inactive.

The result of all this you can see in the following short video.

Better, but still not useful enough. Once we release the button our relay also deactivates. A toggle switch is what is needed in this case, so let’s change the code and make it into one.

And this is where I find joy in working with Arduinos. With just three lines of code I can change the behaviour of a push button. There are loads of examples on the web that use lots more code to achieve the same result, but by being clever it can be done far more efficiently. Discovering this gives me the same kick as working a new DXCC entity, or building a circuit which works the first time. Although it almost wrecks my brain sometimes, the reward makes it all worth it.

Let’s take a quick look at the code. If the Arduino finds that the switch is activated, the buttonPin read HIGH. Only then execute a single command…

digitalWrite(relayPin, !digitalRead(relayPin));

Write to the relayPin and make it the opposite (!) of the state it is in when you read out the relayPin“. So when the relayPin is HIGH, make it LOW and vice versa. The exclamation mark is short for “not” or in this case “the opposite“. Even though we defined that the relayPin is used for OUTPUT, that doesn’t mean you can’t read the status of it. Genius, isn’t it?

To finish it off we add a little delay to prevent button bounce, because this will happen when you use momentary push buttons. (For Arduino purists: I know there are much better ways to combat button bounce – even hardware solutions – but for this article I’d like to keep things as simple as possible: KISS all over).

At the end of the day we now have one problem solved and our switchable loop antenna is one step closer to completion. The next problem: how to tune the loop?

Hans "Fong" van den Boogert, BX2ABT, is a regular contributor to AmateurRadio.com and writes from Taiwan. Contact him at [email protected].

6 Responses to “More Autumn Antenna Adventures….”

  • Dave, WD8CIV:

    Did you know Arduino controllers have built-in pullup resistors on their I/O lines?. You can enable them when you call pinMode() with the type INPUT_PULLUP. Here’s more information about it:

    https://www.arduino.cc/en/Tutorial/DigitalPins

    The values of the internal pullups aren’t terribly precise, but they’re good enough for switch inputs.

  • Hello Dave,

    You’re right and I did know that, but I usually use pull down resistors because when coding it makes a little more sense (switch closed means HIGH). I know historically pull ups are used more often, but for my small projects it makes no difference. Thanks for the comment, 73 de Hans

  • Robert, VA3ROM:

    Interesting articles looking forward to part 3. You may want to take a look at this article on using Arduino Uno relay shields http://wiki.seeed.cc/Relay_Shield_v3/

    I have them and they’re great for switching antennas in/out of circuit (or all to ground) using short range Bluetooth (smartphone control via free BT terminal program) instead of mechanical switches (or via internet for [global] remote control).

    I assume your relay module has a built-in back EMF diode? The relay shields also have wiring blocks for NO and NC connections to each relay so no need to use external solderless breadboard. Or if you want for switches you can use a solderless breadboard shield to mount on top of the relay shield.

    Probably overkill for your needs but something to think about down the road when you want to build a working device and not the “proof-of-concept” prototype.

    73.

  • Hello Robert,

    I thought about a relay shield, but I decided to keep the Arduino in the shack and the relay at the loop. Not sure if this will work out if cable lengths get longer, but we’ll see. I see diodes mounted on the PCB, so I assume they are used for EMF protection, but since this is a receive loop only I am not too concerned. 73 de Hans

  • Clive GM4FZH:

    An interesting article. The advantage of just a toggle switch is its position gives a visual and feel indication of its state rather than having to provide LED indication. This is useful for visually impaired persons.

  • You are absolutely right, Clive, and that’s why I prefer my old style Kenwood TS-440S over my modern ICOM IC-7200. I can feel my way around the Kenwood, but I have to look at the IC-7200 all the time. 73 de Hans

Leave a Comment

Subscribe FREE to AmateurRadio.com's
Amateur Radio Newsletter
News, Opinion, Giveaways & More!

E-mail 
Join over 7,000 subscribers!
We never share your e-mail address.



Also available via RSS feed, Twitter, and Facebook.


Subscribe FREE to AmateurRadio.com's
Amateur Radio Newsletter

 
We never share your e-mail address.


Do you like to write?
Interesting project to share?
Helpful tips and ideas for other hams?

Submit an article and we will review it for publication on AmateurRadio.com!

Have a ham radio product or service?
Consider advertising on our site.

Are you a reporter covering ham radio?
Find ham radio experts for your story.

How to Set Up a Ham Radio Blog
Get started in less than 15 minutes!


  • Matt W1MST, Managing Editor




Sign up for our free
Amateur Radio Newsletter

Enter your e-mail address: