NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » "DIP switch" project (basic nerdkit)

December 20, 2011
by glendon
glendon's Avatar

Hi,

My "DIP switch" project works but I'm stumped by a couple of things. Hope someone could throw some light on these:

  1. Initially, I left the LED's anode on pin 27 of the MCU and the cathode on the GND rail. This causes DIP switch #5 to be unresponsive, even though a wire is connected between DIP switch #5 and MCU's pin 27. No matter whether DIP switch #5 is flipped ON or OFF, the value is always 0. Only when I removed the LED did DIP switch #5 works properly. May I know why having LED's anode on pin 27 causes DIP switch #5 not to work?

  2. When I flipped DIP switch #1 to ON, I get 0. If I flipped it to OFF, I get 1. The same goes for DIP switch #2 to #6. I'm assuming that the opposite should be the desired behavior (i.e. flipping to ON should gives a value of 1). Any ideas what could be the cause?

Thanks.

December 20, 2011
by Striker
Striker's Avatar

I'm still new but I think I know the answer. The second question sort of relates to the first, so I'll answer that first.

In order for the MCU to determine the logic state of a pin, it needs some sort of reference value (high/low). If nothing is connected to the pin, it has no value at all, so the MCU gets confused. The remedy is to either pull the voltage up with an external resistor or use the MCU's internal pull-ups to force it to '1' so it has a starting state.

The sample code uses the internal pull-up, so that the pin is set to logic '1' when nothing is connected to it, and '0' when it's connected to GND. Thus, we have to reverse the way we think about the switch. When the switch is off, there is nothing connected to the pin, so it defaults to 1. When the switch is on, it connects the pin to GND and registers a 0. That's why it seems reversed to you.

With that in mind, think back to your first question and how you left the LED connecting the pin to GND. This means that the pin will always read 0 since it always has something connected to it!

There is such thing as a pull-down which would allow the switch to work the way you would think, but Google couldn't find me a common MCU with internal pull-downs.

If you're curious about how we turn the pull up on:

When a pin is set to OUTPUT, we send signals using PORTB/C/D.

When a pin is set to INPUT, we receive signals using PINB/C/D and set the pull-up using PORTB/C/D

December 20, 2011
by glendon
glendon's Avatar

Hi Striker,

Your explanation really helps. Thanks a lot!

While doing the "DIP switch" project, I researched on the term "high impedance" and "pull up/down registers". I found a really good article on tri-state buffer - http://www.cs.umd.edu/class/spring2003/cmsc311/Notes/CompOrg/tristate.html - It talks about why "high impedance" is needed and how it's being achieved through tri-states.

Now, for some reason, I was thinking that "pull up/down registers" has something to do with "high impedance" too. Your explanation about setting the pull-up register to 1 and that gives the pin a default value clears my thought quite a bit.

So, does this mean that if the MCU has a built-in pull-down register, we should use that instead and set that to 1; and by doing so, when the DIP switches are OFF, they give a 0 (and if ON, give a 1)?

Also, for the LED, does it mean that since its cathode is connected to GND and its anode is connected to MCU's pin 27, that pin is set to 0; thus overriding the fact that the pull-up register for pin 27 has been set?

December 20, 2011
by Striker
Striker's Avatar

Deciding between a pull-up and pull-down may depend on what you're using the pin for. I think (and I'm just guessing) some components may send analog signals in such a way where you may need one or the other. But from what I can tell, internal pull-ups are standard (I think pull-downs may even be considered rare), so everything's probably geared towards integrating with pull-ups.

But to answer your question, if the pin had an internal pull-down, then you could make the switch act the way you first assumed it would. The only difference is that the DIP switch would need to connect the pin to +5V, not GND.

As for the LED, remember that connecting a pin to GND sets the pin to 0 when the internal pull-up is enabled. Since electricity only flows one way, the DIP switch's state does nothing to prevent the pin from crossing over the LED into GND, thus causing the pin to always read 0.

A good way to visualize it would be to imagine that a pulled up pin is a bucket of water that gets filled by the MCU using water from +5V. The LED pokes a hole in the bucket and the water drains instantly into the GND. Toggling the DIP switch on/off will poke/fill its own hole in the bucket, but the LED's hole is still there, keeping the bucket empty. Thus, the pin always reads 0.

If you were using a pull-down, you would reverse the roles. The MCU would drain the pin and the LED and DIP switch would fill it using +5V.

December 21, 2011
by Striker
Striker's Avatar

I was tinkering around today and discovered that you can make your own pull-down by disabling the internal pull-up, connecting the pin to GND with a resistor (experiment with different values), then connecting the DIP switch to +5V.

December 22, 2011
by glendon
glendon's Avatar

Ok, I just set up DIP switch 6 to +5V, placed a 10k resistor between MCU pin 28 and GND and turned off MCU pull-up resistor for MCU pin 28. Now when DIP switch 6 is OFF, it gives 0 (if ON, gives 1).

My concept of pull-up resistor was a bit fuzzy before this. But, by implementing the pull-down resistor, the concept is now pretty clear to me. I also found this article that helps: http://dangerousprototypes.com/docs/Pull-up_resistors,_high_impedance_pins,_and_open_collector_buses

Can't stop grinning right now! Partly because I managed to get the simple experiment working, but mostly because I felt I really learnt something. Thanks for your sharing, Striker. You've made my day! :)

Post a Reply

Please log in to post a reply.

Did you know that talking to the microcontroller over the USB/Serial link is easy under Windows, Linux, and OS X? Learn more...