TOUCH16 channel, wordvariable

TOUCH16 [config], channel, wordvariable

Channel - is a variable/constant specifying the ADC channel (not pin number)

Wordvariable - receives the 16 bit touch reading (10 bit on X2 parts)

Config - is an optional variable/constant specifying a configuration value


Read the touch sensor on the ADC channel and save reading into word variable. This command automatically configures the pin as an ADC and as a touch sensor. The touch16 command is used to read the touch sensor value from the microcontroller touch pin. Note that not all inputs have internal ADC / touch functionality - check the pinout diagrams for the PICAXE chip you are using. Note that touch16 requires use of a word variable (e.g. w1 not b1), use the touch command for a byte variable.

IMPORTANT - Never 'directly touch' a touch sensor (e.g. a piece of bare wire)! A touch sensor must be electrically isolated from the end user. On a commercial PCB this can be as simple as the 'solder resist' lacquer layer printed over the pad, or on a home made PCB this can be achieved by placing a small piece of 2mm plastic over the PCB pad (the copper pad should be at least 15mm in diameter). The top of a plastic project box makes an ideal insulator. Simply stick the PCB to the inside of the box and place a 'sticker' as a target on the outside of the box. Note touch sensor pads must NOT have any other electrical connection than the connection to the PICAXE pin (e.g. touch sensor pads must not include a 10k pull up or pull down resistor as found on many project boards).

Please note that the touch16 reading can be affected by long serial cables connected to the project PCB (e.g. the older AXE026 download cable). Therefore it is not recommended that the older AXE026 serial cable (or AXE026/USB adapter combination) is used when trying to calibrate the touch16 command as it can affect the readings, only use the AXE027 USB cable for this purpose. Due to the design of the silicon inside the microcontroller each pin will give slightly different readings. Therefore each pin must be calibrated separately.

In simple terms a touch sensor works by detecting the change in capacitance when a finger is placed near the touch sensor pad. This capacitance affects the frequency of an internal oscillating signal. By measuring the time it takes for a set number of oscillations, the relative capacitance can be calculated. This value will change when the finger is placed nearby - the finger increases the total capacitance which then decreases the oscillation speed, and so the time taken (value) of the touch16 command increases. Touch sensors do not work when wet, they must be kept dry. A touch sensor pad is made from an area of copper pour on a PCB, approximately 15mm - 20mm in diameter. It can be any shape.

When designing multiple sensors close by each other consider the width of a human finger and that user finger placement will not always be that accurate. Where possible print visual 'targets' above the pad and leave as large as space as possible between pads. The AXE181 '18M2 touch sensor demo board' is the suggested low cost development board for trying out touch sensors. Note that M2 and X2 parts have different internal silicon methods of measuring capacitance change. The X2 method is faster, but gives a 10 bit (0-1023) value instead of a 16 bit value.

Effect of increased clock speed:

The clock speed will effect the count rate and so the result will change for each clock speed. Therefore the touch16 command must be calibrated at the actual clock speed in use.  


Configuration Byte - M2 parts

Normally the default configuration is recommended, so the optional config byte is not required within the touch16 command. However the optional 'config' byte can be used to fine tune the touch16 command operation if desired. Config byte is broken down into 8 bits for M2 parts as follows:

bit7,6,5 = Counter preload value (bits 7-5), e.g.
  = 000 Oscillation count required = 256
  = 010 Oscillation count required = 192
  = 100 Oscillation count required = 128
  = 110 Oscillation count required = 64
  = 111 Oscillation count required = 32
bit4,3 = 00 Touch sensor oscillator is off
  = 01 Low range (0.1uA)
  = 10 Medium range (1.2uA)
  = 11 High Range (18uA)
bit2,1,0 = Counter Prescalar (divide by 2 up to 256) e.g.
  = 001 Prescalar divide by 4

The default value for M2 parts is %000 01 001


Configuration Byte - X2 parts

Normally the default configuration is recommended, so the optional config byte is not required within the touch16 command. However the optional 'config' byte can be used to fine tune the touch16 command operation if desired. Config byte is broken down into 8 bits for X2 parts as follows:

bit7, 6 = Not used
bit5,4 = 00 Touch sensor oscillator is off
  = 01 Nominal charge current
  = 10 Medium current (10 x Nominal)
  = 11 High current (100 x Nominal)
bit 3,2,1,0 = Charge Time in multiples of 2us (1-15)

The default value for X2 parts is %0011 0010 (High current, charge time length multiple 2)

Applies To:
08M2, 14M2, 18M2, 20M2, 28X2, 40X2
See Also:
Related Create:

Detect a touch

Set output B.2 high only when a touch sensor on ADC channel 1 is touched

Code Example:
main:	touch16 1,w0		; read value into w0
	if w0 > 3000 then
	  high B.2		; output B.2 on
	  low B.2		; output B.2 off
	goto main		; else loop back to start
Copy Code Submit an Example

Branch on a 2 second touch

Determine whether a touch to the sensor has been there for 2 seconds. This allows program control to branch to a menu or take other action when the touch is activated.

Code Example:
   touch16 TOUCHPIN, adcreading
   if adcreading > touchOnLevel then
      ; wait 2 seconds
      pause 2000
      ; see if touch is still there
      touch16 TOUCHPIN, adcreading 
      if adcreading > touchOnLevel then
         ; yes - do something here
         gosub doSomething
      ; wait 10 seconds before allowing another no touch calibration
      if time > 10 then
        gosub calibrateNoTouch
   end if


; routine to determine what the base line NO TOUCH
; point is on a 1 inch diameter sensor made from 
; double-sided PCB material

; Periodically determine and store the touch ON/OFF
; levels with no touch

   ; reset every time
   touchOffLevel = 0
   for counter = 1 to 10
      touch16 TOUCHPIN, adcreading
      touchOffLevel = adcreading / 10 + touchOffLevel
   ; add some threshold value
   touchOnLevel = touchOffLevel + 100
   ; restart the time
   time = 0

Copy Code Submit an Example

Submit Your Own Code!

You must be logged in to submit code examples. Login now.


  • Login to leave a comment.