check in
Ongoing

ATtiny13 TinyTouchLight

STDATtiny13 TinyTouchLight

tag

2.2k
0
0
0
Mode:Full

License

CC-BY-SA 3.0

Creation time:2021-09-05 10:09:06Update time:2022-02-03 21:43:27

Description

TinyTouchLight - Dimmable USB Night Light with Capacitive Touch Control

TinyTouchLight is a dimmable USB night light with capacitive touch control based on the ATtiny13A. It plugs into any USB charger or power bank.

TinyTouchLight_pic3.jpg

Capacitive Touch Button

Introduction

This implementation of a touch-sensitive button (touchkey) is based on the charge sharing approach similar to Atmel's QTouchADC and Tim's TinyTouchLib. It works without external components, only a resistor for noise reduction is used, which can also be dispensed with. The touchkey itself is a small copper area on the PCB (sense electrode), which is covered with solder mask. This sense electrode is connected to a single ADC-capable pin of the ATtiny via the resistor.

Basic Principle

The touch sense pin (here PB4) is connected through series resistor RS to the sensor electrode capacitance, represented by Cx. The switch SW represents a finger touching the key. The capacitance introduced by the finger is represented as Ct. When the key is touched, Ct is switched into the circuit forming a parallel capacitor with Cx, changing the effective sensor capacitance to Cx + Ct.

TinyTouchLight_principle.png

It should be noted that Cx and Ct are not physical capacitors. Cx is the effective capacitance of the sense electrode and Cx + Ct is the effective capacitance of the human finger touching the sensor.

Component Selection

The series resistor RS is nominally 1kΩ, but it may be increased to higher values to improve the noise immunity of the circuit. The value of RS should be increased in steps to find the lowest value that provides adequate noise immunity. Resistance values of up to 100kΩ have proven to be useful in extremely noisy environments. For this application a 47kΩ resistor was chosen.

The value of Cx should be close to that of the ADC’s internal sample-and-hold capacitor CS/H (~14pF). For best performance it is recommended that Cx+t should not be greater than ~60pF. If the sensor electrode is designed as a copper surface on the PCB, then it should be roughly as large as the contact surface of a finger (8-15 mm in diameter if the touchkey sensor is round, or with a 8-15 mm side if the touchkey sensor is square). There shouldn't be any traces on the other side of the PCB. The back side can have a ground plane, but this should not be a solid fill.

Sensor Acquisition

The acquisition method works by sharing charge between the ADC’s internal sample-and-hold capacitor (CS/H) and the sense electrode capacitance (Cx). When the sensor is touched the effective capacitance of the sensor electrode increases and becomes Cx + Ct. This affects the amount of charge shared between the capacitors. When pre-charging Cx and sharing with CS/H, charge transferred to CS/H increases on touch and ADC input voltage increases. When pre-charging CS/H and sharing with Cx, charge remaining on CS/H decreases on touch and ADC input voltage decreases. But the resulting signal from the averaged ADC values increases on touch. If the difference between signal and reference is greater than the user-determined threshold (delta), a touch is reported.

The charge sharing is carried out in the following sequence:

  1. Precharge touchkey LOW and S/H cap HIGH.
  2. Connect touchkey and S/H cap in parallel. Read the voltage via the ADC.
  3. Precharge touchkey HIGH and S/H cap LOW.
  4. Connect touchkey and S/H cap in parallel. Read the voltage via the ADC.
  5. Calculate the voltage difference and compare it with the user-determined threshold.

Step 1: Precharge touchkey LOW and S/H cap HIGH.

By setting the touch sense pin (PB4) to OUTPUT LOW, the touchkey is discharged. By setting the ADC muxer to a spare pin (here PB3) and setting this pin to OUTPUT HIGH, the internal S/H capacitor is charged.

TinyTouchLight_step1.png

ADMUX   =  TC_SHC_ADC;              // connect spare pin to S/H cap
PORTB  |=  (1<S/H, due to the remaining charge, is sampled by the ADC.

![TinyTouchLight_step2.png](https://raw.githubusercontent.com/wagiminator/ATtiny13-TinyTouchLight/main/documentation/TinyTouchLight_step2.png)

```c
DDRB   &= ~(1<>4);  // difference with bias value
  if (!TC_touch) {                    // not touched previously?
    if (diff > TC_THRESHOLD_ON) {     // new touch detected?
      TC_touch = 1;                   // set "is touched" flag
      TC_timer = 0;                   // reset touch timer
      return TC_PUSH;                 // return "new push"
    }
    TC_bias = (TC_bias - (TC_bias>>6)) + (tmp>>2); // update bias (low pass)
    return TC_OFF;                    // return "still not pushed" 
  }
  else {                              // touched previously?
    if (diff < TC_THRESHOLD_OFF) {    // touch button released?
      TC_touch = 0;                   // clear "is touched" flag
      return TC_RELEASE;              // return "touch pad released"
    }
    if (TC_timer == TC_TIMEOUT) {     // still touched but for too long?
      TC_bias  = TC_getDelta()<<8;    // maybe stuck situation, read new bias
      return TC_FAIL;                 // return "fail"
    }
    TC_timer++;                       // increase timer
    return TC_ON;                     // return "still pushed"
  }
}

LED Dimmer

The LEDs are controlled via a PWM-capable pin (PB0) and a MOSFET. Timer0 generates the PWM signal, the duty cycle is controlled via the OCR0A register. The main function brings everything together:

int main(void) {
  // Local variables
  uint8_t bright = 64;                // current brightness of LEDs 
  uint8_t dir    = 0;                 // current fade direction

  // Setup
  TCCR0A = (1< Board -> MicroCore** and select **ATtiny13**.
- Go to **Tools** and choose the following board options:
  - **Clock:**  128 kHz internal osc.
  - **BOD:**    disabled
  - **Timing:** Micros disabled
- Connect your programmer to your PC and to the ATtiny.
- Go to **Tools -> Programmer** and select your ISP programmer (e.g. [USBasp](https://aliexpress.com/wholesale?SearchText=usbasp)).
- Go to **Tools -> Burn Bootloader** to burn the fuses.
- Open the TinySat sketch and click **Upload**.

## If using the precompiled hex-file
- Make sure you have installed [avrdude](https://learn.adafruit.com/usbtinyisp/avrdude).
- Connect your programmer to your PC and to the ATtiny.
- Open a terminal.
- Navigate to the folder with the hex-file.
- Execute the following command (if necessary replace "usbasp" with the programmer you use):

avrdude -c usbasp -p t13 -U lfuse:w:0x3b:m -U hfuse:w:0xff:m -U flash:w:tinytouchlight.hex


## If using the makefile (Linux/Mac)
- Make sure you have installed [avr-gcc toolchain and avrdude](http://maxembedded.com/2015/06/setting-up-avr-gcc-toolchain-on-linux-and-mac-os-x/).
- Connect your programmer to your PC and to the ATtiny.
- Open the makefile and change the programmer if you are not using usbasp.
- Open a terminal.
- Navigate to the folder with the makefile and the sketch.
- Run "make install" to compile, burn the fuses and upload the firmware.

# References
1. [Tim's TinyTouchLib](https://github.com/cpldcpu/TinyTouchLib)
2. [AVR3001: QTouchADC](http://ww1.microchip.com/downloads/en/Appnotes/doc8497.pdf)
3. [AN2934: Capacitive Touch Sensor Design Guide](http://ww1.microchip.com/downloads/en/Appnotes/Capacitive-Touch-Sensor-Design-Guide-DS00002934-B.pdf)
4. [ATtiny13A Datasheet](http://ww1.microchip.com/downloads/en/DeviceDoc/doc8126.pdf)

![TinyTouchLight_pic5.jpg](https://raw.githubusercontent.com/wagiminator/ATtiny13-TinyTouchLight/main/documentation/TinyTouchLight_pic5.jpg)
![TinyTouchLight_pic2.jpg](https://raw.githubusercontent.com/wagiminator/ATtiny13-TinyTouchLight/main/documentation/TinyTouchLight_pic2.jpg)
![TinyTouchLight_pic4.jpg](https://raw.githubusercontent.com/wagiminator/ATtiny13-TinyTouchLight/main/documentation/TinyTouchLight_pic4.jpg)

# License
![license.png](https://i.creativecommons.org/l/by-sa/3.0/88x31.png)

This work is licensed under Creative Commons Attribution-ShareAlike 3.0 Unported License. 
(http://creativecommons.org/licenses/by-sa/3.0/)

Design Drawing

schematic diagram
PCB

BOM

IDNameDesignatorFootprintQuantity
1100nC1C_08051
247uC2C_08051
35050LED1,LED2,LED3,LED4LED_50504
4AO3400Q1SOT-23-3_W1
539RR1,R2,R3,R408054

Attachments

OrderFile nameDownload times
No data
Clone
Add to Album
0
0
Share
Report

Project Members

Comment

All Comments(1)
Sort by time|Sort by popularity
Followers0|Likes0
Related projects
Empty

Bottom Navigation