Playing with resistors Part I: 3-way latched switches

Right after finishing my second Control Box (the so-called Radio Box since I planned it to control R800L1 and R828) I looked for a way to increase the flexibility and the capabilities of my control boxes. Something I really wanted to control were the 3-way latched switches. These switches look exactly as the 3-way momentary switches I used on my very first Control Box but are not spring-loaded to return to the central position after the release (I’m going to call them “Momentary” and “Latched” for semplicity from now on). This apparently little detail have a number of consequences: for instance Latched switches cannot be used in a basic button matrix because, by holding their position, they are a constant closed circuit whereas the Momentary are just a temporary one.

A long time ago in a High School far, far away…

There are (as usual I’d say) a number of ways to solve the issue. Mine comes from some reminiscences from the high school: Voltage Dividers. I have mentioned already the theory behind this simple circuit in the article I linked above. Now I’m going a bit more into the details of its implementation.

In primis we need some additional materials, namely two resistors for every Latched we want to implement. I usually build them in pairs unless I’m working on something specific, such as my UFC/PRTz. These resistors will be classic 10kΩ each. In addition, a stripboard or similar is also great.

The Circuit

I have used Voltage Dividers to update the initial implementation of my Radio Box. The following is an extract from the wiring diagram of the first version.

resitors-vd-radio-box-old

As you can see, nothing different from the examples I wrote about in my step by step guide. The Momentary switch is, in fact, part of a button matrix.

As I mentioned earlier, a stripboard is great to build the voltage divider. This is how it will look like:

resitors-vd-voltage-divider-circuit
Voltage Divider

You can build your own board in a different way: for instance, #1 can go straight to GND but I put it there in order to have a more clean and organized wiring diagram.
The circuit is quite simple: GND and Vcc are self-explanatory, An is the Analog input, 1-2-3 are the positions on the 3-way switch. The Analog input is required: it reads a different value depending on the position of the switch and a digital input simply cannot do that. This highlights immediately one of the limitations of this solutions, i.e. the fact that the number of Analog pins on an Arduino Leonardo board is limited to 4.

This is the result when we put everything together:

resitors-vd-radio-box-v2

This is not really difficult, isn’t it?
That’s all for the hardware, now let’s go back to the IDE. I know you can’t wait 🙂

Coding

Since the Latched is not part of the button matrix anymore and works in a complete different way, we have to write some ad hoc code.
In primis we have to verify that the circuit is working correctly. There is a very simple firmware I use very often when I work with Analog inputs. It’s just a loop that outputs the value of an Analog input to the serial monitor. I will write an “Arduino Pill” about it at some point but for now, this is its simplified code:

#define ANALOG_PIN A3 //change A3 with your analog pin number
void setup() {
Serial.begin(9600);
pinMode(ANALOG_PIN, INPUT_PULLUP);
}
void loop() {
int analogValue = analogRead(ANALOG_PIN);
Serial.println(analogValue);
delay(200);
}

Please note the pinMode INPUT_PULLUP instruction. This instruction must be used in the Control Box firmware as well. Going into its details now sounds a bit pointless to me but make sure to have a look here for more info about how it works.

If the firmware is working correctly you should read different values depending on the position of the switch. It should be either:

  • close to zero;
  • close to 10 bit (that’s the analog pin’s precision); therefore 1023;
  • a value in-between: due to how the circuit works and the fact that we have used two identical resistors, the value should be split in two equal parts; therefore int(1023/2)=512.

Unfortunately real life is not as perfect as life is in theory, so due to electrical noise, tolerances, temperature and interferences these values can change a bit. Therefore the easiest way to implement the code is a series of nested IFs instead of comparing a precise value.
resitors-vd-coding
The code looks simple and kinda rushed but works fine. For testing purposes you can toggle the Joystick output into the nested IFs but I have found that it degrades considerably the performance of the board. I guess it’s because the Box “bombards” the PC with Joystick outputs instead of sending them when there is an actual status change (my code does exactly that, as a matter of fact). Anyway, the case-switch construct is easy to use, does its job and it’s very intuitive to amend when friends with no coding experience want to play with my firmwares.
WARNING: justification/rant ahead! Note that we have plenty of memory available and the box doesn’t perform any real-time or timing-sensitive operation. If you have worked with single-resister 8-bit PICs you know what I mean..
/endrant

NOTE: I understand you are probably hating me for putting screenshots of the code instead of actual text but I absolutely hate how WP formats the code. I am using a free hosted versions, I haven’t found any decent plugin so far and I have no intention to pay for a full-feature hosted site. Please bear with me! 🙂

This is a picture of my Radio Box v2. The brownish board you see holds two Voltage dividers.
Radio Box - Version 2

Keep Delving!

As a last note, please google “Voltage Divider Arduino” or similar to find tons of info about how Voltage Dividers can be applied to Arduino. I’m sure you will also find solutions similar and probably better thought and explained than mine (I admit I relied a lot on trial-and-error; luckily I didn’t break anything 😛 ).
Another interesting subject is the PULLUP resistor. I just mentioned it here because its explanation is beyond of the scope of my site but I really suggest you to spend a few minutes and understand how it works. It will be very helpful later!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.