PWM issue while controlling arduino

Hey,

In my case, I am trying to control the DC Motor using analogwrite pwm(0 to 255). But i am facing a problem, when 0 to 127 - Motor stopped completely and 127 to 255- Runs at high Speed.

I am using a Arduino Ethernet shield which is connected to Raspberry via Simulink UDP.

If i run the dc motor simply connecting only arduinoto dc motor. Everything is perfect which means i can control the (0 to 255) according to 0 to 5v which in sense my circuit is good but the problem arise when i use this

Arduino Ethernet shield which is connected to Raspberry via Simulink UDP.

Can anybody, check my dc motor code whether i had made any mistakes. why this problem accur

when 0 to 127 - Motor stopped completely and 127 to 255- Runs at high Speed.

#include <SPI.h> //Load Spi Lib
#include <Ethernet.h> //Load ethernet lib
#include <EthernetUdp.h> //Load UDP lIB
#include <Servo.h> //Load servo lib

/* Pin Configurations */
int servoPin = 8,
    DCMotorPin = 9;
    

int DCMotorPower = 100; 


/* Packet Initial Conditions */
int forward = 0, 
    left = 0, 
    right = 0; 

Servo myservo;
int servoAngle = 90;

/* UDP Configurations */

byte mac[] = { 0xDE, 0xEB, 0xDE, 0xEB, 0xDE, 0xEB };   
IPAddress ip(169, 254, 27, 47); 
unsigned int localPort = 8888;  
byte RaspIP[] = {169, 254, 27, 46}; 
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];  
EthernetUDP Udp; 

void setup() {

  Ethernet.begin(mac, ip); //Initialize Ethernet
  Udp.begin(localPort); 	//Initialize UDP
  
  Serial.begin(9600); //creating a serial monitor with bandrate of 9600 // Turn on Serial port
  
  //command for servo
  myservo.attach(servoPin);  //Initialize Servo
  myservo.write(servoAngle);   

  //command for DC Motor
  pinMode(DCMotorPin, OUTPUT);
  digitalWrite(DCMotorPin, LOW);
}

void loop() {
  

  // Check for incoming Packets

  int packetSize = Udp.parsePacket();  

  if (packetSize) {
    IPAddress remote = Udp.remoteIP();
    if (remote == RaspIP) {
      Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE); 

      forward = packetBuffer[0],
      right = packetBuffer[1],
      left = packetBuffer[2];
    }
  }

    // PWM to control the speed of car 
  if (forward == 1)
    analogWrite(DCMotorPin, DCMotorPower);


  // Calculate Servo Orientation and write the desired value once
  if (right == 1 && left == 0)
    servoAngle = 60;
  else if (left == 1 && right == 0)
    servoAngle = 120;
  else
    servoAngle = 90;
  myservo.write(servoAngle);

 
  Serial.print("Forward: ");
  Serial.println(forward);
  Serial.print("Right: ");
  Serial.println(right);
  Serial.print("Left: ");
  Serial.println(left);
  Serial.print("Servo Angle: ");
  Serial.println(servoAngle);
  
  Serial.println();
  Serial.println("----");

}

#include <Servo.h> Uh-oh.    DCMotorPin = 9;Oh yeah, that'll do it.

Try reading the second paragraph of the Servo library reference.

Second time today.

These problems seem to come in waves. One week it is Servo/PWM then next week Serial.print() in ISRs then delay() problems in all their guises and so on.

UKHeliBob:
These problems seem to come in waves. One week it is Servo/PWM then next week Serial.print() in ISRs then delay() problems in all their guises and so on.

Is there any solution for this problem

Aimtohigh:
Is there any solution for this problem

Did you read the Servo library reference, second paragraph?

Groove:
Did you read the Servo library reference, second paragraph?

Yes, The Servo library supports up to 12 motors on most Arduino boards and 48 on the Arduino Mega. On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins. On the Mega, up to 12 servos can be used without interfering with PWM functionality; use of 12 to 23 motors will disable PWM on pins 11 and 12.

So rather than pin 9 and 10. can i use pin 7 ? and pin 9 to servo ?. whether it will works

No, you can't use pin 9 - read it!

Groove:
No, you can't use pin 9 - read it!

finally, the Arduino board has many timers. One timer to the Servo. That means there is one less timer available for PWM and on an Arduino Uno (or other ATMega) so that we cannot use pins 9 and 10 for PWM when using a Servo.

anyways i can try Servo on pins 9 and 10. Therefore in your case, attaching the Servo to pin 9 and the DC Motor to pin 11 should solve. right

Oh dear - I'm sorry, yes, of course you can use any pin for a servo, including pin 9 - must've had a brain fart.