VW Button Press, Cycle LED - A1(switch), D12(led)

Evening All,

Trying to work to get my nano working with a project I have to press a button on my nano, then have a relay send a signal to open an exhaust valve, then if I press the button again, it should send a signal to close the relay/valve.

… First I’m trying to crack getting the button to function when I press it. I think its a PWM? style button, its a soft press, not ON/OFF hard press switch.

When I hold down the button, it sends the 5V to D15(A1), and turns off the led. But when I let go, it slowly lights back up again.

How do I program the button so when pressed, it issues a command (LED OFF), then when I press, it issues another command (LED ON).

NANO VW


GND(29) 9
D12 (15) 11
D15 (20) 15
+5V (27) 18

/* Basic Digital Read
 * ------------------ 
 *
 * turns on and off a light emitting diode(LED) connected to digital  
 * pin 13, when pressing a pushbutton attached to pin 7. It illustrates the
 * concept of Active-Low, which consists in connecting buttons using a
 * 1K to 10K pull-up resistor.
 *
 * Created 1 December 2005
 * copyleft 2005 DojoDave <http://www.0j0.org>
 * http://arduino.berlios.de
 *
 */

int ledPin = 12; // choose the pin for the LED
int inPin = 15;   // choose the input pin (for a pushbutton)
int val = 0;     // variable for reading the pin status

void setup() {
  pinMode(ledPin, OUTPUT);  // declare LED as output
  pinMode(inPin, INPUT);    // declare pushbutton as input
}

void loop(){
  val = digitalRead(inPin);  // read input value
  if (val == HIGH) {         // check if the input is HIGH (button released)
    digitalWrite(ledPin, LOW);  // turn LED OFF
  } else {
    digitalWrite(ledPin, HIGH);  // turn LED ON
  }
}

Big Thanks again for all of your help!!!

Posting a schematic would help a lot, not a frizzy thing. The pictures are nice but do not tell me anything. Also include links to each hardware device showing technical information. Explain what a PWM style button is.

https://dalys.lt/en/autoparts/vw/tiguan-5n-2-0-tsi-4motion-155-kw/switches-panel/2324509

Above is all I have I’m afraid, with some better images of the switches, I cant pull up the VW diagram for this, but I can tell you that it connects into a canbus management controller… which isnt required for this programming test.

It was my understanding that a PWM style button/switch, is a switch which when pressed can be on or off, a ‘soft’ switch instead of a toggle ‘up for on, down for off’… Perhaps I’m mistaken in my terminology.

Thanks for your support, sounds as if I’ll need to trawl a little deeper for wiring diagrams for the inner components of the switch if my MS paint drawing doesn’t cut it :upside_down_face:

Finally : a look at the internal pins/components of the switch, in the hopes it might also help.

Sorry it is not much. There are switches etc, that need to be defined. Try to download KiCad, it is a great CAD program for schematic capture and is good all the way through making PC boards. It has a steep learning curve but it is great. I have used it for several years and every time I do a design it teaches me something new. If you plan on staying with electronics it will become invaluable. There are others out there but this is the best one for me.

Thanks, I think that’s a great starting point and will certainly help me in future.

I’ve actually found this code to work - when I hold the button down it ensures the led flashes.

So I’m as good as done, I’ll just butched the code to make it do what I want.

#include <stdio.h>
#include <stdlib.h>

const int button = 15;            // GPIO 15 for the button
const int led =12;                // GPIO 12 for the LED
int ledflag=0;                   // LED status flag

void setup() {
  pinMode(button,INPUT);         // define button as an input 
  pinMode(led,OUTPUT);           // define LED as an output
  digitalWrite(led,HIGH);         // turn output off just in case
}

void loop() {
  if (digitalRead(button)==LOW){ // if button is pressed
    if (ledflag==0) {             // and the status flag is LOW
      ledflag=1;                  // make status flag HIGH
      digitalWrite(led,LOW);     // and turn on the LED
      }                           // 
    else {                        // otherwise...
      ledflag=0;                  // make status flag LOW
      digitalWrite(led,HIGH);      // and turn off the LED
    }
  delay(1000);                    // wait a sec for the 
  }                               // hardware to stabilize
}                                 // begin again