Push button relay

Hello ,

I am trying to connect a relay to an Arduino Nano wich is activated for 1 second from a magnetic contact and turns itself off even the magnetic contact (limit switch or push button) stays in NC position. I mean the command from push will be on for one second then off. The push will do nothing on NO then the same routine on NC ( on then off). I would apreciate any help .

Thank You

Welcome to the forum

You don't say what you have tried so far, if anything

It sounds like you need to detect when the contact becomes closed rather than when it is closed

Take a look at the StateChangeDetection example in the IDE

Hello,

I have tried the example with push button and relay and then I added in the loop a delay and " relay off" but if the button is pushed continously it is blocking the loop as expected.

Which input pin is "contact" connected to and how is it wired? Which output pin is relay connected to and how is it wired? Is the relay activated when the output is HIGH or LOW?

Post your best effort

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Pin 2 is push button and 13 relay. When push button is High then relay must be On for 1 second then off with the button pushed continously. It is for a dc motor wich is closing a door silently . The dc motor need only one pulse for closing. The contact stays on when the door is closed and do nothing when the door is open.

There are lines in the code that need changes.

Which input pin is "contact" connected to and how is it wired? Which output pin is relay connected to and how is it wired? Is the relay activated when the output is HIGH or LOW?

If you write in your language and use translate.google.com to have it translated to English, your idea might be clearer to those reading.

This is a basic outline of how I see your sketch... something to start with and pick apart...

byte buttonPin = 2; // tied low with a pulldown resistor
byte relayPin = 13; // connected to signal pin of relay

// relay N.O. = DC motor voltage
// relay COM = DC motor+
// relay N.C. = nothing

void setup() {
  pinMode(buttonPin, INPUT); // using pull-down resistor, HIGH when pressed 
  pinMode(relayPin, OUTPUT); // signal pin to activate relay
}

void loop() {
  if (digitalRead(buttonPin)) { // if the button is pressed...
    digitalWrite(relayPin, HIGH); // ... turn the relay on...
    delay(1000); // ... leave relay on for one second
  }
  else // when button is NOT pressed (buttonPin is LOW)
    digitalWrite(relayPin, LOW); // turn relay off
}
Click here for diagram.json to simulate it on wokwi.com
{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": 0, "left": 0, "attrs": {} },
    { "type": "wokwi-relay-module", "id": "relay1", "top": 67.4, "left": 172.8, "attrs": {} },
    {
      "type": "wokwi-pushbutton",
      "id": "btn1",
      "top": -92.6,
      "left": 192.2,
      "rotate": 180,
      "attrs": { "color": "green" }
    },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": -70.8,
      "left": 282.2,
      "attrs": { "color": "red" }
    },
    { "type": "wokwi-vcc", "id": "vcc1", "top": -66.44, "left": 316.8, "attrs": {} },
    {
      "type": "wokwi-resistor",
      "id": "r1",
      "top": -44.05,
      "left": 96,
      "rotate": 270,
      "attrs": { "value": "10000" }
    }
  ],
  "connections": [
    [ "nano:2", "btn1:2.r", "green", [ "v0" ] ],
    [ "relay1:VCC", "nano:5V", "red", [ "h0" ] ],
    [ "relay1:GND", "nano:GND.1", "black", [ "h0" ] ],
    [ "relay1:IN", "nano:13", "green", [ "h0" ] ],
    [ "nano:GND.2", "led1:C", "black", [ "v-14.4", "h181.9" ] ],
    [ "vcc1:VCC", "relay1:NO", "red", [ "v0" ] ],
    [ "relay1:COM", "led1:A", "green", [ "h0" ] ],
    [ "nano:GND.2", "r1:1", "black", [ "v0" ] ],
    [ "r1:2", "btn1:2.r", "green", [ "v0" ] ],
    [ "nano:5V", "btn1:1.r", "red", [ "v14.4", "h57.1", "v-124.8" ] ]
  ],
  "dependencies": {}
}

Thanks , it is simpler than I expected .The only issue is after I release the pushed button it is activating the relay again . How can I correct this one ?

Where is the code???...

Wild guess, based on the copious information provided: INPUT_PULLUP.

Out on delivery, maybe by a pigeon, I guess.

.:grin:.

Hello,

I seems like OneShot-library is what I need but it doesn't compile to the board. I tried with both Arduino Nano and Uno .

You don't need a library for that, just a basic timer. A simple delay() would work for this purpose, if the board has nothing else to do. Otherwise use a millis() based timer as shown in the "blink without delay" example.

I found this topic " Trying to create a one shot pulse" to be exactly what I needed but the only problem is that when the button is released it transmits the impulse again and I don't need that.

we can't see the code you talking about to explain the behaviour you are observing or suggest ways to fix it.

It may be a matter of "state change detection" so you only get a pulse when the button is pressed, not because it remains pressed or briefly looks like it was pressed again when you lift off, contact bounce.

a7

The code is this one :

<
const int pulseInputPin = 3;
const int ledPin = LED_BUILTIN;

unsigned long previousMillis;
const unsigned long interval = 1000;
bool enabled = false;
volatile bool pulseDetected = false;

void setup()
{
pinMode( pulseInputPin, INPUT_PULLUP);
pinMode( ledPin, OUTPUT);

attachInterrupt( digitalPinToInterrupt( pulseInputPin), triggerISP, FALLING);
}

void loop()
{
unsigned long currentMillis = millis();

if( pulseDetected)
{
digitalWrite( ledPin, HIGH); // turn on led
previousMillis = currentMillis;
enabled = true; // turn on software timer
pulseDetected = false; // reset this variable
}

if( enabled) // software timer is active ?
{
if( currentMillis - previousMillis >= interval)
{
digitalWrite( ledPin, LOW); // turn off led
enabled = false; // stop software timer
}
}
}

void triggerISP()
{
pulseDetected = true;
}
/>

You really haven't got the hang of using code tags, have you ?

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum