How can I make Fade LED using Tpp223 Touch Sensor and Arduino?

Hello everyone, I want the LED to turn off slowly with the code below. I would like to thank in advance the person or people who will help me with this issue!.

" const int TTP223 = 7;

const int led = 11;

void setup() {

pinMode(led, OUTPUT);

pinMode(TTP223, INPUT);

}

void loop() {

int sensorState = digitalRead(TTP223);

if (sensorState == HIGH)

digitalWrite(led, HIGH);

else

digitalWrite(led,LOW);

}"
connecting-TP233-touch-sensor-with-Arduino

welcome to the forum.

Please read How to get the best out of this forum and post accordingly

➜ please edit your post and add code tags and necessary documentation for your ask (and stuff like your exact circuit and power supply, links to components etc when relevant).

You have the LED attached to Pin 11. This is a digital I/O pin. You need to use a PWM pin. The PWM pins for an Uno or Nano are 3, 5, 6, and 9. Follow this example to fade your LED slowly...

https://docs.arduino.cc/built-in-examples/basics/Fade/

Try something like this... (I moved the LED pin)

// https://forum.arduino.cc/t/how-can-i-make-fade-led-using-tpp223-touch-sensor-and-arduino/1261218
// https://docs.arduino.cc/built-in-examples/basics/Fade/

const int TTP223 = 7;
const int led = 9;
const int fadeAmount = 5;  // how many points to fade the LED by
int brightness = 255;  // how bright the LED is


void setup() {
  pinMode(led, OUTPUT);
  pinMode(TTP223, INPUT);
}

void loop() {
  int sensorState = digitalRead(TTP223);
  if (sensorState == HIGH) {
    digitalWrite(led, HIGH);
    fade();
  }
  else
    digitalWrite(led, LOW);
}

void fade() {
  for (int i = 0; i < 255; i++) {
    analogWrite(led, 255 - brightness);
    brightness = brightness - fadeAmount;
  }
  delay(250); // this slows the fade time
}

for WOKWI.COM

diagram.json
{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": 0, "left": 0, "attrs": {} },
    {
      "type": "wokwi-pushbutton",
      "id": "btn1",
      "top": -51.4,
      "left": 134.4,
      "attrs": { "color": "green" }
    },
    {
      "type": "wokwi-resistor",
      "id": "r1",
      "top": -24.85,
      "left": 67.2,
      "attrs": { "value": "10000" }
    },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": -70.8,
      "left": 33,
      "attrs": { "color": "red", "flip": "1" }
    }
  ],
  "connections": [
    [ "nano:7", "r1:1", "green", [ "v0" ] ],
    [ "nano:5V", "btn1:1.r", "red", [ "v14.4", "h85.9", "v-115.2", "h-9.8" ] ],
    [ "nano:GND.2", "r1:2", "black", [ "v0" ] ],
    [ "r1:2", "btn1:2.l", "green", [ "v0" ] ],
    [ "nano:GND.2", "led1:C", "black", [ "v0" ] ],
    [ "nano:9", "led1:A", "green", [ "v0" ] ]
  ],
  "dependencies": {}
}

Hello, I am grateful to you! With these codes, delayed extinction finally occurred. However, the "FadeOut" Event did not occur. Can you give an idea about this issue?
With my love and respect...

Can you explain this? What did you observe? What do you intend for the result?

What I mean is that when I took my hand off the sensor, the LED turned off, but "Fadeout" did not occur!

Try adjusting the "fadeAmount" or the "delay(250);" As this is written, it takes only slightly less time to fade as it is to make ON to OFF.

:nose: :+1: :+1:

Show your work. It will be interesting to know what you find to make your project work.

... also the "delay(250)" is probably slowing the fade too much. Try "delay(25)"...

Yes, this is the final edit and what I wanted came true. Of course, with your contributions...

const int TTP223 = 7;
const int led = 9;
const int fadeAmount = 1; // how many points to fade the LED by
int brightness = 255; // how bright the LED is

void setup() {
pinMode(led, OUTPUT);
pinMode(TTP223, INPUT);
}

void loop() {
int sensorState = digitalRead(TTP223);
if (sensorState == HIGH) {
digitalWrite(led, HIGH);
fade();
}
else
digitalWrite(led, LOW);
}

void fade() {
// Gradually Increase Duty Cycle
for(int i=0; i<255; i++)
{
analogWrite(led, i);
delay(5);
}
// Gradually Decrease Duty Cycle
for(int i=255; i>0; i--)
{
analogWrite(led, i);
delay(5);
}
}

Nice.

you failed to use code tags throughout these posts.

see post #2 and please do your part to help us maintain the forum's look & feel and have readable code.

➜ there is a little pencil below your posts, click on it to edit the post and add code tags and then save please...