Switch a relay on and off in 100ms when I hit a button

Hi all,
I am brand spanking new to Arduino and need some help.
I'm a mechanic and am great with basic electrical but this black magic is outside of anything I've looked at before.
I'm not catching on no matter how much I read others input.

I have an Arduino Nano V3.0 and an Arduino-brand single relay board.

I want to run 12dc through the NC side of the relay.
At the touch of a button that will go to ground, I want the relay to energise for 100ms, cutting the NC side of the relay.

The reason for this is that I am making a quick shifter for a motorcycle (Old carbureted '80's Suzuki).
I have made a shift rod that compresses and makes an earth. I want to then cut ignition for an adjustable amount of time so that the next gear can be selected without using the clutch or backing off the throttle.

It would also be an advantage if the process cannot be repeated for a second or so. Only because once the ignition cuts and the gear is selected, it is possible that the switch can be made again very quickly which would cut ignition again when I want it to be making power.

I apologise for bringing nothing to the table but like I said, I am very very new to this.

Thanks for reading.
Ben

Ok, so you get yourself one of these
https://www.ebay.com/itm/2PCS-1-Channel-DC-5V-Relay-Switch-Board-Module-for-Arduino-Raspberry-Pi-ARM-AVR/322465448278?hash=item4b14704156:g:dzgAAOSwSlBY2bTH
Connect +5, Gnd, and a signal from the Arduino. They are typically low to energize the relay.
On your Arduino, connect to switch that will ground a pin. I used 2 & 3 here:

byte switchPin = 2;
byte relayControl = 3;
void setup(){
pinMode (switchPin, INPUT_PULLUP); // input pin with pullup resistor enabled
pinMode (relayControl, OUTPUT);
digitalWrite (relayControl, HIGH); // turns off relay at power up
}
void loop(){
   if (digitalRead(switchPin) == LOW){ // switch pressed?
   digitalWrite (relayControl, LOW); // energize relay
   delay(100); // dumb delay, do nothing but energize relay for 100mS
   digitalWrite (relayControl, HIGH); // de-energize relay
   delay(900); // dumb delay, ignore any switch presses for balance of 1 second
   }
}

Welcome :slight_smile:

This Sketch is simple but has several things to consider. let this be your starting point and hopefully everyone will jump in and help you tidy it up :slight_smile:

#define ButtonPin 2
#define RelayPin 13

void setup() {
  pinMode(ButtonPin,INPUT_PULLUP);
  pinMode(RelayPin,OUTPUT);
 }

void loop() {
  // put your main code here, to run repeatedly:
  while(digitalRead(ButtonPin) == HIGH){
    //wait forever unless button pin is low
  }

  digitalWrite(RelayPin,HIGH); // energize your relay
  delay(100); // this is the delay you requested it should act an excellent debounce delay also
  digitalWrite(RelayPin,LOW); // release your relay

  while(digitalRead(ButtonPin) == LOW){
    //wait forever unless button pin is high 
    // we need to wait tile your button is released before starting over
  }
// Start the loop over
}

Z

PS @CrossRoads you beat me to it :stuck_out_tongue: lol

Cheers,
I know it's not the done thing to rock up and ask someone to do all the hard work for you so I really appreciate you both helping me.

I'll load them up and see how I go.

Can you suggest any good YouTube channels, books, blogs etc. where I can start to learn the programming?

Kind regards

No suggestions on learning programming, outside of playing with the examples included in the IDE. I'm not really a programmer, got all my experience with BASIC in high school & then lower & higher level languages (assembly, FORTRAN, PASCAL, and others I've forgotten) in college, and then hacking around with the examples here. Tried reading a C++ book once, eyes just glazed over after the first page, it assumed the reader know so much background stuff about C or C++ already, and I didn't. Just enough to be dangerous, and smart enough to manipulate the hardware as needed and make the software do the logic paths I want.

CrossRoads,
That worked a treat except-
My relay ( https://www.auselectronicsdirect.com.au/assets/files/(ST1080)5V%20Relay%20module.pdf )
may be faulty.
During a bench test, I ran it off the ICSP pins and it operated flawlessly. Then after a few minutes, the relay would de-energise and re-energise un-commanded. I thought it may be too much current draw for the ICSP pins.
I swapped it to to run off 12V+ that was running the board via the VIN pin (30) and then I realised the relay was only 5V rated so I swapped it to Pin 27 and got the same result.
I blew air over the relay to cool it which solved the problem but this is not practical as it will be on a motorbike in South East Queensland, Australia where we see warm weather for 8 months of the year.

So I wondered if I can run the ignition through the NC side of the relay and cut power to it by energising the relay? This means the relay is only powered for 0.10sec every 3 secs or so when I am using the quick shifter so it doesn't over heat.
After looking at the code you provided- my thought is to swap the LOW and HIGH values on the RelayControl DigitalWrite commands.

Would this give me the result I need?

Regards and thanks again for your help.

Not very skilled on combustion engines and ignition circuits but do you really need a relay or could you perhaps use a solid state solution like a MOSFET instead? Since it is a motorcycle I would assume it is ~12V (probably never more than 15VDC) which works very well with MOSFET transistors. Driven properly they have almost no losses meaning no waste heat to dispose of.

Hi,

I think we need a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

@kamelryttarn,
You're on the money with the voltage.
I apply a very mechanical approach to electrics and most electronics escape me. If a MOSFET is the better option then I'm happy to look at that.

@TomGeorge,
I would happily do this (a sketch of what I need) however CrossRoads can see what I'm doing.
The only alteration I need in the code is to change from-

  1. an energised relay that de-energises for 100ms to;
  2. A de-energised relay that energises for 100ms.

Cheers

Boogieman1200:
@kamelryttarn,
You're on the money with the voltage.
I apply a very mechanical approach to electrics and most electronics escape me. If a MOSFET is the better option then I'm happy to look at.

Cheers

A MOSFET can in some circuits work similar to a relay but is not the same thing. In a 12V car or motorcycle system you can use a NPN MOSFET to "disconnect and connect" the ground connection to, for instance a light bulb. The light bulb (or whatever you desire) is constantly connected to your positive side of the electrical system and when you apply a voltage (preferably 10-15V) to the gaste of the MOSFET it starts to conduct turning the bulb on.

So...
I googled it and I can see they have 4 terminals-
Body (?)
Source (Source voltage)
Gate (Switching power)
Drain (The load (Ignition system)).

It doesnt seem as simple as that from what I have read.

The other question I have but you may have answered earlier- are they happy to be constantly powered?
The reason I went for a DPST relay is so that power can flow through the NC side constantly and when I want to use the quick shifter, the relay is energised to make the NO circuit (which hasn't got anything on it), de-energising the NC circuit. This to me is efficient and doesn't wear out the relay.

MOSFETs are even happier than a relay to be constantly powered, because they don't consume any more power whether energized or not. A relay wastes a lot of power in the coil when it's on.

A MOSFET can stay on for an indefinite period of time. As long as it is "fully open" the resistance over the transistor is almost zero Ohm so there is basically no heat losses at all.

I can't make any guarantees regarding the body, but there are 3 connections to a N-channel MOSFET (the correct term is N-channel MOSFET but I incorrectly wrote NPN previously).

Source, Gate and Drain. The Gate is the pin you use to control the transistor. The Source is usually connected to the ground and the Drain is the pin you connect to the negative side of your load (light bulb or whatever you need to switch on and off).

When there is no voltage present at the Gate pin, nothing will happen. The transistor will not conduct any electricity. When you apply about 10V positive voltage to the Gate the MOSFET will conduct and turn the light on. It is important not to apply more than about 10-15V to the Gate measured from Source or the MOSFET could break but in your application that probably won't be an issue since you don't have any higher voltages present apart from the ignition/spark voltages.

If you drive the MOSFET directly from your arduino, you can not conduct very much current since the MOSFET will not open fully if driven by 5V at the Gate pin but depending on your application it may or may not be an issue. If the MOSFET gets hot you have two options. Either you cool it or you use a gate driver that will supply 10V to the gate making the MOSFET perform to it's full potential.

You can't talk about "voltage at the gate" alone. It's the voltage between the gate and source.

aarg:
You can't talk about "voltage at the gate" alone. It's the voltage between the gate and source.

That is correct. That is also why I wrote "It is important not to apply more than about 10-15V to the Gate measured from Source"

Boogieman1200:
Can you suggest any good YouTube channels, books, blogs etc. where I can start to learn the programming?

You may find the Programming Electronics Academy series useful.

"The other question I have but you may have answered earlier- are they happy to be constantly powered?"
Not really.

"The reason I went for a DPST relay is so that power can flow through the NC side constantly and when I want to use the quick shifter, the relay is energised to make the NO circuit (which hasn't got anything on it), de-energising the NC circuit. This to me is efficient and doesn't wear out the relay."
That's the approach I would take also.

Excellent,
So how do I make the code switch the relay on for 100ms rather than off for 100ms?
Do I swap the high and low values on the digitalwrite relaycontrol or am I misunderstanding that function.

I stuffed my relay last night so I'll get another today and start messing around with that.

Cheers
Ben

These three lines should do that already.

   digitalWrite (relayControl, LOW); // energize relay
   delay(100); // dumb delay, do nothing but energize relay for 100mS
   digitalWrite (relayControl, HIGH); // de-energize relay

If the relay is energizing with a High vs a LOW, than change HIGH to LOW and LOW to HIGH.

Beautiful.
I'm on it. Thanks for your help CrossRoads.

If you're not into bikes this project probably doesn't sound very exciting but to a rev head, this is gold.

Cheers all
Ben