Two Pins for the Price of One

One of the most common problems in the world of microcontrollers is running out of resources. Sometimes it’s memory, where the code must be pared down to fit into the flash on the microcontroller. Other times, as [Fabien] found out when he ran out of pins, the limitations are entirely physical. Not one to give up, he managed to solve the problem by using one pin for two tasks. (Google Translate from French)
During a recent project, [Fabien] realized he had forgotten to add a piezo buzzer to his project. All of the other pins were in use, though, so his goal was to use one of the input pins to handle button presses but to occasionally switch to output mode when the piezo buzzer was needed. After all, the button is only used at certain times, and the microcontroller pin sits unused otherwise. After a few trials, he has a working solution that manages to neither burn out itself nor the components in the circuit, and none of the components interfere with the other’s normal operation.
While it isn’t the most technically advanced thing we’ve ever seen here, it is a great example of using the tools at your disposal to elegantly solve a problem. More than that, though, it’s a thorough look into the details of pull-up and pull-down resistors, how microcontrollers see voltage as logic levels, and how other pieces of hardware interact with microcontrollers of all different types. This is definitely worth a read, especially if you are a beginner in this world.

22 thoughts on “Two Pins for the Price of One

    1. Negative. True it is an old, tried and true method of multiple pin use.
      But not charlie plexing.
      Charlie plexing is for multiple OUTPUTS only, specifically LEDs (the diode nature of LEDs are a necessity of CP), and is a relatively “new” term.

  1. This is within the spirit of Hackaday. It doesn’t necessarily need to be something complicated, or even different. Some of these simple methods can be expanded and used for other things. I don’t mind this stuff at all, and it is applicable to some of my projects. And making it more complicated with a MUX isn’t always needed.

        1. It’s in parallel with the piezo *and* reverse biased like you would have with a relay to stop a back EMF coming back from the relay coil.

          Fair enough a piezo works in reverse and will convert kinetic energy to electrical energy but I would expect the reflected voltage to be so small that you wouldn’t need to worry about it. The diode isn’t going to shunt any voltage until it get to -0.6 Volts or so.

  2. Reminds me of a product I once worked on, where scope creep nearly killed us. The one button input became two button on the one pin, then became two buttons on one pin with external button removal detection, which then grew to include voltage rail measurement.

    And not an A2D in sight – all with on board comparators and cleaver use of external resistors…

  3. I often use MISO, MOSI, SCK and one extra pin as the 4 data pins for a 2×16 LCD and the ISP header. Each peripheral gets their own chip-select pin to insure they don’t screw with each other. Even the ISP header has an implied “chip-select” in that it’s only active when !RESET is low.

    1. Regarding RESET, I often use it as an input without changing fuses to disable the reset functionality (and thus breaking the ISP). Protip? Memory is preserved during reset.

      So if your program/logic allows for it, your main entrypoint can also be thought of as a label that’s been GOTO/JMP’d to when reset is pressed(low>high). e.g. add a counting variable to use for changing modes.

      If using avr-gcc, you need to add a “__attribute__ ((section(“.noinit”)))” to the variable declaration(s) so gcc’s init function doesn’t zero them out on reset.

    2. Oh yeah, and for multi-SPI slaves, unless you’re sometimes running some in parallel for some reason, I like to use a demultiplexer to cut back on the CS pins. i.e. a 74138 uses 3 pins for 7 devices or 74154 for 4 pins to 15 devices (one of the output pins is unused for a “no CS selected” state)

  4. I sometimes use the power led pin, high to light led, low to drive buzzer. Most of my stuff the power led is actually carrying diagnostic pulses, looks like a solid led, but on a scope lots of pulses that show timing of different segments of code

      1. I’ve done this a few times too, I usually do a bit banged pin when I need the uart elsewhere, but I mostly want critical timing so that’s when I pulse the power led, or vice versa , the power led is on and I pulse it off in various routines so I can measure timings

    1. Well, the best solution would be a debugger :)
      Otherwise in big projects the code for the led would end up in a messy debug which works, but after 2 months if you look at it, you could never understand the why of the pulses. My 2 cents…

  5. I was waiting at a train crossing and a train had passed but I was checking if one came form the other side too. I then thought to myself ‘you can get military spec micro controllers for $1.25 and there is still no indication at crossings what direction and how many trains there are’. Seems all very old and primitive.

    That and they still did not widely adopt my idea of putting LED warning strips next to the tracks on a crossing for bicycles and pedestrians to give a visually connected warning of the location of the danger. I only heard about such use sporadically on normal crossings for people who are engrossed into their pones and don’t look up.

    Sometimes society is very very slow.

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 )

Google+ photo

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

Connecting to %s