Interfacing a tristate switch with one µC pin

This is just a little trick I have not seen anywhere before, so I thought I might as well share it with the world.

If you have a switch with three states, such as an on-off-on switch and you only want to use one microcontroller pin, this is how you can do it:

tristate switch

The image shows the basic setup. Resistor values are given for Atmel controllers, but this should also work for PICs and other µCs.

If the switch is connected to Vcc the input reads high, if it is connected to GND, it reads low. But if the switch is in its open setting, the reading is depending on the pullup resistor beeing switch on or not. This can be used to recognize all three states: high, low and open.

Here is a pseudo c example:

uint8_t getPin(uint8_t num)
{
    setPullup(num, OFF);
    if(readPin(num) == HIGH)
        return 0;                //input is connected to Vcc
    setPullup(num, ON);
    if(readPin(num) == HIGH)
        return 1;                //input is open
    return 2;                    //input is connected to GND
}
symbols are from this site

 

Update 2019:

Just found this old blog post, though I’d update it a bit:
if you are using a µC which has both, pullup and pulldown (STM32 for example), you can of neglect the external resistor.

Electric Glockenspiel

Just found this in the attic…

Glockenspiel (1)

I built it when I was about 15, but never finished it. The next step would have been to install a midi interface…
It is a solenoid controlled glockenspiel and still works pretty well.

It sound great and I guess I will build a nice little midi interface, you can actually control the sound by alternating the duration of a keypress (keys are on the back), or hold it to dampen the sound.

Some nice velocity sensitive interface, the current through the solenoids possibly controlled by pwm should give some nice results 🙂

Glockenspiel (2)

Glockenspiel (3)