CAPACITIVE TOUCH SENSOR USING MSP430G2452 IN ENERGIA

Photo of author

By Jackson Taylor

Home » Hardware » CAPACITIVE TOUCH SENSOR USING MSP430G2452 IN ENERGIA

As an Amazon Associate we earn from qualifying purchases.

A capacitive touch sensor detects a finger without any moving parts. It only needs a metal pad, a resistor and a microcontroller pin. In this guide you will build one with the MSP430G2452 and program it in Energia. The video above shows the project running, and the sections below explain how it works, how to wire it and how to turn the readings into a reliable touch signal.

How capacitive touch sensing works

Every metal pad has a small capacitance to its surroundings. When a finger comes close, the body adds to that capacitance. The change is tiny, but a microcontroller can measure it indirectly by timing how long the pad takes to charge through a resistor.

Charging time follows the RC rule: more capacitance means a longer charge time. So the sensing routine is simple:

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
  1. Discharge the pad so it starts from a known low level.
  2. Start charging it through a high-value resistor.
  3. Count how long the pin takes to read as HIGH.
  4. Compare that count with a stored baseline. A clearly larger count means a touch.

This needs no extra library or external chip, only two GPIO pins and a resistor.

What you need

  • An MSP430 LaunchPad with the MSP430G2452 (the chip is 3.3 V)
  • Energia installed on your computer, with the LaunchPad board selected
  • One resistor of about 1 MΩ
  • A touch pad, such as a small square of copper tape, foil or a bare PCB pad
  • Jumper wires and a breadboard
  • The on-board LEDs, or an external LED with a series resistor, for feedback

About the MSP430G2452 and Energia

The MSP430G2452 is a low-power 16-bit microcontroller from Texas Instruments’ value line, in a 20-pin package with a 10-bit ADC and a small amount of flash and RAM. Energia brings the Arduino-style programming model to these chips, so you can use pinMode, digitalWrite, digitalRead and micros instead of working with registers.

The G2452 has no hardware UART, so printing to the Serial Monitor is limited compared with the G2553. For that reason this project shows the touch state on LEDs. If you have not yet driven LEDs from Energia, start with the LED board using MSP430G2553 and Energia project, because the pin handling is the same.

See also
AUTOMATIC CHASER WITH SPEED CONTROL

Wiring the touch pad

Use two pins: a send pin that drives the charge, and a receive pin that senses it. Pick any two free pins, for example P1_4 and P1_5 in Energia naming.

  1. Connect the 1 MΩ resistor between the send pin and the receive pin.
  2. Connect the touch pad to the receive pin, on the same side of the resistor as the pin.
  3. Keep the wire between the pin and the pad short.
  4. Use the on-board LEDs (RED_LED and GREEN_LED on the LaunchPad) as indicators.

A larger pad is more sensitive but also picks up more noise. A pad roughly the size of a fingertip is a good starting point.

The sketch logic

Energia sketches have a setup and a loop function, as in Arduino. Here is how to structure them.

Setup

  • Set the send pin as an output and the LED pins as outputs.
  • Take several measurements with no finger on the pad, then average them. This is your baseline.

Measurement routine

  1. Set the send pin LOW and the receive pin to OUTPUT LOW for a moment to discharge the pad.
  2. Switch the receive pin to INPUT so it floats and can be charged through the resistor.
  3. Set the send pin HIGH.
  4. In a loop, increment a counter until digitalRead on the receive pin returns HIGH, or until a maximum count is reached so the sketch can never hang.
  5. Return the counter value.

Counting loop iterations gives good resolution for such short times. Using micros also works, but its step size may be too coarse for small changes.

Main loop

  • Call the measurement routine several times and average the results to smooth out noise.
  • If the average is above the baseline plus a margin, turn on the LED. Otherwise turn it off.
  • Add a short delay between readings.

Choosing a threshold

The right threshold depends on your pad size, wire length and resistor, so measure instead of guessing. Print or display the raw counts if you can, or watch the LED while you adjust the margin. Note the typical value with no touch, then the value with a finger, and set the threshold between the two, closer to the untouched value so touches register reliably.

See also
CASCADED MULTILEVEL (7 LEVEL) INVERTER SIMULATION IN MATLAB - SIMULINK

Using a margin above the baseline instead of a fixed number also helps when conditions change. You can re-measure the baseline from time to time while the pad is untouched, which compensates for slow drift from temperature and humidity.

Making it more reliable

  • Average several readings. A single measurement can jump because of noise.
  • Add hysteresis. Use a higher threshold to switch the LED on and a lower one to switch it off, so it does not flicker near the limit.
  • Debounce the result. Require two or three consecutive touch readings before you change state.
  • Cover the pad. A thin insulating layer, such as tape, protects the copper and still lets the sensor work, though it reduces sensitivity.
  • Keep wires short. Long leads act as antennas and add stray capacitance.

Troubleshooting

  • LED always on: The threshold is probably too low, or the pad is picking up noise. Raise the margin, shorten the wiring and recalculate the baseline.
  • LED never turns on: The change is too small. Try a larger pad or a larger resistor, or lower the margin.
  • Readings jump around: Average more samples and keep your hand and other wires away from the pad while you calibrate.
  • Behavior changes when you touch the board: Readings can differ depending on whether the board is grounded through USB to a computer. Calibrate in the same conditions in which you will use it.
  • Counter hits the maximum: The resistor or pad capacitance is too large. Reduce the resistor value.

Where to go next

Once one pad works, you can add more send and receive pairs to make a small keypad. You can also use the touch event to switch a relay or LED driver. The same digital pin logic appears in other Energia projects, such as digital output on the Tiva C LaunchPad. For analog sensing on an MSP430, see the LM35 temperature sensor example. If you want to log or display events on a PC, take a look at SCADA with Arduino or Energia.

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
CD4066B Quad Bilateral Switch

FAQ

Do I need a special library for capacitive touch on the MSP430G2452?

No. The charge-time method described here uses only standard Energia functions. Dedicated touch libraries and hardware features exist, but they are not required for a simple touch button.

Why use a 1 MΩ resistor?

A large resistor slows the charging enough that the small change in capacitance from a finger makes a measurable difference in time. If the readings are too small to tell apart, increase the value. If the counter often reaches its maximum, decrease it.

Can I use the same code on the MSP430G2553?

Yes, in most cases. The method relies on basic digital I/O, so it works on other LaunchPad chips. You only need to check that the board selection in Energia matches your chip and that the pins you chose exist.

Why does my sensor behave differently when I move my hand or cables?

Capacitive sensing reacts to anything that changes the capacitance around the pad, including cables, nearby metal and your hand’s position. Keep the wiring fixed, use short leads and recalibrate the baseline after you change the setup.

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