Circuit 12 Spinning a motor - not working

Hi there,

I followed the instructions of the Circuit 12 from the SIK Guide but it is not responding at all.

The rest of the tutorials worked fine. I tested the DC motor and it is working. I tried the troubleshooting steps from the guide but it does not work. What other options should I try? You can refer to the picture of my setup, should be the same as in the diagram.

https://drive.google.com/file/d/0B8stk_ … sp=sharing

Please let me know what options I can try.

Jose.

The PDF version (at a minimum) has the transistor pinout wrong. As a result the 2N2222A is installed 180 deg rotated (as shown). Look at the pinout shown here …

http://www.hamradioexpress.com/catalog/ … _small.gif

Compare the schematic using the above to the schematic in SIK #12. In SFE’s defense I believe that some transistors have been known to not adhere to the above.

Remove your transistor and re-install rotated by 180.

Hi there, thanks for the quick answer!

I rotated the the transistor but I still have no response.

What other options should I try? I tested the arduino with previous tutorials and they work fine.

Regards,

Jose.

Have you measured voltages at all points in the circuit? Are they what you might expect from the circuit?

Have you tried unpluging and re pluging in the USB cable? I don’t know how much current this motor draws, but computers can shut down USB ports if it draws too much (more than 100ma). Have you tried it with an external powersupply/wallwart?

First thing to do is confirm the motor is good and (some) of your jumpers are also OK. Take the yellow jumper that’s plugged in to the transistor and plug it instead into the ground bus of the bread board. The motor should spin. If it doesn’t then the motor is bad or one of your jumpers is broken.

If the motor does spin then you’ll need a voltmeter to debug the circuit. A cheap DMM is about $20. About the most useful tool you’ll ever buy. If you don’t have one, but one and we’ll continue.

https://learn.sparkfun.com/tutorials/ho … multimeter

Hi there, thanks for the response.

The yellow jumper connected to a ground bus makes the motor spin.

I tested of the jumpers end to end and they all have continuity. I tested continuity from the USB case to a ground plug and I get a beep from the multimeter. Also, happens the same while testing from the power to a positive bus.

I tested the voltage from a positive bus to negative one and it gives me 5V. Now, the motor is supposed to be a 5V one and it should work with USB connection only based on the tutorial.

I thought the problem was the transistor or the diode so I tested the circuit with another transistor and diode and it still does not work.

I tested with another USB port and still the same.

One thing I noticed is that the integrated LED 13 does not light in idle mode.

Please point me to the right direction.

I suspect there’s no voltage at the end of the blue jumper from the Arduino. Check that w/the DVM. Measure each end of the resistor. The blue jumper end should have 5v, the other (transistor base) end about 0.6v.

BTW what number for speed did you enter when asked in the serial monitor ? A 0 will get you 0v and a 255 should get you 5v.

ps - it’s hard to tell for certain from your pic, is the blue jumper in pin9 of the Arduino ?

I checked from the end of the blue jumper to the arduino on pin ~9 and it measures 0.8v. Measuring the the same pin to the base it gives me 0.79v.

I am running the code below from the Circuit 9 tutorial from the Sparkfun Starter kit.

Yes, I am connecting the blue jumper to pin9 of the Arduino. Here is the diagram that I am using:

http://cmuems.com/2012/c/files/2012/10/ … 679691.jpg

Code from tutorial:

Version 2.0 6/2012 MDG

*/

// We’ll be controlling the motor from pin 9.

// This must be one of the PWM-capable pins.

const int motorPin = 9;

void setup()

{

// Set up the motor pin to be an output:

pinMode(motorPin, OUTPUT);

// Set up the serial port:

Serial.begin(9600);

}

void loop()

{

// Here we’ve used comments to disable some of the examples.

// To try different things, uncomment one of the following lines

// and comment the other ones. See the functions below to learn

// what they do and how they work.

// motorOnThenOff();

// motorOnThenOffWithSpeed();

// motorAcceleration();

serialSpeed();

}

// This function turns the motor on and off like the blinking LED.

// Try different values to affect the timing.

void motorOnThenOff()

{

int onTime = 3000; // milliseconds to turn the motor on

int offTime = 3000; // milliseconds to turn the motor off

digitalWrite(motorPin, HIGH); // turn the motor on (full speed)

delay(onTime); // delay for onTime milliseconds

digitalWrite(motorPin, LOW); // turn the motor off

delay(offTime); // delay for offTime milliseconds

}

// This function alternates between two speeds.

// Try different values to affect the timing and speed.

void motorOnThenOffWithSpeed()

{

int Speed1 = 200; // between 0 (stopped) and 255 (full speed)

int Time1 = 3000; // milliseconds for speed 1

int Speed2 = 50; // between 0 (stopped) and 255 (full speed)

int Time2 = 3000; // milliseconds to turn the motor off

analogWrite(motorPin, Speed1); // turns the motor On

delay(Time1); // delay for onTime milliseconds

analogWrite(motorPin, Speed2); // turns the motor Off

delay(Time2); // delay for offTime milliseconds

}

// This function slowly accelerates the motor to full speed,

// then back down to zero.

void motorAcceleration()

{

int speed;

int delayTime = 20; // milliseconds between each speed step

// accelerate the motor

for(speed = 0; speed <= 255; speed++)

{

analogWrite(motorPin,speed); // set the new speed

delay(delayTime); // delay between speed steps

}

// decelerate the motor

for(speed = 255; speed >= 0; speed–)

{

analogWrite(motorPin,speed); // set the new speed

delay(delayTime); // delay between speed steps

}

}

// This function will let you type a speed into the serial

// monitor window. Open the serial monitor using the magnifying-

// glass icon at the top right of the Arduino window. Then

// type your desired speed into the small text entry bar at the

// top of the window and click “Send” or press return. The motor

// will then operate at that speed. The valid range is 0 to 255.

void serialSpeed()

{

int speed;

Serial.println(“Type a speed (0-255) into the box above,”);

Serial.println(“then click [send] or press [return]”);

Serial.println(); // Print a blank line

// In order to type out the above message only once,

// we’ll run the rest of this function in an infinite loop:

while(true) // “true” is always true, so this will loop forever.

{

// First we check to see if incoming data is available:

while (Serial.available() > 0)

{

// If it is, we’ll use parseInt() to pull out any numbers:

speed = Serial.parseInt();

// Because analogWrite() only works with numbers from

// 0 to 255, we’ll be sure the input is in that range:

speed = constrain(speed, 0, 255);

// We’ll print out a message to let you know that the

// number was received:

Serial.print("Setting speed to ");

Serial.println(speed);

// And finally, we’ll set the speed of the motor!

analogWrite(motorPin, speed);

}

}

}

That sketch, unmodified, runs only one of the multiple functions it has included in it. That one function, serialSpeed(), asks that you set a speed btw 0 and 255. You do that as instructed in the code itself.

// This function will let you type a speed into the serial
// monitor window. Open the serial monitor using the magnifying-
// glass icon at the top right of the Arduino window. Then
// type your desired speed into the small text entry bar at the
// top of the window and click "Send" or press return. The motor
// will then operate at that speed. The valid range is 0 to 255.

void serialSpeed()
{
  int speed;
  
  Serial.println("Type a speed (0-255) into the box above,");
  Serial.println("then click [send] or press [return]");
  Serial.println();  // Print a blank line

  // In order to type out the above message only once,
  // we'll run the rest of this function in an infinite loop:

  while(true)  // "true" is always true, so this will loop forever.
  {
    // First we check to see if incoming data is available:
  
    while (Serial.available() > 0)
    {
      // If it is, we'll use parseInt() to pull out any numbers:
      
      speed = Serial.parseInt();
  
      // Because analogWrite() only works with numbers from
      // 0 to 255, we'll be sure the input is in that range:
  
      speed = constrain(speed, 0, 255);
      
      // We'll print out a message to let you know that the
      // number was received:
      
      Serial.print("Setting speed to ");
      Serial.println(speed);
  
      // And finally, we'll set the speed of the motor!
      
      analogWrite(motorPin, speed);
    }
  }
}

So what did you set it to ? Your measurements indicate something close to 0 (or = 0). This might not be high enough to overcome the initial stiction of the motor. Try 255. Better yet chamge this line in the code

int speed;

to

int speed = 255;

and upload the new code so it will run even if you don’t enter a value.

ps … (click on to open)

Hi,

Thanks again for the help. I think we are moving forward to a solution.

I have changed the code as instructed:

void serialSpeed()
{
  int speed = 255;
 
  
  Serial.println("Type a speed (0-255) into the box above,");
  Serial.println("then click [send] or press [return]");
  Serial.println();  // Print a blank line

  // In order to type out the above message only once,
  // we'll run the rest of this function in an infinite loop:

  while(true)  // "true" is always true, so this will loop forever.
  {
    // First we check to see if incoming data is available:
  
    while (Serial.available() > 0)
    {
      // If it is, we'll use parseInt() to pull out any numbers:
      
      speed = Serial.parseInt();

Then I click on verify and upload. The built-in LEDs blink and then the motor has no response.

If I type 255 inside serial speed

void serialSpeed(255)
{
  int speed;

I receive the following output:

Circuit_12:88: error: variable or field ‘serialSpeed’ declared void

Circuit_12:ino: in function ‘void loop()’:

Circuit_12:error: ‘serialSpeed’ was not declared in this scope

Circuit_12:ino: At global scope:

Circuit_12:177: error: variable or field ‘serialSpeed’ declared void

Again this is the complete code from the tutorial.

/*
SparkFun Inventor's Kit
Example sketch 12

SPINNING A MOTOR

  Use a transistor to spin a motor at different speeds.
  We'll also show you how to input data from the serial port
  (see the serialSpeed() function below).

  Motors are the basis for thousands of things in our daily lives,
  and the Arduino can control them. Here we'll use pulse-width
  modulation (PWM) to vary the speed of a motor.

  The Arduino pins are strong enough to light small LEDs (up to
  40 milliAmps), but they're not strong enough to run motors and
  other power-hungry parts. (This motor needs 50-100mA).
  Because the motor needs more current than an Arduino pin can
  provide, we'll use a transistor to do the heavy lifting.
  A transistor is a solid-state switch. When we give it a small
  amount of current, it can switch a much larger current.
  The transistors in your kit (2N2222) can switch up to 200mA.

  You can turn a transistor on and off using the digitalWrite()
  function, but you can also use the analogWrite() function to
  vary the speed of the motor. The analogWrite() function pulses
  a pin, varying the width of the pulse from 0% to 100%. We call
  this technique "PWM", for "Pulse-Width Modulation".

  One thing to keep in mind is that when you lower the speed of
  a motor using PWM, you're also reducing the torque (strength)
  of the motor. For PWM values below 50 or so, the motor won't have
  enough torque to start spinning. It will start spinning when you
  raise the speed a bit.

Hardware connections:

  Transistor:
  
    The transistor has three pins. Looking at the flat side with the
    pins down, the order is COLLECTOR, BASE, EMITTER.
    
    Connect the black wire on the motor to the
    COLLECTOR pin on the transistor.

    Connect the BASE pin through a 330 Ohm resistor to
    digital pin 9.
    
    Connect the EMITTER pin to GND.
 
  Motor:

    You've already connected the black wire on the motor to the
    COLLECTOR pin on the transistor.
    
    Connect the other (red) wire on the motor to 5V.
  
  Flyback diode:

    When the motor is spinning and suddenly turned off, the
    magnetic field inside it collapses, generating a voltage spike.
    This can damage the transistor. To prevent this, we use a
    "flyback diode", which diverts the voltage spike "around" the
    transistor.

    Connect the side of the diode with the band (cathode) to 5V
    Connect the other side of the diode (anode) to the black wire
    on the motor.

This sketch was written by SparkFun Electronics,
with lots of help from the Arduino community.
This code is completely free for any use.
Visit http://learn.sparkfun.com/products/2 for SIK information.
Visit http://www.arduino.cc to learn about the Arduino.

Version 2.0 6/2012 MDG
*/

// We'll be controlling the motor from pin 9.
// This must be one of the PWM-capable pins.

const int motorPin = 9;


void setup()
{
  // Set up the motor pin to be an output:

  pinMode(motorPin, OUTPUT);

  // Set up the serial port:

  Serial.begin(9600);
}


void loop()
{
  // Here we've used comments to disable some of the examples.
  // To try different things, uncomment one of the following lines
  // and comment the other ones. See the functions below to learn
  // what they do and how they work.

  // motorOnThenOff();
  // motorOnThenOffWithSpeed();
  // motorAcceleration();
     serialSpeed();
}


// This function turns the motor on and off like the blinking LED.
// Try different values to affect the timing.

void motorOnThenOff()
{
  int onTime = 3000;  // milliseconds to turn the motor on
  int offTime = 3000; // milliseconds to turn the motor off
  
  digitalWrite(motorPin, HIGH); // turn the motor on (full speed)
  delay(onTime);                // delay for onTime milliseconds
  digitalWrite(motorPin, LOW);  // turn the motor off
  delay(offTime);               // delay for offTime milliseconds
}


// This function alternates between two speeds.
// Try different values to affect the timing and speed.

void motorOnThenOffWithSpeed()
{
  int Speed1 = 200;  // between 0 (stopped) and 255 (full speed)
  int Time1 = 3000;  // milliseconds for speed 1
  
  int Speed2 = 50;   // between 0 (stopped) and 255 (full speed)
  int Time2 = 3000;  // milliseconds to turn the motor off
  
  analogWrite(motorPin, Speed1);  // turns the motor On
  delay(Time1);                   // delay for onTime milliseconds
  analogWrite(motorPin, Speed2);  // turns the motor Off
  delay(Time2);                   // delay for offTime milliseconds
}


// This function slowly accelerates the motor to full speed,
// then back down to zero.

void motorAcceleration()
{
  int speed;
  int delayTime = 20; // milliseconds between each speed step
  
  // accelerate the motor

  for(speed = 0; speed <= 255; speed++)
  {
    analogWrite(motorPin,speed);	// set the new speed
    delay(delayTime);           	// delay between speed steps
  }
  
  // decelerate the motor

  for(speed = 255; speed >= 0; speed--)
  {
    analogWrite(motorPin,speed);	// set the new speed
    delay(delayTime);           	// delay between speed steps
  }
}


// This function will let you type a speed into the serial
// monitor window. Open the serial monitor using the magnifying-
// glass icon at the top right of the Arduino window. Then
// type your desired speed into the small text entry bar at the
// top of the window and click "Send" or press return. The motor
// will then operate at that speed. The valid range is 0 to 255.

void serialSpeed(255)
{
  int speed;
 
  
  Serial.println("Type a speed (0-255) into the box above,");
  Serial.println("then click [send] or press [return]");
  Serial.println();  // Print a blank line

  // In order to type out the above message only once,
  // we'll run the rest of this function in an infinite loop:

  while(true)  // "true" is always true, so this will loop forever.
  {
    // First we check to see if incoming data is available:
  
    while (Serial.available() > 0)
    {
      // If it is, we'll use parseInt() to pull out any numbers:
      
      speed = Serial.parseInt();
  
      // Because analogWrite() only works with numbers from
      // 0 to 255, we'll be sure the input is in that range:
  
      speed = constrain(speed, 0, 255);
      
      // We'll print out a message to let you know that the
      // number was received:
      
      Serial.print("Setting speed to ");
      Serial.println(speed);
  
      // And finally, we'll set the speed of the motor!
      
      analogWrite(motorPin, speed);
    }
  }
}

YoBro:
If I type 255 inside serial speed

void serialSpeed(255)

{
int speed;




I receive the following output:

That’s because you shouldn’t write 255 there all alone. That is where you would define the type and name of a parameter. Just a un-named constant value doesn’t mean anything to the compiler. If you wanted to state a default value for a function parameter then you would first need to declare the function prototype header with the default value assigned. And later in the code you state the function header and the code that it does:

//function prototype declaration, with parameter with default value assigned
void myFunction(int myParameter= 255);    //don't forget the ;

// Function header with code-body
void myFunction(int myParameter) { /* code goes here */ }

BTW For the compiler void myFunction() and myFunction(int myParameter) are two entirely seperate functions that can have different code-content, eventhough they share the same name. The combination of name, number and types of parameters make it unique. If you call it with a parameter value between the round brackets, like myFunction(255), then it knows not to call the other function void myFunction() or void myFunction(float x) as the parameter types do not match.

The message you did before was correct. As Mee_n_Mac suggested: int speed = 255;

The reason the motor is not working is likely not the code. But some electrical issue.

From your earlier message:

YoBro:
I checked from the end of the blue jumper to the arduino on pin ~9 and it measures 0.8v. Measuring the the same pin to the base it gives me 0.79v.

I am running the code below from the Circuit 9 tutorial from the Sparkfun Starter kit.

Yes, I am connecting the blue jumper to pin9 of the Arduino. Here is the diagram that I am using:

http://cmuems.com/2012/c/files/2012/10/ … 679691.jpg

The wire colors in this last link are different than what you used in your picture of your own setup. The wire going to pin 9 is (dark) blue in your picture, but purple in SparkFun’s. And the ground connection is (light) blue in Sparkfun’s schematic, but black on yours. So these voltages measured are a bit confusing where exactly you held your probe tips. Please clarify by stating to which color scheme you adhere to.

Usually one keeps the negative or common probe (black) to ground to measure different voltage levels with the red probe. So they are all measured against the same reference of 0 volts. Sometimes you are specifically interested in the voltage across a part, to measure the drop. Like the voltage across a known value resistor, as Ohm’s Law then predicts the current that runs through.

OK, no problem.

The entry of int speed = 255 showed no errors on the code. I will stick to that.

I’m sorry for the confusion in colors. I scanned the picture of the SIK guide:

It can be found here: https://drive.google.com/file/d/0B8stk_ … sp=sharing

The blue wire would be the purple in that link which is connected to pin9.

Thanks for the help.

OK a couple of things to mention.

First with the modified code look at the pin9 output, just as you did before. If you see 5v on the Arduino side of the 330 ohm resistor and ~0.65 (0.8V is close enough) on the other side of the resistor, then the Arduino and code are working and now I suspect your transistor is in reversed. Give it a 180 deg spin and try again.

Second if you look at the code (I detest the name ‘sketch’) you’ll see it wants you to open the serial monitor and see some printout from the code. It asks for a speed setting. You then respond with a 0-255 number (try 255 to start) and an enter. that should get the above results. I’m not sure how long the code waits for a response from you, I know you can set a time out (I’ve done so myself) and will look into that as a further mod. If the motor still doesn’t spin and you measure the 5v/0.65v where they should be, then again reverse the transistor.

If the motor still doesn’t spin I will get out my chicken bones, goat entrails and spell casting cup. This almost never works but it scares the crap out of anyone close by and that gives me a good laugh … which I’ll need after banging my head against the wall.

Not to necropost but I noticed SFE is now selling the “2N2222A” transistor all by it’s onesies. If you look at it’s data sheet you’ll see the pinout is reversed from the common US 2N2222A. See the comments on the product page as well as here.

http://www.4qdtec.com/trtypes.html

https://www.sparkfun.com/products/12852 … n=TopRight

https://cdn.sparkfun.com/datasheets/Com … 222A-D.PDF

The upshot is that the wiring shown in the SIK is correct for this transistor (assuming they get them all from the same source).