t r o n i x s t u f f

fun and learning with electronics

Tutorial: Arduino and Push-wheel switches

This is chapter forty of a series originally titled “Getting Started/Moving Forward with Arduino!” by John Boxall – a series of articles on the Arduino universe. The first chapter is here, the complete series is detailed here. Any files from tutorials will be found here.

Please note that the tutorials are not currently compatible with Arduino IDE v1.0. Please continue to use v22 or v23 until further notice. 

Update! See the addendum for using four switches at once to read four-digit numbers here

Welcome back fellow arduidans!

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.

So there you have it. Would you actually use these in a project? For one digit – yes. For four? Probably not – 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.

Update! See the addendum for using four switches at once to read four-digit numbers here

Have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column, or join our Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other –  and we can all learn something.

August 15, 2011 - Posted by | arduino, education, I2C, microcontrollers | , , , , , , , , , , , , , , , ,

7 Comments »

  1. I use up to 19 of these in remote controls, for electronic scoreboards because they are so simple to use , I lay them out in the same pattern as the scoreboard and even 80 year old cricket scorers can use the remote :-)

    see half way down the page at a basketball remote at http://scorebauds.co.za/page4.htm#Basketball

    Comment by John Smith | August 15, 2011 | Reply

    • That’s awesome – thank you for sharing
      john

      Comment by John Boxall | August 15, 2011 | Reply

      • Er – I just noticed you have no diodes on the 4 contacts of the switches, but you say all the 1,2,4,8 connections are tied together ?

        Any 1 switch set to 9 would mechanically short out all 4 lines regardless of if the common is connected or not, so all switches would be set to 9.

        I don’t use a 4066 if there are only 4 switches , I just take each common to a pin on the micro to scan in sequence, but I have diodes on all the 1,2,4,and 8 connections of the switches.

        Comment by John Smith | August 16, 2011

      • The second part of the article was a bit of a mess, so we’ve deleted it.
        However we’ve come up with a completely different way of doing it, will be published in the next few days.
        cheers
        john

        Comment by John Boxall | August 27, 2011

  2. These are neat bits of hardware! Is it really necessary to use the 74HC4066, though? Couldn’t you just connect the Arduino pins directly? The current draw ought to be minimal, since the input pins are high impedance.

    Comment by Nick Johnson | August 15, 2011 | Reply

    • Originally I tried it without the 74HC4066 but it went cuckoo bananas. (Something to do with internal pull-up resistors in the ATmega328 I/O pins) So the next thing that came to mind was the 74HC4066.

      Comment by John Boxall | August 15, 2011 | Reply

      • Heres a sketch of how you can use 10 bcd or groups of 4 normal switches http://flic.kr/p/9PB8nm .
        If you cascade the 4017s ( http://flic.kr/p/9S6oUy ) then you can send data from up to25 switches via VirualWire with just 6 pins ( plus the wireless and interrupt pins ) of the arduino in one scan .

        John aka Boffin1 Cape Town

        Comment by John Smith | August 16, 2011


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 )

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

Follow

Get every new post delivered to your Inbox.

Join 2,588 other followers