PUSH BUTTON / SWITCH INTERFACING WITH DSPIC30F2010 USING MIKRO C

Photo of author

By Jackson Taylor

Home » Electronics Topics » dsPIC Microcontroller » PUSH BUTTON / SWITCH INTERFACING WITH DSPIC30F2010 USING MIKRO C

As an Amazon Associate we earn from qualifying purchases.

A push button is often the first input a beginner connects to a microcontroller. In this tutorial we read a switch on a dsPIC30F2010 and use it to turn an LED on and off. The code is written for the Mikro compiler, and the video below shows the circuit working.

If you have not yet tried blinking an LED with the dsPIC30F2010, start there. It covers the output side, and this article adds the input side. For a quick overview of the chip itself, see the dsPIC30F2010 features page.

What you need

  • A dsPIC30F2010 with a working power supply, clock and programmer
  • One push button or switch
  • One 10 kΩ resistor to act as the pull-up
  • One LED and a series resistor (a few hundred ohms is typical)
  • A Mikro compiler for dsPIC (the code below uses Basic syntax)

How the circuit works

A microcontroller input pin must always see a clear high or low level. If the pin is connected only to a button, it floats whenever the button is not pressed, and the reading becomes random.

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video

The usual fix is a pull-up resistor. One end goes to the supply voltage and the other end to the input pin. The button then connects the same pin to ground.

  • Button released: the resistor pulls the pin high, so the pin reads 1.
  • Button pressed: the pin is connected to ground, so the pin reads 0.

This means the logic is inverted: pressing the button gives a 0. That is why the code in this tutorial checks for 0 to detect a press. The comment in the original code says the same thing: a pull-up is required on this pin.

Pin connections

  • Switch: one side to RD1, the other side to ground, with a 10 kΩ resistor from RD1 to the supply voltage.
  • LED: RE1 through a series resistor to the LED, then to ground.
See also
PROJECT DONE IN 2015-16

The code

This is the program used in the video. Note that the program is named BLINK because it started as the blink example, but it reads a switch and lights an LED.


program BLINK
dim LEDRED as sbit at LATE1_bit
dim switch1 as sbit at RD1_bit
main:
TRISD.1 = 1
TRISE.1=0
while TRUE
if (switch1 and 1)=0 then    'pull up required in this pin
LEDRED=1
else
LEDRED=0
end if
wend
end.

Code explained line by line

Naming the pins

The two dim lines give readable names to individual port bits. LEDRED is tied to LATE1_bit, the output latch of pin RE1. switch1 is tied to RD1_bit, which reads the level on pin RD1.

Writing outputs through the LAT register and reading inputs through the PORT register is the recommended way to work with dsPIC I/O pins. It avoids problems that can appear when a PORT register is written to directly.

Setting the direction

Each pin has a matching TRIS bit that sets its direction. A 1 makes the pin an input and a 0 makes it an output.

  • TRISD.1 = 1 makes RD1 an input for the switch.
  • TRISE.1 = 0 makes RE1 an output for the LED.

An easy way to remember it: 1 looks like the letter I (input), and 0 looks like the letter O (output).

The main loop

The while TRUE loop runs forever, which is normal for embedded programs. Inside it, the program checks the switch on every pass. If the pin reads 0 (button pressed), the LED is switched on. Otherwise, the LED is switched off.

The LED therefore follows the button. It stays on only while you hold the button down.

Why the pull-up is needed

Many beginners leave out the resistor and find the LED flickering or reacting when they touch the wires. That is a floating input. Some microcontrollers offer internal pull-ups on certain pins, but you should not rely on that unless the datasheet for your exact chip says it is available on that pin. An external 10 kΩ resistor works on any digital input and is easy to check on a breadboard.

Sustainable Tech

Keep Toxic E-Waste Out of Landfills

Don't throw away your failing laptop, phone, or car batteries. Learn how to recondition them at home in just a few hours. It's better for the planet—and your wallet.

Learn How It Works
See also
PIC16F877A - MIKRO C - LCD

Digital and analog pins

On the dsPIC30F2010, PORTD and PORTE pins are digital I/O, so the example needs no extra setup. Pins that are shared with the analog-to-digital converter, such as those on PORTB, may start up in analog mode. If you move the switch to one of those pins, configure it as digital first. Check the datasheet for the exact register settings.

Taking it further

Toggle the LED with each press

The example turns the LED on only while the button is held. To make it toggle on each press, remember the previous button state and change the LED only when the state moves from released to pressed. Then wait until the button is released before looking for the next press.

Debounce the switch

Mechanical buttons do not switch cleanly. The contacts bounce for a few milliseconds and can produce several rapid transitions. For a simple on/off follower like this one, bounce does not matter. If you count presses or toggle an output, you need debouncing. A short delay after the first change, often around 20 ms, and then a second reading is a common software approach. The Mikro libraries also include a button-reading routine that handles this for you.

Use more than one switch

You can repeat the same pattern with other pins: declare a name for each pin, set each TRIS bit to 1, and test each one inside the loop. For many switches, a matrix keypad uses fewer pins. The PIC tutorial on keypad interfacing with the PIC16F877A shows how that scanning works.

Related tutorials

The same idea of reading a pin and driving an LED appears on other platforms. You can compare it with switch interfacing with the 8051, a push button and LED example on an ARM board, or these Arduino button and switch projects. The wiring concept is the same everywhere, and only the register names and syntax change.

See also
CRYSTAL OSCILLATOR

Troubleshooting

  • LED never lights: check the LED polarity and series resistor, and confirm that TRISE.1 is 0.
  • LED is always on: the switch pin may be shorted to ground, or the wire to the button may be connected to the wrong pin.
  • LED behaves randomly: the input is floating, so check the pull-up resistor.
  • LED works backwards: the logic is inverted by the pull-up. Swap the 1 and 0 outputs, or change the test to compare with 1 if your button connects to the supply instead of ground.
  • Nothing works at all: check the power supply, the oscillator or clock configuration and the programmer connection.

FAQ

Why does the LED turn on when the button is pressed even though the code tests for 0?

Because of the pull-up resistor, the pin reads 1 when the button is open and 0 when it is pressed. The code tests for 0 to detect a press, and then sets the LED output to 1.

What do TRISD and TRISE do?

They are the direction registers for PORTD and PORTE. Setting a bit to 1 makes that pin an input, and setting it to 0 makes it an output.

Do I always need an external pull-up resistor?

Not always, but it is the safest choice. Some chips have internal pull-ups on certain pins, but you must confirm that in the datasheet. An external resistor of about 10 kΩ is a simple and reliable option.

Can I use a different pin for the switch or LED?

Yes. Change the pin names in the dim lines and the matching TRIS bits. Pay attention to pins that are shared with analog functions, as they may need to be set to digital mode first.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works