Follow mercenarymind on Twitter
Tutorial: Arduino and Push-wheel switches
Written by TRONIXSTUFF   
Monday, 26 September 2011 00:00

Authors: TRONIXSTUFF

In this article we go back to the past via the use of push-wheel/thumb-wheel switches with out Arduino systems. Here are some examples sourced from somewhere on eBay:


For the uninitiated, each switch is one vertical segment and they can be connected together to form various sizes. You can use the buttons to select from digits zero through to nine. There are alternatives available that have a wheel you can move with your thumb instead of the increase/decrease buttons. Before the days of fancy user interfaces these switches were quite popular methods for setting numerical data entry. However they are still available today, so let’s see how they work and how we can use them.

The switch’s value is made available via binary-coded decimal. Consider the rear of the switch:


We have common on the left, then contacts for 1, 2, 4 and 8. If you apply a small voltage (say 5V) to common, the value of the switch can be measured by adding the values of the contacts that are in the HIGH state. For example, if you select 3 – contacts 1 and 2 will be at the voltage at common. The values between zero and nine can be represented as such:

By now you should realise that it would be easy to read the value of a switch – and you’re right, it is. We can connect 5V to the common,  the outputs to digital input pins of our Arduino boards, then use digitalRead() to determine the value of each output. In the sketch we use some basic mathematics to convert the BCD value to a decimal number. So let’s do that now.

From a hardware perspective, we need to take into account one more thing – the push-wheel switch behaves electrically like four normally-open push buttons. This means we need to use pull-down resistors in order to have a clear difference between high and low states. So the schematic for one switch would be (click image to enlarge):


Now it is a simple matter to connect the outputs labelled 1, 2, 4, and 8 to (for example) digital pins 8, 9, 10 and 11. Connect 5V to the switch ‘C’ point, and GND to … GND. Next, we need to have a sketch that can read the inputs and convert the BCD output to decimal. Consider the following sketch (download):

Example 40.1

/*
Example 40.1 - display single thumbwheel switch data
http://tronixstuff.wordpress.com/tutorials > chapter 40 | cc v3.0 by-sa-nc
Uses Gravitech SAA1064 numerical display shield http://www.gravitech.us/7segmentshield.html
Uses serial monitor if you don't have the SAA1064 shield
*/

#include

#define q1 8
#define q2 9
#define q4 10
#define q8 11

void setup()
{
Serial.begin(9600);
Wire.begin(); // join i2c bus (address optional for master)
delay(500);
pinMode(q1, INPUT); // thumbwheel '1'
pinMode(q2, INPUT); // thumbwheel '2'
pinMode(q4, INPUT); // thumbwheel '4'
pinMode(q8, INPUT); // thumbwheel '8'
}

void dispSAA1064(int Count)
// sends integer 'Count' to Gravitech SAA1064 shield
{
const int lookup[10] = {
0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F };
int Thousands, Hundreds, Tens, Base;
Wire.beginTransmission(0x38);
Wire.send(0);
Wire.send(B01000111);
Wire.endTransmission();
Wire.beginTransmission(0x38);
Wire.send(1);
Thousands = Count/1000;
Hundreds = (Count-(Thousands*1000))/100;
Tens = (Count-((Thousands*1000)+(Hundreds*100)))/10;
Base = Count-((Thousands*1000)+(Hundreds*100)+(Tens*10));
Wire.send(lookup[Base]);
Wire.send(lookup[Tens]);
Wire.send(lookup[Hundreds]);
Wire.send(lookup[Thousands]);
Wire.endTransmission();
delay(10);
}

int readSwitch()
{
int total=0;
if (digitalRead(q1)==HIGH) { total+=1; }
if (digitalRead(q2)==HIGH) { total+=2; }
if (digitalRead(q4)==HIGH) { total+=4; }
if (digitalRead(q8)==HIGH) { total+=8; }
return total;
}

void loop()
{
dispSAA1064(readSwitch()); // sends switch value to display shield
Serial.println(readSwitch()); // sends switch value to serial monitor box
}

The function readSwitch() is the key. It calculates the value of the switch by adding the numerical representation of each switch output and returns the total as its result. For this example we used a numerical display shield that is controlled by the NXP SAA1064. If you don’t have one, that’s ok – the results are also sent to the serial monitor. Now, let’s see it in action:

Ok it doesn’t look like much, but if you need numerical entry it saves a lot of physical space and offers a precise method of entry. Now let’s repeat the exercise, however using four digits instead of one. This will be somewhat more complex, but is certainly achievable. It is a mess to describe using Fritzing, so instead I will narrate the schematic for you…

All the 1, 2, 4, and 8s of the thumb wheel switches connect together and thence to digital 8~11 respectively, with a 10k ohm pull-down resistor to GND as normal. However – we need to activate one switch at a time, so we use a 74HC4066 to electrically isolate the common for each switch. Please read this now if you are unfamiliar with the 74HC4066.

We connect digital pins 12, 7, 2 and 4 to 74HC4066 pins 13, 5, 6 and 12 respectively. 74HC4066 pins 1, 4, 8, 11 and 14 connect to 5V; pin 7 to GND, and pins 2, 3, 9 and 10 to the commons of the thumb wheel switches from left to right.

Now consider the sketch (download):

Example 40.2

/*
Example 40.2 - display four thumbwheel switch data
http://tronixstuff.wordpress.com/tutorials > chapter 40 | cc v3.0 by-sa-nc
Uses Gravitech SAA1064 numerical display shield http://www.gravitech.us/7segmentshield.html
Uses serial monitor if you don't have the SAA1064 shield
*/

#include

// digital pins for BCD output
#define q1 8
#define q2 9
#define q4 10
#define q8 11

//digital pins for each thumbwheel's 5V
int s1=12;
int s2=7;
int s3=2;
int s4=4;

void setup()
{
Serial.begin(9600);
Wire.begin(); // join i2c bus
delay(500);
pinMode(q1, INPUT); // thumbwheel '1'
pinMode(q2, INPUT); // thumbwheel '2'
pinMode(q4, INPUT); // thumbwheel '4'
pinMode(q8, INPUT); // thumbwheel '8'

pinMode(s1, OUTPUT); // thumbwheel #1~4
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
pinMode(s4, OUTPUT);

digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
digitalWrite(s4, LOW);
}

void dispSAA1064(int Count)
// sends integer 'Count' to Gravitech SAA1064 shield
{
const int lookup[10] = {
0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F };
int Thousands, Hundreds, Tens, Base;
Wire.beginTransmission(0x38);
Wire.send(0);
Wire.send(B01000111);
Wire.endTransmission();
Wire.beginTransmission(0x38);
Wire.send(1);
Thousands = Count/1000;
Hundreds = (Count-(Thousands*1000))/100;
Tens = (Count-((Thousands*1000)+(Hundreds*100)))/10;
Base = Count-((Thousands*1000)+(Hundreds*100)+(Tens*10));
Wire.send(lookup[Base]);
Wire.send(lookup[Tens]);
Wire.send(lookup[Hundreds]);
Wire.send(lookup[Thousands]);
Wire.endTransmission();
delay(10);
}

int readSwitch()
{
int total=0;
if (digitalRead(q1)==HIGH) {
total+=1;
}
if (digitalRead(q2)==HIGH) {
total+=2;
}
if (digitalRead(q4)==HIGH) {
total+=4;
}
if (digitalRead(q8)==HIGH) {
total+=8;
}
return total;
}

int readSwitches()
{
int ones=0;
int tens=0;
int hundreds=0;
int thousands=0;
int final=0;

digitalWrite(s1, HIGH);
thousands=readSwitch();
digitalWrite(s1, LOW);

digitalWrite(s2, HIGH);
hundreds=readSwitch();
digitalWrite(s2, LOW);

digitalWrite(s3, HIGH);
tens=readSwitch();
digitalWrite(s3, LOW);

digitalWrite(s4, HIGH);
ones=readSwitch();
digitalWrite(s4, LOW);
final=(thousands*1000)+(hundreds*100)+(tens*10)+ones;
return final;
}

void loop()
{
dispSAA1064(readSwitches()); // sends switch value to display shield
Serial.println(readSwitches()); // sends switch value to serial monitor box
}

Note the new function readSwitches(). It “activates” each thumbwheel switch one at a time, from left to right. Each switch is read using readSwitch() and the value used to calculate the total displayed on the four switches. As another resident here borrowed my normal camera, here are some still demonstration images:




So there you have it. Would you actually use these in a project? For one digit – yes. For four? Maybe – perhaps it would be easier to use a 12-digit keypad. There’s an idea…  But for now I hope you enjoyed reading this as much as I did writing it for you.


If you have any questions about the processes or details in this article, please ask in our Google Group



Last Updated on Monday, 26 September 2011 01:57