Controlling Light using Touch Sensor and 8051 Microcontroller

Published  April 10, 2019   0
Controlling Light using Touch Sensor and 8051 Microcontroller

In modern electronics world, touch input is used almost everywhere, whether it can be a mobile phone or an LCD monitor switch. Capacitive touch is the widely used in the touch sensor segment and we previously used capacitive touch with a Raspberry Pi. Here in this project, we will interface touch sensor with 8051 microcontroller AT89S52. If you are new to 8051 microcontoller then you can start with LED blinking with 8051.

 

What is a Capacitive Touch Sensor?

Capacitive touch works on the electrostatic charge that is available on our body. The screen is already charged with electric field. When we touch the screen a close circuit forms due to electrostatic charge that flows through our body. Further, software decides the location and the action to be performed. Capacitive touch screen won’t work with hand gloves because there won’t be any conduction between the finger(s) and the screen.

 

Touch Sensor used in this project

The touch sensor used in this project is a capacitive touch sensor module and the sensor driver is based on the driver IC TTP223. The operating voltage of the IC TTP23 is 2.0V to 5.5V and the current consumption of the touch sensor is very low. Due to the inexpensive, low current consumption, and easy to integrate support, the touch sensor with TTP223 is widely popular in its a segment.   

TTP223 Touch Sensor

 

In the above image, both sides of the sensor are shown where the pinout diagram is clearly visible. It also has a solder jumper which can be used to reconfigure the sensor in respect of the output. The jumper is A and B. Default configuration or in the default state of the solder jumper, the output changes from low to high when the sensor is being touched. However, when the jumper is set and the sensor is reconfigured, the output changes its state when the touch sensor detects the touch. The sensitivity of the touch sensor can be also configured by changing the capacitor. For the detailed info, the datasheet of the TTP 223 is very useful.

 

Below chart is showing different outputs at different jumper settings-

Jumper A Jumper B

Output Lock State

Output TTL level

Open Open

No-lock

High

Open Close

Self-lock

High

Close Open

No-Lock

Low

Close Close

Self-Lock

Low

 

For this project, the sensor will be used in default configuration which is available on factory release condition. In this project, the touch sensor will be used to control a AC light bulb using AT89S52 microcontroller.

A relay is interfaced with the 8051 microcontroller. The pinout of the relay can be seen in the below image-

KT-603 5V Relay Pinout

 

NO is normally open and NC is normally connected. L1 and L2 are the two terminals of the Relay coil. When the Voltage is not applied, the relay is turned off and the POLE gets connected with the NC pin. When the voltage is applied across the coil terminals, L1 and L2 of relay gets turned ON and the POLE gets connected with NO. Therefore, connection between POLE and NO can be switched ON or OFF by changing the operation state of the Relay.

 

Materials Required

  1. AT89S52 8051 Microcontroller
  2. Standard Cubic Relay - 5V
  3. 11.592 MHz Crystal
  4. 33pF capacitors - 2pcs
  5. 2k resistor -1 pc
  6. 4.7k resistor - 1 pc
  7. 10uF capacitor
  8. BC549B transistor
  9. TTP223 Sensor
  10. 1N4007 Diode
  11. Light Bulb With Bulb Holder
  12. A breadboard
  13. 5V power supply, A phone charger can work.
  14. Lots of jumper wires or berg wires.
  15. AT89S52 programming environment with Programmer Kit and IDE with compiler

 

Circuit Diagram

The schematic for controlling light using touch sensor and 8051 is given below image,

8051 Microcontroller Capacitive Touch Sensor Interfacing circuit diagram

 

The transistor is used to switch on or off the Relay. The touch sensor is connected with the AT89S52 microcontroller unit. The circuit is constructed using a breadboard.

Circuit Hardware for Controlling Light using Touch Sensor and 8051 MicrocontrollerTesting Light using Touch Sensor and 8051 Microcontroller

 

Programming Atmega AT89S52 Microcontroller

Complete 8051 code is given at the end. Here we are explaining few parts of the code. If you are new to 8051 microcontroller then first learn how to program a 8051 Microcontroller.

 

The below code lines are used for integrating the Relay and the Touch Sensor with 8051 Microcontroller. REGX52 is the header file for the AT89S52 microcontroller unit. A delay function is also declared.

#include<REGX52.h>

// RELAY Pin
sbit RELAY = P1^0;                // Pin P1.0 is named as RELAY

// Touch Sensor Pin
sbit Touch = P1^1;                  // Pin P1.1 is named as Touch Sensor

//Function declarations
void delay(char ms);

 

The touch and relay are initialized as 0. The touch sensor changes the logic 0 to 1. If the statement is true when the touch sensor is activated and due to this, the state of the Relay gets changed. However, to detect the touch accurately, a debounce delay is used.

// Main function
void main(void)
{

   RELAY = 0;

     Touch = 0;
   while(1){
             if (Touch == 1){
                   delay(15); // debounce delay
                   if (Touch == 1){
                         RELAY = !RELAY;   // Toggle RELAY pin
                         delay(30);
                   }                      
            }               
     }
}

 

Below, the delay function is written. The function takes input in milli-seconds format and generates delay using two for loops. This delay is not much accurate but is acceptable and it mostly depends on the clock cycle timing.

/*Delay related Function*/
void delay(char ms){
    int a,b;
    for (a=0; a<1295; a++){
            for (b=0; b<ms; b++);
    }   
}

 

This Touch controlled Light circuit is tested on the breadboard with a low power bulb connected to it. The complete sketch with a demonstration video is attached below. You can check more home automation projects here.

Code
/*  Name     : touchsensor.c
 *  Purpose  : For Circuit Digest. Touch Sensor interfacing with 8051 (AT89S52)
 *  Author   : Sourav Gupta
 *  Date     : 22-03-19
 *  Revision : None
 */
#include<REGX52.h>
 
// RELAY Pin
sbit RELAY = P1^0;    // Pin P1.0 is named as RELAY
// Touch Sensor Pin
sbit Touch = P1^1;    // Pin P1.1 is named as Touch Sensor
 
//Function declarations
void delay(char ms);
 
// Main function
void main(void)
{
   RELAY = 0;
Touch = 0;
   while(1){
if (Touch == 1){
delay(15); // debounce delay
if (Touch == 1){
  RELAY = !RELAY;   // Toggle RELAY pin
delay(30);
}  
}
}
}
 
/*Delay related Function*/
 
void delay(char ms){
int a,b;
for (a=0; a<1295; a++){
for (b=0; b<ms; b++);
}
}
Video

Have any question realated to this Article?

Ask Our Community Members