A while back I created a Mostly 3D Printed Rotary Switch specifically for my Minivac 601 Replica project.
For my new Think-a-Tron 2020 project, I find myself in need of yet another rotary switch. I am looking for a SP5T panel mount switch. An additional requirement is that I will be reading the switch using an Arduino with limited I/O pins available.
I was surprised at how expensive SP5T rotary switches can be. The PCB mount ones are pretty cheap, but too small and unsuitable for my needs. Panel mount switches were $25+ on Digi-Key and I will need two. If I were a patient fellow I probably could have sourced some overseas much cheaper. I could have used an inexpensive potentiometer in conjunction with an analog input to do the job, but I really wanted a solution with proper "detents". So at the end of day I decided to try a DIY approach, and after a couple of days work I came up with the design pictured above.
It's not a compact as a "store bought" switch at 50 mm in diameter, but it's certainly useable in many situations including mine. Like a potentiometer, you can read the five different "stops" with a single analog pin and, as can be seen above, is panel mount.
So let's build one.
Supplies:In addition to the printed parts you will need:
- 6 2K ohm resistors. in value.
- Some small disk magnets 3 mm in diameter and 2 mm deep.
- A short 7 mm length of 2 mm diameter (12 AWG) uninsulated copper wire.
- Some hookup wire. Mine had soft silicon insulation.
Everything you need to make this Rotary Switch is pictured above. For the printed parts I used the following settings (unless otherwise specified):
Print Resolution:.2 mm
Infill: 20%
Filament: AMZ3D PLA
Notes: No supports. Print the parts in their default orientation. To make a Rotary Switch you will need to print the following parts:
- 1 - Rotary Switch Base
- 1 - Rotary Switch Rotor
- 1 - Rotary Switch Piston
- 1 - Rotary Switch Gasket
- 1 - Rotary Switch Base
- 1 - Rotary Switch Wiring Harness (optional)
- Insert 6 of the magnets into the Base piece. Use a small dab of glue to hold them in place. Make sure that the polarity is the same for all 6 magnets.
- Solder the resistors in series as in the photo below. Each should be 15 mm apart.
- I made a small jig to hold them in place for soldering.
- Insert the resistors into the Base channel, behind the "posts" holding the magnets. The resistors go directly behind the posts while the soldered leads go into the "gaps".
- When you are satisfied that all the resistors are positioned correctly, push them down to the bottom of the channel, then secure them in place with the "Gasket" piece.
- Insert a magnet into each of the six holes on side of the rotor. NOTE: The magnets should be oriented so they attract the magnets that have been set into the inside of the Base. Use a little glue to hold all of the magnets in place.
- Insert a stack of four magnets into the hole at the back of the Rotor "trough" pictured above.
- Glue the Rotor Top onto the Rotor so that the trough becomes a small square tunnel. I've aligned the flat edge of the shaft with the left edge of the trough.
- Insert a stack of three magnets into the hole at the "back" of the piston. NOTE: These magnets should be oriented so they repel the magnets that have been set into the inside of the Rotor at the back of the trough. Use a little glue to secure them.
- Solder the 7 mm length of 2 mm diameter copper wire to the end of a short length of hookup wire.
- Push the hookup wire through the hole in the front of the Piston and glue the 7 mm copper wire to the groves in the front of the Piston as in the photo above. Be careful not to get any glue on the front of the copper wire.
- Slide the Piston into the Rotor with the wire pushed through the slot in the bottom as above. The magnets should be pushing the Piston towards the front of the Rotor.
- Slot the wire through the hole in the bottom of the Base, push the Piston towards the back of the Rotor trough, and slide the assembly into the Base.
- This is a good time to test the switch out. The Rotor should turn freely and the Piston should slide into the Base recesses as you turn. You should feel when the Piston snaps into one of the slots, and feel some resistance when you try to twist away from a slot. That is the detent action that I spoke of.
- When you are satisfied that everything is working OK, glue the Base Top onto the Base being careful to to gum up the Rotor.
I connected the rotary switch to an Arduino Nano and wrote a small test sketch to determine the values returned from an analogRead() at each of the five rotary switch positions, and came up with the following values: 233, 196, 159, 115, and 68.
In the following sketch I use these values and set a range of -10 to +10 around them to account for jitter in the readings.
#include "FastLED.h"
#define NUM_LEDS 35
#define LEDS_PIN 6
CRGB leds[NUM_LEDS];
int A[35] = {0,0,1,1,1,1,1,
0,1,0,0,1,0,0,
1,0,0,0,1,0,0,
0,1,0,0,1,0,0,
0,0,1,1,1,1,1};
int B[35] = {1,1,1,1,1,1,1,
1,0,0,1,0,0,1,
1,0,0,1,0,0,1,
1,0,0,1,0,0,1,
0,1,1,0,1,1,0};
int C[35] = {0,1,1,1,1,1,0,
1,0,0,0,0,0,1,
1,0,0,0,0,0,1,
1,0,0,0,0,0,1,
0,1,0,0,0,1,0};
int T[35] = {1,0,0,0,0,0,0,
1,0,0,0,0,0,0,
1,1,1,1,1,1,1,
1,0,0,0,0,0,0,
1,0,0,0,0,0,0};
int F[35] = {1,1,1,1,1,1,1,
1,0,0,1,0,0,0,
1,0,0,1,0,0,0,
1,0,0,1,0,0,0,
1,0,0,0,0,0,0};
int a = 0;
void setup() {
Serial.begin(115200);
Serial.println("Test Resistor Network");
pinMode(A5, INPUT_PULLUP);
FastLED.addLeds<NEOPIXEL, LEDS_PIN>(leds, NUM_LEDS);
Serial.begin(115200);
Serial.println("5x7 LED Array");
FastLED.setBrightness(32);
}
int countA = 0;
int countB = 0;
int countC = 0;
int countT = 0;
int countF = 0;
void loop() {
a = analogRead(5);
Serial.println(a);
if (a <= 78 && a >= 58) countF++;
if (a <= 125 && a >= 105) countT++;
if (a <= 169 && a >= 149) countC++;
if (a <= 206 && a >= 186) countB++;
if (a <= 243 && a >= 223) countA++;
if (countF > 10) {showLetter(F); countA = 0; countB = 0; countC = 0; countT = 0; countF = 0;}
if (countT > 10) {showLetter(T); countA = 0; countB = 0; countC = 0; countT = 0; countF = 0;}
if (countC > 10) {showLetter(C); countA = 0; countB = 0; countC = 0; countT = 0; countF = 0;}
if (countB > 10) {showLetter(B); countA = 0; countB = 0; countC = 0; countT = 0; countF = 0;}
if (countA > 10) {showLetter(A); countA = 0; countB = 0; countC = 0; countT = 0; countF = 0;}
delay(10);
}
void showLetter(int letter[]) {
for (int i = 0; i < NUM_LEDS; i++) {
if (letter[i] == 1) {
leds[i] = CRGB::White;
} else {
leds[i] = CRGB::Black;
}
}
FastLED.show();
}
I printed a small panel to mount the switch on. This is the intended use for the Rotary Switch, to accept a user's answer to a multiple choice question (A, B, C), or a True/False question (T, F). Then I connected a 5x7 NeoPixel Display which is also part of my Think-a-Tron 2020 project.
Here are all the connections to the Arduino:
- Display Red wire to +5V
- Display Green wire to D6
- Display White wire to GND
- Switch Piston wire to A5
- Switch Resistors wire to GND
Here is a video of the Rotary Switch and 5x7 Display in action.
Step 7: Final ThoughtsI'm pretty happy with my DIY Rotary Switch. It works well and has a nice "feel" as you switch between the stops.
Not everyone will want to take the time to make their own rotary switch, and will most certainly have different requirements than I had. However, for someone like me that does a lot of reproduction work, it's nice to know that with a little effort you can obtain exactly what you need to get the job done, without compromise.
Comments