MATLAB Examples

Control Capacitive Sensor

This example shows how to use the MATLAB® Support Package for Arduino® Hardware to control a capacitive sensor built with foil connected between two digital pins on Arduino.

Contents

Hardware Requirements

  • Arduino board
  • A 100K to 50M Ohm resistor
  • A piece of foil

Hardware Setup

Build your own capacitive sensor with digital pin D2 and D9.

  • Connect the resistor between D2 and D9 pin on Arduino board.
  • Connect the foil to the receive pin D2 with a wire.

For more info, see http://playground.arduino.cc/Main/CapacitiveSensor?from=Main.CapSense.

Create Capacitive Sensor Object

Create arduino object with Sensor/Capacitive add-on.

a = arduino('COM22', 'Uno', 'Libraries', 'PaulStoffregen/CapacitiveSensor')
a = 

  arduino with properties:

                    Port: 'COM22'
                   Board: 'Uno'
           AvailablePins: {'D2-D13', 'A0-A5'}
               Libraries: {'PaulStoffregen/CapacitiveSensor'}

Create capacitive sensor object using send pin D9 and receive pin D2.

sensor = addon(a, 'PaulStoffregen/CapacitiveSensor', 'D9', 'D2')
sensor = 

  CapacitiveSensor with properties:

                   Pins: 'D9'(Send), 'D2'(Receive)
    CalibrationInterval: 20 (s)
 AutoCalibrationEnabled: 1

Read Capacitance Value

Turn off autocalibrate.

sensor.AutoCalibrationEnabled = false
sensor = 

  CapacitiveSensor with properties:

                   Pins: 'D9'(Send), 'D2'(Receive)
    CalibrationInterval: 20 (s)
 AutoCalibrationEnabled: 0
 

The returned value is in arbitrary unit and is a total of 30 readings.

value = readCapacitance(sensor, 30);
sprintf('Measured capacitance is %d\n', value)
ans =

Measured capacitance is 59


Clean Up

clear sensor
clear a