# Controlling a 12v 80 watt dc motor with an Arduino

#### Raymond3231976

##### Member
LOL I hope so .The eta on the battery is May 7

#### ChrisP58

##### Well-Known Member
A 12V 18AH battery is able to supply approx 200WH and so that battery will power your 80W motor (at maximum load) for about 2 hours. Is 2 hours long enough? Paralleling will add more hours.

Mike.
Maybe. The AmpHour rating of a typical SLA battery is usually based on a 20 hour discharge time. The capacity is lower if you discharge it faster. Look at the datasheet for the actual battery to verify it's particular derating factor.

#### Pommie

##### Well-Known Member
As the OP has no idea (or isn't telling) what the load will be or how the PWM will be used only a very rough estimate can be given. Two hours at 80W is still less than the full capacity by about 25%. Due to too many unknowns, I think it's a case of "suck it and see".

Mike.

#### Raymond3231976

##### Member
Just to let anyone know the reason for the Potentiometer is to allow me to run the motor at slower speeds not at full speeds all the time. The motor will be spinning a water pump with a v belt, the battery will be recharged by a solar setup.

Last edited:

#### Raymond3231976

##### Member
I got my 20k ohm resistor 1/4 Watt 1% Resistors today .
I got them in a 20 pack so I got some extra for another project later on.

##### Well-Known Member
Moving right along part by part.

Ron

#### Raymond3231976

##### Member
yea just wish they all would get here a little faster

##### Well-Known Member
yea just wish they all would get here a little faster
When I was a kid growing up in New York City and wanted parts I had to first go to the post office and buy a money order, walked or rode my bicycle. Then I would mail my money order to the vendor. Then waited for weeks for the mailman to show up with my parts. Today I just point and click using Amazon and in two days I get my parts.

Your parts will eventually all arrive and things will continue.

Ron

#### Raymond3231976

##### Member
Back then the post office was more reliable .For example I ordered a phone it got to a sorting station near me on the 30th of this last month and the ETA on it was the 1st of this month I was watching the tracking number on it every step of the way .Well come the 1st I didn't get it but it did go to another sorting station 3 hours away and now theres no ETA on it at all ... This happens all the time where Im at. It would be nice to still have electronic parts stores around .

##### Well-Known Member
While I was thinking about this and had some time I put together some code which should work for you. There are also a few things I wanted to pass along as explanation. There are sections of the code you will need to adjust or suit to taste. Also, writing code is not my forte, meaning if I had to rely on my code writing skills to eat I would have starved to death. If anyone sees room for improvement or change, please point changes out.

Early on in the thread I mentioned Hysteresis and why I felt it was needed. This code sample allows for hysteresis and you will need to determine the limits. I used the enum_state which may use slightly more memory but doubt it will matter. There are several ways to apply hysteresis in code for an Arduino and I simply threw together what I thought should work.

You will see delays in my code, they are not necessary but in the code so when you watch it run and test it things do not fly past you as a blur. Once things are setup the delays and the print statements can be removed or just make it so they don't run. I will get to doing that.

Here is the code I came up with:
Code:
// InputHysteresis.ino Turn a motor On and Off based on Battery Voltage.
// We want the motor to run only when the battery voltage exceeds a preset level.
// We also want a potentiometer to be able to set the motor speed when the motor is enabled.
// Define the baud rate for the serial port.
#define BAUD_RATE 9600

// This assumes A0 for Battery Voltage input and A1 for Motor Speed input.
// Battery Voltage allows a digital out for the motor to run or not run.
// Motor Speed controls a PWM output siganl to the motor.
#define INPUT1_PIN A0
#define INPUT2_PIN A1

// PWM Constants
const int pwmOutPin = 5; // Analog output pin that the LED is attached to

// The Arduino Uno built in LED is used for test purposes pin 13.
// The Arduino Uno pin 12 is used to enable or disable the motor.
#ifndef LED_BUILTIN
#define LED_BUILTIN 13
#endif
/****************************************************************/
// Global variables.
// The state of the motor enable output pin can be captured with only two values, High or Low Battery Voltage.
// Battery Voltage is represented by a single bit.
// The following statement defines two symbolic values, one for each possible state.
enum state_t { IS_LOW, IS_HIGH };

// Declare the state variable as a symbolic value.
enum state_t state = IS_LOW;

// The hysteretic response is defined by using two thresholds.
const int high_threshold = 881;
const int low_threshold  = 819;
// The PWM Functions
int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)
int percentValue = 0;       // pump speed percent full scale

// Standard Arduino initialization function to configure the system.
void setup()
{
// initialize the Serial port
Serial.begin( BAUD_RATE );

// configure our trivial I/O
pinMode( LED_BUILTIN, OUTPUT );
pinMode( 12, OUTPUT);

// the LED and pin 12 start out High to match the initial state.
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(12, HIGH);
}

/****************************************************************/
// Standard Arduino polling function.

void loop()
{
// Read the PWM value of the input potentiometer.
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// map pump speed percent of full scale
percentValue = map (outputValue, 0, 255, 0, 100);
// change the analog out value:
analogWrite(pwmOutPin, outputValue);
// print the results to the Serial Monitor:
Serial.print("\t Speed Input = ");
Serial.print(sensorValue);
Serial.print("\t Speed Output = ");
Serial.print(outputValue);
Serial.print("\t Motor Speed Percentage = ");
Serial.println(percentValue);

// wait 500 milliseconds before the next loop for the analog-to-digital
// converter to settle after the last reading:
delay(500); // Allow 0.5 Second and continue

// Read the Battery Voltage level.
float battVolt = (input1 * 5.0) / 1023 * 3;
Serial.print ("\t Battery Voltage is  ");
Serial.print (battVolt);
Serial.println ("  Volts");
delay(1000);

if (state == IS_HIGH) {
if (input1 < low_threshold) {
Serial.print("Low observed at input level ");
Serial.println(input1);
Serial.println("Transitioning to the IS_LOW state.");

state = IS_LOW;
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(12, LOW);
}

} else { // state must be IS_LOW
if (input1 > high_threshold) {
Serial.print("High observed at input level ");
Serial.println(input1);
Serial.println("Transitioning to the IS_HIGH state.");

state = IS_HIGH;
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(12, HIGH);
}
}
}

/****************************************************************/
When you get your Arduino Uno you can go to the Arduino site and download the IDE and install it. Copy and paste the already posted code just to see how things work with the analog input(s). Pommie provided a good working code sample in Post #61. Practice learning the IDE software using that code.

When you see // before a line that tells the system to ignore that line of code. This allows inserting remarks in the code. For example:

// InputHysteresis.ino Turn a motor On and Off based on Battery Voltage.
// We want the motor to run only when the battery voltage exceeds a preset level.
// We also want a potentiometer to be able to set the motor speed when the motor is enabled.
// Define the baud rate for the serial port.

All that is would be remarks and user information. It actually does not run and do anything.

This is just a rough overview and I know there will be questions so just ask. I just wanted to toss this out there as a sample you can build on or streamline to suit your needs.

Ron

#### Raymond3231976

##### Member
I've downloaded the Arduino IDE software. Thank you for your help as soon as i get all my parts Im going to put this together and hope for the best .Im sure the code you wrote will help out alot thank you

##### Well-Known Member
There is more stuff to point out but we will get into that later. These lines will need trimmed for your application and pump:
Code:
 // Read the PWM value of the input potentiometer.
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// map pump speed percent of full scale
percentValue = map (outputValue, 0, 255, 0, 100);
// change the analog out value:
analogWrite(pwmOutPin, outputValue);
Again, later once things are working.

Ron

#### Raymond3231976

##### Member
Thanks for the help ...

#### Raymond3231976

##### Well-Known Member
One piece at a time.

Ron

#### Raymond3231976

##### Member
Yeah snail mail takes sooo long.. LOL

#### Raymond3231976

##### Member
I got my Arduino UNO today in the mail .....One part at a time with snail mail lol.

#### Raymond3231976

##### Member
Any ideas what i could do to just test the board to make sure its a working unit ...

##### Well-Known Member
Yes, there are a few things you can do. I mentioned earlier how the Arduino UNO has a built in LED on pin 13 of the digital out pins. So let's try this:

Code:
int ledPin = 13;

void setup()

{

pinMode(ledPin, OUTPUT);

}

void loop()

{

digitalWrite(ledPin, HIGH);

delay(1000);

digitalWrite(ledPin, LOW);

delay(1000);

}
Open the downloaded Arduino IDE software you downloaded and installed. Click Tools and make sure your Arduino is connected to the correct port and the correct Board is selected. Then click File and New and copy and paste the above code into the IDE. The onboard LED should blink, on for a second and off for a second.

This is the simplest way to start I know of.

Ron

#### Raymond3231976

##### Member
Im getting errors in the IDE software on my macbook for the Arduino UNO R3 ATMEGA328P-16AU CH340G.
It says programer is not responding

any suggestions for this

here is where i got the board from

Last edited: