Replacing Mosfet Control code to Relay Signal

Hello, I am currently doing a project, making a select fire blaster to be specific. I have decided to use this code provided on GitHub.

The code was made to be used with two mosfets, as motor drive and motor braking. Since mosfets are confusing and hard, I decided to go with relays. Can therefore anyone here help me alter the codes to be compatible with relays? I want my relay signal out to be connected to pin D5. I am using a logic level relay so signal from the arduino should be able to get wired up directly to the relay. Any help would be appreciated. Thank you

The code was meant to control the select fire mechanism, and also rate of fire speed using a potentiometer connected to A0.

Codes and Schematics are both attached. Relay used is a SPDT 5V Relay with model number SONGLE SRD-05VDC-SL-C with optocoupler

Select Fire Rapidstrike.pdf (33.4 KB)

select-fire-rapistrike.txt (26.3 KB)

There are multiple header files that are missing. Also the text file is filled with random 's. Post the link to the GitHub page where you found the code.

I will start with this. Watch some videos on MOSFET's. They really aren't that difficult to understand.
Read about mosfets here

That aside, from a brief glance at the schematic, it's going to be quite simple to replace the mosfets with relays. That however is much less programming than it is basic electronics.

Relays are also going to introduce a delay during switching. I'm not sure how critical the timing will be.

The top braking mosfet is a P-Channel. It will turn on when the base is pulled low. That is what the gate driver is for. There is a pullup resistor that goes to the positive battery voltage that keeps the gate high (mosfet off).

The line driver has an inverter and an internal n-channel mosfet. When the input to the line driver goes LOW, the inverter sends a high signal to the internal mosfets base and turns it on. This causes the base of the brake mosfet to be pulled LOW, causing it to conduct and "brake" the motor.

This means you will probably need to invert the signal from the code you have because generally a relay will switch on with a HIGH applied to it. you could also use the line driver to invert the signal, but remember that a pull-up resistor straight to the battery may be too much voltage for the relays optocoupler.

For the bottom MOSFET, it will start conducting when a HIGH is present at the base.

Here is the link to the GitHub page : select-fire-rapidstrike-kit/v1.0 at master · Suild/select-fire-rapidstrike-kit · GitHub

the relay I got will be triggered when a LOW signal is received, also some delay isnt an issue since it is not really critical here. Any help in altering the codes would be great.

States are defined as follows

#define ON true
#define OFF false

from line 339 onward there are several functions

void turnOffAllFETs() {
  transitionHighSideFET(OFF);
  transitionLowSideFET(OFF);  
}

void setFETsForBraking() {
  transitionHighSideFET(ON);
  transitionLowSideFET(OFF);  
}

void setFETsForOn() {
  transitionHighSideFET(OFF);
  transitionLowSideFET(ON);
}

// Abstracts away digitalWrites, helps make code more readable
void transitionHighSideFET(bool toTurnOn) {
  // transitioning high side FET can be confusing since the digitalWrite() must
  // be inverted
  digitalWrite(HIGH_SIDE_PIN, !toTurnOn);
}

// Abstracts away digitalWrites, helps make code more readable
void transitionLowSideFET(bool toTurnOn) {
  digitalWrite(LOW_SIDE_PIN, toTurnOn);
}

you can either change the function call arguments (OFF, ON) or use an ! in front of toTurnOn

For example you could change these if you need to invert an output but it might get confusing because the ON could mean OFF if your circuit is different

transitionHighSideFET(OFF); -> transitionHighSideFET(ON); 
transitionLowSideFET(ON); -> transitionHighSideFET(OFF);

Or you could do it the right way: (Note the exclamation marks)

digitalWrite(HIGH_SIDE_PIN, !toTurnOn); -> digitalWrite(HIGH_SIDE_PIN, toTurnOn);
digitalWrite(LOW_SIDE_PIN, toTurnOn); -> digitalWrite(LOW_SIDE_PIN, !toTurnOn);

Changing the ON and OFF arguments is the easy way, just make sure that both relay's don't turn on at the same time. That would not end well for your circuit. You should test everything before applying power to avoid shorts.

If you still don't get it, tell me what state of the Arduino pins will cause the relays to be on and I will change the code for you. I cant tell because your schematic has MOSFETs. (low = on, or high = on)

I still think relays are going to cause problems, if they short the battery because they don't open in time then they may get stuck from too much current and possibly cause a fire. Make sure you use a fuse.