Urgent: Need help regarding high pitched sound from non-functioning DC motor

Hi everyone. I urgently need help regarding a squealing high pitched sound coming from the DC geared motors I’m using in my robot. Here is a schematic of it. My apologies for the hand drawn diagram.

Also, here is the code I’m using:

#include <JoystickShield.h>
#include <Servo.h>

// pins for motor outputs
int motor_pin1 = 4;      //assignes a pin number to the output terminal of the gear motor
int motor_pin2 = 5;
int motor_pin3 = 6;
int motor_pin4 = 7;

JoystickShield joystickShield; // create an instance of JoystickShield object

void setup ()
{
  // pinModes for motor outputs
  pinMode(motor_pin1,OUTPUT);  //telling Arduino that the given pin is to be used as output
  pinMode(motor_pin2,OUTPUT);
  pinMode(motor_pin3,OUTPUT);
  pinMode(motor_pin4,OUTPUT);
}

void loop ()
{
  joystickShield.processEvents(); // process events

  if (joystickShield.isUp()) 
  {
     forward();
  }

  if (joystickShield.isRightUp()) 
  {
     turnright();
     forward();
  }

  if (joystickShield.isRight()) 
  {
     turnright();
  }

  if (joystickShield.isRightDown()) 
  {
     turnright();
     backward();
  }

  if (joystickShield.isDown()) 
  {
     backward;
  }

  if (joystickShield.isLeftDown()) 
  {
     turnleft();
     backward();
  }

  if (joystickShield.isLeft()) 
  {
     turnleft();
  }

  if (joystickShield.isLeftUp()) 
  {
     turnleft();
     forward();
  }
  
  else
  {
    halt();
  }
}

void forward() 
{              
   digitalWrite(motor_pin1,LOW);
   digitalWrite(motor_pin2,HIGH);
   digitalWrite(motor_pin3,HIGH);
   digitalWrite(motor_pin4,LOW);
   return;
}

void backward() 
{
  digitalWrite(motor_pin1,HIGH);
  digitalWrite(motor_pin2,LOW);
  digitalWrite(motor_pin3,LOW);
  digitalWrite(motor_pin4,HIGH);
  //delay(500);
  //halt();
  return;
}

void halt () 
{
  digitalWrite(motor_pin1,LOW);
  digitalWrite(motor_pin2,LOW);
  digitalWrite(motor_pin3,LOW);
  digitalWrite(motor_pin4,LOW);
  //delay(500);                          //wait after stopping
  return;
}

void turnleft () 
{
  digitalWrite(motor_pin1,LOW);       
  digitalWrite(motor_pin2,HIGH);      //right motor rotates forward and left motor backward
  digitalWrite(motor_pin3,LOW);
  digitalWrite(motor_pin4,HIGH);
  //delay(1000);                     // wait for the robot to make the turn
  //halt();
  return;
}

void turnright () 
{
  digitalWrite(motor_pin1,HIGH);       
  digitalWrite(motor_pin2,LOW);    //left motor rotates forward and right motor backward
  digitalWrite(motor_pin3,HIGH);
  digitalWrite(motor_pin4,LOW);
  //delay(1000);                              // wait for the robot to make the turn
  //halt();
  return;
}

When I test my motors and the joystick separately, they work perfectly fine. However, the moment I hook them up as shown in the diagram, the motor starts producing a high pitched sound and stops working.

Here are the components I’m using:

Sparkfun Joystick Shield: SparkFun Joystick Shield Kit - DEV-09760 - SparkFun Electronics
DC Geared motor: http://www.nex-robotics.com/index.php?page=shop.product_details&flypage=flypage.tpl&product_id=714&category_id=31&option=com_virtuemart&Itemid=45
Standard servo: http://www.nex-robotics.com/index.php?page=shop.product_details&flypage=flypage.tpl&product_id=816&category_id=217&option=com_virtuemart&Itemid=45

I need to present this robot in a competition, in exactly 7 more hours and it’s already 11:30 in the night here :~ . I’d really appreciate it if someone could help me out as soon as possible.

I’m so sorry for posting this question under two separate subjects, but I’m desperate for an answer at this hour. Please help me.
Thanks.

Pins 4,5,6,7 are PWM pins. The PWM frequency is a very audible 490 Hz, that's what you're hearing.
Altering that will involve changing the associated timers for those PWMs to a different (higher, inaudible) freq. but there are other considerations.

> > > bad assessment, a big c-u

I really don't mind the sound. I mean, I don't like it very much, but I can live with it. It's just that my motors have stopped working altogether. How do I fix that? Oh, and could you mention some of the other considerations?

But he's not using analogWrite, he's using digitalWrite.

One problem I can is that the joystick shield uses digital pins 2-6 for the push buttons. Therefore, you must not use these pins for connecting the motor shield - but that is exactly what you have done with pins 4,5,6. Change your wiring and code to use different pins.

Another problem is that when the joystick is in one of the corners e.g. topright, you will alternate calling forward() and turnright() in quick succession - which may well cause the motors to hum.

dc42:
But he's not using analogWrite, he's using digitalWrite.

Well.. that's no way to run a robot! :grin:

I should have looked closer. :blush:

I have a question. If I'm not using the PWM features of the pins at all, why is the sound there? And more importantly, why aren't the motors working at all?

Is your wiring diagram literally how you have wired the parts together? In other words, you have not stacked the joystick shield on top of the Arduino? If so, then at the very least you need to make a connection between the +5V pin of the joystick shield and the +5V pin of the Arduino.

I have already given you a possible explanation of why the motors hum.

I'm sorry, I did connect them together, but forgot to show it in the diagram.

I suggest the following:

  1. Rewire your motor shield connections to avoid conflicting with pins used by the joystick shield. Even if you haven't connected those pins between the Arduino and the joystick shield, the joystick driver software will still access those pins.

  2. Amend your motor test program to use the new pins, then run it to test that the motors, shield and new wiring all work.

  3. Reconnect the joystick shield, then re-run the motor test program to make sure the motors etc. still work.

  4. Re-run the joystick test program to make sure it works even with the motor shield connected.

  5. Finally, combine the two programs in a very simple way. For example, support only forward and backward joystick motion.

PS - what I suspect is happening is that either the +5v or the ground connection to the joystick shield isn't making contact, so the joystick position always appears to be in one of the corners. You can check this by running the joystick test program. Or you could add some Serial.print commands to your program so that you print out the joystick position before telling the motors which way to move.

5V isn't enough to run an L298 shield, it loses 2 to 3V in the output drivers so your
motors are getting hardly anything (unless they are little 3V jobs?). For 12V motors use
12V+ is advisable. Your 2A BEC isn't up to providing enough current for even one of
your gearmotors anyway.

I'd suggest a minimum of 12V 5A supply to get them moving well from what the
datasheet suggests.

Thanks everyone. I tried rewiring the robot and using new pin numbers and it works now. Turns out, I shorted the ground and one of the analog pins on the joystick shield, so it wasn't working as it should have been.