DC motor and Servo control in the same code

I am working of a project to make a fan. I want to power the servo motor and the DC motor from the same code. I can power them individually with two separate codes but when I combine the two codes only the servo works. I don't know if its the #include <Servo.h> from the library or if there is not enough power to run both motors. I have them both connected to 5v. I am also using a potentiometer to control the speed of the DC motor. On the breadboard I have a wire connected to 5v on the arduino and a wire connected to the gnd and - bar on the breadboard. I have the +- of the servo on the breadboard, the dc motor +- on the breadboard, and the potentiometer +- on the breadboard bar. Is it even possible to control a Servo and a DC motor in the same code?

Here is my code:

int DC= 10; // DC motor attached to pin 10

#include <Servo.h> // Make code inServo.h available to this sketch
Servo myservo; // Create servo object called "myservo"
int servo_pin=9; // The servo must be attached to pin 9 or pin 10

void setup()
{
pinMode(DC,OUTPUT );//output power to DC motor pin 10
myservo.attach(servo_pin);//output power to servo pin 9
}

void loop()
{
int duty,pot_pin=0, reading;//control power to DC motor
reading =analogRead(pot_pin); // read potentiometer
duty = map(reading,0,1023,0,255); // rescale to 8-bit
duty = constrain(duty,0,255); // be safe
analogWrite(DC,duty); // set duty cycle

int pos= 0; // variable to store the servo position
int dtwait=15; // duration of wait at the end of each step
for(pos= 0;pos< 180;pos+= 1) {
myservo.write(pos); // Move to position in variable 'pos'
delay(dtwait); // wait dtwait for the servo to reach the position
}
for(pos= 180;pos>=1;pos-= 1){
myservo.write(pos); // Move to position in variable 'pos'
delay(dtwait); // waitdtwait for the servo to reach the position
}
}

What does the Servo library reference page say about your problem? (Hint)

reading =analogRead(pot_pin);
analogWrite(DC, reading / 4);

So much simpler, no?

Yes, I think the code:

reading =analogRead(pot_pin);
analogWrite(DC, reading / 4);

is simpler for arduino. 1023/4 is faster to write than:

int duty,pot_pin=0, reading;//control power to DC motor
reading =analogRead(pot_pin); // read potentiometer
duty = map(reading,0,1023,0,255); // rescale to 8-bit
duty = constrain(duty,0,255); // be safe
analogWrite(DC,duty); // set duty cycle

I'm not sure if that is what fixed it though. I just changed the DC motor pin to pin 11 and now the Servo and DC motor run at the same time. The only issue that I have is I have to turn the DC motor shaft manually to get it started? I'm not sure why. I wrote the code your way and my way and I have turn the shaft manually for both code blocks. Do you maybe know why? Is that normal?

My Code:

int DC= 11; // DC motor attached to pin 11

const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 11; // Analog output pin that the DC motor is attached to is attached to

int sensorValue = 0;// value read from the pot
int outputValue = 0; //value read from pmw 11

#include <Servo.h> // Make code inServo.havailable to this sketch
Servo myservo; // Create servo object called "myservo"
int servo_pin=9; // The servo must be attached to pin 9 or pin 10

void setup()
{
Serial.begin(9600);//turn on serial monitor
pinMode(DC,OUTPUT );//output power to DC motor pin 11
myservo.attach(servo_pin);//output power to servo pin 9
}

void loop()
{
//Code block for DC Motor
int duty,pot_pin=0, reading;//control power to DC motor
reading =analogRead(pot_pin); // read potentiometer
duty = map(reading,0,1023,0,255); // rescale to 8-bit
duty = constrain(duty,0,255); // be safe
analogWrite(DC,duty); // set duty cycle

//Code for reading input and output for pin A0 and 11 in Serial Monitor
sensorValue = analogRead(pot_pin); //
outputValue = map(sensorValue, 0, 1023, 0, 255);
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
//Code block for Servo Motor
int pos= 0; // variable to store the servo position
int dtwait=15; // duration of wait at the end of each step
for(pos= 0;pos< 180;pos+= 1) {
myservo.write(pos); // Move to position in variable 'pos'
delay(dtwait); // wait dtwait for the servo to reach the position
}
for(pos= 180;pos>=1;pos-= 1){
myservo.write(pos); // Move to position in variable 'pos'
delay(dtwait); // waitdtwait for the servo to reach the position
}
}

Your Code:

int DC= 11; // DC motor attached to pin 11

const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 11; // Analog output pin that the DC motor is attached to is attached to

int sensorValue = 0;// value read from the pot
int outputValue = 0; //value read from pmw 11

#include <Servo.h> // Make code inServo.havailable to this sketch
Servo myservo; // Create servo object called "myservo"
int servo_pin=9; // The servo must be attached to pin 9 or pin 10

void setup()
{
Serial.begin(9600);//turn on serial monitor
pinMode(DC,OUTPUT );//output power to DC motor pin 11
myservo.attach(servo_pin);//output power to servo pin 9
}

void loop()
{
//Code block for DC Motor
int pot_pin=0, reading;//control power to DC motor
reading =analogRead(pot_pin);
analogWrite(DC, reading / 4);

//Code for reading input and output for pin A0 and 11 in Serial Monitor
sensorValue = analogRead(pot_pin); //
outputValue = map(sensorValue, 0, 1023, 0, 255);
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
//Code block for Servo Motor
int pos= 0; // variable to store the servo position
int dtwait=15; // duration of wait at the end of each step
for(pos= 0;pos< 180;pos+= 1) {
myservo.write(pos); // Move to position in variable 'pos'
delay(dtwait); // wait dtwait for the servo to reach the position
}
for(pos= 180;pos>=1;pos-= 1){
myservo.write(pos); // Move to position in variable 'pos'
delay(dtwait); // waitdtwait for the servo to reach the position
}
}

You're the engineering student.
What is the spec for the motor?
Does what you supply the motor with meet the spec?

Can you read a manual?
Does your posting meet the posting guidelines?

Why not?

I have two DC Motors. The 5V DC's shaft didn't even turn but I can hear that its getting some power. The one that I got to turn with some manual help was the 3.3V DC motor. I am only using one at a time with the servo.

I can read a manual.

I used my multimeter and it is set at 20 V and its reading .08V. The motor is not getting enough power.

I read the posting guidelines and my post meets the guidelines.

I wasn't sure if it was a coding issue or a hardware issue. Originally I thought it had to do with the servo library declaration #include <Servo.h> .

Hi, if you have access to a lab or power supply and meters, do a power supply test of your motor, run it on the power supply at its rated voltage and measure the current.
Now read the spec on the arduino and see how much current it can supply out of the 5V pin.
Also check the spec on the servo and see how much current it needs.

Tom...... :slight_smile:
PS Also look at how to use this forum, particularly how to paste code, using code tags, makes reading your code sooooo much easier.

Thanks Tom that helps. I'm pretty sure I'm not getting enough power from the arduino. I think I need to get a secondary power supply from a battery for the DC motor. I will test it. I will look up how to post my code as well to make it easier to read.