Arduino Code with H Bridges and Bilge Pumps

Control Boards, Controllers, Tethers, Ect.
Thumquas
Posts: 11
Joined: Sep 2nd, 2022, 12:40 am

Arduino Code with H Bridges and Bilge Pumps

Post by Thumquas »

Hello all. The hope is to eventually have a basic'ish Arduino code for New ROV builders to use and base there projects off of.
I've searched the web and only found Sketches for ESC brush-less drivers and none for H Bridges and bilge pumps.
I know the advantages of ESC's and brush-less motors but many want to start with bilge pumps, plus they don't fair well in salt water for long and bilge pumps are readily available where I live (remote marine area <>< ) and easy to change over when they eventually corrode over.

This way somebody starting out in ROV's can easily set up a 100' Cat5e or Cat6 cable to an Arduino board and have a good starting point with bilge pumps.
I am in the process of learning and coding for a 2 H Bridge and 4 bilge pump ROV. 2 for Forward/Back/Turn, 1 for Up/Down and 1 for Strafe Left/Right and hoping that I can get some help with this project that could possibly help many others finish there project and to start a new underwater exploration hobby.

So far I have a joystick module controlling forward/reverse/turn and trying to code in a second joystick module to control up/down and strafe.
I have some power leaks to the other motors, I think its the new coder I have working on this "ME" :roll: , and will soon be adding in joystick button press headlight function.
Threw my many trials so far I've found its best to separate your video feed from the motor noise and run a separate cat5e or Cat6 cable for Video. Grab yourself a USB2.0 auto focus Web Cam and a USB 2.0 Extender to RJ45 Over Cat5e or Cat6 with power connection on the camera side, this way you get a really clear and fast video feed to your laptop or whatever you hook the Video USB too.

Other functions for later would be "Low Voltage LED", "Leak LED" (prob hooked up to a small float on an upside-down trigger switch), Headlight state LED and Servo function for a claw and/or camera position.
Eventually having a Controller Arduino and an ROV Arduino "Master/Slave" set up for longer/cleaner 3 wire signal threw the tether and ability to have more functions would be on the table. But keeping things simple will be the main focus.

If anyone has interest in this project please let me know and if you can help fix/add to the sketch or if you are looking for a finished sketch for your ROV project.
Hopefully with interest will come a complete product. I'm happy to share the Arduino code I have so far, but remember I'm new to this and there isn't a proper working product yet.


Here's a link to the very basic 4 thruster Arduino code https://drive.google.com/file/d/1fN9McA ... sp=sharing
Last edited by Thumquas on Sep 6th, 2022, 7:22 pm, edited 1 time in total.
Thumquas
Posts: 11
Joined: Sep 2nd, 2022, 12:40 am

Re: Arduino Code with H Bridges and Bilge Pumps

Post by Thumquas »

Here's the First edition of the code, I'm waiting for my other joystick module to show up, as my 2nd one doesn't seem to function properly, to test everything so far.
If anyone sees a problem or has any suggestions please let me know. I believe I'm going to change the joystick press function to a simple High/Low single pin function for a relay, But best to have 2 working joysticks to test what I have first.

Parts List so far for ROV control:
1 - Arduino Mega (I'm sure a Uno would be just fine, just change the pin numbers to work for your board)
2 - L298N Dual H-Bridge for Arduino
4 - Bilge pumps 1100gph
2 - Dual-axis Joystick Modules for Arduino
1 - LM2596 DC to DC Voltage Regulator 4-40V to 1.5-35V (for powering board)
1 - DC single pole switch (for turning power to Board and H-Bridges on/off so you don't fry your PC when connecting the USB to upload)
lots - Breadboard Jumper Wires Ribbon Cables, Male to Female, Male to Male, Female to Female
1- 12v Battery (I like the ones for alarm systems, small and simple)
1 - pre-wired inline Blade Fuse and fuse ( safety first)
A couple - LED's and 330 resistors (for testing and resistors so you don't burn out the leds)
2 - DC 5V Relay Module for Arduino
4 - RC boat props (2 right 2 Left)
4 - 3.17mm to 4mm Brass Shaft Coupling Joint Connector
4 - 4mm bolts to cut and shape to connect props to bilge pumps
1 - 100' Cat5e or Cat6
2 - RJ45 couplers
1 - short Cat5e or Cat6 cable (to cut in half to solder pins onto for connections)
......Hopefully that's everything......

*********Remember this is a work in progress and I'm still learning********* (All tested and working so far with my 100' Cat5e cable setup)

/*
Arduino code CROC from https://www.instructables.com/Build-You ... h/#discuss was referenced and used. Big thank you to them for making this avalible

Feel free to reuse/share/improve this code.


Parts:
DC Motor Vertical axis Up/Down (MotorV)
DC Motor Strafe axis Left/Right (MotorS)
DC Motor Horizontal plane Right (MotorHR)
DC Motor Horizontal plane Left (MotorHL)
Light A (Light1)
Light B (Light2)

Inputs:
JoystickA X axis : analogic
JoystickA Y axis : analogic
JoystickA Z axis : analogic (button Press) (usually digital)
JoystickB X axis : analogic
JoystickB Y axis : analogic
JoystickB Z axis : analogic (button press) (usually digital)

Outputs:
Light A : digital
Light B : digital
MotorHR in1 : PWM
MotorHR in2 : PWM
MotorHR enable : PWM (usually digital)
MotorHL in1 : PWM
MotorHL in2 : PWM
MotorHL enable : PWM (usually digital)
MotorV in1 : PWM
MotorV in2 : PWM
MotorV enable : PWM (usually digital)
MotorS in1 : PWM
MotorS in2 : PWM
MotorS enable : PWM
*/

#include <VirtualWire.h>

double HL(int X, int Y)
{
int x=max(X,-511);
x=min(x,511);
int y=max(Y,-511);
y=min(y,511);

double res=0;
if (x+y>0)
res=min(x+y,512);
else
res=max(x+y,-512);
return res/512;
}

double HR(int X, int Y)
{
int x=max(X,-511);
x=min(x,511);
int y=max(Y,-511);
y=min(y,511);

double res=0;
if (y-x>0)
res=min(y-x,512);
else
res=max(y-x,-512);
return res/512;
}


// Inputs = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = #

// Joysticks
int pinJoyAX = A14; // analog
int pinJoyAY = A13; // analog
int pinJoyAZ = A15; // analog
int pinJoyBX = A11; // analog
int pinJoyBY = A12; // analog
int pinJoyBZ = A10; // analog

// Motors
// Horizontal Right
int pinMotHR1 = 2; // PWM
int pinMotHR2 = 3; // PWM
int pinMotHREn = 4; // PWM

// Vertical
int pinMotV1 = 5; // PWM
int pinMotV2 = 6; // PWM
int pinMotVEn = 7; // PWM

// Strafe
int pinMotS1 = 8; // PWM
int pinMotS2 = 9; // PWM
int pinMotSEn = 10; // PWM

// Horizontal Left
int pinMotHL1 = 11; // PWM
int pinMotHL2 = 12; // PWM
int pinMotHLEn = 13; // PWM

// Lights
int pinLightA1 = 23; // digital
int pinLightB1 = 22; // digital


// VARIABLES
int valJoyAX = 0;
int valJoyAY = 0;
int valJoyAZ = 0;
int valJoyBX = 0;
int valJoyBY = 0;
int valJoyBZ = 0;

double valHL = 0;
double valHR = 0;
int valJoyAZPrev = 0;
int valJoyBZPrev = 0;
int calJoyAX = 0;
int calJoyAY = 0;
int calJoyBX = 0;
int calJoyBY = 0;

int lightState = 0;



void setup()
{

calJoyAX = analogRead(pinJoyAX);
calJoyAY = analogRead(pinJoyAY);
calJoyBX = analogRead(pinJoyBX);
calJoyBY = analogRead(pinJoyBY);

pinMode(pinJoyAZ, INPUT_PULLUP);
pinMode(pinLightA1, OUTPUT);
pinMode(pinJoyBZ, INPUT_PULLUP);
pinMode(pinLightB1, OUTPUT);

Serial.begin(9600);
}


void loop()
{

// READ INPUTS = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = #


valJoyAX = analogRead(pinJoyAX);
valJoyAY = analogRead(pinJoyAY);
valJoyAZPrev = valJoyAZ;
valJoyAZ = digitalRead(pinJoyAZ);
valJoyBX = analogRead(pinJoyBX);
valJoyBY = analogRead(pinJoyBY);
valJoyBZPrev = valJoyBZ;
valJoyBZ = digitalRead(pinJoyBZ);



// DEBUG = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = #

/*
Serial.println("-------------");
Serial.print("Joystick A : ");
Serial.print(valJoyAX);
Serial.print(", ");
Serial.print(valJoyAY);
Serial.print(", ");
Serial.println(valJoyAZ);

Serial.print("Joystick B : ");
Serial.print(valJoyBY);
Serial.print(", ");
Serial.print(valJoyBX);
Serial.print(", ");
Serial.println(valJoyBZ);

*/



// LIGHTS = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = #


if(valJoyAZ==0 && valJoyAZPrev==1 && lightState==0)
{
digitalWrite(pinLightA1,HIGH);


lightState = 1;
}
else
{
if(valJoyAZ==0 && valJoyAZPrev==1 && lightState== 1)
{
digitalWrite(pinLightA1,LOW);

lightState = 0;
}
}


if(valJoyBZ==0 && valJoyBZPrev==1 && lightState==0)
{
digitalWrite(pinLightB1,HIGH);


lightState = 1;
}
else
{
if(valJoyBZ==0 && valJoyBZPrev==1 && lightState== 1)
{
digitalWrite(pinLightB1,LOW);

lightState = 0;
}
}

// MOTORS = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = #


// Manette inversee

//valHL = HL(valJoyAY-calJoyAY, valJoyAX-calJoyAX);
//valHR = HR(valJoyAY-calJoyAY, valJoyAX-calJoyAX);
valHL = HL(calJoyAY-valJoyAY, calJoyAX-valJoyAX);
valHR = HR(calJoyAY-valJoyAY, calJoyAX-valJoyAX);

// Motor HL = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = #

if(valHL>0)
{
analogWrite(pinMotHL1,255);
analogWrite(pinMotHL2,0);
analogWrite(pinMotHLEn,255*valHL);
}
else
{
analogWrite(pinMotHL1,0);
analogWrite(pinMotHL2,255);
analogWrite(pinMotHLEn,-255*valHL);
}



// Motor HR = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = #

if(valHR>0)
{
analogWrite(pinMotHR1,255);
analogWrite(pinMotHR2,0);
analogWrite(pinMotHREn,255*valHR);
}
else
{
analogWrite(pinMotHR1,0);
analogWrite(pinMotHR2,255);
analogWrite(pinMotHREn,-255*valHR);
}

// Motor Vertical = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = #

if(valJoyBY - calJoyBY > 10)
{
analogWrite(pinMotV1,255);
analogWrite(pinMotV2,0);
analogWrite(pinMotVEn,min(valJoyBY-calJoyBY,255));
}
else
{
if(valJoyBY - calJoyBY < -10)
{
analogWrite(pinMotV1,0);
analogWrite(pinMotV2,255);
analogWrite(pinMotVEn,min(calJoyBY-valJoyBY,255));
}
else // switch off the vertical motor
{
analogWrite(pinMotV1,0);
analogWrite(pinMotV2,0);
analogWrite(pinMotVEn,0);
}
}

// Motor Strafe = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = #

if(valJoyBX - calJoyBX > 10)
{
analogWrite(pinMotS1,255);
analogWrite(pinMotS2,0);
analogWrite(pinMotSEn,min(valJoyBX-calJoyBX,255));
}
else
{
if(valJoyBX - calJoyBX < -10)
{
analogWrite(pinMotS1,0);
analogWrite(pinMotS2,255);
analogWrite(pinMotSEn,min(calJoyBX-valJoyBX,255));
}
else // switch off the Strafe motor
{
analogWrite(pinMotS1,0);
analogWrite(pinMotS2,0);
analogWrite(pinMotSEn,0);
}
}

delay(200);
}
Last edited by Thumquas on Sep 6th, 2022, 6:49 pm, edited 4 times in total.
sgarbo
Posts: 2
Joined: Sep 4th, 2022, 6:55 pm

Re: Arduino Code with H Bridges and Bilge Pumps

Post by sgarbo »

I have been working on a similar project since the winter, I had the "winter bores" and I stumbled on these ROV projects. I thought it would be a cool project to create. This project went far from the simple idea of a few relays and some dc motors and became a long drawn out project. My Rov is not yet ready for the water as I continue to complete the electronics. The direction of this project went from dc motors + relays to the Arduino, brushless motors and PS2 controller idea to where it is now using two Arduinos to talked to each other, simple analog controllers top side and bilge pumps with 43amp motor controllers. To give a brief explanation.

I have taken a long time with this project since there are many things to consider and deciding which one is the best approach for me. If my design fails at least it will look cool on my shelf

The long explanation of my project is as follows. I have an Arduino Uno topside with a five axis joystick for left/right and a mini thumb joystick for up and down. The Arduino will communicate via serial over cat 5 to the Arduino bottom side. The Arduino topside will be powered with a 12v gel-sel and the bottom side will be powered using my 40v Ryobi electric lawn mower battery using a 36/48v to 12v converter inside the rov. Both the topside and bottom side have 12v to 5v converters for the items needing 5volts. I started using an UNO on the bottom side, but I was worried I would not have pins available for adding sensors in the future; therefore I just rewired the bottom side this week using an Arduino mega. The bottom side is using BTS7960 motor drivers which claim to handle 43amps. I also have the standard bilge pump and prop assembly.

One challenge I faced was finding a code that would work for me, this lead me down the path of trying to properly learn to code. I am just learning and I have a long way to go, but I have a good working code that to me seems very simple. I really appreciate Paul McWhorter https://www.youtube.com/c/mcwhorpj who's lessons helped me a lot . I learned how to use the H drivers and motors using a video from "drone bot" on youtube https://dronebotworkshop.com/dc-motors-l298n-h-bridge/

I have a lot of work to still and I do hope my project is a success. Since I appreciate all the information online which has guided me I would like to add any support I can give.



Motor Driver BTS7960 43A
https://www.instructables.com/Motor-Driver-BTS7960-43A/
Attachments
20220817_223149.jpg
20220817_223149.jpg (686.99 KiB) Viewed 5737 times
20220817_223210.jpg
20220817_223210.jpg (632.25 KiB) Viewed 5737 times
20220817_223157.jpg
20220817_223157.jpg (640.41 KiB) Viewed 5737 times
20220817_223153.jpg
20220817_223153.jpg (601.65 KiB) Viewed 5737 times
20220817_223302.jpg
20220817_223302.jpg (624.86 KiB) Viewed 5737 times
Thumquas
Posts: 11
Joined: Sep 2nd, 2022, 12:40 am

Re: Arduino Code with H Bridges and Bilge Pumps

Post by Thumquas »

Great looking set up!
and thank you for the info contribution.
Maybe this will turn into a great spot for new ROV programmers to sift threw info and code a find what they need to get going?
I know how fast these projects get complicated and messy but getting a stable start defiantly helps, seems there isn't much out there for the basics.
Thumquas
Posts: 11
Joined: Sep 2nd, 2022, 12:40 am

Re: Arduino Code with H Bridges and Bilge Pumps

Post by Thumquas »

Update on the code:
Just received my new second joystick module in the mail, hooked it up to the Arduino and the code works for steering and A1 Light.
I'm going to change the lighting code for single pin relay connections for both A and B lights, instead of another H bridge connection.
Keeping things as simple as possible and will keep posting code when I can so others can look threw and take what they want.

Please remember that I'm a newbie and most of this code has been sourced from others as well.
Please be respectful and know that I take no responsibility if you decide to use any of mine or others information, code or suggestions.
Thumquas
Posts: 11
Joined: Sep 2nd, 2022, 12:40 am

Re: Arduino Code with H Bridges and Bilge Pumps

Post by Thumquas »

Another Update:
All code is working properly and very happy with the results.
Will be adding a link to my first post for Code Download.

Here is my set up for testing. I have the 100' Cat5e cable in between the joystick modules and the Arduino, working great and thinking of making it longer to see how far I can go...
Attachments
ROVIMG.jpg
ROVIMG.jpg (606.5 KiB) Viewed 5702 times
sgarbo
Posts: 2
Joined: Sep 4th, 2022, 6:55 pm

Re: Arduino Code with H Bridges and Bilge Pumps

Post by sgarbo »

Thumquas, I am not sure those H driver will handle the current the pumps will need, my understanding is they are only good for 2amps. I decided on the H drivers with my ROV since they can handle lots of current. Id suggest looking at the specs to confirm. Also it would be worth wild to run a pump in some water with an amp meter to confirm the current draw of the pump under a load. Other then that I like your approach. If I am understanding correctly your Arduino will be inside the ROV and the controllers on shore ? ( only using one Arduino ?)

For those interested in a two Arduino setup I have added my code.
Attachments
Garbo ROV.zip
(3.1 KiB) Downloaded 247 times
Thumquas
Posts: 11
Joined: Sep 2nd, 2022, 12:40 am

Re: Arduino Code with H Bridges and Bilge Pumps

Post by Thumquas »

That's a good idea to run the pumps under load to check before putting everything together.
The Pumps are rated 3.2A MAX and the H bridges are rated for 3A max, I thought it might be an OK match as both usually run around 2A normally.
They were an early purchase for testing and your right that I should have bought larger ones.
I am setting up to run with only one Arduino in the ROV and the controller on shore. Simplicity is my goal for right now.
Thank you for adding your ROV code, I will defiantly be checking it out.
menco
Posts: 4
Joined: Feb 15th, 2022, 3:40 am

Re: Arduino Code with H Bridges and Bilge Pumps

Post by menco »

Hello,
I have been working on a similar rov project with an Uno on the top side and Mega onboard the rov with a master slave system. I am using 6x 1100gph bilge pump motors along with that many BTS7960 drivers. I am using half duplex through over RS485 to communicate between the arduino boards over a cat6 tether.
Now I am facing an issue with the available pwm pins. I am using pwm pins 2 through 13 to control the 6 motor drivers, and can't seem to use pwm on pins 44,45,46 to control the LED brightness and the 2 camera servos controlling pan & tilt. PWM will not work on these 3 pins.
I researched on the internet and came across a few articles that stated attaching servos on Mega, disables pwm on pins 44,45 & 46 as servos use the same arduino timer that is used for pwm on these pins. Now my question is there a work around or can I enable pwm on any other pin to get my 2 servos and LED intensity working?
Thumquas
Posts: 11
Joined: Sep 2nd, 2022, 12:40 am

Re: Arduino Code with H Bridges and Bilge Pumps

Post by Thumquas »

I'm still a beginner at this Arduino stuff but if I'm thinking correctly you might not need every connection from your motor controller to the mega to be PWM. That means you should be able to change some - and + pins from your motor controllers to the extras on the mega and that should leave you more pins in the 2 - 13 area for your servos/leds ... Remember I'm very new to this so I defiantly could be way wrong...
Post Reply